Search in sources :

Example 66 with IndexOutput

use of org.apache.lucene.store.IndexOutput in project jackrabbit by apache.

the class PersistentIndex method copyIndex.

/**
     * Copies <code>index</code> into this persistent index. This method should
     * only be called when <code>this</code> index is empty otherwise the
     * behaviour is undefined.
     *
     * @param index the index to copy from.
     * @throws IOException if an error occurs while copying.
     */
void copyIndex(AbstractIndex index) throws IOException {
    // commit changes to directory on other index.
    index.commit(true);
    // simply copy over the files
    byte[] buffer = new byte[1024];
    Directory dir = index.getDirectory();
    Directory dest = getDirectory();
    String[] files = dir.listAll();
    for (String file : files) {
        IndexInput in = dir.openInput(file);
        try {
            IndexOutput out = dest.createOutput(file);
            try {
                long remaining = in.length();
                while (remaining > 0) {
                    int num = (int) Math.min(remaining, buffer.length);
                    in.readBytes(buffer, 0, num);
                    out.writeBytes(buffer, num);
                    remaining -= num;
                }
            } finally {
                out.close();
            }
        } finally {
            in.close();
        }
    }
    // refresh current generation
    indexDelPolicy.readCurrentGeneration();
}
Also used : IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) Directory(org.apache.lucene.store.Directory)

Example 67 with IndexOutput

use of org.apache.lucene.store.IndexOutput in project jackrabbit-oak by apache.

the class OakDirectoryTest method assertWrites.

byte[] assertWrites(Directory dir, int blobSize) throws IOException {
    byte[] data = randomBytes(fileSize);
    IndexOutput o = dir.createOutput("test", IOContext.DEFAULT);
    o.writeBytes(data, data.length);
    o.close();
    assertTrue(dir.fileExists("test"));
    assertEquals(fileSize, dir.fileLength("test"));
    IndexInput i = dir.openInput("test", IOContext.DEFAULT);
    assertEquals(fileSize, i.length());
    byte[] result = new byte[fileSize];
    i.readBytes(result, 0, result.length);
    assertTrue(Arrays.equals(data, result));
    NodeBuilder testNode = builder.child(INDEX_DATA_CHILD_NAME).child("test");
    assertEquals(blobSize, testNode.getProperty(PROP_BLOB_SIZE).getValue(Type.LONG).longValue());
    List<Blob> blobs = newArrayList(testNode.getProperty(JCR_DATA).getValue(BINARIES));
    assertEquals(blobSize + UNIQUE_KEY_SIZE, blobs.get(0).length());
    return data;
}
Also used : ArrayBasedBlob(org.apache.jackrabbit.oak.plugins.memory.ArrayBasedBlob) Blob(org.apache.jackrabbit.oak.api.Blob) IndexInput(org.apache.lucene.store.IndexInput) IndexOutput(org.apache.lucene.store.IndexOutput) NodeBuilder(org.apache.jackrabbit.oak.spi.state.NodeBuilder)

Example 68 with IndexOutput

use of org.apache.lucene.store.IndexOutput in project jackrabbit-oak by apache.

the class OakDirectoryTest method dirNameInException_Writes.

@Test
public void dirNameInException_Writes() throws Exception {
    FailOnDemandBlobStore blobStore = new FailOnDemandBlobStore();
    FileStore store = FileStoreBuilder.fileStoreBuilder(tempFolder.getRoot()).withMemoryMapping(false).withBlobStore(blobStore).build();
    SegmentNodeStore nodeStore = SegmentNodeStoreBuilders.builder(store).build();
    String indexPath = "/foo/bar";
    int minFileSize = SegmentTestConstants.MEDIUM_LIMIT;
    int blobSize = minFileSize + 1000;
    builder = nodeStore.getRoot().builder();
    builder.setProperty(LuceneIndexConstants.BLOB_SIZE, blobSize);
    Directory dir = createDir(builder, false, indexPath);
    blobStore.startFailing();
    IndexOutput o = dir.createOutput("test1.txt", IOContext.DEFAULT);
    try {
        o.writeBytes(randomBytes(blobSize + 10), blobSize + 10);
        fail();
    } catch (IOException e) {
        assertThat(e.getMessage(), containsString(indexPath));
        assertThat(e.getMessage(), containsString("test1.txt"));
    }
    blobStore.reset();
    IndexOutput o3 = dir.createOutput("test3.txt", IOContext.DEFAULT);
    o3.writeBytes(randomBytes(minFileSize), minFileSize);
    blobStore.startFailing();
    try {
        o3.flush();
        fail();
    } catch (IOException e) {
        assertThat(e.getMessage(), containsString(indexPath));
        assertThat(e.getMessage(), containsString("test3.txt"));
    }
    store.close();
}
Also used : FileStore(org.apache.jackrabbit.oak.segment.file.FileStore) IndexOutput(org.apache.lucene.store.IndexOutput) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) IOException(java.io.IOException) SegmentNodeStore(org.apache.jackrabbit.oak.segment.SegmentNodeStore) Directory(org.apache.lucene.store.Directory) Test(org.junit.Test)

