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();
}
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();
}
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));
}
}
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);
}
}
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);
}
}
Aggregations