Search in sources :

Example 21 with HugeType

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;
}
Also used : Condition(com.baidu.hugegraph.backend.query.Condition) HugeException(com.baidu.hugegraph.HugeException) HugeType(com.baidu.hugegraph.type.HugeType) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) Relation(com.baidu.hugegraph.backend.query.Condition.Relation) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) Id(com.baidu.hugegraph.backend.id.Id) IndexType(com.baidu.hugegraph.type.define.IndexType)

Example 22 with HugeType

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;
}
Also used : NoIndexException(com.baidu.hugegraph.exception.NoIndexException) SortByCountIdHolderList(com.baidu.hugegraph.backend.page.SortByCountIdHolderList) IdHolderList(com.baidu.hugegraph.backend.page.IdHolderList) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) IndexLabel(com.baidu.hugegraph.schema.IndexLabel) IdHolder(com.baidu.hugegraph.backend.page.IdHolder) FixedIdHolder(com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder) PagingIdHolder(com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder) BatchIdHolder(com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder) SchemaLabel(com.baidu.hugegraph.schema.SchemaLabel) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType) HugeException(com.baidu.hugegraph.HugeException) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 23 with HugeType

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;
}
Also used : BackendMutation(com.baidu.hugegraph.backend.store.BackendMutation) Action(com.baidu.hugegraph.type.define.Action) BackendAction(com.baidu.hugegraph.backend.store.BackendAction) BinaryBackendEntry(com.baidu.hugegraph.backend.serializer.BinaryBackendEntry) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Example 24 with HugeType

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);
}
Also used : IncrCounter(com.baidu.hugegraph.backend.store.raft.RaftBackendStore.IncrCounter) HugeType(com.baidu.hugegraph.type.HugeType)

Example 25 with HugeType

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
        }
    }
}
Also used : BackendAction(com.baidu.hugegraph.backend.store.BackendAction) ArrayList(java.util.ArrayList) PeerId(com.alipay.sofa.jraft.entity.PeerId) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Aggregations

HugeType (com.baidu.hugegraph.type.HugeType)34 Id (com.baidu.hugegraph.backend.id.Id)16 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)5 Query (com.baidu.hugegraph.backend.query.Query)4 ExistedException (com.baidu.hugegraph.exception.ExistedException)4 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)4 ArrayList (java.util.ArrayList)4 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)3 Condition (com.baidu.hugegraph.backend.query.Condition)3 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)3 BinaryBackendEntry (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry)3 BinaryId (com.baidu.hugegraph.backend.serializer.BinaryBackendEntry.BinaryId)3 BackendAction (com.baidu.hugegraph.backend.store.BackendAction)3 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)3 Lock (java.util.concurrent.locks.Lock)3 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)3 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)3 HugeException (com.baidu.hugegraph.HugeException)2 BackendException (com.baidu.hugegraph.backend.BackendException)2 BinaryEntryIterator (com.baidu.hugegraph.backend.serializer.BinaryEntryIterator)2