use of com.tinkerpop.blueprints.Direction in project titan by thinkaurelius.
the class EdgeSerializer method writeRelation.
public Entry writeRelation(InternalRelation relation, int position, StandardTitanTx tx) {
Preconditions.checkArgument(position < relation.getLen());
TitanType type = relation.getType();
long typeid = type.getID();
Direction dir = EdgeDirection.fromPosition(position);
int dirID = getDirID(dir, relation.isProperty() ? RelationType.PROPERTY : RelationType.EDGE);
DataOutput colOut = serializer.getDataOutput(DEFAULT_COLUMN_CAPACITY, true);
IDHandler.writeEdgeType(colOut, typeid, dirID);
InternalType definition = (InternalType) type;
long[] sortKey = definition.getSortKey();
int startPosition = colOut.getPosition();
if (!type.isUnique(dir)) {
writeInlineTypes(sortKey, relation, colOut, tx);
}
int endPosition = colOut.getPosition();
DataOutput writer = colOut;
long vertexIdDiff = 0;
long relationIdDiff = relation.getID() - relation.getVertex(position).getID();
if (relation.isEdge())
vertexIdDiff = relation.getVertex((position + 1) % 2).getID() - relation.getVertex(position).getID();
if (type.isUnique(dir)) {
writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
if (relation.isEdge())
VariableLong.write(writer, vertexIdDiff);
VariableLong.write(writer, relationIdDiff);
} else {
if (relation.isEdge())
VariableLong.writeBackward(writer, vertexIdDiff);
VariableLong.writeBackward(writer, relationIdDiff);
}
if (!type.isUnique(dir)) {
writer = serializer.getDataOutput(DEFAULT_VALUE_CAPACITY, true);
}
if (relation.isProperty()) {
Preconditions.checkArgument(relation.isProperty());
Object value = ((TitanProperty) relation).getValue();
Preconditions.checkNotNull(value);
TitanKey key = (TitanKey) type;
assert key.getDataType().isInstance(value);
if (hasGenericDataType(key)) {
writer.writeClassAndObject(value);
} else {
writer.writeObjectNotNull(value);
}
}
// Write signature & sort key if unique
if (type.isUnique(dir)) {
writeInlineTypes(sortKey, relation, writer, tx);
}
long[] signature = definition.getSignature();
writeInlineTypes(signature, relation, writer, tx);
// Write remaining properties
LongSet writtenTypes = new LongOpenHashSet(sortKey.length + signature.length);
if (sortKey.length > 0 || signature.length > 0) {
for (long id : sortKey) writtenTypes.add(id);
for (long id : signature) writtenTypes.add(id);
}
LongArrayList remainingTypes = new LongArrayList(8);
for (TitanType t : relation.getPropertyKeysDirect()) {
if (!writtenTypes.contains(t.getID())) {
remainingTypes.add(t.getID());
}
}
// Sort types before writing to ensure that value is always written the same way
long[] remaining = remainingTypes.toArray();
Arrays.sort(remaining);
for (long tid : remaining) {
TitanType t = tx.getExistingType(tid);
writeInline(writer, t, relation.getProperty(t), true);
}
StaticBuffer column = ((InternalType) type).getSortOrder() == Order.DESC ? colOut.getStaticBufferFlipBytes(startPosition, endPosition) : colOut.getStaticBuffer();
return new StaticBufferEntry(column, writer.getStaticBuffer());
}
Aggregations