Search in sources :

Example 1 with NodeStoreFixture

use of org.apache.jackrabbit.oak.run.cli.NodeStoreFixture in project jackrabbit-oak by apache.

the class TextExtractorMain method main.

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    Options opts = new Options();
    opts.setCommandName(TikaCommandOptions.NAME);
    opts.setSummary("Provides text extraction related operations");
    opts.setConnectionString(CommonOptions.DEFAULT_CONNECTION_STRING);
    opts.registerOptionsFactory(TikaCommandOptions.FACTORY);
    // NodeStore is only required for generate command. So make it optional
    opts.parseAndConfigure(parser, args, false);
    TikaCommandOptions tikaOpts = opts.getOptionBean(TikaCommandOptions.class);
    // If generate then check that NodeStore is specified
    if (tikaOpts.generate()) {
        opts.checkNonOptions();
    }
    try (Closer closer = Closer.create()) {
        boolean report = tikaOpts.report();
        boolean extract = tikaOpts.extract();
        boolean generate = tikaOpts.generate();
        BlobStore blobStore;
        NodeStore nodeStore = null;
        File dataFile = tikaOpts.getDataFile();
        File storeDir = tikaOpts.getStoreDir();
        File tikaConfigFile = tikaOpts.getTikaConfig();
        BinaryResourceProvider binaryResourceProvider = null;
        BinaryStats stats = null;
        String path = tikaOpts.getPath();
        if (tikaConfigFile != null) {
            checkArgument(tikaConfigFile.exists(), "Tika config file %s does not exist", tikaConfigFile.getAbsolutePath());
        }
        if (storeDir != null) {
            if (storeDir.exists()) {
                checkArgument(storeDir.isDirectory(), "Path [%s] specified for storing extracted " + "text content is not a directory", storeDir.getAbsolutePath());
            }
        }
        checkNotNull(dataFile, "Data file not configured with %s", tikaOpts.getDataFileSpecOpt());
        if (!generate) {
            // For report and extract case we do not need NodeStore access so create BlobStore directly
            BlobStoreFixture blobStoreFixture = BlobStoreFixtureProvider.create(opts);
            closer.register(blobStoreFixture);
            blobStore = checkNotNull(blobStoreFixture).getBlobStore();
        } else {
            NodeStoreFixture nodeStoreFixture = NodeStoreFixtureProvider.create(opts);
            closer.register(nodeStoreFixture);
            blobStore = nodeStoreFixture.getBlobStore();
            nodeStore = nodeStoreFixture.getStore();
        }
        checkNotNull(blobStore, "This command requires an external BlobStore configured");
        if (generate) {
            checkNotNull(dataFile, "Data file path not provided");
            log.info("Generated csv data to be stored in {}", dataFile.getAbsolutePath());
            BinaryResourceProvider brp = new NodeStoreBinaryResourceProvider(nodeStore, blobStore);
            CSVFileGenerator generator = new CSVFileGenerator(dataFile);
            generator.generate(brp.getBinaries(path));
        }
        if (report || extract) {
            checkArgument(dataFile.exists(), "Data file %s does not exist", dataFile.getAbsolutePath());
            CSVFileBinaryResourceProvider csvProvider = new CSVFileBinaryResourceProvider(dataFile, blobStore);
            closer.register(csvProvider);
            binaryResourceProvider = csvProvider;
            stats = new BinaryStats(tikaConfigFile, binaryResourceProvider);
            String summary = stats.getSummary();
            log.info(summary);
        }
        if (extract) {
            checkNotNull(storeDir, "Directory to store extracted text content " + "must be specified via %s", tikaOpts.getStoreDirSpecOpt());
            checkNotNull(blobStore, "BlobStore found to be null.");
            DataStoreTextWriter writer = new DataStoreTextWriter(storeDir, false);
            TextExtractor extractor = new TextExtractor(writer);
            if (tikaOpts.isPoolSizeDefined()) {
                extractor.setThreadPoolSize(tikaOpts.getPoolSize());
            }
            if (tikaConfigFile != null) {
                extractor.setTikaConfig(tikaConfigFile);
            }
            closer.register(writer);
            closer.register(extractor);
            extractor.setStats(stats);
            log.info("Using path {}", path);
            extractor.extract(binaryResourceProvider.getBinaries(path));
            extractor.close();
            writer.close();
        }
    }
}
Also used : Closer(com.google.common.io.Closer) CommonOptions(org.apache.jackrabbit.oak.run.cli.CommonOptions) Options(org.apache.jackrabbit.oak.run.cli.Options) DataStoreTextWriter(org.apache.jackrabbit.oak.plugins.index.datastore.DataStoreTextWriter) OptionParser(joptsimple.OptionParser) NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) BlobStoreFixture(org.apache.jackrabbit.oak.run.cli.BlobStoreFixture) NodeStoreFixture(org.apache.jackrabbit.oak.run.cli.NodeStoreFixture) File(java.io.File) BlobStore(org.apache.jackrabbit.oak.spi.blob.BlobStore)

