Java Code Examples for org.apache.lucene.store.IndexOutput#writeBytes()

The following examples show how to use org.apache.lucene.store.IndexOutput#writeBytes() . 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: TestIndexInput.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testRawIndexInputRead() throws IOException {
  for (int i = 0; i < 10; i++) {
    Random random = random();
    final Directory dir = newDirectory();
    IndexOutput os = dir.createOutput("foo", newIOContext(random));
    os.writeBytes(READ_TEST_BYTES, READ_TEST_BYTES.length);
    os.close();
    IndexInput is = dir.openInput("foo", newIOContext(random));
    checkReads(is, IOException.class);
    is.close();
  
    os = dir.createOutput("bar", newIOContext(random));
    os.writeBytes(RANDOM_TEST_BYTES, RANDOM_TEST_BYTES.length);
    os.close();
    is = dir.openInput("bar", newIOContext(random));
    checkRandomReads(is);
    is.close();
    dir.close();
  }
}
 
Example 2
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 3
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 4
Source File: TestIndexFileDeleter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void copyFile(Directory dir, String src, String dest) throws IOException {
  IndexInput in = dir.openInput(src, newIOContext(random()));
  IndexOutput out = dir.createOutput(dest, newIOContext(random()));
  byte[] b = new byte[1024];
  long remainder = in.length();
  while(remainder > 0) {
    int len = (int) Math.min(b.length, remainder);
    in.readBytes(b, 0, len);
    out.writeBytes(b, len);
    remainder -= len;
  }
  in.close();
  out.close();
}
 
Example 5
Source File: TestPagedBytes.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Ignore // memory hole
public void testOverflow() throws IOException {
  BaseDirectoryWrapper dir = newFSDirectory(createTempDir("testOverflow"));
  if (dir instanceof MockDirectoryWrapper) {
    ((MockDirectoryWrapper)dir).setThrottling(MockDirectoryWrapper.Throttling.NEVER);
  }
  final int blockBits = TestUtil.nextInt(random(), 14, 28);
  final int blockSize = 1 << blockBits;
  byte[] arr = new byte[TestUtil.nextInt(random(), blockSize / 2, blockSize * 2)];
  for (int i = 0; i < arr.length; ++i) {
    arr[i] = (byte) i;
  }
  final long numBytes = (1L << 31) + TestUtil.nextInt(random(), 1, blockSize * 3);
  final PagedBytes p = new PagedBytes(blockBits);
  final IndexOutput out = dir.createOutput("foo", IOContext.DEFAULT);
  for (long i = 0; i < numBytes; ) {
    assertEquals(i, out.getFilePointer());
    final int len = (int) Math.min(arr.length, numBytes - i);
    out.writeBytes(arr, len);
    i += len;
  }
  assertEquals(numBytes, out.getFilePointer());
  out.close();
  final IndexInput in = dir.openInput("foo", IOContext.DEFAULT);
  p.copy(in, numBytes);
  final PagedBytes.Reader reader = p.freeze(random().nextBoolean());

  for (long offset : new long[] {0L, Integer.MAX_VALUE, numBytes - 1,
      TestUtil.nextLong(random(), 1, numBytes - 2)}) {
    BytesRef b = new BytesRef();
    reader.fillSlice(b, offset, 1);
    assertEquals(arr[(int) (offset % arr.length)], b.bytes[b.offset]);
  }
  in.close();
  dir.close();
}
 
Example 6
Source File: CustomBufferedIndexInput.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Flushes the in-memory bufer to the given output, copying at most
 * <code>numBytes</code>.
 * <p>
 * <b>NOTE:</b> this method does not refill the buffer, however it does
 * advance the buffer position.
 * 
 * @return the number of bytes actually flushed from the in-memory buffer.
 */
protected int flushBuffer(IndexOutput out, long numBytes) throws IOException {
  int toCopy = bufferLength - bufferPosition;
  if (toCopy > numBytes) {
    toCopy = (int) numBytes;
  }
  if (toCopy > 0) {
    out.writeBytes(buffer, bufferPosition, toCopy);
    bufferPosition += toCopy;
  }
  return toCopy;
}
 
