Example usage for org.apache.lucene.store RAMDirectory createOutput

List of usage examples for org.apache.lucene.store RAMDirectory createOutput

Introduction

In this page you can find the example usage for org.apache.lucene.store RAMDirectory createOutput.

Prototype

@Override
    public IndexOutput createOutput(String name, IOContext context) throws IOException 

Source Link

Usage

From source file:com.bah.lucene.blockcache_v2.CacheIndexInputTest.java

License:Apache License

@Test
public void test1() throws IOException {
    RAMDirectory directory = new RAMDirectory();

    String name = "test";

    IndexOutput output = directory.createOutput(name, IOContext.DEFAULT);
    byte[] bs = "hello world".getBytes();
    output.writeBytes(bs, bs.length);//from w ww.  ja  v  a  2 s.  c om
    output.close();

    IndexInput input = directory.openInput(name, IOContext.DEFAULT);
    Cache cache = getCache();
    CacheIndexInput cacheInput = new CacheIndexInput(null, name, input, cache);
    byte[] buf = new byte[bs.length];
    cacheInput.readBytes(buf, 0, buf.length);
    cacheInput.close();

    assertArrayEquals(bs, buf);
    directory.close();
}

From source file:com.bah.lucene.blockcache_v2.CacheIndexInputTest.java

License:Apache License

@Test
public void test2() throws IOException {
    Cache cache = getCache();/*from  w w w.ja v  a2 s.c  o m*/
    RAMDirectory directory = new RAMDirectory();
    Random random = new Random(seed);

    String name = "test2";
    long size = (10 * 1024 * 1024) + 13;

    IndexOutput output = directory.createOutput(name, IOContext.DEFAULT);
    writeRandomData(size, random, output);
    output.close();

    IndexInput input = directory.openInput(name, IOContext.DEFAULT);
    IndexInput testInput = new CacheIndexInput(null, name, input.clone(), cache);
    readRandomData(input, testInput, random, sampleSize, maxBufSize, maxOffset);
    readRandomDataShort(input, testInput, random, sampleSize);
    readRandomDataInt(input, testInput, random, sampleSize);
    readRandomDataLong(input, testInput, random, sampleSize);
    testInput.close();
    input.close();
    directory.close();
}

From source file:com.bah.lucene.blockcache_v2.CacheIndexOutputTest.java

License:Apache License

@Test
public void test1() throws IOException {
    Random random = new Random(seed);
    RAMDirectory directory = new RAMDirectory();
    IndexOutput output = directory.createOutput("test", IOContext.DEFAULT);

    Cache cache = CacheIndexInputTest.getCache();
    CacheIndexOutput indexOutput = new CacheIndexOutput(null, "test", output, cache);
    indexOutput.writeByte((byte) 1);
    indexOutput.writeByte((byte) 2);
    byte[] b = new byte[16000];
    random.nextBytes(b);/*w  w w  . j a  v  a 2  s  . c o m*/
    indexOutput.writeBytes(b, 16000);
    indexOutput.close();

    IndexInput input = directory.openInput("test", IOContext.DEFAULT);
    assertEquals(16002, input.length());
    assertEquals(1, input.readByte());
    assertEquals(2, input.readByte());

    byte[] buf = new byte[16000];
    input.readBytes(buf, 0, 16000);
    input.close();
    assertArrayEquals(b, buf);
    directory.close();
}

From source file:com.bah.lucene.blockcache_v2.CacheIndexOutputTest.java

License:Apache License

@Test
public void test2() throws IOException {
    Cache cache = CacheIndexInputTest.getCache();
    RAMDirectory directory = new RAMDirectory();
    RAMDirectory directory2 = new RAMDirectory();

    Random random = new Random(seed);

    String name = "test2";
    long size = (10 * 1024 * 1024) + 13;

    IndexOutput output = directory.createOutput(name, IOContext.DEFAULT);
    IndexOutput output2 = directory2.createOutput(name, IOContext.DEFAULT);
    CacheIndexOutput cacheIndexOutput = new CacheIndexOutput(null, name, output2, cache);
    CacheIndexInputTest.writeRandomData(size, random, output, cacheIndexOutput);
    output.close();//from   w w  w . ja  v a 2s. c  om
    cacheIndexOutput.close();

    IndexInput input = directory.openInput(name, IOContext.DEFAULT);
    IndexInput testInput = directory2.openInput(name, IOContext.DEFAULT);
    CacheIndexInputTest.readRandomData(input, testInput, random, sampleSize, maxBufSize, maxOffset);
    testInput.close();
    input.close();
    directory.close();
    directory2.close();
}

