Search in sources :

Example 6 with Repository

use of org.molgenis.data.Repository in project molgenis by molgenis.

the class L2Cache method get.

/**
 * Retrieves an entity from the cache or the underlying repository.
 *
 * @param repository the underlying repository
 * @param id         the ID of the entity to retrieve
 * @return the retrieved Entity, or null if the entity is not present.
 * @throws com.google.common.util.concurrent.UncheckedExecutionException if the repository throws an error when
 *                                                                       loading the entity
 */
public Entity get(Repository<Entity> repository, Object id) {
    LoadingCache<Object, Optional<Map<String, Object>>> cache = getEntityCache(repository);
    EntityType entityType = repository.getEntityType();
    return cache.getUnchecked(id).map(e -> entityHydration.hydrate(e, entityType)).orElse(null);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) Optional.empty(java.util.Optional.empty) LoadingCache(com.google.common.cache.LoadingCache) LoggerFactory(org.slf4j.LoggerFactory) MINUTES(java.util.concurrent.TimeUnit.MINUTES) Scheduled(org.springframework.scheduling.annotation.Scheduled) MetaDataService(org.molgenis.data.meta.MetaDataService) ConcurrentMap(java.util.concurrent.ConcurrentMap) Collectors.toMap(java.util.stream.Collectors.toMap) EntityKey(org.molgenis.data.EntityKey) Service(org.springframework.stereotype.Service) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) Nonnull(javax.annotation.Nonnull) Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Logger(org.slf4j.Logger) CaffeinatedGuava(com.github.benmanes.caffeine.guava.CaffeinatedGuava) TransactionInformation(org.molgenis.data.transaction.TransactionInformation) DefaultMolgenisTransactionListener(org.molgenis.data.transaction.DefaultMolgenisTransactionListener) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) TransactionManager(org.molgenis.data.transaction.TransactionManager) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Stream(java.util.stream.Stream) StreamSupport.stream(java.util.stream.StreamSupport.stream) Maps.newConcurrentMap(com.google.common.collect.Maps.newConcurrentMap) Repository(org.molgenis.data.Repository) Optional(java.util.Optional) EntityHydration(org.molgenis.data.cache.utils.EntityHydration) MolgenisDataException(org.molgenis.data.MolgenisDataException) Entity(org.molgenis.data.Entity) Optional(java.util.Optional)

Aggregations

Repository (org.molgenis.data.Repository)6 Test (org.testng.annotations.Test)4 InputStream (java.io.InputStream)3 EntityType (org.molgenis.data.meta.model.EntityType)3 File (java.io.File)2 Entity (org.molgenis.data.Entity)2 FileMeta (org.molgenis.data.file.model.FileMeta)2 Caffeine (com.github.benmanes.caffeine.cache.Caffeine)1 CaffeinatedGuava (com.github.benmanes.caffeine.guava.CaffeinatedGuava)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 Maps.newConcurrentMap (com.google.common.collect.Maps.newConcurrentMap)1 List (java.util.List)1 Map (java.util.Map)1 Objects.requireNonNull (java.util.Objects.requireNonNull)1 Optional (java.util.Optional)1 Optional.empty (java.util.Optional.empty)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ExecutionException (java.util.concurrent.ExecutionException)1 MINUTES (java.util.concurrent.TimeUnit.MINUTES)1