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);
}
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;
}
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();
}
}
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();
}
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;
}
Aggregations