use of com.thinkaurelius.titan.graphdb.internal.InternalType 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());
}
use of com.thinkaurelius.titan.graphdb.internal.InternalType in project titan by thinkaurelius.
the class IndexSerializer method processSingleCondition.
private List<Object> processSingleCondition(ElementType resultType, PredicateCondition pc, final int limit, BackendTransaction tx) {
Preconditions.checkArgument(resultType == ElementType.EDGE || resultType == ElementType.VERTEX);
Preconditions.checkArgument(pc.getPredicate() == Cmp.EQUAL, "Only equality index retrievals are supported on standard index");
Preconditions.checkNotNull(pc.getValue());
Preconditions.checkArgument(limit >= 0);
TitanKey key = (TitanKey) pc.getKey();
Preconditions.checkArgument(key.hasIndex(Titan.Token.STANDARD_INDEX, resultType.getElementType()), "Cannot retrieve for given property key - it does not have an index [%s]", key.getName());
Object value = pc.getValue();
StaticBuffer column = getUniqueIndexColumn(key);
KeySliceQuery sq = new KeySliceQuery(getIndexKey(value), column, SliceQuery.pointRange(column), ((InternalType) key).isStatic(Direction.IN)).setLimit(limit);
List<Entry> r;
if (resultType == ElementType.VERTEX) {
r = tx.vertexIndexQuery(sq);
} else {
r = tx.edgeIndexQuery(sq);
}
List<Object> results = new ArrayList<Object>(r.size());
for (Entry entry : r) {
ReadBuffer entryValue = entry.getReadValue();
if (resultType == ElementType.VERTEX) {
results.add(VariableLong.readPositive(entryValue));
} else {
results.add(bytebuffer2RelationId(entryValue));
}
}
Preconditions.checkArgument(!(resultType == ElementType.VERTEX && key.isUnique(Direction.IN)) || results.size() <= 1);
return results;
}
Aggregations