yuqi1129 commented on code in PR #7354:
URL: https://github.com/apache/gravitino/pull/7354#discussion_r2137027010
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -196,6 +314,15 @@ public void insertRelation(
Entity.EntityType dstType,
boolean override)
throws IOException {
- backend.insertRelation(relType, srcIdentifier, srcType, dstIdentifier,
dstType, true);
+ if (cacheEnabled) {
+ cache.withCacheLock(
+ () -> {
+ cache.invalidate(srcIdentifier, srcType, relType);
+ backend.insertRelation(
Review Comment:
This has not be resolved.
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -83,47 +107,121 @@ private static RelationalBackend
createRelationalEntityBackend(Config config) {
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType) throws
IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, false);
+ entities.forEach(cache::put);
+
+ return entities;
+ });
+ }
+
return backend.list(namespace, entityType, false);
}
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType,
boolean allFields)
throws IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, allFields);
+ if (namespaceSet.add(namespace)) {
+ entities.forEach(cache::put);
+ }
+
+ return entities;
+ });
+ }
+
return backend.list(namespace, entityType, allFields);
}
@Override
public boolean exists(NameIdentifier ident, Entity.EntityType entityType)
throws IOException {
+ if (cacheEnabled) {
+ boolean existsInCache = cache.contains(ident, entityType);
+ return existsInCache || backend.exists(ident, entityType);
+ }
+
return backend.exists(ident, entityType);
}
@Override
public <E extends Entity & HasIdentifier> void put(E e, boolean overwritten)
throws IOException, EntityAlreadyExistsException {
+ if (cacheEnabled) {
+ cache.withCacheLock(
+ () -> {
+ backend.insert(e, overwritten);
+
+ if (e.type() == Entity.EntityType.MODEL_VERSION) {
+ NameIdentifier modelIdent = ((ModelVersionEntity)
e).modelIdentifier();
+ cache.invalidate(modelIdent, Entity.EntityType.MODEL);
+ }
+ cache.put(e);
+ });
+ return;
+ }
+
backend.insert(e, overwritten);
}
@Override
public <E extends Entity & HasIdentifier> E update(
NameIdentifier ident, Class<E> type, Entity.EntityType entityType,
Function<E, E> updater)
throws IOException, NoSuchEntityException, EntityAlreadyExistsException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ cache.invalidate(ident, entityType);
+ E updatedEntity = backend.update(ident, entityType, updater);
+ cache.put(updatedEntity);
+
+ return updatedEntity;
+ });
+ }
+
return backend.update(ident, entityType, updater);
}
@Override
public <E extends Entity & HasIdentifier> E get(
NameIdentifier ident, Entity.EntityType entityType, Class<E> e)
throws NoSuchEntityException, IOException {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ Optional<E> entityFromCache = cache.getIfPresent(ident,
entityType);
+ if (entityFromCache.isPresent()) {
+ return entityFromCache.get();
+ }
+
+ E entity = backend.get(ident, entityType);
+ cache.put(entity);
+ return entity;
+ });
+ }
+
return backend.get(ident, entityType);
}
@Override
public boolean delete(NameIdentifier ident, Entity.EntityType entityType,
boolean cascade)
throws IOException {
try {
+ if (cacheEnabled) {
+ return cache.withCacheLock(
+ () -> {
+ cache.clear();
+ return backend.delete(ident, entityType, cascade);
Review Comment:
This can also be moved out of the cache lock block.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]