Java Code Examples for org.apache.ratis.thirdparty.com.google.protobuf.ByteString#size()

The following examples show how to use org.apache.ratis.thirdparty.com.google.protobuf.ByteString#size() . 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: FileStore.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
CompletableFuture<Integer> write(
    long index, String relative, boolean close, long offset, ByteString data) {
  final int size = data != null? data.size(): 0;
  LOG.trace("write {}, offset={}, size={}, close? {} @{}:{}",
      relative, offset, size, close, getId(), index);
  final boolean createNew = offset == 0L;
  final UnderConstruction uc;
  if (createNew) {
    uc = new UnderConstruction(normalize(relative));
    files.putNew(uc);
  } else {
    try {
      uc = files.get(relative).asUnderConstruction();
    } catch (FileNotFoundException e) {
      return FileStoreCommon.completeExceptionally(
          index, "Failed to write to " + relative, e);
    }
  }

  return size == 0 && !close? CompletableFuture.completedFuture(0)
      : createNew? uc.submitCreate(this::resolve, data, close, writer, getId(), index)
      : uc.submitWrite(offset, data, close, writer, getId(), index);
}
 
Example 2
Source File: FileStore.java    From ratis with Apache License 2.0 6 votes vote down vote up
CompletableFuture<Integer> write(
    long index, String relative, boolean close, long offset, ByteString data) {
  final int size = data != null? data.size(): 0;
  LOG.trace("write {}, offset={}, size={}, close? {} @{}:{}",
      relative, offset, size, close, getId(), index);
  final boolean createNew = offset == 0L;
  final UnderConstruction uc;
  if (createNew) {
    uc = new UnderConstruction(normalize(relative));
    files.putNew(uc);
  } else {
    try {
      uc = files.get(relative).asUnderConstruction();
    } catch (FileNotFoundException e) {
      return FileStoreCommon.completeExceptionally(
          index, "Failed to write to " + relative, e);
    }
  }

  return size == 0 && !close? CompletableFuture.completedFuture(0)
      : createNew? uc.submitCreate(this::resolve, data, close, writer, getId(), index)
      : uc.submitWrite(offset, data, close, writer, getId(), index);
}
 
Example 3
Source File: FileInfo.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private int write(long offset, ByteString data, boolean close) throws IOException {
  // If leader finish write data with offset = 4096 and writeSize become 8192,
  // and 2 follower has not written the data with offset = 4096,
  // then start a leader election. So client will retry send the data with offset = 4096,
  // then offset < writeSize in leader.
  if (offset < writeSize) {
    return data.size();
  }
  if (offset != writeSize) {
    throw new IOException("Offset/size mismatched: offset = " + offset
        + " != writeSize = " + writeSize + ", path=" + getRelativePath());
  }
  if (out == null) {
    throw new IOException("File output is not initialized, path=" + getRelativePath());
  }

  synchronized (out) {
    int n = 0;
    if (data != null) {
      final ByteBuffer buffer = data.asReadOnlyByteBuffer();
      try {
        for (; buffer.remaining() > 0; ) {
          n += out.write(buffer);
        }
      } finally {
        writeSize += n;
      }
    }

    if (close) {
      out.close();
    }
    return n;
  }
}
 
Example 4
Source File: StringUtils.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static String bytes2HexShortString(ByteString bytes) {
  final int size = bytes.size();
  if (size == 0) {
    return "<EMPTY>";
  } else if (size > 10) {
    // return only the first 10 bytes
    return bytes2HexString(bytes.substring(0, 10)) + "...(size=" + size + ")";
  } else {
    return bytes2HexString(bytes);
  }
}
 
Example 5
Source File: StreamImpl.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<RaftClientReply> streamAsync(Message message, SizeInBytes subSize) {
  final int n = subSize.getSizeInt();
  final MessageOutputStream out = stream();
  final ByteString bytes = message.getContent();
  for(int i = 0; i < bytes.size(); ) {
    final int j = Math.min(i + n, bytes.size());
    final ByteString sub = bytes.substring(i, j);
    out.sendAsync(Message.valueOf(sub));
    i = j;
  }
  return out.closeAsync();
}
 
Example 6
Source File: StringUtils.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static String bytes2HexShortString(ByteString bytes) {
  final int size = bytes.size();
  if (size == 0) {
    return "<EMPTY>";
  } else if (size > 10) {
    // return only the first 10 bytes
    return bytes2HexString(bytes.substring(0, 10)) + "...(size=" + size + ")";
  } else {
    return bytes2HexString(bytes);
  }
}