private void verifyData(ByteBufferDirectory dir) throws IOException {
    byte[] test = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};
    assertThat(dir.fileExists("value1"), equalTo(true));
    assertThat(dir.fileLength("value1"), equalTo(38l));

    IndexInput indexInput = dir.openInput("value1", IOContext.DEFAULT);
    indexInput.readBytes(test, 0, 5);
    assertThat(test[0], equalTo((byte) 8));
    assertThat(indexInput.readInt(), equalTo(-1));
    assertThat(indexInput.readLong(), equalTo((long) 10));
    assertThat(indexInput.readInt(), equalTo(0));
    assertThat(indexInput.readInt(), equalTo(0));
    indexInput.readBytes(test, 0, 8);
    assertThat(test[0], equalTo((byte) 1));
    assertThat(test[7], equalTo((byte) 8));
    indexInput.readBytes(test, 0, 5);
    assertThat(test[0], equalTo((byte) 1));
    assertThat(test[4], equalTo((byte) 5));

    indexInput.seek(28);
    assertThat(indexInput.readByte(), equalTo((byte) 4));
    indexInput.seek(30);
    assertThat(indexInput.readByte(), equalTo((byte) 6));

    indexInput.seek(0);
    indexInput.readBytes(test, 0, 5);
    assertThat(test[0], equalTo((byte) 8));

    indexInput.close();

    indexInput = dir.openInput("value1", IOContext.DEFAULT);
    // iterate over all the data
    for (int i = 0; i < 38; i++) {
      indexInput.readByte();
    }
    indexInput.close();
  }
Пример #2
0
 private void readIndexInputFullyWithRandomSeeks(IndexInput indexInput) throws IOException {
   BytesRef ref = new BytesRef(scaledRandomIntBetween(1, 1024));
   long pos = 0;
   while (pos < indexInput.length()) {
     assertEquals(pos, indexInput.getFilePointer());
     int op = random().nextInt(5);
     if (op == 0) {
       int shift = 100 - randomIntBetween(0, 200);
       pos = Math.min(indexInput.length() - 1, Math.max(0, pos + shift));
       indexInput.seek(pos);
     } else if (op == 1) {
       indexInput.readByte();
       pos++;
     } else {
       int min = (int) Math.min(indexInput.length() - pos, ref.bytes.length);
       indexInput.readBytes(ref.bytes, ref.offset, min);
       pos += min;
     }
   }
 }
Пример #3
0
 @Test
 public void testVerifyingIndexOutput() throws IOException {
   Directory dir = newDirectory();
   IndexOutput output = dir.createOutput("foo.bar", IOContext.DEFAULT);
   int iters = scaledRandomIntBetween(10, 100);
   for (int i = 0; i < iters; i++) {
     BytesRef bytesRef = new BytesRef(TestUtil.randomRealisticUnicodeString(random(), 10, 1024));
     output.writeBytes(bytesRef.bytes, bytesRef.offset, bytesRef.length);
   }
   CodecUtil.writeFooter(output);
   output.close();
   IndexInput indexInput = dir.openInput("foo.bar", IOContext.DEFAULT);
   String checksum = Store.digestToString(CodecUtil.retrieveChecksum(indexInput));
   indexInput.seek(0);
   BytesRef ref = new BytesRef(scaledRandomIntBetween(1, 1024));
   long length = indexInput.length();
   IndexOutput verifyingOutput =
       new Store.LuceneVerifyingIndexOutput(
           new StoreFileMetaData("foo1.bar", length, checksum),
           dir.createOutput("foo1.bar", IOContext.DEFAULT));
   while (length > 0) {
     if (random().nextInt(10) == 0) {
       verifyingOutput.writeByte(indexInput.readByte());
       length--;
     } else {
       int min = (int) Math.min(length, ref.bytes.length);
       indexInput.readBytes(ref.bytes, ref.offset, min);
       verifyingOutput.writeBytes(ref.bytes, ref.offset, min);
       length -= min;
     }
   }
   Store.verify(verifyingOutput);
   verifyingOutput.writeByte((byte) 0x0);
   try {
     Store.verify(verifyingOutput);
     fail("should be a corrupted index");
   } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
     // ok
   }
   IOUtils.close(indexInput, verifyingOutput, dir);
 }
 @Override
 public byte readByte() throws IOException {
   final byte b = main.readByte();
   digest.update(b);
   return b;
 }