Search in sources :

Example 21 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class SerializerTest method testLegacyPointSerialization.

@Test
public void testLegacyPointSerialization() {
    Geoshape geo = Geoshape.point(0.5, 2.5);
    Geoshape geo2 = Geoshape.point(1.5, 3.5);
    DataOutput out = serialize.getDataOutput(128);
    int length = geo.size();
    VariableLong.writePositive(out, length);
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < length; j++) {
            Geoshape.Point point = geo.getPoint(j);
            out.putFloat((float) (i == 0 ? geo.getPoint(j).getLatitude() : geo.getPoint(j).getLongitude()));
        }
    }
    // Add a second point to the same buffer
    length = geo2.size();
    VariableLong.writePositive(out, length);
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < length; j++) {
            Geoshape.Point point = geo2.getPoint(j);
            out.putFloat((float) (i == 0 ? geo2.getPoint(j).getLatitude() : geo2.getPoint(j).getLongitude()));
        }
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    assertEquals(geo, serialize.readObjectNotNull(b, Geoshape.class));
    assertEquals(geo2, serialize.readObjectNotNull(b, Geoshape.class));
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) Geoshape(org.janusgraph.core.attribute.Geoshape) Test(org.junit.jupiter.api.Test)

Example 22 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class SerializerTest method enumSerializeTest.

@Test
public void enumSerializeTest() {
    serialize.registerClass(1, TEnum.class, new TEnumSerializer());
    DataOutput out = serialize.getDataOutput(128);
    out.writeObjectNotNull(TEnum.TWO);
    out.writeObjectNotNull(TEnum.THREE);
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    if (printStats)
        log.debug(bufferStats(b));
    assertEquals(TEnum.TWO, serialize.readObjectNotNull(b, TEnum.class));
    assertEquals(TEnum.THREE, serialize.readObjectNotNull(b, TEnum.class));
    assertFalse(b.hasRemaining());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) TEnum(org.janusgraph.graphdb.serializer.attributes.TEnum) TEnumSerializer(org.janusgraph.graphdb.serializer.attributes.TEnumSerializer) Test(org.junit.jupiter.api.Test)

Example 23 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class SerializerTestCommon method multipleStringWrite.