Example 7
Source File: ReusedBufferedIndexInput.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
/**
 * Flushes the in-memory bufer to the given output, copying at most
 * <code>numBytes</code>.
 * <p>
 * <b>NOTE:</b> this method does not refill the buffer, however it does
 * advance the buffer position.
 * 
 * @return the number of bytes actually flushed from the in-memory buffer.
 */
protected final int flushBuffer(IndexOutput out, long numBytes) throws IOException {
  int toCopy = bufferLength - bufferPosition;
  if (toCopy > numBytes) {
    toCopy = (int) numBytes;
  }
  if (toCopy > 0) {
    out.writeBytes(buffer, bufferPosition, toCopy);
    bufferPosition += toCopy;
  }
  return toCopy;
}
 
Example 8
Source File: CacheDirectoryLoadUnloadTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private void write(IndexOutput output, long length, Random random) throws IOException {
  byte[] buf = new byte[10000];
  while (length > 0) {
    random.nextBytes(buf);
    int len = (int) Math.min(length, buf.length);
    output.writeBytes(buf, len);
    length -= len;
  }
}
 
Example 9
Source File: CacheDirectoryTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
@Test
public void test2() throws IOException {
  IndexOutput output = _cacheDirectory.createOutput("test.file", IOContext.DEFAULT);
  byte[] buf = new byte[9000];
  for (int i = 0; i < buf.length; i++) {
    buf[i] = (byte) i;
  }
  output.writeBytes(buf, buf.length);
  output.close();

  IndexInput input = _cacheDirectory.openInput("test.file", IOContext.DEFAULT);
  assertEquals(9000, input.length());
  input.close();
}
 
Example 10
Source File: HdfsDirectoryResourceTest.java    From incubator-retired-blur with Apache License 2.0 5 votes vote down vote up
private void writeData(IndexOutput output, long write) throws IOException {
  byte[] buf = new byte[1024];
  while (write > 0) {
    random.nextBytes(buf);
    int length = (int) Math.min(write, buf.length);
    output.writeBytes(buf, length);
    write -= length;
  }
}
 
Example 11
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 12
Source File: CustomBufferedIndexInput.java    From clue with Apache License 2.0 5 votes vote down vote up
/**
 * Flushes the in-memory bufer to the given output, copying at most
 * <code>numBytes</code>.
 * <p>
 * <b>NOTE:</b> this method does not refill the buffer, however it does
 * advance the buffer position.
 * 
 * @return the number of bytes actually flushed from the in-memory buffer.
 */
protected int flushBuffer(IndexOutput out, long numBytes) throws IOException {
  int toCopy = bufferLength - bufferPosition;
  if (toCopy > numBytes) {
    toCopy = (int) numBytes;
  }
  if (toCopy > 0) {
    out.writeBytes(buffer, bufferPosition, toCopy);
    bufferPosition += toCopy;
  }
  return toCopy;
}
 
Example 13
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();
        }
      }
    }
  }
}
 
Example 14
Source File: ReplicationClient.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private void copyBytes(IndexOutput out, InputStream in) throws IOException {
  int numBytes;
  while ((numBytes = in.read(copyBuffer)) > 0) {
    out.writeBytes(copyBuffer, 0, numBytes);
  }
}
 
Example 15
Source File: OrdsBlockTreeTermsWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void writeBytesRef(IndexOutput out, BytesRef bytes) throws IOException {
  out.writeVInt(bytes.length);
  out.writeBytes(bytes.bytes, bytes.offset, bytes.length);
}
 
Example 16
Source File: VersionBlockTreeTermsWriter.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
private static void writeBytesRef(IndexOutput out, BytesRef bytes) throws IOException {
  out.writeVInt(bytes.length);
  out.writeBytes(bytes.bytes, bytes.offset, bytes.length);
}