public Fields getFields() throws IOException {
    if (hasTermVectors() && isExists()) {
      if (!sourceCopied) { // make the bytes safe
        headerRef = headerRef.copyBytesArray();
        termVectors = termVectors.copyBytesArray();
      }
      return new TermVectorsFields(headerRef, termVectors);
    } else {
      return new Fields() {
        @Override
        public Iterator<String> iterator() {
          return Iterators.emptyIterator();
        }

        @Override
        public Terms terms(String field) throws IOException {
          return null;
        }

        @Override
        public int size() {
          return 0;
        }
      };
    }
  }
 public void testCopyBytesArray() throws IOException {
   // small PBR which would normally share the first page
   int length = randomIntBetween(10, PAGE_SIZE);
   BytesReference pbr = newBytesReference(length);
   BytesArray ba = pbr.copyBytesArray();
   BytesArray ba2 = pbr.copyBytesArray();
   assertNotNull(ba);
   assertNotSame(ba, ba2);
   assertNotSame(ba.array(), ba2.array());
 }
Esempio n. 3
0
 @Override
 protected void beforeStart() {
   if (sourceUnsafe) {
     source = source.copyBytesArray();
     sourceUnsafe = false;
   }
 }
Esempio n. 4
0
 public BytesReference safeSource() {
   if (sourceUnsafe) {
     source = source.copyBytesArray();
     sourceUnsafe = false;
   }
   return source;
 }
  public void testSliceCopyBytesArray() throws IOException {
    int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8));
    BytesReference pbr = newBytesReference(length);
    int sliceOffset = randomIntBetween(0, pbr.length());
    int sliceLength = randomIntBetween(pbr.length() - sliceOffset, pbr.length() - sliceOffset);
    BytesReference slice = pbr.slice(sliceOffset, sliceLength);

    BytesArray ba1 = slice.copyBytesArray();
    BytesArray ba2 = slice.copyBytesArray();
    assertNotNull(ba1);
    assertNotNull(ba2);
    assertNotSame(ba1.array(), ba2.array());
    assertArrayEquals(slice.toBytes(), ba1.array());
    assertArrayEquals(slice.toBytes(), ba2.array());
    assertArrayEquals(ba1.array(), ba2.array());
  }