Skip to content
This repository was archived by the owner on Sep 16, 2024. It is now read-only.

Commit ed257ba

Browse files
committed
#92 Can now merge and reindex a database easily
1 parent aae28c5 commit ed257ba

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

src/main/java/com/marklogic/mgmt/databases/DatabaseManager.java

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,43 @@ public DatabaseManager(ManageClient manageClient) {
1515
super(manageClient);
1616
}
1717

18+
/**
19+
* This will catch and log any exception by default, as the most frequent reason why this fails is because the
20+
* database doesn't exist yet.
21+
*
22+
* @param databaseIdOrName
23+
*/
1824
public void clearDatabase(String databaseIdOrName) {
19-
String path = format("/manage/v2/databases/%s", databaseIdOrName);
20-
logger.info(format("Clearing database %s", databaseIdOrName));
25+
clearDatabase(databaseIdOrName, true);
26+
}
27+
28+
public void clearDatabase(String databaseIdOrName, boolean catchException) {
2129
try {
22-
getManageClient().postJson(path, "{\"operation\":\"clear-database\"}");
23-
logger.info(format("Cleared database %s", databaseIdOrName));
30+
invokeOperation(databaseIdOrName, "clear-database");
2431
} catch (Exception e) {
25-
logger.error("Unable to clear database; cause: " + e.getMessage());
32+
if (catchException) {
33+
logger.error("Unable to clear database; cause: " + e.getMessage());
34+
} else {
35+
throw e;
36+
}
2637
}
2738
}
2839

40+
public void mergeDatabase(String databaseIdOrName) {
41+
invokeOperation(databaseIdOrName, "merge-database");
42+
}
43+
44+
public void reindexDatabase(String databaseIdOrName) {
45+
invokeOperation(databaseIdOrName, "reindex-database");
46+
}
47+
48+
private void invokeOperation(String databaseIdOrName, String operation) {
49+
String path = format("/manage/v2/databases/%s", databaseIdOrName);
50+
logger.info(format("Invoking operation %s on database %s", operation, databaseIdOrName));
51+
getManageClient().postJson(path, format("{\"operation\":\"%s\"}", operation));
52+
logger.info(format("Finished invoking operation %s on database %s", operation, databaseIdOrName));
53+
}
54+
2955
public void deleteByName(String databaseName) {
3056
String json = format("{\"database-name\":\"%s\"}", databaseName);
3157
delete(json);
@@ -46,13 +72,14 @@ public List<String> getForestIds(String databaseNameOrId) {
4672
*/
4773
public List<String> getForestNames(String databaseNameOrId) {
4874
Fragment f = getAsXml(databaseNameOrId);
49-
return f.getElementValues("/node()/db:relations/db:relation-group[db:typeref='forests']/db:relation/db:nameref");
75+
return f.getElementValues(
76+
"/node()/db:relations/db:relation-group[db:typeref='forests']/db:relation/db:nameref");
5077
}
5178

5279
/**
5380
* @param databaseNameOrId
5481
* @return the IDs of all primary forests related to the database. The properties endpoint for a database lists
55-
* primary forest IDs, but not replica forest IDs.
82+
* primary forest IDs, but not replica forest IDs.
5683
*/
5784
public List<String> getPrimaryForestIds(String databaseNameOrId) {
5885
return getPropertiesAsXml(databaseNameOrId).getElementValues("/node()/m:forests/m:forest");

src/test/java/com/marklogic/appdeployer/AbstractAppDeployerTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public void closeAppContext() {
7474
}
7575
}
7676

77+
protected void deploySampleApp() {
78+
appDeployer.deploy(appConfig);
79+
}
80+
7781
protected void undeploySampleApp() {
7882
try {
7983
appDeployer.undeploy(appConfig);
@@ -98,4 +102,8 @@ protected LoadModulesCommand buildLoadModulesCommand() {
98102
command.setModulesLoader(loader);
99103
return command;
100104
}
105+
106+
protected void setConfigBaseDir(String path) {
107+
appConfig.getConfigDir().setBaseDir(new File("src/test/resources/" + path));
108+
}
101109
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.marklogic.appdeployer.command.databases;
2+
3+
import org.junit.After;
4+
import org.junit.Test;
5+
6+
import com.marklogic.appdeployer.AbstractAppDeployerTest;
7+
import com.marklogic.mgmt.databases.DatabaseManager;
8+
9+
public class InvokeDatabaseOperationsTest extends AbstractAppDeployerTest {
10+
11+
@After
12+
public void teardown() {
13+
undeploySampleApp();
14+
}
15+
16+
@Test
17+
public void test() {
18+
setConfigBaseDir("sample-app/db-only-config");
19+
20+
initializeAppDeployer(new DeployContentDatabasesCommand(1));
21+
deploySampleApp();
22+
23+
DatabaseManager mgr = new DatabaseManager(manageClient);
24+
25+
// Just smoke-testing these to make sure they don't blow up
26+
mgr.mergeDatabase(appConfig.getContentDatabaseName());
27+
mgr.reindexDatabase(appConfig.getContentDatabaseName());
28+
}
29+
}

0 commit comments

Comments
 (0)