Search in sources :

Example 76 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class SerializerTest method testStringCompression.

@Test
public void testStringCompression() {
    // ASCII encoding
    for (int t = 0; t < 100; t++) {
        String x = getRandomString(StringSerializer.TEXT_COMPRESSION_THRESHOLD - 1, ASCII_VALUE);
        assertEquals(x.length() + 1, getStringBuffer(x).length());
    }
    // SMAZ Encoding
    // String[] texts = {
    // "To Sherlock Holmes she is always the woman. I have seldom heard him mention her under any other name. In his eyes she eclipses and predominates the whole of her sex.",
    // "His manner was not effusive. It seldom was; but he was glad, I think, to see me. With hardly a word spoken, but with a kindly eye, he waved me to an armchair",
    // "I could not help laughing at the ease with which he explained his process of deduction.",
    // "A man entered who could hardly have been less than six feet six inches in height, with the chest and limbs of a Hercules. His dress was rich with a richness which would, in England"
    // };
    // for (String text : texts) {
    // assertTrue(text.length()> StringSerializer.TEXT_COMPRESSION_THRESHOLD);
    // StaticBuffer s = getStringBuffer(text);
    // //            System.out.println(String.format("String length [%s] -> byte size [%s]",text.length(),s.length()));
    // assertTrue(text.length()>s.length()); //Test that actual compression is happening
    // }
    // Gzip Encoding
    String[] patterns = { "aQd>@!as/df5h", "sdfodoiwk", "sdf", "ab", "asdfwewefefwdfkajhqwkdhj" };
    int targetLength = StringSerializer.LONG_COMPRESSION_THRESHOLD * 5;
    for (String pattern : patterns) {
        StringBuilder sb = new StringBuilder(targetLength);
        for (int i = 0; i < targetLength / pattern.length(); i++) sb.append(pattern);
        String text = sb.toString();
        assertTrue(text.length() > StringSerializer.LONG_COMPRESSION_THRESHOLD);
        StaticBuffer s = getStringBuffer(text);
        // System.out.println(String.format("String length [%s] -> byte size [%s]",text.length(),s.length()));
        // Test that radical compression is happening
        assertTrue(text.length() > s.length() * 10);
    }
    for (int t = 0; t < 10000; t++) {
        String x = STRING_FACTORY.newInstance();
        DataOutput o = serialize.getDataOutput(64);
        o.writeObject(x, String.class);
        ReadBuffer r = o.getStaticBuffer().asReadBuffer();
        String y = serialize.readObject(r, String.class);
        assertEquals(x, y);
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.jupiter.api.Test)

Example 77 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class SerializerTest method testSerializationMixture.

@Test
public void testSerializationMixture() {
    serialize.registerClass(1, TClass1.class, new TClass1Serializer());
    for (int t = 0; t < 1000; t++) {
        DataOutput out = serialize.getDataOutput(128);
        int num = random.nextInt(100) + 1;
        final List<SerialEntry> entries = new ArrayList<>(num);
        for (int i = 0; i < num; i++) {
            Map.Entry<Class, Factory> type = Iterables.get(TYPES.entrySet(), random.nextInt(TYPES.size()));
            Object element = type.getValue().newInstance();
            boolean notNull = true;
            if (random.nextDouble() < 0.5) {
                notNull = false;
                if (random.nextDouble() < 0.2)
                    element = null;
            }
            entries.add(new SerialEntry(element, type.getKey(), notNull));
            if (notNull)
                out.writeObjectNotNull(element);
            else
                out.writeObject(element, type.getKey());
        }
        StaticBuffer sb = out.getStaticBuffer();
        ReadBuffer in = sb.asReadBuffer();
        for (SerialEntry entry : entries) {
            Object read;
            if (entry.notNull)
                read = serialize.readObjectNotNull(in, entry.clazz);
            else
                read = serialize.readObject(in, entry.clazz);
            if (entry.object == null)
                assertNull(read);
            else if (entry.clazz.isArray()) {
                assertEquals(Array.getLength(entry.object), Array.getLength(read));
                for (int i = 0; i < Array.getLength(read); i++) {
                    assertEquals(Array.get(entry.object, i), Array.get(read, i));
                }
            } else
                assertEquals(entry.object, read);
        }
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ArrayList(java.util.ArrayList) LoggerFactory(org.slf4j.LoggerFactory) SpatialContextFactory(org.locationtech.spatial4j.context.SpatialContextFactory) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) HashMap(java.util.HashMap) Map(java.util.Map) TClass1Serializer(org.janusgraph.graphdb.serializer.attributes.TClass1Serializer) Test(org.junit.jupiter.api.Test)

Example 78 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class SerializerTest method comparableStringSerialization.

@Test
public void comparableStringSerialization() {
    // Characters
    DataOutput out = serialize.getDataOutput(((int) Character.MAX_VALUE) * 2 + 8);
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
        out.writeObjectNotNull(c);
    }
    ReadBuffer b = out.getStaticBuffer().asReadBuffer();
    for (char c = Character.MIN_VALUE; c < Character.MAX_VALUE; c++) {
        assertEquals(c, serialize.readObjectNotNull(b, Character.class).charValue());
    }
    // String
    for (int t = 0; t < 10000; t++) {
        DataOutput out1 = serialize.getDataOutput(32 + 5);
        DataOutput out2 = serialize.getDataOutput(32 + 5);
        String s1 = RandomGenerator.randomString(1, 32);
        String s2 = RandomGenerator.randomString(1, 32);
        out1.writeObjectByteOrder(s1, String.class);
        out2.writeObjectByteOrder(s2, String.class);
        StaticBuffer b1 = out1.getStaticBuffer();
        StaticBuffer b2 = out2.getStaticBuffer();
        assertEquals(s1, serialize.readObjectByteOrder(b1.asReadBuffer(), String.class));
        assertEquals(s2, serialize.readObjectByteOrder(b2.asReadBuffer(), String.class));
        assertEquals(Integer.signum(s1.compareTo(s2)), Integer.signum(b1.compareTo(b2)), s1 + " vs " + s2);
    }
}
Also used : DataOutput(org.janusgraph.graphdb.database.serialize.DataOutput) ReadBuffer(org.janusgraph.diskstorage.ReadBuffer) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer) Test(org.junit.jupiter.api.Test)

