use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.
the class IndexLabelAPI method list.
@GET
@Timed
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=index_label_read" })
public String list(@Context GraphManager manager, @PathParam("graph") String graph, @QueryParam("names") List<String> names) {
boolean listAll = CollectionUtils.isEmpty(names);
if (listAll) {
LOG.debug("Graph [{}] list index labels", graph);
} else {
LOG.debug("Graph [{}] get index labels by names {}", graph, names);
}
HugeGraph g = graph(manager, graph);
List<IndexLabel> labels;
if (listAll) {
labels = g.schema().getIndexLabels();
} else {
labels = new ArrayList<>(names.size());
for (String name : names) {
labels.add(g.schema().getIndexLabel(name));
}
}
return manager.serializer(g).writeIndexlabels(mapIndexLabels(labels));
}
use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.
the class JsonUtilTest method testSerializeIndexLabel.
@Test
public void testSerializeIndexLabel() {
FakeObjects fakeObject = new FakeObjects();
PropertyKey name = fakeObject.newPropertyKey(IdGenerator.of(1), "name");
PropertyKey age = fakeObject.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
PropertyKey city = fakeObject.newPropertyKey(IdGenerator.of(3), "city");
VertexLabel vl = fakeObject.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
IndexLabel il = fakeObject.newIndexLabel(IdGenerator.of(1), "personByAgeAndCity", HugeType.VERTEX_LABEL, vl.id(), IndexType.SECONDARY, age.id(), city.id());
Mockito.when(fakeObject.graph().vertexLabel(vl.id())).thenReturn(vl);
Mockito.when(fakeObject.graph().mapPkId2Name(il.indexFields())).thenReturn(Arrays.asList(age.name(), city.name()));
String json = JsonUtil.toJson(il);
Assert.assertEquals("{\"id\":1," + "\"name\":\"personByAgeAndCity\"," + "\"base_type\":\"VERTEX_LABEL\"," + "\"base_value\":\"person\"," + "\"index_type\":\"SECONDARY\"," + "\"fields\":[\"age\",\"city\"]," + "\"status\":\"CREATED\"," + "\"user_data\":{}}", json);
}
use of com.baidu.hugegraph.schema.IndexLabel 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.schema.IndexLabel in project incubator-hugegraph by apache.
the class GraphIndexTransaction method matchJointIndexes.
/**
* Collect index label(s) whose prefix index fields are contained in
* property-keys in query
*/
private static Set<IndexLabel> matchJointIndexes(ConditionQuery query, Set<IndexLabel> indexLabels) {
if (query.hasNeqCondition()) {
return ImmutableSet.of();
}
Set<Id> queryPropKeys = query.userpropKeys();
assert !queryPropKeys.isEmpty();
Set<IndexLabel> allILs = InsertionOrderUtil.newSet(indexLabels);
// Handle range/search index first
Set<IndexLabel> matchedIndexLabels = InsertionOrderUtil.newSet();
if (query.hasRangeCondition() || query.hasSearchCondition()) {
matchedIndexLabels = matchRangeOrSearchIndexLabels(query, allILs);
if (matchedIndexLabels.isEmpty()) {
return ImmutableSet.of();
}
allILs.removeAll(matchedIndexLabels);
// Remove matched queryPropKeys
for (IndexLabel il : matchedIndexLabels) {
// Only one field each range/search index-label
queryPropKeys.remove(il.indexField());
}
// Return if all fields are matched
if (queryPropKeys.isEmpty()) {
return matchedIndexLabels;
}
}
// Handle secondary indexes
Set<Id> indexFields = InsertionOrderUtil.newSet();
for (IndexLabel indexLabel : allILs) {
// Range index equal-condition and secondary index can joint
if (indexLabel.indexType().isSearch()) {
// Search index must be handled at the previous step
continue;
}
List<Id> fields = indexLabel.indexFields();
// Collect all fields prefix
for (Id field : fields) {
if (!queryPropKeys.contains(field)) {
break;
}
matchedIndexLabels.add(indexLabel);
indexFields.add(field);
}
}
// Must match all fields
if (indexFields.equals(queryPropKeys)) {
return matchedIndexLabels;
} else {
return ImmutableSet.of();
}
}
use of com.baidu.hugegraph.schema.IndexLabel in project incubator-hugegraph by apache.
the class GraphIndexTransaction method queryByUserprop.
@Watched(prefix = "index")
private IdHolderList queryByUserprop(ConditionQuery query) {
// related index labels
if (!this.graph().readMode().showOlap()) {
for (Id pkId : query.userpropKeys()) {
PropertyKey propertyKey = this.graph().propertyKey(pkId);
if (propertyKey.olap()) {
throw new NotAllowException("Not allowed to query by olap property key '%s'" + " when graph-read-mode is '%s'", propertyKey, this.graph().readMode());
}
}
}
Set<MatchedIndex> indexes = this.collectMatchedIndexes(query);
if (indexes.isEmpty()) {
Id label = query.condition(HugeKeys.LABEL);
throw noIndexException(this.graph(), query, label);
}
// Value type of Condition not matched
boolean paging = query.paging();
if (!validQueryConditionValues(this.graph(), query)) {
return IdHolderList.empty(paging);
}
// Do index query
IdHolderList holders = new IdHolderList(paging);
for (MatchedIndex index : indexes) {
for (IndexLabel il : index.indexLabels()) {
validateIndexLabel(il);
}
if (paging && index.indexLabels().size() > 1) {
throw new NotSupportException("joint index query in paging");
}
if (index.containsSearchIndex()) {
// Do search-index query
holders.addAll(this.doSearchIndex(query, index));
} else {
// Do secondary-index, range-index or shard-index query
IndexQueries queries = index.constructIndexQueries(query);
assert !paging || queries.size() <= 1;
IdHolder holder = this.doSingleOrJointIndex(queries);
holders.add(holder);
}
/*
* NOTE: need to skip the offset if offset > 0, but can't handle
* it here because the query may a sub-query after flatten,
* so the offset will be handle in QueryList.IndexQuery
*
* TODO: finish early here if records exceeds required limit with
* FixedIdHolder.
*/
}
return holders;
}
Aggregations