本文整理了Java中org.apache.ratis.thirdparty.com.google.protobuf.ByteString.toByteArray()
方法的一些代码示例,展示了ByteString.toByteArray()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ByteString.toByteArray()
方法的具体详情如下:
包路径:org.apache.ratis.thirdparty.com.google.protobuf.ByteString
类名称:ByteString
方法名:toByteArray
暂无
代码示例来源:origin: apache/incubator-ratis
public AssignmentMessage(ByteString bytes) {
this(bytes.toByteArray(), 0);
}
代码示例来源:origin: hortonworks/ratis
public static List<byte[]> toListByteArray(List<ByteString> list) {
List<byte[]> retVal = new ArrayList<byte[]>(list.size());
for(int i=0; i < list.size(); i++) {
retVal.add(list.get(i).toByteArray());
}
return retVal;
}
代码示例来源:origin: apache/incubator-ratis
@Override
protected void operation(RaftClient client) throws IOException {
RaftClientReply getValue =
client.sendReadOnly(Expression.Utils.toMessage(new Variable(name)));
Expression response =
Expression.Utils.bytes2Expression(getValue.getMessage().getContent().toByteArray(), 0);
System.out.println(String.format("%s=%s", name, (DoubleValue) response).toString());
}
}
代码示例来源:origin: apache/incubator-ratis
@Override
public CompletableFuture<Message> query(Message request) {
final Expression q = Expression.Utils.bytes2Expression(request.getContent().toByteArray(), 0);
final Double result;
try(final AutoCloseableLock readLock = readLock()) {
result = q.evaluate(variables);
}
final Expression r = Expression.Utils.double2Expression(result);
LOG.debug("QUERY: {} = {}", q, r);
return CompletableFuture.completedFuture(Expression.Utils.toMessage(r));
}
代码示例来源:origin: hortonworks/ratis
list.add(entry.getStateMachineLogEntry().getLogData().toByteArray());
} catch (RaftLogIOException e) {
t = e;
代码示例来源:origin: hortonworks/ratis
@Override
public int readBulk(List<ByteBuffer> buffers) throws IOException {
try {
RaftClientReply reply = raftClient.sendReadOnly(Message.valueOf(LogServiceProtoUtil
.toReadLogRequestProto(parent.getName(), currentRecordId, buffers.size()).toByteString()));
ReadLogReplyProto proto = ReadLogReplyProto.parseFrom(reply.getMessage().getContent());
if (proto.hasException()) {
LogServiceException e = proto.getException();
throw new IOException(e.getErrorMsg());
}
// TODO correct current record
int n = proto.getLogRecordCount();
currentRecordId += n;
for (int i = 0; i < n; i++) {
buffers.get(i).put(proto.getLogRecord(i).toByteArray());
}
return n;
} catch (Exception e) {
throw new IOException(e);
}
}
代码示例来源:origin: hortonworks/ratis
@Override
public List<ByteBuffer> readBulk(int numRecords) throws IOException {
try {
RaftClientReply reply = raftClient
.sendReadOnly(Message.valueOf(LogServiceProtoUtil
.toReadLogRequestProto(parent.getName(), currentRecordId, numRecords).toByteString()));
ReadLogReplyProto proto = ReadLogReplyProto.parseFrom(reply.getMessage().getContent());
if (proto.hasException()) {
LogServiceException e = proto.getException();
throw new IOException(e.getErrorMsg());
}
int n = proto.getLogRecordCount();
// TODO correct current record
currentRecordId += n;
List<ByteBuffer> ret = new ArrayList<ByteBuffer>();
for (int i = 0; i < n; i++) {
ret.add(ByteBuffer.wrap(proto.getLogRecord(i).toByteArray()));
}
return ret;
} catch (Exception e) {
throw new IOException(e);
}
}
代码示例来源:origin: apache/incubator-ratis
out.write(chunk.getData().toByteArray());
} finally {
IOUtils.cleanup(null, out);
new MD5Hash(chunk.getFileDigest().toByteArray());
代码示例来源:origin: apache/incubator-ratis
static boolean logEntriesContains(RaftLog log, long startIndex, long endIndex, SimpleMessage... expectedMessages) {
int idxEntries = 0;
int idxExpected = 0;
TermIndex[] termIndices = log.getEntries(startIndex, endIndex);
while (idxEntries < termIndices.length
&& idxExpected < expectedMessages.length) {
try {
if (Arrays.equals(expectedMessages[idxExpected].getContent().toByteArray(),
log.get(termIndices[idxEntries].getIndex()).getStateMachineLogEntry().getLogData().toByteArray())) {
++idxExpected;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
++idxEntries;
}
return idxExpected == expectedMessages.length;
}
代码示例来源:origin: hortonworks/ratis
@Override
public void readNext(ByteBuffer buffer) throws IOException {
try {
RaftClientReply reply =
raftClient
.sendReadOnly(Message.valueOf(LogServiceProtoUtil
.toReadLogRequestProto(parent.getName(), currentRecordId, 1).toByteString()));
ReadLogReplyProto proto = ReadLogReplyProto.parseFrom(reply.getMessage().getContent());
if (proto.hasException()) {
LogServiceException e = proto.getException();
throw new IOException(e.getErrorMsg());
}
currentRecordId++;
if (proto.getLogRecordCount() > 0) {
// TODO limits
buffer.put(proto.getLogRecord(0).toByteArray());
}
} catch (Exception e) {
throw new IOException(e);
}
}
代码示例来源:origin: apache/incubator-ratis
static void checkLogEntries(RaftLog log, SimpleMessage[] expectedMessages,
Predicate<LogEntryProto> predicate) {
TermIndex[] termIndices = log.getEntries(0, Long.MAX_VALUE);
for (int i = 0; i < termIndices.length; i++) {
for (int j = 0; j < expectedMessages.length; j++) {
final LogEntryProto e;
try {
e = log.get(termIndices[i].getIndex());
if (Arrays.equals(expectedMessages[j].getContent().toByteArray(),
e.getStateMachineLogEntry().getLogData().toByteArray())) {
Assert.assertTrue(predicate.test(e));
}
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
}
代码示例来源:origin: apache/incubator-ratis
@Test
public void testReadReply() {
List<byte[]> entries = new ArrayList<byte[]>();
byte[] e1 = new byte[] {1,1};
byte[] e2 = new byte[] {2,2};
entries.add(e1);
entries.add(e2);
ReadLogReplyProto proto =
LogServiceProtoUtil.toReadLogReplyProto(entries, null);
assertEquals(2, proto.getLogRecordCount());
assertTrue(TestUtils.equals(e1, proto.getLogRecord(0).toByteArray()));
assertTrue(TestUtils.equals(e2, proto.getLogRecord(1).toByteArray()));
}
代码示例来源:origin: hortonworks/ratis
@Override
public ByteBuffer readNext() throws IOException {
try {
RaftClientReply reply =
raftClient
.sendReadOnly(Message.valueOf(LogServiceProtoUtil
.toReadLogRequestProto(parent.getName(), currentRecordId, 1).toByteString()));
ReadLogReplyProto proto = ReadLogReplyProto.parseFrom(reply.getMessage().getContent());
if (proto.hasException()) {
LogServiceException e = proto.getException();
throw new IOException(e.getErrorMsg());
}
currentRecordId++;
if (proto.getLogRecordCount() > 0) {
proto.getLogRecord(0);
return ByteBuffer.wrap(proto.getLogRecord(0).toByteArray());
} else {
return null;
}
} catch (Exception e) {
throw new IOException(e);
}
}
代码示例来源:origin: apache/incubator-ratis
static void assertLogEntries(List<LogEntryProto> entries, long expectedTerm, SimpleMessage... expectedMessages) {
long logIndex = 0;
Assert.assertEquals(expectedMessages.length, entries.size());
for (int i = 0; i < expectedMessages.length; i++) {
final LogEntryProto e = entries.get(i);
Assert.assertTrue(e.getTerm() >= expectedTerm);
if (e.getTerm() > expectedTerm) {
expectedTerm = e.getTerm();
}
Assert.assertTrue(e.getIndex() > logIndex);
logIndex = e.getIndex();
Assert.assertArrayEquals(expectedMessages[i].getContent().toByteArray(),
e.getStateMachineLogEntry().getLogData().toByteArray());
}
}
代码示例来源:origin: apache/incubator-ratis
@Test
public void testAppendRequest() {
LogName name = LogName.of("test");
List<byte[]> entries = new ArrayList<byte[]>();
byte[] e1 = new byte[] {1,1};
byte[] e2 = new byte[] {2,2};
entries.add(e1);
entries.add(e2);
LogServiceRequestProto proto = LogServiceProtoUtil.toAppendEntryLogRequestProto(name, entries);
AppendLogEntryRequestProto request = proto.getAppendRequest();
assertEquals(name.getName(), request.getLogName().getName());
assertEquals(2, request.getDataCount());
assertTrue(TestUtils.equals(e1, request.getData(0).toByteArray()));
assertTrue(TestUtils.equals(e2, request.getData(1).toByteArray()));
}
代码示例来源:origin: apache/incubator-ratis
private void checkLog(RaftLog raftLog, long expectedCommittedIndex,
Supplier<byte[]> s) throws IOException {
long committedIndex = raftLog.getLastCommittedIndex();
Assert.assertEquals(expectedCommittedIndex, committedIndex);
// check the log content
TermIndex[] entries = raftLog.getEntries(1, expectedCommittedIndex + 1);
for (TermIndex entry : entries) {
RaftProtos.LogEntryProto log = raftLog.get(entry.getIndex());
byte[] logData = log.getStateMachineLogEntry().getLogData().toByteArray();
byte[] expected = s.get();
LOG.info("log " + entry + " " + log.getLogEntryBodyCase() + " " + StringUtils.bytes2HexString(logData));
Assert.assertEquals(expected.length, logData.length);
Assert.assertArrayEquals(expected, logData);
}
}
代码示例来源:origin: apache/incubator-ratis
static Expression assertRaftClientReply(RaftClientReply reply, Double expected) {
Assert.assertTrue(reply.isSuccess());
final Expression e = Expression.Utils.bytes2Expression(
reply.getMessage().getContent().toByteArray(), 0);
if (expected != null) {
Assert.assertEquals(expected, e.evaluate(null));
}
return e;
}
}
代码示例来源:origin: apache/incubator-ratis
totalSize = 0;
for (TermIndex e : entries) {
byte[] eValue = log.get(e.getIndex()).getStateMachineLogEntry().getLogData().toByteArray();
Assert.assertEquals(ByteValue.BUFFERSIZE, eValue.length);
System.arraycopy(eValue, 0, actual, totalSize, eValue.length);
代码示例来源:origin: hortonworks/ratis
static void assertLeaderContent(MiniRaftCluster cluster) throws Exception {
final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster);
final RaftLog leaderLog = leader.getState().getLog();
final long lastIndex = leaderLog.getLastEntryTermIndex().getIndex();
final LogEntryProto e = leaderLog.get(lastIndex);
Assert.assertTrue(e.hasMetadataEntry());
Assert.assertEquals(leaderLog.getLastCommittedIndex() - 1, e.getMetadataEntry().getCommitIndex());
final LogEntryProto[] entries = SimpleStateMachine4Testing.get(leader).getContent();
long message = 0;
for (int i = 0; i < entries.length; i++) {
LOG.info("{}) {} {}", i, message, entries[i]);
if (entries[i].hasStateMachineLogEntry()) {
final SimpleMessage m = new SimpleMessage("m" + message++);
Assert.assertArrayEquals(m.getContent().toByteArray(),
entries[i].getStateMachineLogEntry().getLogData().toByteArray());
}
}
}
代码示例来源:origin: apache/incubator-ratis
LogEntryProto entry = in.nextEntry();
Assert.assertArrayEquals(content,
entry.getStateMachineLogEntry().getLogData().toByteArray());
Assert.assertNull(in.nextEntry());
内容来源于网络,如有侵权,请联系作者删除!