Search in sources :

Example 1 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class CachedGraphTransaction method queryEdgesFromBackend.

@Override
@Watched(prefix = "graphcache")
protected final Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
    RamTable ramtable = this.params().ramtable();
    if (ramtable != null && ramtable.matched(query)) {
        return ramtable.query(query);
    }
    if (!this.enableCacheEdge() || query.empty() || query.paging() || query.bigCapacity()) {
        // Query all edges or query edges in paging, don't cache it
        return super.queryEdgesFromBackend(query);
    }
    Id cacheKey = new QueryId(query);
    Object value = this.edgesCache.get(cacheKey);
    @SuppressWarnings("unchecked") Collection<HugeEdge> edges = (Collection<HugeEdge>) value;
    if (value != null) {
        for (HugeEdge edge : edges) {
            if (edge.expired()) {
                this.edgesCache.invalidate(cacheKey);
                value = null;
            }
        }
    }
    if (value != null) {
        // Not cached or the cache expired
        return edges.iterator();
    }
    Iterator<HugeEdge> rs = super.queryEdgesFromBackend(query);
    /*
         * Iterator can't be cached, caching list instead
         * there may be super node and too many edges in a query,
         * try fetch a few of the head results and determine whether to cache.
         */
    final int tryMax = 1 + MAX_CACHE_EDGES_PER_QUERY;
    assert tryMax > MAX_CACHE_EDGES_PER_QUERY;
    edges = new ArrayList<>(tryMax);
    for (int i = 0; rs.hasNext() && i < tryMax; i++) {
        edges.add(rs.next());
    }
    if (edges.size() == 0) {
        this.edgesCache.update(cacheKey, Collections.emptyList());
    } else if (edges.size() <= MAX_CACHE_EDGES_PER_QUERY) {
        this.edgesCache.update(cacheKey, edges);
    }
    return new ExtendableIterator<>(edges.iterator(), rs);
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Collection(java.util.Collection) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 2 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class CachedGraphTransaction method queryVerticesByIds.

@Watched(prefix = "graphcache")
private Iterator<HugeVertex> queryVerticesByIds(IdQuery query) {
    if (query.idsSize() == 1) {
        Id vertexId = query.ids().iterator().next();
        HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
        if (vertex != null) {
            if (!vertex.expired()) {
                return QueryResults.iterator(vertex);
            }
            this.verticesCache.invalidate(vertexId);
        }
        Iterator<HugeVertex> rs = super.queryVerticesFromBackend(query);
        vertex = QueryResults.one(rs);
        if (vertex == null) {
            return QueryResults.emptyIterator();
        }
        if (needCacheVertex(vertex)) {
            this.verticesCache.update(vertex.id(), vertex);
        }
        return QueryResults.iterator(vertex);
    }
    IdQuery newQuery = new IdQuery(HugeType.VERTEX, query);
    List<HugeVertex> vertices = new ArrayList<>();
    for (Id vertexId : query.ids()) {
        HugeVertex vertex = (HugeVertex) this.verticesCache.get(vertexId);
        if (vertex == null) {
            newQuery.query(vertexId);
        } else if (vertex.expired()) {
            newQuery.query(vertexId);
            this.verticesCache.invalidate(vertexId);
        } else {
            vertices.add(vertex);
        }
    }
    // Join results from cache and backend
    ExtendableIterator<HugeVertex> results = new ExtendableIterator<>();
    if (!vertices.isEmpty()) {
        results.extend(vertices.iterator());
    } else {
        // Just use the origin query if find none from the cache
        newQuery = query;
    }
    if (!newQuery.empty()) {
        Iterator<HugeVertex> rs = super.queryVerticesFromBackend(newQuery);
        // Generally there are not too much data with id query
        ListIterator<HugeVertex> listIterator = QueryResults.toList(rs);
        for (HugeVertex vertex : listIterator.list()) {
            // Skip large vertex
            if (needCacheVertex(vertex)) {
                this.verticesCache.update(vertex.id(), vertex);
            }
        }
        results.extend(listIterator);
    }
    return results;
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) ArrayList(java.util.ArrayList) IdQuery(com.baidu.hugegraph.backend.query.IdQuery) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 3 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class RamCache method write.

