use of com.yahoo.messagebus.Routable in project vespa by vespa-engine.
the class SimpleProtocolTestCase method requireThatReplyCanBeDecoded.
@Test
public void requireThatReplyCanBeDecoded() {
SimpleReply reply = new SimpleReply("foo");
byte[] buf = PROTOCOL.encode(Version.emptyVersion, reply);
Routable routable = PROTOCOL.decode(Version.emptyVersion, buf);
assertNotNull(routable);
assertEquals(SimpleReply.class, routable.getClass());
reply = (SimpleReply) routable;
assertEquals("foo", reply.getValue());
}
use of com.yahoo.messagebus.Routable in project vespa by vespa-engine.
the class SimpleProtocolTestCase method requireThatMessageCanBeEncodedAndDecoded.
@Test
public void requireThatMessageCanBeEncodedAndDecoded() {
SimpleMessage msg = new SimpleMessage("foo");
byte[] buf = PROTOCOL.encode(Version.emptyVersion, msg);
Routable routable = PROTOCOL.decode(Version.emptyVersion, buf);
assertNotNull(routable);
assertEquals(SimpleMessage.class, routable.getClass());
msg = (SimpleMessage) routable;
assertEquals("foo", msg.getValue());
}
use of com.yahoo.messagebus.Routable in project vespa by vespa-engine.
the class RoutableRepository method decode.
/**
* Decodes a {@link Routable} from the given byte array. This uses the content of the byte array to dispatch the
* decode request to the appropriate {@link RoutableFactory} that was previously registered.
*
* If a routable can not be decoded, this method returns null.
*
* @param version The version of the encoded routable.
* @param data The byte array containing the encoded routable.
* @return The decoded routable.
*/
Routable decode(DocumentTypeManager docMan, Version version, byte[] data) {
if (data == null || data.length == 0) {
log.log(LogLevel.ERROR, "Received empty byte array for deserialization.");
return null;
}
DocumentDeserializer in;
if (version.getMajor() >= 5) {
in = DocumentDeserializerFactory.createHead(docMan, GrowableByteBuffer.wrap(data));
} else {
in = DocumentDeserializerFactory.create42(docMan, GrowableByteBuffer.wrap(data));
}
int type = in.getInt(null);
RoutableFactory factory = getFactory(version, type);
if (factory == null) {
log.log(LogLevel.ERROR, "No routable factory found for routable type " + type + " (version " + version + ").");
return null;
}
Routable ret = factory.decode(in, loadTypes);
if (ret == null) {
log.log(LogLevel.ERROR, "Routable factory " + factory.getClass().getName() + " failed to deserialize " + "routable of type " + type + " (version " + version + ").");
log.log(LogLevel.ERROR, Arrays.toString(data));
return null;
}
return ret;
}
use of com.yahoo.messagebus.Routable in project vespa by vespa-engine.
the class MessagesTestBase method deserialize.
/**
* Reads the content of the given file and creates a corresponding routable.
*
* @param filename The name of the file to read from.
* @param classId The type that the routable must decode as.
* @param lang The language constant that dictates what file format to read from.
* @return The decoded routable.
*/
public Routable deserialize(String filename, int classId, Language lang) {
Version version = version();
String path = getPath(version + "-" + (lang == Language.JAVA ? "java" : "cpp") + "-" + filename + ".dat");
System.out.println("Deserializing from '" + path + "'..");
byte[] data;
try {
data = TestFileUtil.readFile(path);
} catch (IOException e) {
throw new AssertionError(e);
}
Routable ret = protocol.decode(version, data);
assertNotNull(ret);
assertEquals(classId, ret.getType());
return ret;
}
use of com.yahoo.messagebus.Routable in project vespa by vespa-engine.
the class DocumentProcessingHandlerTransformingMessagesTestCase method batchDocumentUpdate.
private void batchDocumentUpdate() throws InterruptedException {
DocumentUpdate doc1 = new DocumentUpdate(getType(), new DocumentId("userdoc:test:12345:batch:nodocstatus:keep:this"));
DocumentUpdate doc2 = new DocumentUpdate(getType(), new DocumentId("userdoc:test:12345:batch:nodocstatus:skip:this"));
Field testField = getType().getField("foostring");
doc1.addFieldUpdate(FieldUpdate.createAssign(testField, new StringFieldValue("1 not yet processed")));
doc2.addFieldUpdate(FieldUpdate.createAssign(testField, new StringFieldValue("2 not yet processed")));
BatchDocumentUpdateMessage message = new BatchDocumentUpdateMessage(12345);
message.addUpdate(doc1);
message.addUpdate(doc2);
Routable result = sendMessageAndGetResult(message);
assertThat(result, instanceOf(UpdateDocumentMessage.class));
DocumentUpdate outputUpd = ((UpdateDocumentMessage) result).getDocumentUpdate();
assertThat(outputUpd.getId().toString(), is("userdoc:test:12345:batch:nodocstatus:keep:this"));
}
Aggregations