From source file:com.xiaomi.linden.hadoop.indexing.reduce.RAMDirectoryUtil.java

License:Apache License

/**
 * Read a number of files from a data input to a ram directory.
 * @param in  the data input// w ww.  j  ava2s  .  c o  m
 * @param dir  the ram directory
 * @throws IOException
 */
public static void readRAMFiles(DataInput in, RAMDirectory dir) throws IOException {
    int numFiles = in.readInt();

    for (int i = 0; i < numFiles; i++) {
        String name = Text.readString(in);
        long length = in.readLong();

        if (length > 0) {
            // can we avoid the extra copy?
            IndexOutput output = null;
            try {
                IOContext context = new IOContext();
                output = dir.createOutput(name, context);

                int position = 0;
                byte[] buffer = new byte[BUFFER_SIZE];

                while (position < length) {
                    int len = position + BUFFER_SIZE <= length ? BUFFER_SIZE : (int) (length - position);
                    in.readFully(buffer, 0, len);
                    output.writeBytes(buffer, 0, len);
                    position += len;
                }
            } finally {
                if (output != null) {
                    output.close();
                }
            }
        }
    }
}

From source file:org.apache.blur.store.blockcache_v2.CacheIndexOutputTest.java

License:Apache License

@Test
public void test2() throws IOException {
    Cache cache = CacheIndexInputTest.getCache();
    RAMDirectory directory = new RAMDirectory();
    RAMDirectory directory2 = new RAMDirectory();

    Random random = new Random(seed);

    String name = "test2";
    long size = (10 * 1024 * 1024) + 13;

    IndexOutput output = directory.createOutput(name, IOContext.DEFAULT);
    CacheIndexOutput cacheIndexOutput = new CacheIndexOutput(null, name, cache, directory2, IOContext.DEFAULT);
    CacheIndexInputTest.writeRandomData(size, random, output, cacheIndexOutput);
    output.close();/*from w ww .  j  av  a2  s  .  c  o m*/
    cacheIndexOutput.close();

    IndexInput input = directory.openInput(name, IOContext.DEFAULT);
    IndexInput testInput = directory2.openInput(name, IOContext.DEFAULT);
    CacheIndexInputTest.readRandomData(input, testInput, random, sampleSize, maxBufSize, maxOffset);
    testInput.close();
    input.close();
    directory.close();
    directory2.close();
}

From source file:org.elasticsearch.common.lucene.store.InputStreamIndexInputTests.java

License:Apache License

@Test
public void testSingleReadSingleByteLimit() throws IOException {
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 1);
    }/*  w w w . j ava  2 s  .  c o m*/
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 2);
    }

    output.close();

    IndexInput input = dir.openInput("test", IOContext.DEFAULT);

    for (int i = 0; i < 3; i++) {
        InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
        assertThat(input.getFilePointer(), lessThan(input.length()));
        assertThat(is.actualSizeToRead(), equalTo(1l));
        assertThat(is.read(), equalTo(1));
        assertThat(is.read(), equalTo(-1));
    }

    for (int i = 0; i < 3; i++) {
        InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
        assertThat(input.getFilePointer(), lessThan(input.length()));
        assertThat(is.actualSizeToRead(), equalTo(1l));
        assertThat(is.read(), equalTo(2));
        assertThat(is.read(), equalTo(-1));
    }

    assertThat(input.getFilePointer(), equalTo(input.length()));
    InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
    assertThat(is.actualSizeToRead(), equalTo(0l));
    assertThat(is.read(), equalTo(-1));
}

From source file:org.elasticsearch.common.lucene.store.InputStreamIndexInputTests.java