Example 2 with NodeStoreFixture

use of org.apache.jackrabbit.oak.run.cli.NodeStoreFixture in project jackrabbit-oak by apache.

the class JsonIndexTest method readWrite.

@Test
public void readWrite() throws Exception {
    JsonIndexCommand index = new JsonIndexCommand();
    try (NodeStoreFixture fixture = memoryFixture()) {
        NodeStore store = fixture.getStore();
        index.session = JsonIndexCommand.openSession(store, "admin", "admin");
        assertCommand(index, combineLines(""), "{'addNode':'/foo', 'node':{'jcr:primaryType': 'nt:unstructured', 'x': 1, 'y':{}}}", "{'session': 'save'}");
        assertCommand(index, combineLines("/foo", "/jcr:system", "/oak:index", "/rep:security"), "{'xpath':'/jcr:root/* order by @jcr:path'}");
        assertCommand(index, combineLines("/oak:index/counter"), "{'xpath':'/jcr:root//element(*, oak:QueryIndexDefinition)[@type=`counter`] " + "order by @jcr:path'}");
        assertCommand(index, combineLines("[nt:unstructured] as [a] /* property test = 1 " + "where ([a].[x] = 1) and (isdescendantnode([a], [/])) */"), "{'addNode':'/oak:index/test', 'node':{ " + "'jcr:primaryType':'oak:QueryIndexDefinition', " + "'type':'property', " + "'reindex':true, " + "'entryCount': 1, " + "'{Name}declaringNodeTypes': ['nt:unstructured'], " + "'{Name}propertyNames':['x'] " + "}}", "{'session':'save'}", "{'xpath':'explain /jcr:root//element(*, nt:unstructured)[@x=1]'}", "{'xpath':'/jcr:root//element(*, nt:unstructured)[@x=2]'}");
        assertCommand(index, combineLines("50"), "{'addNode':'/foo/test', 'node':{'jcr:primaryType': 'oak:Unstructured', 'child':{}}}", "{'$x':1}", "{'loop':[" + "{'$p': '/foo/test/child/n', '+': '$x'}, " + "{'addNode': '$p', 'node': {'x': '$x', 'jcr:primaryType': 'nt:unstructured'}}, " + "{'session':'save'}, " + "{'$x': '$x', '+':1}, " + "{'$break': true, 'if': '$x', '=': 100}]}", "{'session':'save'}", "{'xpath':'/jcr:root//element(*, nt:unstructured)[@x<50]', 'quiet':true}", "{'$y':0}", "{'for':'$result', 'do': [{'$y': '$y', '+': 1}]}", "{'print': '$y'}");
        assertCommand(index, combineLines("[nt:unstructured] as [a] /* nodeType Filter(query=" + "explain select [jcr:path], [jcr:score], * from [nt:unstructured] as a " + "where [x] = 1 and isdescendantnode(a, '/') /* xpath: " + "/jcr:root//element(*, nt:unstructured)[@x=1] */, path=//*, " + "property=[x=[1]]) where ([a].[x] = 1) and (isdescendantnode([a], [/])) */"), "{'setProperty': '/oak:index/test/type', 'value': 'disabled'}", "{'session':'save'}", "{'xpath':'explain /jcr:root//element(*, nt:unstructured)[@x=1]'}");
        assertCommand(index, combineLines("[nt:unstructured] as [a] /* traverse '*' " + "where [a].[x] = 1 */"), "{'removeNode': '/oak:index/nodetype'}", "{'session':'save'}", "{'sql':'explain select * from [nt:unstructured] as [a] where [x]=1'}");
        assertCommand(index, combineLines("['/foo': {\n" + "  'jcr:primaryType': 'nt:unstructured', '{Long}x': '1', 'y': {}, 'test': {}\n" + "}]"), "{'xpath':'/jcr:root/foo', 'depth':2}");
        index.session.logout();
    }
}
Also used : NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) NodeStoreFixture(org.apache.jackrabbit.oak.run.cli.NodeStoreFixture) Test(org.junit.Test)

Example 3 with NodeStoreFixture

use of org.apache.jackrabbit.oak.run.cli.NodeStoreFixture in project jackrabbit-oak by apache.

the class IndexCommand method connectInReadWriteModeAndImportIndex.

private void connectInReadWriteModeAndImportIndex(IndexOptions indexOpts, File indexDir) throws Exception {
    try (Closer closer = Closer.create()) {
        configureCustomizer(opts, closer, false);
        NodeStoreFixture fixture = NodeStoreFixtureProvider.create(opts);
        closer.register(fixture);
        IndexHelper indexHelper = createIndexHelper(fixture, indexOpts, closer);
        importIndex(indexHelper, indexDir);
    }
}
Also used : Closer(com.google.common.io.Closer) NodeStoreFixture(org.apache.jackrabbit.oak.run.cli.NodeStoreFixture)

Example 4 with NodeStoreFixture

