use of com.baidu.hugegraph.type.define.IndexType in project incubator-hugegraph by apache.
the class TextSerializer method readIndexLabel.
@Override
public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry backendEntry) {
if (backendEntry == null) {
return null;
}
TextBackendEntry entry = this.convertEntry(backendEntry);
Id id = readId(entry.id());
String name = JsonUtil.fromJson(entry.column(HugeKeys.NAME), String.class);
String baseType = entry.column(HugeKeys.BASE_TYPE);
String baseValue = entry.column(HugeKeys.BASE_VALUE);
String indexType = entry.column(HugeKeys.INDEX_TYPE);
String indexFields = entry.column(HugeKeys.FIELDS);
String status = entry.column(HugeKeys.STATUS);
IndexLabel indexLabel = new IndexLabel(graph, id, name);
indexLabel.baseType(JsonUtil.fromJson(baseType, HugeType.class));
indexLabel.baseValue(readId(baseValue));
indexLabel.indexType(JsonUtil.fromJson(indexType, IndexType.class));
indexLabel.indexFields(readIds(indexFields));
readUserdata(indexLabel, entry);
indexLabel.status(JsonUtil.fromJson(status, SchemaStatus.class));
return indexLabel;
}
use of com.baidu.hugegraph.type.define.IndexType in project incubator-hugegraph by apache.
the class GraphIndexTransaction method matchSingleOrCompositeIndex.
private static Set<IndexLabel> matchSingleOrCompositeIndex(ConditionQuery query, Set<IndexLabel> indexLabels) {
if (query.hasNeqCondition()) {
return ImmutableSet.of();
}
boolean requireRange = query.hasRangeCondition();
boolean requireSearch = query.hasSearchCondition();
Set<Id> queryPropKeys = query.userpropKeys();
for (IndexLabel indexLabel : indexLabels) {
List<Id> indexFields = indexLabel.indexFields();
// Try to match fields
if (!matchIndexFields(queryPropKeys, indexFields)) {
continue;
}
/*
* Matched all fields, try to match index type.
* For range-index or search-index there must be only one condition.
* The following terms are legal:
* 1.hasSearchCondition and IndexType.SEARCH
* 2.hasRangeCondition and IndexType.RANGE
* 3.not hasRangeCondition but has range-index equal-condition
* 4.secondary (composite) index
*/
IndexType indexType = indexLabel.indexType();
if ((requireSearch && !indexType.isSearch()) || (!requireSearch && indexType.isSearch())) {
continue;
}
if (requireRange && !indexType.isNumeric()) {
continue;
}
return ImmutableSet.of(indexLabel);
}
return ImmutableSet.of();
}
use of com.baidu.hugegraph.type.define.IndexType in project incubator-hugegraph by apache.
the class TableSerializer method readIndexLabel.
@Override
public IndexLabel readIndexLabel(HugeGraph graph, BackendEntry backendEntry) {
if (backendEntry == null) {
return null;
}
TableBackendEntry entry = this.convertEntry(backendEntry);
Number id = schemaColumn(entry, HugeKeys.ID);
String name = schemaColumn(entry, HugeKeys.NAME);
HugeType baseType = schemaEnum(entry, HugeKeys.BASE_TYPE, HugeType.class);
Number baseValueId = schemaColumn(entry, HugeKeys.BASE_VALUE);
IndexType indexType = schemaEnum(entry, HugeKeys.INDEX_TYPE, IndexType.class);
Object indexFields = schemaColumn(entry, HugeKeys.FIELDS);
SchemaStatus status = schemaEnum(entry, HugeKeys.STATUS, SchemaStatus.class);
IndexLabel indexLabel = new IndexLabel(graph, this.toId(id), name);
indexLabel.baseType(baseType);
indexLabel.baseValue(this.toId(baseValueId));
indexLabel.indexType(indexType);
indexLabel.indexFields(this.toIdArray(indexFields));
indexLabel.status(status);
this.readUserdata(indexLabel, entry);
return indexLabel;
}
use of com.baidu.hugegraph.type.define.IndexType 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;
}
Aggregations