protected void multipleStringWrite() {
    // 26 chars
    String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int no = 100;
    DataOutput out = serialize.getDataOutput(128);
    for (int i = 0; i < no; i++) {
        String str = base + (i + 1);
        out.writeObjectNotNull(str);
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    if (printStats)
        log.debug(bufferStats(b));
    for (int i = 0; i < no; i++) {
        String str = base + (i + 1);
        String read = serialize.readObjectNotNull(b, String.class);
        assertEquals(str, read);
    }
    assertFalse(b.hasRemaining());
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer)

Example 24 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class IndexSerializer method getIndexKey.

private StaticBuffer getIndexKey(CompositeIndexType index, Object[] values) {
    final DataOutput out = serializer.getDataOutput(8 * DEFAULT_OBJECT_BYTELEN + 8);
    VariableLong.writePositive(out, index.getID());
    final IndexField[] fields = index.getFieldKeys();
    Preconditions.checkArgument(fields.length > 0 && fields.length == values.length);
    for (int i = 0; i < fields.length; i++) {
        final IndexField f = fields[i];
        final Object value = values[i];
        Preconditions.checkNotNull(value);
        if (InternalAttributeUtil.hasGenericDataType(f.getFieldKey())) {
            out.writeClassAndObject(value);
        } else {
            assert value.getClass().equals(f.getFieldKey().dataType()) : value.getClass() + " - " + f.getFieldKey().dataType();
            out.writeObjectNotNull(value);
        }
    }
    StaticBuffer key = out.getStaticBuffer();
    if (hashKeys)
        key = HashingUtil.hashPrefixKey(hashLength, key);
    return key;
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) IndexField(org.janusgraph.graphdb.types.IndexField) ParameterIndexField(org.janusgraph.graphdb.types.ParameterIndexField)

Example 25 with DataOutput

use of org.janusgraph.graphdb.database.serialize.DataOutput in project janusgraph by JanusGraph.

the class IDManagementTest method edgeTypeIDTest.

@Test
public void edgeTypeIDTest() {
    int partitionBits = 16;
    IDManager eid = new IDManager(partitionBits);
    int trails = 1000000;
    assertEquals(eid.getPartitionBound(), (1L << partitionBits));
    Serializer serializer = new StandardSerializer();
    for (int t = 0; t < trails; t++) {
        long count = RandomGenerator.randomLong(1, IDManager.getSchemaCountBound());
        long id;
        IDHandler.DirectionID dirID;
        RelationCategory type;
        if (Math.random() < 0.5) {
            id = IDManager.getSchemaId(IDManager.VertexIDType.UserEdgeLabel, count);
            assertTrue(eid.isEdgeLabelId(id));
            assertFalse(IDManager.isSystemRelationTypeId(id));
            type = RelationCategory.EDGE;
            if (Math.random() < 0.5)
                dirID = IDHandler.DirectionID.EDGE_IN_DIR;
            else
                dirID = IDHandler.DirectionID.EDGE_OUT_DIR;
        } else {
            type = RelationCategory.PROPERTY;
            id = IDManager.getSchemaId(IDManager.VertexIDType.UserPropertyKey, count);
            assertTrue(eid.isPropertyKeyId(id));
            assertFalse(IDManager.isSystemRelationTypeId(id));
            dirID = IDHandler.DirectionID.PROPERTY_DIR;
        }
        assertTrue(eid.isRelationTypeId(id));
        StaticBuffer b = IDHandler.getRelationType(id, dirID, false);
        // System.out.println(dirID);
        // System.out.println(getBinary(id));
        // System.out.println(getBuffer(b.asReadBuffer()));
        ReadBuffer rb = b.asReadBuffer();
        IDHandler.RelationTypeParse parse = IDHandler.readRelationType(rb);
        assertEquals(id, parse.typeId);
        assertEquals(dirID, parse.dirID);
        assertFalse(rb.hasRemaining());
        // Inline edge type
        WriteBuffer wb = new WriteByteBuffer(9);
        IDHandler.writeInlineRelationType(wb, id);
        long newId = IDHandler.readInlineRelationType(wb.getStaticBuffer().asReadBuffer());
        assertEquals(id, newId);
        // Compare to Kryo
        DataOutput out = serializer.getDataOutput(10);
        IDHandler.writeRelationType(out, id, dirID, false);
        assertEquals(b, out.getStaticBuffer());
        // Make sure the bounds are right
        StaticBuffer[] bounds = IDHandler.getBounds(type, false);
        assertTrue(bounds[0].compareTo(b) < 0);
        assertTrue(bounds[1].compareTo(b) > 0);
        bounds = IDHandler.getBounds(RelationCategory.RELATION, false);
        assertTrue(bounds[0].compareTo(b) < 0);
        assertTrue(bounds[1].compareTo(b) > 0);
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) WriteBuffer(org.janusgraph.diskstorage.WriteBuffer) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) RelationCategory(org.janusgraph.graphdb.internal.RelationCategory) WriteByteBuffer(org.janusgraph.diskstorage.util.WriteByteBuffer) StandardSerializer(org.janusgraph.graphdb.database.serialize.StandardSerializer) IDHandler(org.janusgraph.graphdb.database.idhandling.IDHandler) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Serializer(org.janusgraph.graphdb.database.serialize.Serializer) StandardSerializer(org.janusgraph.graphdb.database.serialize.StandardSerializer) Test(org.junit.jupiter.api.Test)

Aggregations

DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)39 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)16 Test (org.junit.jupiter.api.Test)14 StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)10 HashMap (java.util.HashMap)3 Map (java.util.Map)3 StaticArrayEntry (org.janusgraph.diskstorage.util.StaticArrayEntry)3 TClass1Serializer (org.janusgraph.graphdb.serializer.attributes.TClass1Serializer)3 TClass2 (org.janusgraph.graphdb.serializer.attributes.TClass2)3 SpatialContextFactory (org.locationtech.spatial4j.context.SpatialContextFactory)3 EnumMap (java.util.EnumMap)2 PropertyKey (org.janusgraph.core.PropertyKey)2 Geoshape (org.janusgraph.core.attribute.Geoshape)2 DirectionID (org.janusgraph.graphdb.database.idhandling.IDHandler.DirectionID)2 RelationCategory (org.janusgraph.graphdb.internal.RelationCategory)2 TClass1 (org.janusgraph.graphdb.serializer.attributes.TClass1)2 TEnum (org.janusgraph.graphdb.serializer.attributes.TEnum)2 JanusGraphSchemaVertex (org.janusgraph.graphdb.types.vertices.JanusGraphSchemaVertex)2 LoggerFactory (org.slf4j.LoggerFactory)2 LongArrayList (com.carrotsearch.hppc.LongArrayList)1