Example 69 with IndexOutput

use of org.apache.lucene.store.IndexOutput in project jackrabbit-oak by apache.

the class OakDirectoryTest method writeFile.

private static void writeFile(Directory directory, String fileName, long size) throws Exception {
    IndexOutput o = directory.createOutput(fileName, IOContext.DEFAULT);
    o.copyBytes(new InputStreamDataInput(new NullInputStream(size)), size);
    o.close();
}
Also used : IndexOutput(org.apache.lucene.store.IndexOutput) InputStreamDataInput(org.apache.lucene.store.InputStreamDataInput) NullInputStream(org.apache.commons.io.input.NullInputStream)

Example 70 with IndexOutput

use of org.apache.lucene.store.IndexOutput in project elasticsearch by elastic.

the class Store method markStoreCorrupted.

/**
     * Marks this store as corrupted. This method writes a <tt>corrupted_${uuid}</tt> file containing the given exception
     * message. If a store contains a <tt>corrupted_${uuid}</tt> file {@link #isMarkedCorrupted()} will return <code>true</code>.
     */
public void markStoreCorrupted(IOException exception) throws IOException {
    ensureOpen();
    if (!isMarkedCorrupted()) {
        String uuid = CORRUPTED + UUIDs.randomBase64UUID();
        try (IndexOutput output = this.directory().createOutput(uuid, IOContext.DEFAULT)) {
            CodecUtil.writeHeader(output, CODEC, VERSION);
            BytesStreamOutput out = new BytesStreamOutput();
            out.writeException(exception);
            BytesReference bytes = out.bytes();
            output.writeVInt(bytes.length());
            BytesRef ref = bytes.toBytesRef();
            output.writeBytes(ref.bytes, ref.offset, ref.length);
            CodecUtil.writeFooter(output);
        } catch (IOException ex) {
            logger.warn("Can't mark store as corrupted", ex);
        }
        directory().sync(Collections.singleton(uuid));
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexOutput(org.apache.lucene.store.IndexOutput) IOException(java.io.IOException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

IndexOutput (org.apache.lucene.store.IndexOutput)157 IndexInput (org.apache.lucene.store.IndexInput)69 Directory (org.apache.lucene.store.Directory)66 RAMDirectory (org.apache.lucene.store.RAMDirectory)28 FilterDirectory (org.apache.lucene.store.FilterDirectory)26 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)22 CorruptIndexException (org.apache.lucene.index.CorruptIndexException)19 CorruptingIndexOutput (org.apache.lucene.store.CorruptingIndexOutput)18 BytesRef (org.apache.lucene.util.BytesRef)18 RAMFile (org.apache.lucene.store.RAMFile)16 RAMOutputStream (org.apache.lucene.store.RAMOutputStream)16 IOException (java.io.IOException)14 IOContext (org.apache.lucene.store.IOContext)12 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)11 RAMInputStream (org.apache.lucene.store.RAMInputStream)11 NRTCachingDirectory (org.apache.lucene.store.NRTCachingDirectory)10 ArrayList (java.util.ArrayList)9 IntersectVisitor (org.apache.lucene.index.PointValues.IntersectVisitor)9 Relation (org.apache.lucene.index.PointValues.Relation)9 HashMap (java.util.HashMap)8