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

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

Introduction

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

Prototype

@Override
public IndexInput openInput(String name, IOContext context) throws IOException 

Source Link

Document

Returns a stream reading an existing file.

Usage

From source file:DocIndexer.java

License:Apache License

private byte[] zip(RAMDirectory dir) throws IOException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream();
    try (ZipOutputStream zip = new ZipOutputStream(buf)) {
        for (String name : dir.listAll()) {
            try (IndexInput in = dir.openInput(name, null)) {
                int len = (int) in.length();
                byte[] tmp = new byte[len];
                ZipEntry entry = new ZipEntry(name);
                entry.setSize(len);/* w  w w  . jav  a 2  s. co  m*/
                in.readBytes(tmp, 0, len);
                zip.putNextEntry(entry);
                zip.write(tmp, 0, len);
                zip.closeEntry();
            }
        }
    }

    return buf.toByteArray();
}

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  .  j a  v  a2 s. c o m*/
    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 . j a  v a  2 s.  co  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  ww .  ja  v a  2s .co 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();/*w ww  . jav  a  2 s.  co 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:com.sensei.indexing.hadoop.reduce.RAMDirectoryUtil.java

License:Apache License

/**
 * Write a number of files from a ram directory to a data output.
 * @param out  the data output/*w w  w . j  a  v  a2  s.c  o  m*/
 * @param dir  the ram directory
 * @param names  the names of the files to write
 * @throws IOException
 */
public static void writeRAMFiles(DataOutput out, RAMDirectory dir, String[] names) throws IOException {
    out.writeInt(names.length);

    for (int i = 0; i < names.length; i++) {
        Text.writeString(out, names[i]);
        long length = dir.fileLength(names[i]);
        out.writeLong(length);

        if (length > 0) {
            // can we avoid the extra copy?
            IndexInput input = null;
            try {
                input = dir.openInput(names[i], BUFFER_SIZE);

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

                while (position < length) {
                    int len = position + BUFFER_SIZE <= length ? BUFFER_SIZE : (int) (length - position);
                    input.readBytes(buffer, 0, len);
                    out.write(buffer, 0, len);
                    position += len;
                }
            } finally {
                if (input != null) {
                    input.close();
                }
            }
        }
    }
}

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

License:Apache License

/**
 * Write a number of files from a ram directory to a data output.
 * @param out  the data output//w  w  w.  ja v a 2  s . c  om
 * @param dir  the ram directory
 * @throws IOException
 */
public static void writeRAMFiles(DataOutput out, RAMDirectory dir) throws IOException {
    String[] names = dir.listAll();
    out.writeInt(names.length);
    for (int i = 0; i < names.length; i++) {
        Text.writeString(out, names[i]);
        long length = dir.fileLength(names[i]);
        out.writeLong(length);

        if (length > 0) {
            // can we avoid the extra copy?
            IndexInput input = null;
            try {
                IOContext context = new IOContext();
                input = dir.openInput(names[i], context);

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

                while (position < length) {
                    int len = position + BUFFER_SIZE <= length ? BUFFER_SIZE : (int) (length - position);
                    input.readBytes(buffer, 0, len);
                    out.write(buffer, 0, len);
                    position += len;
                }
            } finally {
                if (input != null) {
                    input.close();
                }
            }
        }
    }
}

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

License:Apache License

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

    Cache cache = CacheIndexInputTest.getCache();
    CacheIndexOutput indexOutput = new CacheIndexOutput(null, "test", cache, directory, IOContext.DEFAULT);
    indexOutput.writeByte((byte) 1);
    indexOutput.writeByte((byte) 2);
    byte[] b = new byte[16000];
    random.nextBytes(b);// w  w  w .j  a  v  a  2s  .c om
    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: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 www . j ava  2 s .  co  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);
    }/*from  ww w .j  av a2  s . co 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));
}