Java Code Examples for org.apache.lucene.store.RAMDirectory#createOutput()

The following examples show how to use org.apache.lucene.store.RAMDirectory#createOutput() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ComplexVectorTest.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testReadWrite() {
  Vector v1 = new ComplexVector(new short[] { -1, 8000, 16000 });
  RAMDirectory directory = new RAMDirectory();
  try {
    IndexOutput indexOutput = directory.createOutput("complexvectors.bin", IOContext.DEFAULT);
    v1.writeToLuceneStream(indexOutput);
    indexOutput.close();

    IndexInput indexInput = directory.openInput("complexvectors.bin", IOContext.DEFAULT);
    ComplexVector cv2 = new ComplexVector(3, Mode.POLAR_SPARSE);
    cv2.readFromLuceneStream(indexInput);
    assertFloatArrayEquals(
        new float[] {0, 0, -0.997290f, 0.073564f, 0.989176f, -0.1467304f},
        cv2.getCoordinates(), TOL);
  } catch (IOException e) {
    e.printStackTrace();
    fail();
  }
  directory.close();
}
 
Example 2
Source File: BinaryVectorTest.java    From semanticvectors with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test
public void testGenerateRandomVectorWriteAndRead() {
  Random random = new Random(0);

  Vector vector = VectorFactory.generateRandomVector(VectorType.BINARY, 64, 2, random);
  // The exact string depends on fail Java's implementation of Random, so we only check for length.
  String vectorString = vector.writeToString();
  assertEquals(64, vectorString.length());

  RAMDirectory directory = new RAMDirectory();
  try {
    IndexOutput indexOutput = directory.createOutput("binaryvectors.bin", IOContext.DEFAULT);
    vector.writeToLuceneStream(indexOutput);
    indexOutput.close();
    IndexInput indexInput = directory.openInput("binaryvectors.bin", IOContext.DEFAULT);
    Vector vector2 = VectorFactory.createZeroVector(VectorType.BINARY, 64);
    assertEquals("0000000000000000000000000000000000000000000000000000000000000000", vector2.writeToString());
    vector2.readFromLuceneStream(indexInput);
    assertEquals(vectorString, vector2.writeToString());
  } catch (IOException e) {
    e.printStackTrace();
    fail();
  }
  directory.close();
}
 
Example 3
Source File: CacheIndexInputTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@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);
  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();
}
 
Example 4
Source File: CacheIndexInputTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@Test
public void test2() throws IOException {
  Cache cache = getCache();
  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();
}
 
Example 5
Source File: CacheIndexOutputTest.java    From incubator-retired-blur with Apache License 2.0 6 votes vote down vote up
@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();
  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();
}
 
Example 6
Source File: RAMDirectoryUtil.java    From linden with Apache License 2.0 5 votes vote down vote up
/**
 * Read a number of files from a data input to a ram directory.
 * @param in  the data input
 * @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();
        }
      }
    }
  }
}
 
Example 7
Source File: RAMDirectoryUtil.java    From RDFS with Apache License 2.0 5 votes vote down vote up
/**
 * Read a number of files from a data input to a ram directory.
 * @param in  the data input
 * @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 {
        output = dir.createOutput(name);

        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();
        }
      }
    }
  }
}
 
Example 8
Source File: RAMDirectoryUtil.java    From hadoop-gpu with Apache License 2.0 5 votes vote down vote up
/**
 * Read a number of files from a data input to a ram directory.
 * @param in  the data input
 * @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 {
        output = dir.createOutput(name);

        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();
        }
      }
    }
  }
}