Search in sources :

Example 71 with Closeable

use of java.io.Closeable in project jackrabbit-oak by apache.

the class RandomOpCompare method getMongo.

private static NodeStoreFixture getMongo() {
    return new NodeStoreFixture() {

        @Override
        public NodeStore createNodeStore() {
            MongoConnection connection;
            try {
                connection = new MongoConnection("mongodb://localhost:27017/oak");
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            DB mongoDB = connection.getDB();
            return new DocumentMK.Builder().memoryCacheSize(0).setMongoDB(mongoDB, 16).setPersistentCache("target/persistentCache,time").getNodeStore();
        }

        @Override
        public NodeStore createNodeStore(int clusterNodeId) {
            return null;
        }

        @Override
        public void dispose(NodeStore nodeStore) {
            if (nodeStore instanceof Closeable) {
                try {
                    ((Closeable) nodeStore).close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    };
}
Also used : NodeStore(org.apache.jackrabbit.oak.spi.state.NodeStore) NodeStoreFixture(org.apache.jackrabbit.oak.fixture.NodeStoreFixture) Closeable(java.io.Closeable) IOException(java.io.IOException) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection) IOException(java.io.IOException) RepositoryException(javax.jcr.RepositoryException) DB(com.mongodb.DB)

Example 72 with Closeable

use of java.io.Closeable in project lucene-solr by apache.

the class BKDWriter method finish.

/** Writes the BKD tree to the provided {@link IndexOutput} and returns the file offset where index was written. */
public long finish(IndexOutput out) throws IOException {
    // Catch user silliness:
    if (heapPointWriter == null && tempInput == null) {
        throw new IllegalStateException("already finished");
    }
    if (offlinePointWriter != null) {
        offlinePointWriter.close();
    }
    if (pointCount == 0) {
        throw new IllegalStateException("must index at least one point");
    }
    LongBitSet ordBitSet;
    if (numDims > 1) {
        if (singleValuePerDoc) {
            ordBitSet = new LongBitSet(maxDoc);
        } else {
            ordBitSet = new LongBitSet(pointCount);
        }
    } else {
        ordBitSet = null;
    }
    long countPerLeaf = pointCount;
    long innerNodeCount = 1;
    while (countPerLeaf > maxPointsInLeafNode) {
        countPerLeaf = (countPerLeaf + 1) / 2;
        innerNodeCount *= 2;
    }
    int numLeaves = (int) innerNodeCount;
    checkMaxLeafNodeCount(numLeaves);
    // NOTE: we could save the 1+ here, to use a bit less heap at search time, but then we'd need a somewhat costly check at each
    // step of the recursion to recompute the split dim:
    // Indexed by nodeID, but first (root) nodeID is 1.  We do 1+ because the lead byte at each recursion says which dim we split on.
    byte[] splitPackedValues = new byte[Math.toIntExact(numLeaves * (1 + bytesPerDim))];
    // +1 because leaf count is power of 2 (e.g. 8), and innerNodeCount is power of 2 minus 1 (e.g. 7)
    long[] leafBlockFPs = new long[numLeaves];
    // Make sure the math above "worked":
    assert pointCount / numLeaves <= maxPointsInLeafNode : "pointCount=" + pointCount + " numLeaves=" + numLeaves + " maxPointsInLeafNode=" + maxPointsInLeafNode;
    // Sort all docs once by each dimension:
    PathSlice[] sortedPointWriters = new PathSlice[numDims];
    // This is only used on exception; on normal code paths we close all files we opened:
    List<Closeable> toCloseHeroically = new ArrayList<>();
    boolean success = false;
    try {
        //long t0 = System.nanoTime();
        for (int dim = 0; dim < numDims; dim++) {
            sortedPointWriters[dim] = new PathSlice(sort(dim), 0, pointCount);
        }
        if (tempInput != null) {
            tempDir.deleteFile(tempInput.getName());
            tempInput = null;
        } else {
            assert heapPointWriter != null;
            heapPointWriter = null;
        }
        final int[] parentSplits = new int[numDims];
        build(1, numLeaves, sortedPointWriters, ordBitSet, out, minPackedValue, maxPackedValue, parentSplits, splitPackedValues, leafBlockFPs, toCloseHeroically);
        assert Arrays.equals(parentSplits, new int[numDims]);
        for (PathSlice slice : sortedPointWriters) {
            slice.writer.destroy();
        }
        // If no exception, we should have cleaned everything up:
        assert tempDir.getCreatedFiles().isEmpty();
        //long t2 = System.nanoTime();
        //System.out.println("write time: " + ((t2-t1)/1000000.0) + " msec");
        success = true;
    } finally {
        if (success == false) {
            IOUtils.deleteFilesIgnoringExceptions(tempDir, tempDir.getCreatedFiles());
            IOUtils.closeWhileHandlingException(toCloseHeroically);
        }
    }
    //System.out.println("Total nodes: " + innerNodeCount);
    // Write index:
    long indexFP = out.getFilePointer();
    writeIndex(out, Math.toIntExact(countPerLeaf), leafBlockFPs, splitPackedValues);
    return indexFP;
}
Also used : Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) LongBitSet(org.apache.lucene.util.LongBitSet)

Example 73 with Closeable

use of java.io.Closeable in project lucene-solr by apache.

the class MockDirectoryWrapper method crash.

/** Simulates a crash of OS or machine by overwriting
   *  unsynced files. */
public synchronized void crash() throws IOException {
    openFiles = new HashMap<>();
    openFilesForWrite = new HashSet<>();
    openFilesDeleted = new HashSet<>();
    // first force-close all files, so we can corrupt on windows etc.
    // clone the file map, as these guys want to remove themselves on close.
    Map<Closeable, Exception> m = new IdentityHashMap<>(openFileHandles);
    for (Closeable f : m.keySet()) {
        try {
            f.close();
        } catch (Exception ignored) {
        }
    }
    corruptFiles(unSyncedFiles);
    crashed = true;
    unSyncedFiles = new HashSet<>();
}
Also used : IdentityHashMap(java.util.IdentityHashMap) Closeable(java.io.Closeable) NoSuchFileException(java.nio.file.NoSuchFileException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 74 with Closeable

use of java.io.Closeable in project lucene-solr by apache.

the class SolrSuggester method init.

/**
   * Uses the <code>config</code> and the <code>core</code> to initialize the underlying 
   * Lucene suggester
   * */
public String init(NamedList<?> config, SolrCore core) {
    LOG.info("init: " + config);
    // read the config
    name = config.get(NAME) != null ? (String) config.get(NAME) : DEFAULT_DICT_NAME;
    sourceLocation = (String) config.get(LOCATION);
    lookupImpl = (String) config.get(LOOKUP_IMPL);
    dictionaryImpl = (String) config.get(DICTIONARY_IMPL);
    String store = (String) config.get(STORE_DIR);
    if (lookupImpl == null) {
        lookupImpl = LookupFactory.DEFAULT_FILE_BASED_DICT;
        LOG.info("No " + LOOKUP_IMPL + " parameter was provided falling back to " + lookupImpl);
    }
    contextFilterQueryAnalyzer = new TokenizerChain(new StandardTokenizerFactory(Collections.EMPTY_MAP), null);
    // initialize appropriate lookup instance
    factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class);
    lookup = factory.create(config, core);
    if (lookup != null && lookup instanceof Closeable) {
        core.addCloseHook(new CloseHook() {

            @Override
            public void preClose(SolrCore core) {
                try {
                    ((Closeable) lookup).close();
                } catch (IOException e) {
                    LOG.warn("Could not close the suggester lookup.", e);
                }
            }

            @Override
            public void postClose(SolrCore core) {
            }
        });
    }
    // if store directory is provided make it or load up the lookup with its content
    if (store != null && !store.isEmpty()) {
        storeDir = new File(store);
        if (!storeDir.isAbsolute()) {
            storeDir = new File(core.getDataDir() + File.separator + storeDir);
        }
        if (!storeDir.exists()) {
            storeDir.mkdirs();
        } else if (getStoreFile().exists()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("attempt reload of the stored lookup from file " + getStoreFile());
            }
            try {
                lookup.load(new FileInputStream(getStoreFile()));
            } catch (IOException e) {
                LOG.warn("Loading stored lookup data failed, possibly not cached yet");
            }
        }
    }
    // dictionary configuration
    if (dictionaryImpl == null) {
        dictionaryImpl = (sourceLocation == null) ? DictionaryFactory.DEFAULT_INDEX_BASED_DICT : DictionaryFactory.DEFAULT_FILE_BASED_DICT;
        LOG.info("No " + DICTIONARY_IMPL + " parameter was provided falling back to " + dictionaryImpl);
    }
    dictionaryFactory = core.getResourceLoader().newInstance(dictionaryImpl, DictionaryFactory.class);
    dictionaryFactory.setParams(config);
    LOG.info("Dictionary loaded with params: " + config);
    return name;
}
Also used : CloseHook(org.apache.solr.core.CloseHook) TokenizerChain(org.apache.solr.analysis.TokenizerChain) SolrCore(org.apache.solr.core.SolrCore) Closeable(java.io.Closeable) StandardTokenizerFactory(org.apache.lucene.analysis.standard.StandardTokenizerFactory) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 75 with Closeable

use of java.io.Closeable in project lucene-solr by apache.

the class Suggester method init.

@Override
public String init(NamedList config, SolrCore core) {
    LOG.info("init: " + config);
    String name = super.init(config, core);
    threshold = config.get(THRESHOLD_TOKEN_FREQUENCY) == null ? 0.0f : (Float) config.get(THRESHOLD_TOKEN_FREQUENCY);
    sourceLocation = (String) config.get(LOCATION);
    lookupImpl = (String) config.get(LOOKUP_IMPL);
    // support the old classnames without -Factory for config file backwards compatibility.
    if (lookupImpl == null || "org.apache.solr.spelling.suggest.jaspell.JaspellLookup".equals(lookupImpl)) {
        lookupImpl = JaspellLookupFactory.class.getName();
    } else if ("org.apache.solr.spelling.suggest.tst.TSTLookup".equals(lookupImpl)) {
        lookupImpl = TSTLookupFactory.class.getName();
    } else if ("org.apache.solr.spelling.suggest.fst.FSTLookup".equals(lookupImpl)) {
        lookupImpl = FSTLookupFactory.class.getName();
    }
    factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class);
    lookup = factory.create(config, core);
    core.addCloseHook(new CloseHook() {

        @Override
        public void preClose(SolrCore core) {
            if (lookup != null && lookup instanceof Closeable) {
                try {
                    ((Closeable) lookup).close();
                } catch (IOException e) {
                    LOG.warn("Could not close the suggester lookup.", e);
                }
            }
        }

        @Override
        public void postClose(SolrCore core) {
        }
    });
    String store = (String) config.get(STORE_DIR);
    if (store != null) {
        storeDir = new File(store);
        if (!storeDir.isAbsolute()) {
            storeDir = new File(core.getDataDir() + File.separator + storeDir);
        }
        if (!storeDir.exists()) {
            storeDir.mkdirs();
        } else {
            // attempt reload of the stored lookup
            try {
                lookup.load(new FileInputStream(new File(storeDir, factory.storeFileName())));
            } catch (IOException e) {
                LOG.warn("Loading stored lookup data failed", e);
            }
        }
    }
    return name;
}
Also used : CloseHook(org.apache.solr.core.CloseHook) FSTLookupFactory(org.apache.solr.spelling.suggest.fst.FSTLookupFactory) TSTLookupFactory(org.apache.solr.spelling.suggest.tst.TSTLookupFactory) JaspellLookupFactory(org.apache.solr.spelling.suggest.jaspell.JaspellLookupFactory) SolrCore(org.apache.solr.core.SolrCore) Closeable(java.io.Closeable) JaspellLookupFactory(org.apache.solr.spelling.suggest.jaspell.JaspellLookupFactory) FSTLookupFactory(org.apache.solr.spelling.suggest.fst.FSTLookupFactory) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Aggregations

Closeable (java.io.Closeable)216 IOException (java.io.IOException)88 Test (org.junit.Test)56 ArrayList (java.util.ArrayList)29 File (java.io.File)26 HashMap (java.util.HashMap)12 VirtualFile (org.jboss.vfs.VirtualFile)12 URL (java.net.URL)9 Path (java.nio.file.Path)9 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)8 Map (java.util.Map)7 ISE (io.druid.java.util.common.ISE)6 InputStream (java.io.InputStream)6 MountHandle (org.jboss.as.server.deployment.module.MountHandle)6 ResourceRoot (org.jboss.as.server.deployment.module.ResourceRoot)6 ProgramController (co.cask.cdap.app.runtime.ProgramController)5 ProgramType (co.cask.cdap.proto.ProgramType)4 ProgramId (co.cask.cdap.proto.id.ProgramId)4 Pair (io.druid.java.util.common.Pair)4 FileOutputStream (java.io.FileOutputStream)4