Example 79 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer in project janusgraph by JanusGraph.

the class IDManagementTest method testEdgeTypeWriting.

public void testEdgeTypeWriting(long edgeTypeId) {
    IDHandler.DirectionID[] dir = IDManager.VertexIDType.EdgeLabel.is(edgeTypeId) ? new IDHandler.DirectionID[] { IDHandler.DirectionID.EDGE_IN_DIR, IDHandler.DirectionID.EDGE_OUT_DIR } : new IDHandler.DirectionID[] { IDHandler.DirectionID.PROPERTY_DIR };
    boolean invisible = IDManager.isSystemRelationTypeId(edgeTypeId);
    for (IDHandler.DirectionID d : dir) {
        StaticBuffer b = IDHandler.getRelationType(edgeTypeId, d, invisible);
        IDHandler.RelationTypeParse parse = IDHandler.readRelationType(b.asReadBuffer());
        assertEquals(d, parse.dirID);
        assertEquals(edgeTypeId, parse.typeId);
    }
}
Also used : IDHandler(org.janusgraph.graphdb.database.idhandling.IDHandler) StaticBuffer(org.janusgraph.diskstorage.StaticBuffer)

Example 80 with StaticBuffer

use of org.janusgraph.diskstorage.StaticBuffer 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

StaticBuffer (org.janusgraph.diskstorage.StaticBuffer)101 Entry (org.janusgraph.diskstorage.Entry)36 Test (org.junit.jupiter.api.Test)36 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)20 Map (java.util.Map)19 StoreTransaction (org.janusgraph.diskstorage.keycolumnvalue.StoreTransaction)17 KeySliceQuery (org.janusgraph.diskstorage.keycolumnvalue.KeySliceQuery)16 StaticArrayEntry (org.janusgraph.diskstorage.util.StaticArrayEntry)16 BackendException (org.janusgraph.diskstorage.BackendException)15 List (java.util.List)14 EntryList (org.janusgraph.diskstorage.EntryList)14 TemporaryBackendException (org.janusgraph.diskstorage.TemporaryBackendException)14 KCVMutation (org.janusgraph.diskstorage.keycolumnvalue.KCVMutation)13 PermanentBackendException (org.janusgraph.diskstorage.PermanentBackendException)12 Instant (java.time.Instant)11 DataOutput (org.janusgraph.graphdb.database.serialize.DataOutput)10 ReadBuffer (org.janusgraph.diskstorage.ReadBuffer)8 ConsistentKeyLockStatus (org.janusgraph.diskstorage.locking.consistentkey.ConsistentKeyLockStatus)7 BackendOperation (org.janusgraph.diskstorage.util.BackendOperation)7