Search in sources :

Example 1 with WriteBuffer

use of com.thinkaurelius.titan.diskstorage.WriteBuffer in project titan by thinkaurelius.

the class IDHandler method getRelationType.

public static StaticBuffer getRelationType(long relationTypeId, DirectionID dirID, boolean invisible) {
    WriteBuffer b = new WriteByteBuffer(relationTypeLength(relationTypeId));
    IDHandler.writeRelationType(b, relationTypeId, dirID, invisible);
    return b.getStaticBuffer();
}
Also used : WriteBuffer(com.thinkaurelius.titan.diskstorage.WriteBuffer) WriteByteBuffer(com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer)

Example 2 with WriteBuffer

use of com.thinkaurelius.titan.diskstorage.WriteBuffer in project titan by thinkaurelius.

the class VariableLong method positiveBuffer.

public static StaticBuffer positiveBuffer(long[] value) {
    int len = 0;
    for (long aValue : value) len += positiveLength(aValue);
    WriteBuffer buffer = new WriteByteBuffer(len);
    for (long aValue : value) writePositive(buffer, aValue);
    return buffer.getStaticBuffer();
}
Also used : WriteBuffer(com.thinkaurelius.titan.diskstorage.WriteBuffer) WriteByteBuffer(com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer)

Example 3 with WriteBuffer

use of com.thinkaurelius.titan.diskstorage.WriteBuffer in project titan by thinkaurelius.

the class VariableLongTest method readWriteTest.

private void readWriteTest(final ReadWriteLong impl, long maxValue, long jump, boolean negative, boolean backward) {
    Preconditions.checkArgument(maxValue % jump == 0);
    long allocate = maxValue / jump * 8 * (negative ? 2 : 1);
    Preconditions.checkArgument(allocate < (1 << 28));
    WriteBuffer wb = new WriteByteBuffer((int) allocate);
    int num = 0;
    StopWatch w = new StopWatch();
    w.start();
    for (long i = (negative ? -maxValue : 0); i <= maxValue; i += jump) {
        impl.write(wb, i);
        num++;
    }
    //for (int i=0;i<b.remaining();i++) System.out.print(b.get(i)+"|");
    w.stop();
    ReadBuffer rb = wb.getStaticBuffer().asReadBuffer();
    log.info("Writing " + num + " longs in " + rb.length() + " bytes. in time: " + w.getTime());
    final ReadVerify read = new ReadVerify() {

        @Override
        public void next(ReadBuffer rb, long expected) {
            int beforePos = rb.getPosition();
            long value = impl.read(rb);
            assertEquals(expected, value);
            int length = Math.abs(rb.getPosition() - beforePos);
            assertEquals("On: " + expected, length, impl.length(expected));
        }
    };
    if (backward) {
        rb.movePositionTo(rb.length());
        for (long i = maxValue; i != (negative ? -maxValue : 0); i -= jump) {
            read.next(rb, i);
        }
    } else {
        for (long i = (negative ? -maxValue : 0); i <= maxValue; i += jump) {
            read.next(rb, i);
        }
    }
    //Test boundaries
    wb = new WriteByteBuffer(512);
    impl.write(wb, 0);
    impl.write(wb, Long.MAX_VALUE);
    if (negative)
        impl.write(wb, -Long.MAX_VALUE);
    rb = wb.getStaticBuffer().asReadBuffer();
    if (backward) {
        rb.movePositionTo(rb.length());
        if (negative)
            assertEquals(-Long.MAX_VALUE, impl.read(rb));
        assertEquals(Long.MAX_VALUE, impl.read(rb));
        assertEquals(0, impl.read(rb));
    } else {
        assertEquals(0, impl.read(rb));
        assertEquals(Long.MAX_VALUE, impl.read(rb));
        if (negative)
            assertEquals(-Long.MAX_VALUE, impl.read(rb));
    }
}
Also used : ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) WriteBuffer(com.thinkaurelius.titan.diskstorage.WriteBuffer) WriteByteBuffer(com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer) StopWatch(org.apache.commons.lang.time.StopWatch)

