예제 #1
0
 private void writeBytesReference(BytesReference value) throws IOException {
   BytesReference bytes = value;
   if (!bytes.hasArray()) {
     bytes = bytes.toBytesArray();
   }
   generator.writeBinary(bytes.array(), bytes.arrayOffset(), bytes.length());
 }
예제 #2
0
 /**
  * Writes the binary content of the given BytesReference Use {@link
  * org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back
  */
 public XContentBuilder field(String name, BytesReference value) throws IOException {
   field(name);
   if (!value.hasArray()) {
     value = value.toBytesArray();
   }
   generator.writeBinary(value.array(), value.arrayOffset(), value.length());
   return this;
 }
 public void testArrayOffset() throws IOException {
   int length = randomInt(PAGE_SIZE * randomIntBetween(2, 5));
   BytesReference pbr = newBytesReference(length);
   if (pbr.hasArray()) {
     assertEquals(0, pbr.arrayOffset());
   } else {
     expectThrows(IllegalStateException.class, () -> pbr.arrayOffset());
   }
 }
예제 #4
0
 /**
  * Writes the binary content of the given BytesReference Use {@link
  * org.elasticsearch.common.xcontent.XContentParser#binaryValue()} to read the value back
  */
 public XContentBuilder value(BytesReference value) throws IOException {
   if (value == null) {
     return nullValue();
   }
   if (!value.hasArray()) {
     value = value.toBytesArray();
   }
   generator.writeBinary(value.array(), value.arrayOffset(), value.length());
   return this;
 }
예제 #5
0
 @Override
 public void messageReceived(final RecoveryFileChunkRequest request, TransportChannel channel)
     throws Exception {
   try (RecoveriesCollection.StatusRef statusRef =
       onGoingRecoveries.getStatusSafe(request.recoveryId(), request.shardId())) {
     final RecoveryStatus recoveryStatus = statusRef.status();
     final Store store = recoveryStatus.store();
     recoveryStatus.state().getTranslog().totalOperations(request.totalTranslogOps());
     final RecoveryState.Index indexState = recoveryStatus.state().getIndex();
     if (request.sourceThrottleTimeInNanos() != RecoveryState.Index.UNKNOWN) {
       indexState.addSourceThrottling(request.sourceThrottleTimeInNanos());
     }
     IndexOutput indexOutput;
     if (request.position() == 0) {
       indexOutput =
           recoveryStatus.openAndPutIndexOutput(request.name(), request.metadata(), store);
     } else {
       indexOutput = recoveryStatus.getOpenIndexOutput(request.name());
     }
     BytesReference content = request.content();
     if (!content.hasArray()) {
       content = content.toBytesArray();
     }
     RateLimiter rl = recoverySettings.rateLimiter();
     if (rl != null) {
       long bytes = bytesSinceLastPause.addAndGet(content.length());
       if (bytes > rl.getMinPauseCheckBytes()) {
         // Time to pause
         bytesSinceLastPause.addAndGet(-bytes);
         long throttleTimeInNanos = rl.pause(bytes);
         indexState.addTargetThrottling(throttleTimeInNanos);
         recoveryStatus.indexShard().recoveryStats().addThrottleTime(throttleTimeInNanos);
       }
     }
     indexOutput.writeBytes(content.array(), content.arrayOffset(), content.length());
     indexState.addRecoveredBytesToFile(request.name(), content.length());
     if (indexOutput.getFilePointer() >= request.length() || request.lastChunk()) {
       try {
         Store.verify(indexOutput);
       } finally {
         // we are done
         indexOutput.close();
       }
       // write the checksum
       recoveryStatus.legacyChecksums().add(request.metadata());
       final String temporaryFileName = recoveryStatus.getTempNameForFile(request.name());
       assert Arrays.asList(store.directory().listAll()).contains(temporaryFileName);
       store.directory().sync(Collections.singleton(temporaryFileName));
       IndexOutput remove = recoveryStatus.removeOpenIndexOutputs(request.name());
       assert remove == null || remove == indexOutput; // remove maybe null if we got finished
     }
   }
   channel.sendResponse(TransportResponse.Empty.INSTANCE);
 }
 public static int bytesHashCode(BytesReference a) {
   if (!a.hasArray()) {
     a = a.toBytesArray();
   }
   int result = 0;
   final int end = a.arrayOffset() + a.length();
   for (int i = a.arrayOffset(); i < end; i++) {
     result = 31 * result + a.array()[i];
   }
   return result;
 }
 public void testSliceArrayOffset() throws IOException {
   int length = randomInt(PAGE_SIZE * randomIntBetween(2, 5));
   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);
   if (slice.hasArray()) {
     assertEquals(sliceOffset, slice.arrayOffset());
   } else {
     expectThrows(IllegalStateException.class, () -> slice.arrayOffset());
   }
 }
 public static boolean bytesEqual(BytesReference a, BytesReference b) {
   if (a == b) {
     return true;
   }
   if (a.length() != b.length()) {
     return false;
   }
   if (!a.hasArray()) {
     a = a.toBytesArray();
   }
   if (!b.hasArray()) {
     b = b.toBytesArray();
   }
   int bUpTo = b.arrayOffset();
   final byte[] aArray = a.array();
   final byte[] bArray = b.array();
   final int end = a.arrayOffset() + a.length();
   for (int aUpTo = a.arrayOffset(); aUpTo < end; aUpTo++, bUpTo++) {
     if (aArray[aUpTo] != bArray[bUpTo]) {
       return false;
     }
   }
   return true;
 }
 @Override
 protected void writeObjectRaw(String fieldName, BytesReference content, OutputStream bos)
     throws IOException {
   writeFieldName(fieldName);
   SmileParser parser;
   if (content.hasArray()) {
     parser =
         SmileXContent.smileFactory.createParser(
             content.array(), content.arrayOffset(), content.length());
   } else {
     parser = SmileXContent.smileFactory.createParser(content.streamInput());
   }
   try {
     parser.nextToken();
     generator.copyCurrentStructure(parser);
   } finally {
     parser.close();
   }
 }
예제 #10
0
 public static StreamInput wrap(BytesReference reference) {
   if (reference.hasArray() == false) {
     reference = reference.toBytesArray();
   }
   return wrap(reference.array(), reference.arrayOffset(), reference.length());
 }