Class MapDBCache
- All Implemented Interfaces:
AutoCloseable,Cache<PersistentCacheKey,CompletableFuture<? extends DataRetrievalResponse<?>>>
HTreeMap.
This class is thread-safe for concurrent access through its public methods on
a single MapDBCache instance. All access is guarded by
synchronization on map.
Cache entry creation, invalidation, and replacement behavior are delegated to the configured cache policies.
The cache enforces a maximum capacity. If the persisted map contains more entries than allowed when opened, entries are evicted according to the configured replacement policy until the capacity constraint is satisfied.
Caveats:
Thread safety is provided only at the level of this cache instance. If two MapDBCache objects point to the same persisted file, this class does not coordinate:
- replacement policy state
- capacity enforcement
- invalidation/eviction decisions
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.mapdb.DBprotected static final intprotected static final StringFields inherited from class se.liu.ida.hefquin.federation.access.impl.cache.CacheLayer
capacity, entryFactory, invalidationPolicy, lock, map, replacementPolicy -
Constructor Summary
ConstructorsConstructorDescriptionMapDBCache(int capacity, String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Creates a cache with the given capacity and file path.MapDBCache(int capacity, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Creates a cache with the given capacity and the default file path.MapDBCache(String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Constructs a newMapDBCachewith a custom file path and the default cache capacity.MapDBCache(CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, PersistentCacheEntry> policies) Creates a cache with the default capacity and default file path. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the underlying MapDB database and releases associated resources.protected static FileensureParentDirectoryExists(String filename) Ensures that the parent directory of the given file path exists.
-
Field Details
-
DEFAULT_CAPACITY
protected static final int DEFAULT_CAPACITY- See Also:
-
DEFAULT_FILENAME
- See Also:
-
db
protected final org.mapdb.DB db
-
-
Constructor Details
-
MapDBCache
public MapDBCache(CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Creates a cache with the default capacity and default file path.- Parameters:
policies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
MapDBCache
public MapDBCache(int capacity, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Creates a cache with the given capacity and the default file path.- Parameters:
capacity- the maximum number of cache entriespolicies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
MapDBCache
public MapDBCache(String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Constructs a newMapDBCachewith a custom file path and the default cache capacity.- Parameters:
filename- the path to the cache filepolicies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
MapDBCache
public MapDBCache(int capacity, String filename, CachePolicies<PersistentCacheKey, CompletableFuture<? extends DataRetrievalResponse<?>>, throws IOExceptionPersistentCacheEntry> policies) Creates a cache with the given capacity and file path.If the persisted map already contains entries, the replacement policy is initialized accordingly and eviction is performed if necessary to satisfy the capacity constraint.
- Parameters:
capacity- the maximum number of cache entriesfilename- the path to the cache filepolicies- the cache policies to use- Throws:
IOException- if the cache file cannot be created or opened
-
-
Method Details
-
ensureParentDirectoryExists
Ensures that the parent directory of the given file path exists.- Parameters:
filename- the path to the file- Returns:
- a
Fileobject for the given path - Throws:
IllegalStateException- if the parent directory does not exist and cannot be created
-
close
public void close()Closes the underlying MapDB database and releases associated resources.After this method returns, the cache must not be used anymore.
-