use of com.baidu.hugegraph.backend.query.IdPrefixQuery in project incubator-hugegraph by apache.
the class BinarySerializer method prefixQuery.
private static Query prefixQuery(ConditionQuery query, Id prefix) {
Query newQuery;
if (query.paging() && !query.page().isEmpty()) {
/*
* If used paging and the page number is not empty, deserialize
* the page to id and use it as the starting row for this query
*/
byte[] position = PageState.fromString(query.page()).position();
E.checkArgument(Bytes.compare(position, prefix.asBytes()) >= 0, "Invalid page out of lower bound");
BinaryId start = new BinaryId(position, null);
newQuery = new IdPrefixQuery(query, start, prefix);
} else {
newQuery = new IdPrefixQuery(query, prefix);
}
return newQuery;
}
use of com.baidu.hugegraph.backend.query.IdPrefixQuery in project incubator-hugegraph by apache.
the class TextSerializer method writeQueryEdgeRangeCondition.
private Query writeQueryEdgeRangeCondition(ConditionQuery cq) {
List<Condition> sortValues = cq.syspropConditions(HugeKeys.SORT_VALUES);
E.checkArgument(sortValues.size() >= 1 && sortValues.size() <= 2, "Edge range query must be with sort-values range");
// Would ignore target vertex
Object vertex = cq.condition(HugeKeys.OWNER_VERTEX);
Object direction = cq.condition(HugeKeys.DIRECTION);
if (direction == null) {
direction = Directions.OUT;
}
Object label = cq.condition(HugeKeys.LABEL);
List<String> start = new ArrayList<>(cq.conditionsSize());
start.add(writeEntryId((Id) vertex));
start.add(writeType(((Directions) direction).type()));
start.add(writeId((Id) label));
List<String> end = new ArrayList<>(start);
RangeConditions range = new RangeConditions(sortValues);
if (range.keyMin() != null) {
start.add((String) range.keyMin());
}
if (range.keyMax() != null) {
end.add((String) range.keyMax());
}
// Sort-value will be empty if there is no start sort-value
String startId = EdgeId.concat(start.toArray(new String[0]));
// Set endId as prefix if there is no end sort-value
String endId = EdgeId.concat(end.toArray(new String[0]));
if (range.keyMax() == null) {
return new IdPrefixQuery(cq, IdGenerator.of(startId), range.keyMinEq(), IdGenerator.of(endId));
}
return new IdRangeQuery(cq, IdGenerator.of(startId), range.keyMinEq(), IdGenerator.of(endId), range.keyMaxEq());
}
use of com.baidu.hugegraph.backend.query.IdPrefixQuery in project incubator-hugegraph by apache.
the class BinarySerializer method writeRangeIndexQuery.
private Query writeRangeIndexQuery(ConditionQuery query) {
Id index = query.condition(HugeKeys.INDEX_LABEL_ID);
E.checkArgument(index != null, "Please specify the index label");
List<Condition> fields = query.syspropConditions(HugeKeys.FIELD_VALUES);
E.checkArgument(!fields.isEmpty(), "Please specify the index field values");
HugeType type = query.resultType();
Id start = null;
if (query.paging() && !query.page().isEmpty()) {
byte[] position = PageState.fromString(query.page()).position();
start = new BinaryId(position, null);
}
RangeConditions range = new RangeConditions(fields);
if (range.keyEq() != null) {
Id id = formatIndexId(type, index, range.keyEq(), true);
if (start == null) {
return new IdPrefixQuery(query, id);
}
E.checkArgument(Bytes.compare(start.asBytes(), id.asBytes()) >= 0, "Invalid page out of lower bound");
return new IdPrefixQuery(query, start, id);
}
Object keyMin = range.keyMin();
Object keyMax = range.keyMax();
boolean keyMinEq = range.keyMinEq();
boolean keyMaxEq = range.keyMaxEq();
if (keyMin == null) {
E.checkArgument(keyMax != null, "Please specify at least one condition");
// Set keyMin to min value
keyMin = NumericUtil.minValueOf(keyMax.getClass());
keyMinEq = true;
}
Id min = formatIndexId(type, index, keyMin, false);
if (!keyMinEq) {
/*
* Increase 1 to keyMin, index GT query is a scan with GT prefix,
* inclusiveStart=false will also match index started with keyMin
*/
increaseOne(min.asBytes());
keyMinEq = true;
}
if (start == null) {
start = min;
} else {
E.checkArgument(Bytes.compare(start.asBytes(), min.asBytes()) >= 0, "Invalid page out of lower bound");
}
if (keyMax == null) {
keyMax = NumericUtil.maxValueOf(keyMin.getClass());
keyMaxEq = true;
}
Id max = formatIndexId(type, index, keyMax, false);
if (keyMaxEq) {
keyMaxEq = false;
increaseOne(max.asBytes());
}
return new IdRangeQuery(query, start, keyMinEq, max, keyMaxEq);
}
use of com.baidu.hugegraph.backend.query.IdPrefixQuery in project incubator-hugegraph by apache.
the class BinarySerializer method writeQueryEdgeRangeCondition.
private Query writeQueryEdgeRangeCondition(ConditionQuery cq) {
List<Condition> sortValues = cq.syspropConditions(HugeKeys.SORT_VALUES);
E.checkArgument(sortValues.size() >= 1 && sortValues.size() <= 2, "Edge range query must be with sort-values range");
// Would ignore target vertex
Id vertex = cq.condition(HugeKeys.OWNER_VERTEX);
Directions direction = cq.condition(HugeKeys.DIRECTION);
if (direction == null) {
direction = Directions.OUT;
}
Id label = cq.condition(HugeKeys.LABEL);
BytesBuffer start = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
writePartitionedId(HugeType.EDGE, vertex, start);
start.write(direction.type().code());
start.writeId(label);
BytesBuffer end = BytesBuffer.allocate(BytesBuffer.BUF_EDGE_ID);
end.copyFrom(start);
RangeConditions range = new RangeConditions(sortValues);
if (range.keyMin() != null) {
start.writeStringRaw((String) range.keyMin());
}
if (range.keyMax() != null) {
end.writeStringRaw((String) range.keyMax());
}
// Sort-value will be empty if there is no start sort-value
Id startId = new BinaryId(start.bytes(), null);
// Set endId as prefix if there is no end sort-value
Id endId = new BinaryId(end.bytes(), null);
boolean includeStart = range.keyMinEq();
if (cq.paging() && !cq.page().isEmpty()) {
includeStart = true;
byte[] position = PageState.fromString(cq.page()).position();
E.checkArgument(Bytes.compare(position, startId.asBytes()) >= 0, "Invalid page out of lower bound");
startId = new BinaryId(position, null);
}
if (range.keyMax() == null) {
return new IdPrefixQuery(cq, startId, includeStart, endId);
}
return new IdRangeQuery(cq, startId, includeStart, endId, range.keyMaxEq());
}
use of com.baidu.hugegraph.backend.query.IdPrefixQuery in project incubator-hugegraph by apache.
the class InMemoryDBTable method query.
@Override
public Iterator<BackendEntry> query(BackendSession session, Query query) {
String page = query.page();
if (page != null && !page.isEmpty()) {
throw new NotSupportException("paging by InMemoryDBStore");
}
Map<Id, BackendEntry> rs = this.store;
if (query instanceof IdPrefixQuery) {
IdPrefixQuery pq = (IdPrefixQuery) query;
rs = this.queryByIdPrefix(pq.start(), pq.inclusiveStart(), pq.prefix(), rs);
}
if (query instanceof IdRangeQuery) {
IdRangeQuery rq = (IdRangeQuery) query;
rs = this.queryByIdRange(rq.start(), rq.inclusiveStart(), rq.end(), rq.inclusiveEnd(), rs);
}
// Query by id(s)
if (query.idsSize() > 0) {
rs = this.queryById(query.ids(), rs);
}
// Query by condition(s)
if (query.conditionsSize() > 0) {
ConditionQuery condQuery = (ConditionQuery) query;
if (condQuery.containsScanRelation()) {
return this.queryByRange(condQuery);
}
rs = this.queryByFilter(query.conditions(), rs);
}
Iterator<BackendEntry> iterator = rs.values().iterator();
long offset = query.offset() - query.actualOffset();
if (offset >= rs.size()) {
query.goOffset(rs.size());
return QueryResults.emptyIterator();
}
if (offset > 0L) {
query.goOffset(offset);
iterator = this.skipOffset(iterator, offset);
}
if (!query.noLimit() && query.total() < rs.size()) {
iterator = this.dropTails(iterator, query.limit());
}
return iterator;
}
Aggregations