Skip to content

Commit

Permalink
OAK-10357: Path option for documentstore-check
Browse files Browse the repository at this point in the history
  • Loading branch information
mreutegg committed Aug 25, 2023
1 parent fbf7413 commit 5b05f24
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,26 @@
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.jackrabbit.guava.common.io.Closer;

import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
import org.apache.jackrabbit.oak.commons.json.JsopBuilder;
import org.apache.jackrabbit.oak.plugins.document.Collection;
import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore;
import org.apache.jackrabbit.oak.plugins.document.DocumentStore;
import org.apache.jackrabbit.oak.plugins.document.NodeDocument;
import org.apache.jackrabbit.oak.plugins.document.mongo.MongoDocumentStore;
import org.apache.jackrabbit.oak.plugins.document.util.Utils;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.jackrabbit.JcrConstants.JCR_BASEVERSION;
Expand Down Expand Up @@ -91,6 +95,8 @@ public class DocumentStoreCheck {

private final boolean consistency;

private final List<String> paths;

private DocumentStoreCheck(DocumentNodeStore ns,
DocumentStore store,
Closer closer,
Expand All @@ -106,7 +112,8 @@ private DocumentStoreCheck(DocumentNodeStore ns,
boolean predecessors,
boolean successors,
boolean uuid,
boolean consistency) {
boolean consistency,
List<String> paths) {
this.ns = ns;
this.store = store;
this.closer = closer;
Expand All @@ -129,14 +136,15 @@ private DocumentStoreCheck(DocumentNodeStore ns,
this.successors = successors;
this.uuid = uuid;
this.consistency = consistency;
this.paths = paths;
}

public void run() throws Exception {
BlockingQueue<Result> results = new LinkedBlockingQueue<>(1000);
scheduleResultWriter(results);

DocumentProcessor processor = createDocumentProcessor();
for (NodeDocument doc : getAllDocs(store)) {
for (NodeDocument doc : paths.isEmpty() ? getAllDocs(store) : getDocs(store, paths)) {
processor.processDocument(doc, results);
}
processor.end(results);
Expand Down Expand Up @@ -233,6 +241,14 @@ private static Iterable<NodeDocument> getAllDocs(DocumentStore store) {
}
}

private static Iterable<NodeDocument> getDocs(DocumentStore store,
List<String> paths) {
return paths.stream()
.map(p -> store.find(Collection.NODES, Utils.getIdFromPath(p)))
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

public static class Builder {

private final DocumentNodeStore ns;
Expand Down Expand Up @@ -267,6 +283,8 @@ public static class Builder {

private boolean consistency;

private final List<String> paths = new ArrayList<>();

public Builder(DocumentNodeStore ns,
DocumentStore store,
Closer closer) {
Expand Down Expand Up @@ -340,10 +358,16 @@ public Builder withConsistency(boolean enable) {
return this;
}

public Builder withPaths(List<String> paths) {
this.paths.clear();
this.paths.addAll(paths);
return this;
}

public DocumentStoreCheck build() {
return new DocumentStoreCheck(ns, store, closer, progress, silent,
summary, counter, numThreads, output, orphan, baseVersion,
versionHistory, predecessors, successors, uuid, consistency);
versionHistory, predecessors, successors, uuid, consistency, paths);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.apache.jackrabbit.oak.run;

import java.util.List;

import org.apache.jackrabbit.guava.common.io.Closer;

import org.apache.jackrabbit.oak.plugins.document.DocumentNodeStore;
Expand Down Expand Up @@ -74,6 +76,7 @@ public void execute(String... args) throws Exception {
.withSummary(options.withSummary())
.withCounter(options.withCounter())
.withNumThreads(options.getNumThreads())
.withPaths(options.getPaths())
.build().run();

} catch (Throwable e) {
Expand Down Expand Up @@ -111,6 +114,8 @@ private static final class CheckOptions extends Utils.NodeStoreOptions {

final OptionSpec<Integer> numThreads;

final OptionSpec<String> paths;

public CheckOptions(String usage) {
super(usage);

Expand Down Expand Up @@ -139,6 +144,8 @@ public CheckOptions(String usage) {
.withOptionalArg().ofType(Boolean.class).defaultsTo(Boolean.TRUE);
numThreads = parser.accepts("numThreads", "Use this number of threads to check consistency")
.withRequiredArg().ofType(Integer.class).defaultsTo(Runtime.getRuntime().availableProcessors());
paths = parser.accepts("path", "Limit check to given path")
.withRequiredArg().ofType(String.class);
}

@Override
Expand Down Expand Up @@ -199,6 +206,10 @@ public int getNumThreads() {
return numThreads.value(options);
}

public List<String> getPaths() {
return paths.values(options);
}

boolean isHelp() {
return options.has(help);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,37 @@ public void uuidWithoutIndexEntry() throws Exception {
assertThat(lines.get(0), containsString(JCR_UUID));
}

@Test
public void path() throws Exception {
createNodeWithUUID(false);
DocumentStoreCheckCommand cmd = new DocumentStoreCheckCommand();
cmd.execute(
"--summary", "false",
"--counter", "false",
"--path", "/referenceable",
"--out", output.getAbsolutePath(),
MongoUtils.URL
);
List<String> lines = Files.readAllLines(output.toPath(), UTF_8);
assertEquals(1, lines.size());
assertThat(lines.get(0), containsString(JCR_UUID));
}

@Test
public void pathDoesNotExist() throws Exception {
createNodeWithUUID(false);
DocumentStoreCheckCommand cmd = new DocumentStoreCheckCommand();
cmd.execute(
"--summary", "false",
"--counter", "false",
"--path", "/does-not-exist",
"--out", output.getAbsolutePath(),
MongoUtils.URL
);
List<String> lines = Files.readAllLines(output.toPath(), UTF_8);
assertThat(lines, is(empty()));
}

private DocumentNodeStore createDocumentNodeStore() {
MongoConnection c = connectionFactory.getConnection();
assertNotNull(c);
Expand Down

0 comments on commit 5b05f24

Please sign in to comment.