Search in sources :

Example 1 with TClass2

use of org.janusgraph.graphdb.serializer.attributes.TClass2 in project janusgraph by JanusGraph.

the class SerializerGraphConfiguration method testOnlyRegisteredSerialization.

@Test
public void testOnlyRegisteredSerialization() {
    JanusGraphManagement management = graph.openManagement();
    PropertyKey time = management.makePropertyKey("time").dataType(Integer.class).make();
    management.makePropertyKey("any").cardinality(Cardinality.LIST).dataType(Object.class).make();
    management.buildIndex("byTime", Vertex.class).addKey(time).buildCompositeIndex();
    management.makeEdgeLabel("knows").make();
    management.makeVertexLabel("person").make();
    management.commit();
    JanusGraphTransaction tx = graph.newTransaction();
    JanusGraphVertex v = tx.addVertex("person");
    v.property("time", 5);
    v.property("any", 5.0);
    v.property("any", new TClass1(5, 1.5f));
    v.property("any", TEnum.THREE);
    tx.commit();
    tx = graph.newTransaction();
    v = tx.query().has("time", 5).vertices().iterator().next();
    assertEquals(5, (int) v.value("time"));
    assertEquals(3, Iterators.size(v.properties("any")));
    tx.rollback();
    // Verify that non-registered objects aren't allowed
    for (Object o : new Object[] { new TClass2("abc", 5) }) {
        tx = graph.newTransaction();
        v = tx.addVertex("person");
        try {
            // Should not be allowed
            v.property("any", o);
            tx.commit();
            fail();
        } catch (IllegalArgumentException ignored) {
        } finally {
            if (tx.isOpen())
                tx.rollback();
        }
    }
}
Also used : JanusGraphManagement(org.janusgraph.core.schema.JanusGraphManagement) TClass2(org.janusgraph.graphdb.serializer.attributes.TClass2) TClass1(org.janusgraph.graphdb.serializer.attributes.TClass1) JanusGraphTransaction(org.janusgraph.core.JanusGraphTransaction) JanusGraphVertex(org.janusgraph.core.JanusGraphVertex) PropertyKey(org.janusgraph.core.PropertyKey) Test(org.junit.Test)

Example 2 with TClass2

use of org.janusgraph.graphdb.serializer.attributes.TClass2 in project janusgraph by JanusGraph.

the class SerializerTest method parallelDeserialization.

@Test
public void parallelDeserialization() throws InterruptedException {
    serialize.registerClass(1, TClass2.class, new TClass2Serializer());
    final long value = 8;
    final String str = "123456";
    final TClass2 c = new TClass2("abcdefg", 333);
    DataOutput out = serialize.getDataOutput(128);
    out.putLong(value);
    out.writeClassAndObject(value);
    out.writeObject(c, TClass2.class);
    out.writeObjectNotNull(str);
    final StaticBuffer b = out.getStaticBuffer();
    int numThreads = 4;
    Thread[] threads = new Thread[numThreads];
    for (int i = 0; i < numThreads; i++) {
        threads[i] = new Thread(() -> {
            for (int j = 0; j < 100000; j++) {
                ReadBuffer buffer = b.asReadBuffer();
                assertEquals(8, buffer.getLong());
                assertEquals(value, (long) serialize.readClassAndObject(buffer));
                assertEquals(c, serialize.readObject(buffer, TClass2.class));
                assertEquals(str, serialize.readObjectNotNull(buffer, String.class));
            }
        });
        threads[i].start();
    }
    for (int i = 0; i < numThreads; i++) {
        threads[i].join();
    }
}
Also used : TClass2(org.janusgraph.graphdb.serializer.attributes.TClass2) DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) TClass2Serializer(org.janusgraph.graphdb.serializer.attributes.TClass2Serializer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.jupiter.api.Test)

Example 3 with TClass2

use of org.janusgraph.graphdb.serializer.attributes.TClass2 in project janusgraph by JanusGraph.

the class SerializerTestCommon method objectWriteRead.

protected void objectWriteRead() {
    TClass1 t1 = new TClass1(3245234223423433123L, 0.333f);
    TClass2 t2 = new TClass2("This is a test", 4234234);
    TEnum t3 = TEnum.THREE;
    TEnum t4 = TEnum.TWO;
    DataOutput out = serialize.getDataOutput(128);
    out.writeObjectNotNull(t1);
    out.writeClassAndObject(t2);
    out.writeObject(t3, TEnum.class);
    out.writeClassAndObject(t4);
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(t1, serialize.readObjectNotNull(b, TClass1.class));
    assertEquals(t2, serialize.readClassAndObject(b));
    assertEquals(t3, serialize.readObject(b, TEnum.class));
    assertEquals(t4, serialize.readClassAndObject(b));
    assertFalse(b.hasRemaining());
}
Also used : TClass2(org.janusgraph.graphdb.serializer.attributes.TClass2) DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) TClass1(org.janusgraph.graphdb.serializer.attributes.TClass1) TEnum(org.janusgraph.graphdb.serializer.attributes.TEnum)

Example 4 with TClass2

use of org.janusgraph.graphdb.serializer.attributes.TClass2 in project janusgraph by JanusGraph.

the class SerializerTest method testObjectVerification.

@Test
public void testObjectVerification() {
    serialize.registerClass(2, TClass1.class, new TClass1Serializer());
    TClass1 t1 = new TClass1(24223, 0.25f);
    DataOutput out = serialize.getDataOutput(128);
    out.writeClassAndObject(t1);
    out.writeClassAndObject(null);
    out.writeObject(t1, TClass1.class);
    out.writeObject(null, TClass1.class);
    // Test failure
    for (Object o : new Object[] { new TClass2("abc", 2), Calendar.getInstance(), Lists.newArrayList() }) {
        try {
            out.writeObjectNotNull(o);
            fail();
        } catch (Exception ignored) {
        }
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(t1, serialize.readClassAndObject(b));
    assertNull(serialize.readClassAndObject(b));
    assertEquals(t1, serialize.readObject(b, TClass1.class));
    assertNull(serialize.readObject(b, TClass1.class));
    assertFalse(b.hasRemaining());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) TClass2(org.janusgraph.graphdb.serializer.attributes.TClass2) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) TClass1(org.janusgraph.graphdb.serializer.attributes.TClass1) IOException(java.io.IOException) TClass1Serializer(org.janusgraph.graphdb.serializer.attributes.TClass1Serializer) Test(org.junit.jupiter.api.Test)

Aggregations

TClass2 (org.janusgraph.graphdb.serializer.attributes.TClass2)4 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)3 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)3 TClass1 (org.janusgraph.graphdb.serializer.attributes.TClass1)3 Test (org.junit.jupiter.api.Test)2 IOException (java.io.IOException)1 JanusGraphTransaction (org.janusgraph.core.JanusGraphTransaction)1 JanusGraphVertex (org.janusgraph.core.JanusGraphVertex)1 PropertyKey (org.janusgraph.core.PropertyKey)1 JanusGraphManagement (org.janusgraph.core.schema.JanusGraphManagement)1 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)1 TClass1Serializer (org.janusgraph.graphdb.serializer.attributes.TClass1Serializer)1 TClass2Serializer (org.janusgraph.graphdb.serializer.attributes.TClass2Serializer)1 TEnum (org.janusgraph.graphdb.serializer.attributes.TEnum)1 Test (org.junit.Test)1