xunliu commented on code in PR #7354:
URL: https://github.com/apache/gravitino/pull/7354#discussion_r2134930160
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -83,46 +101,124 @@ 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 {
- return backend.list(namespace, entityType, false);
+ if (!cacheEnabled) {
+ return backend.list(namespace, entityType, false);
+ }
+
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, false);
+ entities.forEach(cache::put);
+
+ return entities;
Review Comment:
Can we list entities from cache?
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -54,12 +58,26 @@ public class RelationalEntityStore
Configs.DEFAULT_ENTITY_RELATIONAL_STORE,
JDBCBackend.class.getCanonicalName());
private RelationalBackend backend;
private RelationalGarbageCollector garbageCollector;
+ private EntityCache cache;
+ private boolean cacheEnabled;
+
+ /**
+ * Return whether the cache is enabled or not.
+ *
+ * @return {@code true} if cache is enable, otherwise {@code false}
+ */
+ public boolean cacheEnabled() {
+ return cacheEnabled;
+ }
@Override
public void initialize(Config config) throws RuntimeException {
this.backend = createRelationalEntityBackend(config);
this.garbageCollector = new RelationalGarbageCollector(backend, config);
this.garbageCollector.start();
+ // TODO USE SPI to load the cache
+ this.cache = new CaffeineEntityCache(config);
+ this.cacheEnabled = config.get(Configs.CACHE_ENABLED);
Review Comment:
maybe we need modify these code?
```
this.cacheEnabled = config.get(Configs.CACHE_ENABLED);
if (!cacheEnabled) {
this.cache = new CaffeineEntityCache(config);
}
```
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -83,46 +101,124 @@ 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 {
- return backend.list(namespace, entityType, false);
+ if (!cacheEnabled) {
+ return backend.list(namespace, entityType, false);
+ }
+
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, false);
+ entities.forEach(cache::put);
+
+ return entities;
+ });
}
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType,
boolean allFields)
throws IOException {
- return backend.list(namespace, entityType, allFields);
+ if (!cacheEnabled) {
+ return backend.list(namespace, entityType, allFields);
+ }
+
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, allFields);
+ entities.forEach(cache::put);
+
+ return entities;
+ });
}
@Override
public boolean exists(NameIdentifier ident, Entity.EntityType entityType)
throws IOException {
- return backend.exists(ident, entityType);
+ if (!cacheEnabled) {
+ return backend.exists(ident, entityType);
+ }
+
+ return cache.withCacheLock(
+ () -> {
+ if (cache.contains(ident, entityType)) {
+ return true;
+ }
+
+ return backend.exists(ident, entityType);
+ });
Review Comment:
I think maybe we need to adjust the code order?
We need to check the cache first, then the database
```
if (cacheEnabled) {
# check from cache
# if exist then return xxx
}
return backend.exists(ident, entityType);
```
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -184,7 +281,26 @@ public List<TagEntity> associateTagsWithMetadataObject(
public <E extends Entity & HasIdentifier> List<E> listEntitiesByRelation(
Type relType, NameIdentifier nameIdentifier, Entity.EntityType
identType, boolean allFields)
throws IOException {
- return backend.listEntitiesByRelation(relType, nameIdentifier, identType,
allFields);
+ if (!cacheEnabled) {
+ return backend.listEntitiesByRelation(relType, nameIdentifier,
identType, allFields);
+ }
+
+ return cache.withCacheLock(
+ () -> {
+ Optional<List<E>> entities = cache.getIfPresent(relType,
nameIdentifier, identType);
+ if (entities.isPresent()) {
+ return entities.get();
+ }
+
+ List<E> EntitiesFromDb =
Review Comment:
Maybe the var name`backendEntities` is better than `EntitiesFromDb`?
##########
core/src/main/java/org/apache/gravitino/storage/relational/RelationalEntityStore.java:
##########
@@ -83,46 +101,124 @@ 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 {
- return backend.list(namespace, entityType, false);
+ if (!cacheEnabled) {
+ return backend.list(namespace, entityType, false);
+ }
+
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, false);
+ entities.forEach(cache::put);
+
+ return entities;
+ });
}
@Override
public <E extends Entity & HasIdentifier> List<E> list(
Namespace namespace, Class<E> type, Entity.EntityType entityType,
boolean allFields)
throws IOException {
- return backend.list(namespace, entityType, allFields);
+ if (!cacheEnabled) {
+ return backend.list(namespace, entityType, allFields);
+ }
+
+ return cache.withCacheLock(
+ () -> {
+ List<E> entities = backend.list(namespace, entityType, allFields);
+ entities.forEach(cache::put);
+
+ return entities;
Review Comment:
Can we list entities from cache?
--
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]