@Override
@Watched(prefix = "ramcache")
protected final boolean write(Id id, Object value, long timeOffset) {
    assert id != null;
    long capacity = this.capacity();
    assert capacity > 0;
    final Lock lock = this.keyLock.lock(id);
    try {
        // The cache is full
        while (this.map.size() >= capacity) {
            /*
                 * Remove the oldest from the queue
                 * NOTE: it maybe return null if someone else (that's other
                 * threads) are doing dequeue() and the queue may be empty.
                 */
            LinkNode<Id, Object> removed = this.queue.dequeue();
            if (removed == null) {
                /*
                     * If at this time someone add some new items, these will
                     * be cleared in the map, but still stay in the queue, so
                     * the queue will have some more nodes than the map.
                     */
                this.map.clear();
                break;
            }
            /*
                 * Remove the oldest from the map
                 * NOTE: it maybe return null if other threads are doing remove
                 */
            this.map.remove(removed.key());
            if (LOG.isDebugEnabled()) {
                LOG.debug("RamCache replaced '{}' with '{}' (capacity={})", removed.key(), id, capacity);
            }
            /*
                 * Release the object
                 * NOTE: we can't reuse the removed node due to someone else
                 * may access the node (will do remove() -> enqueue())
                 */
            removed = null;
        }
        // Remove the old node if exists
        LinkNode<Id, Object> node = this.map.get(id);
        if (node != null) {
            this.queue.remove(node);
        }
        // Add the new item to tail of the queue, then map it
        this.map.put(id, this.queue.enqueue(id, value, timeOffset));
        return true;
    } finally {
        lock.unlock();
    }
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) Lock(java.util.concurrent.locks.Lock) KeyLock(com.baidu.hugegraph.concurrent.KeyLock) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 4 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class HugeEdge method properties.

@Watched(prefix = "edge")
// (Property<V>) prop
@SuppressWarnings("unchecked")
@Override
public <V> Iterator<Property<V>> properties(String... keys) {
    this.ensureFilledProperties(true);
    // Capacity should be about the following size
    int propsCapacity = keys.length == 0 ? this.sizeOfProperties() : keys.length;
    List<Property<V>> props = new ArrayList<>(propsCapacity);
    if (keys.length == 0) {
        for (HugeProperty<?> prop : this.getProperties()) {
            assert prop instanceof Property;
            props.add((Property<V>) prop);
        }
    } else {
        for (String key : keys) {
            Id pkeyId;
            try {
                pkeyId = this.graph().propertyKey(key).id();
            } catch (IllegalArgumentException ignored) {
                continue;
            }
            HugeProperty<?> prop = this.getProperty(pkeyId);
            if (prop == null) {
                // Not found
                continue;
            }
            assert prop instanceof Property;
            props.add((Property<V>) prop);
        }
    }
    return props.iterator();
}
Also used : ArrayList(java.util.ArrayList) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) Id(com.baidu.hugegraph.backend.id.Id) Property(org.apache.tinkerpop.gremlin.structure.Property) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 5 with Watched

use of com.baidu.hugegraph.perf.PerfUtil.Watched in project incubator-hugegraph by apache.

the class HugeEdge method sortValues.

@Watched(prefix = "edge")
protected List<Object> sortValues() {
    List<Id> sortKeys = this.schemaLabel().sortKeys();
    if (sortKeys.isEmpty()) {
        return ImmutableList.of();
    }
    List<Object> propValues = new ArrayList<>(sortKeys.size());
    for (Id sk : sortKeys) {
        HugeProperty<?> property = this.getProperty(sk);
        E.checkState(property != null, "The value of sort key '%s' can't be null", sk);
        Object propValue = property.serialValue(true);
        if (Strings.EMPTY.equals(propValue)) {
            propValue = ConditionQuery.INDEX_VALUE_EMPTY;
        }
        propValues.add(propValue);
    }
    return propValues;
}
Also used : ArrayList(java.util.ArrayList) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Aggregations

Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)65 Id (com.baidu.hugegraph.backend.id.Id)30 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)9 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)8 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)8 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)7 ArrayList (java.util.ArrayList)7 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)6 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)6 BatchIdHolder (com.baidu.hugegraph.backend.page.IdHolder.BatchIdHolder)5 HugeIndex (com.baidu.hugegraph.structure.HugeIndex)5 IdHolder (com.baidu.hugegraph.backend.page.IdHolder)4 FixedIdHolder (com.baidu.hugegraph.backend.page.IdHolder.FixedIdHolder)4 PagingIdHolder (com.baidu.hugegraph.backend.page.IdHolder.PagingIdHolder)4 IdQuery (com.baidu.hugegraph.backend.query.IdQuery)4 Query (com.baidu.hugegraph.backend.query.Query)4 SchemaJob (com.baidu.hugegraph.job.schema.SchemaJob)4 SchemaLabel (com.baidu.hugegraph.schema.SchemaLabel)4 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)4 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4