use of org.apache.jackrabbit.oak.run.cli.NodeStoreFixture in project jackrabbit-oak by apache.

the class IndexCommand method execute.

@Override
public void execute(String... args) throws Exception {
    OptionParser parser = new OptionParser();
    opts = new Options();
    opts.setCommandName(NAME);
    opts.setSummary(summary);
    opts.setConnectionString(CommonOptions.DEFAULT_CONNECTION_STRING);
    opts.registerOptionsFactory(IndexOptions.FACTORY);
    opts.parseAndConfigure(parser, args);
    indexOpts = opts.getOptionBean(IndexOptions.class);
    // Clean up before setting up NodeStore as the temp
    // directory might be used by NodeStore for cache stuff like persistentCache
    setupDirectories(indexOpts);
    setupLogging(indexOpts);
    logCliArgs(args);
    boolean success = false;
    try {
        if (indexOpts.isReindex() && opts.getCommonOpts().isReadWrite()) {
            performReindexInReadWriteMode(indexOpts);
        } else {
            try (Closer closer = Closer.create()) {
                configureCustomizer(opts, closer, true);
                NodeStoreFixture fixture = NodeStoreFixtureProvider.create(opts);
                closer.register(fixture);
                execute(fixture, indexOpts, closer);
                tellReportPaths();
            }
        }
        success = true;
    } catch (Throwable e) {
        log.error("Error occurred while performing index tasks", e);
        e.printStackTrace(System.err);
        if (disableExitOnError) {
            throw e;
        }
    } finally {
        shutdownLogging();
    }
    if (!success) {
        System.exit(1);
    }
}
Also used : Closer(com.google.common.io.Closer) CommonOptions(org.apache.jackrabbit.oak.run.cli.CommonOptions) Options(org.apache.jackrabbit.oak.run.cli.Options) NodeStoreFixture(org.apache.jackrabbit.oak.run.cli.NodeStoreFixture) OptionParser(joptsimple.OptionParser)

Example 5 with NodeStoreFixture

use of org.apache.jackrabbit.oak.run.cli.NodeStoreFixture in project jackrabbit-oak by apache.

the class Console method main.

public static void main(String[] args) throws Exception {
    OptionParser parser = new OptionParser();
    OptionSpec quiet = parser.accepts("quiet", "be less chatty");
    OptionSpec shell = parser.accepts("shell", "run the shell after executing files");
    Options opts = new Options();
    OptionSet options = opts.parseAndConfigure(parser, args);
    int code = 0;
    try (NodeStoreFixture fixture = NodeStoreFixtureProvider.create(opts)) {
        List<String> nonOptions = opts.getCommonOpts().getNonOptions();
        List<String> scriptArgs = nonOptions.size() > 1 ? nonOptions.subList(1, nonOptions.size()) : Collections.emptyList();
        IO io = new IO();
        if (options.has(quiet)) {
            io.setVerbosity(IO.Verbosity.QUIET);
        }
        if (!opts.getCommonOpts().isReadWrite()) {
            io.out.println("Repository connected in read-only mode. Use '--read-write' for write operations");
        }
        GroovyConsole console = new GroovyConsole(ConsoleSession.create(fixture.getStore(), fixture.getWhiteboard()), new IO(), fixture);
        if (!scriptArgs.isEmpty()) {
            if (!options.has(shell)) {
                Preferences.verbosity = IO.Verbosity.QUIET;
            }
            code = console.execute(scriptArgs);
        }
        if (scriptArgs.isEmpty() || options.has(shell)) {
            code = console.run();
        }
    }
    System.exit(code);
}
Also used : OptionSpec(joptsimple.OptionSpec) Options(org.apache.jackrabbit.oak.run.cli.Options) IO(org.codehaus.groovy.tools.shell.IO) NodeStoreFixture(org.apache.jackrabbit.oak.run.cli.NodeStoreFixture) OptionSet(joptsimple.OptionSet) OptionParser(joptsimple.OptionParser)

Aggregations

NodeStoreFixture (org.apache.jackrabbit.oak.run.cli.NodeStoreFixture)7 Closer (com.google.common.io.Closer)4 OptionParser (joptsimple.OptionParser)4 Options (org.apache.jackrabbit.oak.run.cli.Options)4 File (java.io.File)3 CommonOptions (org.apache.jackrabbit.oak.run.cli.CommonOptions)3 NodeStore (org.apache.jackrabbit.oak.spi.state.NodeStore)2 Stopwatch (com.google.common.base.Stopwatch)1 OptionSet (joptsimple.OptionSet)1 OptionSpec (joptsimple.OptionSpec)1 DataStoreTextWriter (org.apache.jackrabbit.oak.plugins.index.datastore.DataStoreTextWriter)1 BlobStoreFixture (org.apache.jackrabbit.oak.run.cli.BlobStoreFixture)1 BlobStore (org.apache.jackrabbit.oak.spi.blob.BlobStore)1 IO (org.codehaus.groovy.tools.shell.IO)1 Test (org.junit.Test)1