Search in sources :

Example 6 with EventHub

use of com.baidu.hugegraph.event.EventHub in project incubator-hugegraph by apache.

the class CachedGraphTransaction method listenChanges.

private void listenChanges() {
    // Listen store event: "store.init", "store.clear", ...
    Set<String> storeEvents = ImmutableSet.of(Events.STORE_INIT, Events.STORE_CLEAR, Events.STORE_TRUNCATE);
    this.storeEventListener = event -> {
        if (storeEvents.contains(event.name())) {
            LOG.debug("Graph {} clear graph cache on event '{}'", this.graph(), event.name());
            this.clearCache(null, true);
            return true;
        }
        return false;
    };
    this.store().provider().listen(this.storeEventListener);
    // Listen cache event: "cache"(invalid cache item)
    this.cacheEventListener = event -> {
        LOG.debug("Graph {} received graph cache event: {}", this.graph(), event);
        Object[] args = event.args();
        E.checkArgument(args.length > 0 && args[0] instanceof String, "Expect event action argument");
        if (Cache.ACTION_INVALID.equals(args[0])) {
            event.checkArgs(String.class, HugeType.class, Object.class);
            HugeType type = (HugeType) args[1];
            if (type.isVertex()) {
                // Invalidate vertex cache
                Object arg2 = args[2];
                if (arg2 instanceof Id) {
                    Id id = (Id) arg2;
                    this.verticesCache.invalidate(id);
                } else if (arg2 != null && arg2.getClass().isArray()) {
                    int size = Array.getLength(arg2);
                    for (int i = 0; i < size; i++) {
                        Object id = Array.get(arg2, i);
                        E.checkArgument(id instanceof Id, "Expect instance of Id in array, " + "but got '%s'", id.getClass());
                        this.verticesCache.invalidate((Id) id);
                    }
                } else {
                    E.checkArgument(false, "Expect Id or Id[], but got: %s", arg2);
                }
            } else if (type.isEdge()) {
                /*
                     * Invalidate edge cache via clear instead of invalidate
                     * because of the cacheKey is QueryId not EdgeId
                     */
                // this.edgesCache.invalidate(id);
                this.edgesCache.clear();
            }
            return true;
        } else if (Cache.ACTION_CLEAR.equals(args[0])) {
            event.checkArgs(String.class, HugeType.class);
            HugeType type = (HugeType) args[1];
            this.clearCache(type, false);
            return true;
        }
        return false;
    };
    EventHub graphEventHub = this.params().graphEventHub();
    if (!graphEventHub.containsListener(Events.CACHE)) {
        graphEventHub.listen(Events.CACHE, this.cacheEventListener);
    }
}
Also used : EventHub(com.baidu.hugegraph.event.EventHub) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) HugeType(com.baidu.hugegraph.type.HugeType)

Example 7 with EventHub

use of com.baidu.hugegraph.event.EventHub in project incubator-hugegraph by apache.

the class CachedGraphTransaction method notifyChanges.

private void notifyChanges(String action, HugeType type, Id[] ids) {
    EventHub graphEventHub = this.params().graphEventHub();
    graphEventHub.notify(Events.CACHE, action, type, ids);
}
Also used : EventHub(com.baidu.hugegraph.event.EventHub)

Example 8 with EventHub

use of com.baidu.hugegraph.event.EventHub in project incubator-hugegraph by apache.

the class CachedGraphTransaction method unlistenChanges.

private void unlistenChanges() {
    // Unlisten store event
    this.store().provider().unlisten(this.storeEventListener);
    // Unlisten cache event
    EventHub graphEventHub = this.params().graphEventHub();
    graphEventHub.unlisten(Events.CACHE, this.cacheEventListener);
}
Also used : EventHub(com.baidu.hugegraph.event.EventHub)

Aggregations

EventHub (com.baidu.hugegraph.event.EventHub)8 Id (com.baidu.hugegraph.backend.id.Id)2 HugeType (com.baidu.hugegraph.type.HugeType)2 HugeException (com.baidu.hugegraph.HugeException)1 ContextGremlinServer (com.baidu.hugegraph.auth.ContextGremlinServer)1 QueryId (com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId)1 ConfigUtil (com.baidu.hugegraph.util.ConfigUtil)1 E (com.baidu.hugegraph.util.E)1 Log (com.baidu.hugegraph.util.Log)1 GremlinServer (org.apache.tinkerpop.gremlin.server.GremlinServer)1 Settings (org.apache.tinkerpop.gremlin.server.Settings)1 Before (org.junit.Before)1 Logger (org.slf4j.Logger)1