use of com.thinkaurelius.titan.graphdb.internal.RelationType in project titan by thinkaurelius.
the class EdgeSerializer method parseRelation.
private RelationCache parseRelation(long vertexid, Entry data, boolean parseHeaderOnly, StandardTitanTx tx) {
assert vertexid > 0;
ReadBuffer column = data.getReadColumn();
ReadBuffer value = data.getReadValue();
LongObjectOpenHashMap properties = parseHeaderOnly ? null : new LongObjectOpenHashMap(4);
long[] typeAndDir = IDHandler.readEdgeType(column);
int dirID = (int) typeAndDir[1];
long typeId = typeAndDir[0];
Direction dir;
RelationType rtype;
switch(dirID) {
case PROPERTY_DIR:
dir = Direction.OUT;
rtype = RelationType.PROPERTY;
break;
case EDGE_OUT_DIR:
dir = Direction.OUT;
rtype = RelationType.EDGE;
break;
case EDGE_IN_DIR:
dir = Direction.IN;
rtype = RelationType.EDGE;
break;
default:
throw new IllegalArgumentException("Invalid dirID read from disk: " + dirID);
}
TitanType titanType = tx.getExistingType(typeId);
InternalType def = (InternalType) titanType;
long[] keysig = def.getSortKey();
if (!parseHeaderOnly && !titanType.isUnique(dir)) {
ReadBuffer sortKeyReader = def.getSortOrder() == Order.DESC ? column.invert() : column;
readInlineTypes(keysig, properties, sortKeyReader, tx);
}
long relationIdDiff, vertexIdDiff = 0;
if (titanType.isUnique(dir)) {
if (rtype == RelationType.EDGE)
vertexIdDiff = VariableLong.read(value);
relationIdDiff = VariableLong.read(value);
} else {
// Move position to end to read backwards
column.movePosition(column.length() - column.getPosition() - 1);
relationIdDiff = VariableLong.readBackward(column);
if (rtype == RelationType.EDGE)
vertexIdDiff = VariableLong.readBackward(column);
}
assert relationIdDiff + vertexid > 0;
long relationId = relationIdDiff + vertexid;
Object other;
switch(rtype) {
case EDGE:
Preconditions.checkArgument(titanType.isEdgeLabel());
other = vertexid + vertexIdDiff;
break;
case PROPERTY:
Preconditions.checkArgument(titanType.isPropertyKey());
TitanKey key = ((TitanKey) titanType);
other = hasGenericDataType(key) ? serializer.readClassAndObject(value) : serializer.readObjectNotNull(value, key.getDataType());
break;
default:
throw new AssertionError();
}
assert other != null;
if (!parseHeaderOnly) {
// value signature & sort key if unique
if (titanType.isUnique(dir)) {
readInlineTypes(keysig, properties, value, tx);
}
readInlineTypes(def.getSignature(), properties, value, tx);
// Third: read rest
while (value.hasRemaining()) {
TitanType type = tx.getExistingType(IDHandler.readInlineEdgeType(value));
Object pvalue = readInline(value, type);
assert pvalue != null;
properties.put(type.getID(), pvalue);
}
}
return new RelationCache(dir, typeId, relationId, other, properties);
}
use of com.thinkaurelius.titan.graphdb.internal.RelationType in project titan by thinkaurelius.
the class EdgeSerializer method getQuery.
public SliceQuery getQuery(InternalType type, Direction dir, TypedInterval[] sortKey, VertexConstraint vertexCon) {
Preconditions.checkNotNull(type);
Preconditions.checkNotNull(dir);
StaticBuffer sliceStart = null, sliceEnd = null;
boolean isStatic;
RelationType rt = type.isPropertyKey() ? RelationType.PROPERTY : RelationType.EDGE;
if (dir == Direction.BOTH) {
isStatic = type.isStatic(Direction.OUT) && type.isStatic(Direction.IN);
sliceStart = IDHandler.getEdgeType(type.getID(), getDirID(Direction.OUT, rt));
sliceEnd = IDHandler.getEdgeType(type.getID(), getDirID(Direction.IN, rt));
assert ByteBufferUtil.isSmallerThan(sliceStart, sliceEnd);
sliceEnd = ByteBufferUtil.nextBiggerBuffer(sliceEnd);
} else {
isStatic = type.isStatic(dir);
int dirID = getDirID(dir, rt);
DataOutput colStart = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY, true);
DataOutput colEnd = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY, true);
IDHandler.writeEdgeType(colStart, type.getID(), dirID);
IDHandler.writeEdgeType(colEnd, type.getID(), dirID);
long[] sortKeyIDs = type.getSortKey();
Preconditions.checkArgument(sortKey.length == sortKeyIDs.length);
assert colStart.getPosition() == colEnd.getPosition();
int startPosition = colStart.getPosition();
int i;
boolean wroteInterval = false;
for (i = 0; i < sortKeyIDs.length && sortKey[i] != null; i++) {
TitanType t = sortKey[i].type;
Interval interval = sortKey[i].interval;
if (interval == null || interval.isEmpty()) {
break;
}
Preconditions.checkArgument(t.getID() == sortKeyIDs[i]);
Preconditions.checkArgument(!type.isUnique(dir), "Cannot apply sort key to the unique direction");
if (interval.isPoint()) {
writeInline(colStart, t, interval.getStart(), false);
writeInline(colEnd, t, interval.getEnd(), false);
} else {
if (interval.getStart() != null)
writeInline(colStart, t, interval.getStart(), false);
if (interval.getEnd() != null)
writeInline(colEnd, t, interval.getEnd(), false);
switch(type.getSortOrder()) {
case ASC:
sliceStart = colStart.getStaticBuffer();
sliceEnd = colEnd.getStaticBuffer();
if (!interval.startInclusive())
sliceStart = ByteBufferUtil.nextBiggerBuffer(sliceStart);
if (interval.endInclusive())
sliceEnd = ByteBufferUtil.nextBiggerBuffer(sliceEnd);
break;
case DESC:
sliceEnd = colStart.getStaticBufferFlipBytes(startPosition, colStart.getPosition());
sliceStart = colEnd.getStaticBufferFlipBytes(startPosition, colEnd.getPosition());
if (interval.startInclusive())
sliceEnd = ByteBufferUtil.nextBiggerBuffer(sliceEnd);
if (!interval.endInclusive())
sliceStart = ByteBufferUtil.nextBiggerBuffer(sliceStart);
break;
default:
throw new AssertionError(type.getSortOrder().toString());
}
assert sliceStart.compareTo(sliceEnd) <= 0;
wroteInterval = true;
break;
}
}
boolean wroteEntireSortKey = (i >= sortKeyIDs.length);
assert !wroteEntireSortKey || !wroteInterval;
assert !wroteInterval || vertexCon == null;
if (!wroteInterval) {
assert (colStart.getPosition() == colEnd.getPosition());
int endPosition = colStart.getPosition();
if (vertexCon != null) {
assert !wroteInterval;
Preconditions.checkArgument(wroteEntireSortKey && !type.isUnique(dir));
Preconditions.checkArgument(type.isEdgeLabel());
long vertexIdDiff = vertexCon.getVertexIdDiff();
VariableLong.writeBackward(colStart, vertexIdDiff);
// VariableLong.writeBackward(colStart,relationIdDiff);
}
switch(type.getSortOrder()) {
case ASC:
sliceStart = colStart.getStaticBuffer();
break;
case DESC:
sliceStart = colStart.getStaticBufferFlipBytes(startPosition, endPosition);
break;
default:
throw new AssertionError(type.getSortOrder().toString());
}
sliceEnd = ByteBufferUtil.nextBiggerBuffer(sliceStart);
}
}
return new SliceQuery(sliceStart, sliceEnd, isStatic);
}
use of com.thinkaurelius.titan.graphdb.internal.RelationType in project titan by thinkaurelius.
the class IDManagementTest method edgeTypeIDTest.
@Test
public void edgeTypeIDTest() {
int partitionBits = 21;
IDManager eid = new IDManager(partitionBits);
int trails = 1000000;
assertEquals(eid.getMaxPartitionCount(), (1 << partitionBits) - 1);
KryoSerializer serializer = new KryoSerializer();
for (int t = 0; t < trails; t++) {
long count = RandomGenerator.randomLong(1, eid.getMaxTitanTypeCount());
long id;
int dirID;
RelationType type;
if (Math.random() < 0.5) {
id = eid.getEdgeLabelID(count);
assertTrue(eid.isEdgeLabelID(id));
type = RelationType.EDGE;
if (Math.random() < 0.5)
dirID = IDHandler.EDGE_IN_DIR;
else
dirID = IDHandler.EDGE_OUT_DIR;
} else {
type = RelationType.PROPERTY;
id = eid.getPropertyKeyID(count);
assertTrue(eid.isPropertyKeyID(id));
dirID = IDHandler.PROPERTY_DIR;
}
assertTrue(eid.isTypeID(id));
StaticBuffer b = IDHandler.getEdgeType(id, dirID);
// System.out.println(dirID);
// System.out.println(getBinary(id));
// System.out.println(getBuffer(b.asReadBuffer()));
ReadBuffer rb = b.asReadBuffer();
long[] vals = IDHandler.readEdgeType(rb);
assertEquals(id, vals[0]);
assertEquals(dirID, vals[1]);
assertFalse(rb.hasRemaining());
// Inline edge type
WriteBuffer wb = new WriteByteBuffer(9);
IDHandler.writeInlineEdgeType(wb, id);
long newId = IDHandler.readInlineEdgeType(wb.getStaticBuffer().asReadBuffer());
assertEquals(id, newId);
// Compare to Kryo
DataOutput out = serializer.getDataOutput(10, true);
IDHandler.writeEdgeType(out, id, dirID);
assertEquals(b, out.getStaticBuffer());
// Make sure the bounds are right
StaticBuffer[] bounds = IDHandler.getBounds(type);
assertTrue(bounds[0].compareTo(b) < 0);
assertTrue(bounds[1].compareTo(b) > 0);
bounds = IDHandler.getBounds(RelationType.RELATION);
assertTrue(bounds[0].compareTo(b) < 0);
assertTrue(bounds[1].compareTo(b) > 0);
}
}
use of com.thinkaurelius.titan.graphdb.internal.RelationType in project titan by thinkaurelius.
the class IDManagementTest method testDirectionPrefix.
@Test
public void testDirectionPrefix() {
for (RelationType type : RelationType.values()) {
StaticBuffer[] bounds = IDHandler.getBounds(type);
assertEquals(1, bounds[0].length());
assertEquals(1, bounds[1].length());
assertTrue(bounds[0].compareTo(bounds[1]) < 0);
}
}
Aggregations