use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.
the class GraphIndexTransaction method constructQuery.
private static ConditionQuery constructQuery(ConditionQuery query, IndexLabel indexLabel) {
IndexType indexType = indexLabel.indexType();
boolean requireRange = query.hasRangeCondition();
boolean supportRange = indexType.isNumeric();
if (requireRange && !supportRange) {
LOG.debug("There is range query condition in '{}', " + "but the index label '{}' is unable to match", query, indexLabel.name());
return null;
}
Set<Id> queryKeys = query.userpropKeys();
List<Id> indexFields = indexLabel.indexFields();
if (!matchIndexFields(queryKeys, indexFields)) {
return null;
}
LOG.debug("Matched index fields: {} of index '{}'", indexFields, indexLabel);
ConditionQuery indexQuery;
switch(indexType) {
case SEARCH:
E.checkState(indexFields.size() == 1, "Invalid index fields size for %s: %s", indexType, indexFields);
Object fieldValue = query.userpropValue(indexFields.get(0));
assert fieldValue instanceof String;
// Will escape special char inside concatValues()
fieldValue = ConditionQuery.concatValues(fieldValue);
indexQuery = new ConditionQuery(indexType.type(), query);
indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
indexQuery.eq(HugeKeys.FIELD_VALUES, fieldValue);
break;
case SECONDARY:
List<Id> joinedKeys = indexFields.subList(0, queryKeys.size());
// Will escape special char inside userpropValuesString()
String joinedValues = query.userpropValuesString(joinedKeys);
indexQuery = new ConditionQuery(indexType.type(), query);
indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
indexQuery.eq(HugeKeys.FIELD_VALUES, joinedValues);
break;
case RANGE_INT:
case RANGE_FLOAT:
case RANGE_LONG:
case RANGE_DOUBLE:
if (query.userpropConditions().size() > 2) {
throw new HugeException("Range query has two conditions at most, " + "but got: %s", query.userpropConditions());
}
// Replace the query key with PROPERTY_VALUES, set number value
indexQuery = new ConditionQuery(indexType.type(), query);
indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
for (Condition condition : query.userpropConditions()) {
assert condition instanceof Condition.Relation;
Condition.Relation r = (Condition.Relation) condition;
Number value = NumericUtil.convertToNumber(r.value());
Condition.Relation sys = new Condition.SyspropRelation(HugeKeys.FIELD_VALUES, r.relation(), value);
condition = condition.replace(r, sys);
indexQuery.query(condition);
}
break;
case SHARD:
HugeType type = indexLabel.indexType().type();
indexQuery = new ConditionQuery(type, query);
indexQuery.eq(HugeKeys.INDEX_LABEL_ID, indexLabel.id());
List<Condition> conditions = constructShardConditions(query, indexLabel.indexFields(), HugeKeys.FIELD_VALUES);
indexQuery.query(conditions);
break;
default:
throw new AssertionError(String.format("Unknown index type '%s'", indexType));
}
/*
* Set limit for single index or composite index, also for joint index,
* to avoid redundant element ids and out of capacity.
* NOTE: not set offset because this query might be a sub-query,
* see queryByUserprop()
*/
indexQuery.page(query.page());
indexQuery.limit(query.total());
indexQuery.capacity(query.capacity());
indexQuery.olap(indexLabel.olap());
return indexQuery;
}
use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.
the class GraphIndexTransaction method queryByLabel.
@Watched(prefix = "index")
private IdHolderList queryByLabel(ConditionQuery query) {
HugeType queryType = query.resultType();
IndexLabel il = IndexLabel.label(queryType);
validateIndexLabel(il);
Id label = query.condition(HugeKeys.LABEL);
assert label != null;
HugeType indexType;
SchemaLabel schemaLabel;
if (queryType.isVertex()) {
indexType = HugeType.VERTEX_LABEL_INDEX;
schemaLabel = this.graph().vertexLabel(label);
} else if (queryType.isEdge()) {
indexType = HugeType.EDGE_LABEL_INDEX;
schemaLabel = this.graph().edgeLabel(label);
} else {
throw new HugeException("Can't query %s by label", queryType);
}
if (!this.store().features().supportsQueryByLabel() && !schemaLabel.enableLabelIndex()) {
throw new NoIndexException("Don't accept query by label '%s', " + "label index is disabled", schemaLabel);
}
ConditionQuery indexQuery = new ConditionQuery(indexType, query);
indexQuery.eq(HugeKeys.INDEX_LABEL_ID, il.id());
indexQuery.eq(HugeKeys.FIELD_VALUES, label);
/*
* We can avoid redundant element ids if set limit, but if there are
* label index overridden by other vertices with different label,
* query with limit like g.V().hasLabel('xx').limit(10) may lose some
* results, so can't set limit here. But in this case, the following
* query results may be still different:
* g.V().hasLabel('xx').count() // label index count
* g.V().hasLabel('xx').limit(-1).count() // actual vertices count
* It’s a similar situation for the offset, like:
* g.V().hasLabel('xx').range(26, 27)
* g.V().hasLabel('xx').range(27, 28)
* we just reset limit here, but don't reset offset due to performance
* optimization with index+offset query, see Query.skipOffsetIfNeeded().
* NOTE: if set offset the backend itself will skip the offset
*/
indexQuery.copyBasic(query);
indexQuery.limit(Query.NO_LIMIT);
IdHolder idHolder = this.doIndexQuery(il, indexQuery);
IdHolderList holders = new IdHolderList(query.paging());
holders.add(idHolder);
return holders;
}
use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.
the class StoreSerializer method readMutation.
public static BackendMutation readMutation(BytesBuffer buffer) {
int size = buffer.readVInt();
BackendMutation mutation = new BackendMutation(size);
for (int i = 0; i < size; i++) {
// read action
Action action = Action.fromCode(buffer.read());
// read HugeType
HugeType type = SerialEnum.fromCode(HugeType.class, buffer.read());
// read id
byte[] idBytes = buffer.readBytes();
// read subId
Id subId = buffer.readId();
if (subId.equals(IdGenerator.ZERO)) {
subId = null;
}
// read ttl
long ttl = buffer.readVLong();
BinaryBackendEntry entry = new BinaryBackendEntry(type, idBytes);
entry.subId(subId);
entry.ttl(ttl);
// read columns
int columnsSize = buffer.readVInt();
for (int c = 0; c < columnsSize; c++) {
byte[] name = buffer.readBytes();
byte[] value = buffer.readBytes();
entry.column(BackendColumn.of(name, value));
}
mutation.put(entry, action);
}
return mutation;
}
use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.
the class StoreSerializer method readIncrCounter.
public static IncrCounter readIncrCounter(BytesBuffer buffer) {
HugeType type = SerialEnum.fromCode(HugeType.class, buffer.read());
long increment = buffer.readVLong();
return new IncrCounter(type, increment);
}
use of com.baidu.hugegraph.type.HugeType in project incubator-hugegraph by apache.
the class RaftSharedContext method updateCacheIfNeeded.
protected void updateCacheIfNeeded(BackendMutation mutation, boolean forwarded) {
// Update cache only when graph run in general mode
if (this.graphMode() != GraphMode.NONE) {
return;
}
/*
* 1. If Follower, need to update cache from store to tx
* 3. If Leader, request is forwarded by follower, need to update cache
* 2. If Leader, request comes from leader, don't need to update cache,
* because the cache will be updated by upper layer
*/
if (!forwarded && this.node().selfIsLeader()) {
return;
}
for (HugeType type : mutation.types()) {
List<Id> ids = new ArrayList<>((int) Query.COMMIT_BATCH);
if (type.isSchema() || type.isGraph()) {
java.util.Iterator<BackendAction> it = mutation.mutation(type);
while (it.hasNext()) {
ids.add(it.next().entry().originId());
}
this.notifyCache(Cache.ACTION_INVALID, type, ids);
} else {
// Ignore other types due to not cached them
}
}
}
Aggregations