You are viewing a plain text version of this content. The canonical link for it is here.
Posted to oak-commits@jackrabbit.apache.org by st...@apache.org on 2023/11/20 15:36:00 UTC
(jackrabbit-oak) 01/01: OAK-10382 : added flatfile command to the project
This is an automated email from the ASF dual-hosted git repository.
stefanegli pushed a commit to branch OAK-10382-flatfile
in repository https://gitbox.apache.org/repos/asf/jackrabbit-oak.git
commit 8bbfea22e9b7f961247cde9de06597fd5552b4be
Author: Stefan Egli <st...@apache.org>
AuthorDate: Mon Nov 20 16:35:40 2023 +0100
OAK-10382 : added flatfile command to the project
---
.../apache/jackrabbit/oak/run/AvailableModes.java | 1 +
.../apache/jackrabbit/oak/run/FlatFileCommand.java | 138 +++++++++++++++++++++
2 files changed, 139 insertions(+)
diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/AvailableModes.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/AvailableModes.java
index ecfd16d4b2..6c723cb93f 100644
--- a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/AvailableModes.java
+++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/AvailableModes.java
@@ -45,6 +45,7 @@ public final class AvailableModes {
.put(DocumentStoreCheckCommand.NAME, new DocumentStoreCheckCommand())
.put("explore", new ExploreCommand())
.put(NodeStateExportCommand.NAME, new NodeStateExportCommand())
+ .put(FlatFileCommand.NAME, new FlatFileCommand())
.put(FrozenNodeRefsByScanningCommand.NAME, new FrozenNodeRefsByScanningCommand())
.put(FrozenNodeRefsUsingIndexCommand.NAME, new FrozenNodeRefsUsingIndexCommand())
.put("garbage", new GarbageCommand())
diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/run/FlatFileCommand.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/FlatFileCommand.java
new file mode 100644
index 0000000000..0f14b955ef
--- /dev/null
+++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/run/FlatFileCommand.java
@@ -0,0 +1,138 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.jackrabbit.oak.run;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.commons.io.FileUtils;
+import org.apache.jackrabbit.guava.common.collect.ImmutableSet;
+import org.apache.jackrabbit.oak.index.indexer.document.flatfile.SimpleFlatFileUtil;
+import org.apache.jackrabbit.oak.run.cli.BlobStoreOptions;
+import org.apache.jackrabbit.oak.run.cli.BlobStoreOptions.Type;
+import org.apache.jackrabbit.oak.run.cli.NodeStoreFixture;
+import org.apache.jackrabbit.oak.run.cli.NodeStoreFixtureProvider;
+import org.apache.jackrabbit.oak.run.cli.Options;
+import org.apache.jackrabbit.oak.run.cli.OptionsBean;
+import org.apache.jackrabbit.oak.run.commons.Command;
+import org.apache.jackrabbit.oak.spi.state.NodeState;
+import org.apache.jackrabbit.oak.spi.state.NodeStore;
+
+import joptsimple.OptionParser;
+import joptsimple.OptionSet;
+import joptsimple.OptionSpec;
+
+/**
+ * The flatfile command is an extract of the ability to create a filefile from
+ * the index command. Other than the origin, the flatfile command doesn't
+ * require any checkpoint nor index name.
+ */
+@SuppressWarnings({ "rawtypes", "unchecked" })
+public class FlatFileCommand implements Command {
+
+ public static final String NAME = "flatfile";
+ private final String summary = "Provides flatfile operations";
+
+ public static class FlatFileOptions implements OptionsBean {
+
+ private final OptionSpec<File> outFileOpt;
+ private final OptionSpec<Void> flatfile;
+ protected OptionSet options;
+ protected final Set<OptionSpec> actionOpts;
+ private final Set<String> operationNames;
+
+ public FlatFileOptions(OptionParser parser) {
+ flatfile = parser.accepts("flatfile",
+ "Create a flatfile based on head of a repository");
+ outFileOpt = parser.accepts("out", "Name of the flatfile to create")
+ .withRequiredArg().ofType(File.class).defaultsTo(new File("temp"));
+ // Set of options which define action
+ actionOpts = ImmutableSet.of(flatfile);
+ operationNames = collectionOperationNames(actionOpts);
+ }
+
+ @Override
+ public void configure(OptionSet options) {
+ this.options = options;
+ }
+
+ @Override
+ public String title() {
+ return "";
+ }
+
+ @Override
+ public String description() {
+ return "The flatfile command supports creation of a flatfile for current head of a repository.";
+ }
+
+ @Override
+ public int order() {
+ return 50;
+ }
+
+ @Override
+ public Set<String> operationNames() {
+ return operationNames;
+ }
+
+ public File getOutFile() throws IOException {
+ File outFile = outFileOpt.value(options);
+ FileUtils.forceMkdir(outFile.getParentFile());
+ return outFile;
+ }
+
+ private static Set<String> collectionOperationNames(Set<OptionSpec> actionOpts) {
+ Set<String> result = new HashSet<>();
+ for (OptionSpec spec : actionOpts) {
+ result.addAll(spec.options());
+ }
+ return result;
+ }
+ }
+
+ @Override
+ public void execute(String... args) throws Exception {
+ OptionParser parser = new OptionParser();
+ Options opts = new Options();
+ opts.setCommandName(NAME);
+ opts.setSummary(summary);
+ opts.registerOptionsFactory(FlatFileOptions::new);
+ opts.registerOptionsFactory(BlobStoreOptions::new);
+ opts.parseAndConfigure(parser, args);
+ BlobStoreOptions bsOpts = opts.getOptionBean(BlobStoreOptions.class);
+ Type bsType = bsOpts.getBlobStoreType();
+ if (bsType == Type.NONE) {
+ System.err.println("BlobStore args missing.");
+ System.err.println("Hint: consider use fake one via: --fake-ds-path=.");
+ System.exit(1);
+ }
+ FlatFileOptions ffOpts = opts.getOptionBean(FlatFileOptions.class);
+ File out = ffOpts.getOutFile();
+
+ try (NodeStoreFixture fixture = NodeStoreFixtureProvider.create(opts, true)) {
+ NodeStore store = fixture.getStore();
+ NodeState root = store.getRoot();
+ SimpleFlatFileUtil.createFlatFileFor(root, out);
+ }
+ }
+
+}
\ No newline at end of file