Search in sources :

Example 1 with Routable

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());
}
Also used : Routable(com.yahoo.messagebus.Routable) Test(org.junit.Test)

Example 2 with Routable

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());
}
Also used : Routable(com.yahoo.messagebus.Routable) Test(org.junit.Test)

Example 3 with Routable

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;
}
Also used : Routable(com.yahoo.messagebus.Routable)

Example 4 with Routable

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;
}
Also used : Version(com.yahoo.component.Version) IOException(java.io.IOException) Routable(com.yahoo.messagebus.Routable)

Example 5 with Routable

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"));
}
Also used : StringFieldValue(com.yahoo.document.datatypes.StringFieldValue) Routable(com.yahoo.messagebus.Routable)

Aggregations

Routable (com.yahoo.messagebus.Routable)6 Test (org.junit.Test)2 Version (com.yahoo.component.Version)1 StringFieldValue (com.yahoo.document.datatypes.StringFieldValue)1 EmptyReply (com.yahoo.messagebus.EmptyReply)1 Error (com.yahoo.messagebus.Error)1 Message (com.yahoo.messagebus.Message)1 Protocol (com.yahoo.messagebus.Protocol)1 Reply (com.yahoo.messagebus.Reply)1 IOException (java.io.IOException)1