Example 4 with WriteBuffer

use of com.thinkaurelius.titan.diskstorage.WriteBuffer in project titan by thinkaurelius.

the class VariableLongTest method byteOrderPreservingPositiveBackward.

@Test
public void byteOrderPreservingPositiveBackward() {
    long[] scalingFactors = { Long.MAX_VALUE, 1000, 1000000000l };
    for (int t = 0; t < 10000000; t++) {
        StaticBuffer[] b = new StaticBuffer[2];
        long[] l = new long[2];
        for (int i = 0; i < 2; i++) {
            l[i] = randomPosLong(scalingFactors[random.nextInt(scalingFactors.length)]);
            WriteBuffer out = new WriteByteBuffer(11);
            VariableLong.writePositiveBackward(out, l[i]);
            b[i] = out.getStaticBuffer();
            ReadBuffer res = b[i].asReadBuffer();
            res.movePositionTo(res.length());
            assertEquals(l[i], VariableLong.readPositiveBackward(res));
        }
        //            System.out.println(l[0] + " vs " + l[1]);
        assertEquals(Math.signum(Long.compare(l[0], l[1])), Math.signum(b[0].compareTo(b[1])), 0.01);
    }
}
Also used : ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) WriteBuffer(com.thinkaurelius.titan.diskstorage.WriteBuffer) WriteByteBuffer(com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Test(org.junit.Test)

Example 5 with WriteBuffer

use of com.thinkaurelius.titan.diskstorage.WriteBuffer in project titan by thinkaurelius.

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, eid.getSchemaCountBound());
        long id;
        IDHandler.DirectionID dirID;
        RelationCategory type;
        if (Math.random() < 0.5) {
            id = eid.getSchemaId(IDManager.VertexIDType.UserEdgeLabel, count);
            assertTrue(eid.isEdgeLabelId(id));
            assertFalse(eid.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 = eid.getSchemaId(IDManager.VertexIDType.UserPropertyKey, count);
            assertTrue(eid.isPropertyKeyId(id));
            assertFalse(eid.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(com.thinkaurelius.titan.graphdb.database.serialize.DataOutput) WriteBuffer(com.thinkaurelius.titan.diskstorage.WriteBuffer) ReadBuffer(com.thinkaurelius.titan.diskstorage.ReadBuffer) RelationCategory(com.thinkaurelius.titan.graphdb.internal.RelationCategory) WriteByteBuffer(com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer) StandardSerializer(com.thinkaurelius.titan.graphdb.database.serialize.StandardSerializer) IDHandler(com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler) StaticBuffer(com.thinkaurelius.titan.diskstorage.StaticBuffer) Serializer(com.thinkaurelius.titan.graphdb.database.serialize.Serializer) StandardSerializer(com.thinkaurelius.titan.graphdb.database.serialize.StandardSerializer) Test(org.junit.Test)

Aggregations

WriteBuffer (com.thinkaurelius.titan.diskstorage.WriteBuffer)10 WriteByteBuffer (com.thinkaurelius.titan.diskstorage.util.WriteByteBuffer)10 ReadBuffer (com.thinkaurelius.titan.diskstorage.ReadBuffer)4 Test (org.junit.Test)3 StaticBuffer (com.thinkaurelius.titan.diskstorage.StaticBuffer)2 IDHandler (com.thinkaurelius.titan.graphdb.database.idhandling.IDHandler)1 DataOutput (com.thinkaurelius.titan.graphdb.database.serialize.DataOutput)1 Serializer (com.thinkaurelius.titan.graphdb.database.serialize.Serializer)1 StandardSerializer (com.thinkaurelius.titan.graphdb.database.serialize.StandardSerializer)1 RelationCategory (com.thinkaurelius.titan.graphdb.internal.RelationCategory)1 StopWatch (org.apache.commons.lang.time.StopWatch)1