xunliu commented on code in PR #7192: URL: https://github.com/apache/gravitino/pull/7192#discussion_r2101879218
########## core/src/main/java/org/apache/gravitino/storage/relational/CachedEntityStore.java: ########## @@ -0,0 +1,268 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.storage.relational; + +import java.io.IOException; +import java.util.List; +import java.util.Optional; +import java.util.function.Function; +import org.apache.gravitino.Config; +import org.apache.gravitino.Entity; +import org.apache.gravitino.EntityAlreadyExistsException; +import org.apache.gravitino.EntityStore; +import org.apache.gravitino.HasIdentifier; +import org.apache.gravitino.MetadataObject; +import org.apache.gravitino.NameIdentifier; +import org.apache.gravitino.Namespace; +import org.apache.gravitino.SupportsRelationOperations; +import org.apache.gravitino.cache.EntityCache; +import org.apache.gravitino.exceptions.NoSuchEntityException; +import org.apache.gravitino.meta.TagEntity; +import org.apache.gravitino.tag.SupportsTagOperations; +import org.apache.gravitino.utils.Executable; + +/** Cached Entity store, which caches metadata in memory. */ +public class CachedEntityStore + implements EntityStore, SupportsTagOperations, SupportsRelationOperations { + private final RelationalEntityStore entityStore; + + private final EntityCache cache; + + /** + * Constructs a new instance of CachedEntityStore. + * + * @param entityStore The {@link RelationalEntityStore} instance to do the real work. + * @param cache The {@link EntityCache} instance to cache metadata in memory. + */ + public CachedEntityStore(EntityStore entityStore, EntityCache cache) { + this.entityStore = (RelationalEntityStore) entityStore; + this.cache = cache; + } + + /** {@inheritDoc} */ + @Override + public void initialize(Config config) throws RuntimeException { + entityStore.initialize(config); + } + + /** {@inheritDoc} */ + @Override + public <E extends Entity & HasIdentifier> List<E> list( + Namespace namespace, Class<E> type, Entity.EntityType entityType) throws IOException { + return cache.withCacheLock( + () -> { + List<E> entities = entityStore.list(namespace, type, entityType); + entities.forEach(cache::put); + + return entities; + }); + } + + /** {@inheritDoc} */ + @Override + public <E extends Entity & HasIdentifier> List<E> list( + Namespace namespace, Class<E> type, Entity.EntityType entityType, boolean allFields) + throws IOException { + return cache.withCacheLock( + () -> { + List<E> entities = entityStore.list(namespace, type, entityType, allFields); + entities.forEach(cache::put); + + return entities; + }); + } + + /** {@inheritDoc} */ + @Override + public boolean exists(NameIdentifier ident, Entity.EntityType entityType) throws IOException { + return cache.withCacheLock( + () -> { + if (cache.contains(ident, entityType)) { + return true; + } + return entityStore.exists(ident, entityType); + }); + } + + /** {@inheritDoc} */ + @Override + public <E extends Entity & HasIdentifier> void put(E e, boolean overwritten) + throws IOException, EntityAlreadyExistsException { + cache.withCacheLock( + () -> { + entityStore.put(e, overwritten); + cache.put(e); + }); + } + + /** {@inheritDoc} */ + @Override + public <E extends Entity & HasIdentifier> E update( + NameIdentifier ident, Class<E> type, Entity.EntityType entityType, Function<E, E> updater) + throws IOException, NoSuchEntityException, EntityAlreadyExistsException { + return cache.withCacheLock( + () -> { + cache.invalidate(ident, entityType); + E updatedEntity = entityStore.update(ident, type, entityType, updater); + cache.put(updatedEntity); + + return updatedEntity; + }); + } + + /** {@inheritDoc} */ + @Override + public <E extends Entity & HasIdentifier> E get( + NameIdentifier ident, Entity.EntityType entityType, Class<E> e) + throws NoSuchEntityException, IOException { + + return (E) + cache.withCacheLock( + () -> { + if (cache.contains(ident, entityType)) { + return cache.getIfPresent(ident, entityType).get(); + } + + E entity = entityStore.get(ident, entityType, e); + cache.put(entity); + + return entity; + }); + } + + /** {@inheritDoc} */ + @Override + public boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolean cascade) + throws IOException { + return cache.withCacheLock( + () -> { + cache.invalidate(ident, entityType); + return entityStore.delete(ident, entityType, cascade); + }); + } + + /** {@inheritDoc} */ + @Override + public <R, E extends Exception> R executeInTransaction(Executable<R, E> executable) + throws E, IOException { + throw new UnsupportedOperationException( + "Unsupported operation in cached relational entity store."); + } + + /** {@inheritDoc} */ + @Override + public void close() throws IOException { + cache.withCacheLock( + () -> { + entityStore.close(); + cache.clear(); + }); + } + + /** {@inheritDoc} */ + @Override + public SupportsTagOperations tagOperations() { + return entityStore.tagOperations(); + } + + /** {@inheritDoc} */ + @Override + public SupportsRelationOperations relationOperations() { + return entityStore.relationOperations(); + } + + /** {@inheritDoc} */ + @Override + public List<MetadataObject> listAssociatedMetadataObjectsForTag(NameIdentifier tagIdent) + throws IOException { + return entityStore.listAssociatedMetadataObjectsForTag(tagIdent); Review Comment: Can we get a tag from the 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]