License:Apache License

@Test
public void testReadMultiSingleByteLimit1() throws IOException {
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 1);
    }/*from  www .j  a v  a2  s  .co m*/
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 2);
    }

    output.close();

    IndexInput input = dir.openInput("test", IOContext.DEFAULT);

    byte[] read = new byte[2];

    for (int i = 0; i < 3; i++) {
        assertThat(input.getFilePointer(), lessThan(input.length()));
        InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
        assertThat(is.actualSizeToRead(), equalTo(1l));
        assertThat(is.read(read), equalTo(1));
        assertThat(read[0], equalTo((byte) 1));
    }

    for (int i = 0; i < 3; i++) {
        assertThat(input.getFilePointer(), lessThan(input.length()));
        InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
        assertThat(is.actualSizeToRead(), equalTo(1l));
        assertThat(is.read(read), equalTo(1));
        assertThat(read[0], equalTo((byte) 2));
    }

    assertThat(input.getFilePointer(), equalTo(input.length()));
    InputStreamIndexInput is = new InputStreamIndexInput(input, 1);
    assertThat(is.actualSizeToRead(), equalTo(0l));
    assertThat(is.read(read), equalTo(-1));
}

From source file:org.elasticsearch.common.lucene.store.InputStreamIndexInputTests.java

License:Apache License

@Test
public void testSingleReadTwoBytesLimit() throws IOException {
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 1);
    }//from  ww w  .jav a 2s.  co m
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 2);
    }

    output.close();

    IndexInput input = dir.openInput("test", IOContext.DEFAULT);

    assertThat(input.getFilePointer(), lessThan(input.length()));
    InputStreamIndexInput is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(2l));
    assertThat(is.read(), equalTo(1));
    assertThat(is.read(), equalTo(1));
    assertThat(is.read(), equalTo(-1));

    assertThat(input.getFilePointer(), lessThan(input.length()));
    is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(2l));
    assertThat(is.read(), equalTo(1));
    assertThat(is.read(), equalTo(2));
    assertThat(is.read(), equalTo(-1));

    assertThat(input.getFilePointer(), lessThan(input.length()));
    is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(2l));
    assertThat(is.read(), equalTo(2));
    assertThat(is.read(), equalTo(2));
    assertThat(is.read(), equalTo(-1));

    assertThat(input.getFilePointer(), equalTo(input.length()));
    is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(0l));
    assertThat(is.read(), equalTo(-1));
}

From source file:org.elasticsearch.common.lucene.store.InputStreamIndexInputTests.java

License:Apache License

@Test
public void testReadMultiTwoBytesLimit1() throws IOException {
    RAMDirectory dir = new RAMDirectory();
    IndexOutput output = dir.createOutput("test", IOContext.DEFAULT);
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 1);
    }/*from  w w  w . j av  a  2  s  .c  om*/
    for (int i = 0; i < 3; i++) {
        output.writeByte((byte) 2);
    }

    output.close();

    IndexInput input = dir.openInput("test", IOContext.DEFAULT);

    byte[] read = new byte[2];

    assertThat(input.getFilePointer(), lessThan(input.length()));
    InputStreamIndexInput is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(2l));
    assertThat(is.read(read), equalTo(2));
    assertThat(read[0], equalTo((byte) 1));
    assertThat(read[1], equalTo((byte) 1));

    assertThat(input.getFilePointer(), lessThan(input.length()));
    is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(2l));
    assertThat(is.read(read), equalTo(2));
    assertThat(read[0], equalTo((byte) 1));
    assertThat(read[1], equalTo((byte) 2));

    assertThat(input.getFilePointer(), lessThan(input.length()));
    is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(2l));
    assertThat(is.read(read), equalTo(2));
    assertThat(read[0], equalTo((byte) 2));
    assertThat(read[1], equalTo((byte) 2));

    assertThat(input.getFilePointer(), equalTo(input.length()));
    is = new InputStreamIndexInput(input, 2);
    assertThat(is.actualSizeToRead(), equalTo(0l));
    assertThat(is.read(read), equalTo(-1));
}