Search in sources :

Example 1 with ICorfuSMR

use of org.corfudb.runtime.object.ICorfuSMR in project CorfuDB by CorfuDB.

the class JSONSerializer method serialize.

/**
     * Serialize an object into a given byte buffer.
     *
     * @param o The object to serialize.
     * @param b The bytebuf to serialize it into.
     */
@Override
public void serialize(Object o, ByteBuf b) {
    String className = o == null ? "null" : o.getClass().getName();
    if (className.endsWith(ICorfuSMR.CORFUSMR_SUFFIX)) {
        String SMRClass = className.split("\\$")[0];
        className = "CorfuObject";
        byte[] classNameBytes = className.getBytes();
        b.writeShort(classNameBytes.length);
        b.writeBytes(classNameBytes);
        byte[] SMRClassNameBytes = SMRClass.getBytes();
        b.writeShort(SMRClassNameBytes.length);
        b.writeBytes(SMRClassNameBytes);
        UUID id = ((ICorfuSMR) o).getCorfuStreamID();
        log.trace("Serializing a CorfuObject of type {} as a stream pointer to {}", SMRClass, id);
        b.writeLong(id.getMostSignificantBits());
        b.writeLong(id.getLeastSignificantBits());
    } else {
        byte[] classNameBytes = className.getBytes();
        b.writeShort(classNameBytes.length);
        b.writeBytes(classNameBytes);
        if (o == null) {
            return;
        }
        try (ByteBufOutputStream bbos = new ByteBufOutputStream(b)) {
            try (OutputStreamWriter osw = new OutputStreamWriter(bbos)) {
                gson.toJson(o, o.getClass(), osw);
            }
        } catch (IOException ie) {
            log.error("Exception during serialization!", ie);
        }
    }
}
Also used : ByteBufOutputStream(io.netty.buffer.ByteBufOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) UUID(java.util.UUID) ICorfuSMR(org.corfudb.runtime.object.ICorfuSMR)

Example 2 with ICorfuSMR

use of org.corfudb.runtime.object.ICorfuSMR in project CorfuDB by CorfuDB.

the class SMRMapTest method canGetID.

@Test
@SuppressWarnings("unchecked")
public void canGetID() throws Exception {
    UUID id = UUID.randomUUID();
    ICorfuSMR testMap = (ICorfuSMR) getRuntime().getObjectsView().build().setStreamID(id).setTypeToken(new TypeToken<SMRMap<String, String>>() {
    }).open();
    assertThat(id).isEqualTo(testMap.getCorfuStreamID());
}
Also used : ICorfuSMR(org.corfudb.runtime.object.ICorfuSMR) Test(org.junit.Test) AbstractViewTest(org.corfudb.runtime.view.AbstractViewTest)

Aggregations

ICorfuSMR (org.corfudb.runtime.object.ICorfuSMR)2 ByteBufOutputStream (io.netty.buffer.ByteBufOutputStream)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 UUID (java.util.UUID)1 AbstractViewTest (org.corfudb.runtime.view.AbstractViewTest)1 Test (org.junit.Test)1