diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java index f94d52d78d..f6de54d384 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/backend/cache/CachedSchemaTransaction.java @@ -111,20 +111,7 @@ private void listenChanges() { event.checkArgs(String.class, HugeType.class, Id.class); HugeType type = (HugeType) args[1]; Id id = (Id) args[2]; - this.arrayCaches.remove(type, id); - - id = generateId(type, id); - Object value = this.idCache.get(id); - if (value != null) { - // Invalidate id cache - this.idCache.invalidate(id); - - // Invalidate name cache - SchemaElement schema = (SchemaElement) value; - Id prefixedName = generateId(schema.type(), - schema.name()); - this.nameCache.invalidate(prefixedName); - } + this.invalidateCache(type, id); this.resetCachedAll(type); return true; } else if (Cache.ACTION_CLEAR.equals(args[0])) { @@ -140,21 +127,6 @@ private void listenChanges() { } } - private final void resetCachedAll(HugeType type) { - // Set the cache all flag of the schema type to false - this.cachedTypes().put(type, false); - } - - private void clearCache(boolean notify) { - this.idCache.clear(); - this.nameCache.clear(); - this.arrayCaches.clear(); - - if (notify) { - this.notifyChanges(Cache.ACTION_CLEARED, null, null); - } - } - private void unlistenChanges() { // Unlisten store event this.store().provider().unlisten(this.storeEventListener); @@ -164,11 +136,16 @@ private void unlistenChanges() { schemaEventHub.unlisten(Events.CACHE, this.cacheEventListener); } - private void notifyChanges(String action, HugeType type, Id id) { + private final void notifyChanges(String action, HugeType type, Id id) { EventHub graphEventHub = this.params().schemaEventHub(); graphEventHub.notify(Events.CACHE, action, type, id); } + private final void resetCachedAll(HugeType type) { + // Set the cache all flag of the schema type to false + this.cachedTypes().put(type, false); + } + private final void resetCachedAllIfReachedCapacity() { if (this.idCache.size() >= this.idCache.capacity()) { LOG.warn("Schema cache reached capacity({}): {}", @@ -181,20 +158,17 @@ private final CachedTypes cachedTypes() { return this.arrayCaches.cachedTypes(); } - private static Id generateId(HugeType type, Id id) { - // NOTE: it's slower performance to use: - // String.format("%x-%s", type.code(), name) - return IdGenerator.of(type.string() + "-" + id.asString()); - } + private final void clearCache(boolean notify) { + this.idCache.clear(); + this.nameCache.clear(); + this.arrayCaches.clear(); - private static Id generateId(HugeType type, String name) { - return IdGenerator.of(type.string() + "-" + name); + if (notify) { + this.notifyChanges(Cache.ACTION_CLEARED, null, null); + } } - @Override - protected void addSchema(SchemaElement schema) { - super.addSchema(schema); - + private final void updateCache(SchemaElement schema) { this.resetCachedAllIfReachedCapacity(); // update id cache @@ -207,6 +181,39 @@ protected void addSchema(SchemaElement schema) { // update optimized array cache this.arrayCaches.updateIfNeeded(schema); + } + + private final void invalidateCache(HugeType type, Id id) { + // remove from id cache and name cache + Id prefixedId = generateId(type, id); + Object value = this.idCache.get(prefixedId); + if (value != null) { + this.idCache.invalidate(prefixedId); + + SchemaElement schema = (SchemaElement) value; + Id prefixedName = generateId(schema.type(), schema.name()); + this.nameCache.invalidate(prefixedName); + } + + // remove from optimized array cache + this.arrayCaches.remove(type, id); + } + + private static Id generateId(HugeType type, Id id) { + // NOTE: it's slower performance to use: + // String.format("%x-%s", type.code(), name) + return IdGenerator.of(type.string() + "-" + id.asString()); + } + + private static Id generateId(HugeType type, String name) { + return IdGenerator.of(type.string() + "-" + name); + } + + @Override + protected void addSchema(SchemaElement schema) { + super.addSchema(schema); + + this.updateCache(schema); this.notifyChanges(Cache.ACTION_INVALIDED, schema.type(), schema.id()); } @@ -227,19 +234,15 @@ protected T getSchema(HugeType type, Id id) { if (value == null) { value = super.getSchema(type, id); if (value != null) { - this.resetCachedAllIfReachedCapacity(); - - this.idCache.update(prefixedId, value); - SchemaElement schema = (SchemaElement) value; - Id prefixedName = generateId(schema.type(), schema.name()); - this.nameCache.update(prefixedName, schema); + // update id cache, name cache and optimized array cache + this.updateCache(schema); } + } else { + // update optimized array cache for the result from id cache + this.arrayCaches.updateIfNeeded((SchemaElement) value); } - // update optimized array cache - this.arrayCaches.updateIfNeeded((SchemaElement) value); - return (T) value; } @@ -252,13 +255,8 @@ protected T getSchema(HugeType type, if (value == null) { value = super.getSchema(type, name); if (value != null) { - this.resetCachedAllIfReachedCapacity(); - - this.nameCache.update(prefixedName, value); - SchemaElement schema = (SchemaElement) value; - Id prefixedId = generateId(schema.type(), schema.id()); - this.idCache.update(prefixedId, schema); + this.updateCache(schema); } } return (T) value; @@ -268,18 +266,7 @@ protected T getSchema(HugeType type, protected void removeSchema(SchemaElement schema) { super.removeSchema(schema); - Id prefixedId = generateId(schema.type(), schema.id()); - Object value = this.idCache.get(prefixedId); - if (value != null) { - this.idCache.invalidate(prefixedId); - - schema = (SchemaElement) value; - Id prefixedName = generateId(schema.type(), schema.name()); - this.nameCache.invalidate(prefixedName); - } - - // remove from optimized array cache - this.arrayCaches.remove(schema.type(), schema.id()); + this.invalidateCache(schema.type(), schema.id()); this.notifyChanges(Cache.ACTION_INVALIDED, schema.type(), schema.id()); } @@ -304,11 +291,7 @@ protected List getAllSchema(HugeType type) { if (results.size() <= free) { // Update cache for (T schema : results) { - Id prefixedId = generateId(schema.type(), schema.id()); - this.idCache.update(prefixedId, schema); - - Id prefixedName = generateId(schema.type(), schema.name()); - this.nameCache.update(prefixedName, schema); + this.updateCache(schema); } this.cachedTypes().putIfAbsent(type, true); }