Skip to content

Commit

Permalink
improve CachedSchemaTransaction
Browse files Browse the repository at this point in the history
Change-Id: Ibcdef0f33c9c1127908cc9bfdba5f4fd7f1e309a
  • Loading branch information
javeme committed Jan 11, 2022
1 parent ebe9960 commit 022bcdf
Showing 1 changed file with 57 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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])) {
Expand All @@ -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);
Expand All @@ -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({}): {}",
Expand All @@ -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
Expand All @@ -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());
}
Expand All @@ -227,19 +234,15 @@ protected <T extends SchemaElement> 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;
}

Expand All @@ -252,13 +255,8 @@ protected <T extends SchemaElement> 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;
Expand All @@ -268,18 +266,7 @@ protected <T extends SchemaElement> 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());
}
Expand All @@ -299,16 +286,13 @@ protected <T extends SchemaElement> List<T> getAllSchema(HugeType type) {
});
return results;
} else {
this.cachedTypes().remove(type);
List<T> results = super.getAllSchema(type);
long free = this.idCache.capacity() - this.idCache.size();
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);
}
Expand Down

0 comments on commit 022bcdf

Please sign in to comment.