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);