Skip to content

Commit cbd4797

Browse files
authored
Merge pull request #70 from Roche-CSI/CacheDocs
2 parents 6afd396 + 5a53745 commit cbd4797

File tree

7 files changed

+125
-5
lines changed

7 files changed

+125
-5
lines changed

re-cache/src/main/java/com/g2forge/reassert/cache/ICache.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
import com.g2forge.alexandria.java.function.IFunction1;
44

5+
/**
6+
* An on disk cache, capable of creating multiple cache areas. Each cache area can have it's own key and value types.
7+
*/
58
public interface ICache {
9+
/**
10+
* Create a cache area from it's descriptor.
11+
*
12+
* @param <K>
13+
* The type of keys for this cache area.
14+
* @param <V>
15+
* The type of values for this cache area.
16+
* @param areaDescriptor
17+
* The cache area descriptor.
18+
* @return A cache area. A function from keys to values.
19+
*/
620
public <K, V> IFunction1<? super K, ? extends V> createArea(ICacheAreaDescriptor<K, V> areaDescriptor);
721
}

re-cache/src/main/java/com/g2forge/reassert/cache/ICacheAreaDescriptor.java

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,88 @@
22

33
import java.nio.file.Path;
44

5+
import com.g2forge.alexandria.java.adt.name.INamed;
56
import com.g2forge.alexandria.java.function.IFunction1;
67
import com.g2forge.alexandria.java.function.IFunction2;
78
import com.g2forge.reassert.cache.store.ICacheStore;
89

9-
public interface ICacheAreaDescriptor<K, V> {
10+
/**
11+
* A descriptor for a cache area. The methods on this interface allow the cache to learn how to compute values, load &amp; store keys, exceptions, and values,
12+
* and where to create cache entries within this area. Caches should cache exceptions when possible, and should verify that the key for any cache entry matches
13+
* to expected key in case of collisions in {@link #getHashFunction()}.
14+
*
15+
* @param <K>
16+
* The type of keys for this cache area.
17+
* @param <V>
18+
* The type of values for this cache area.
19+
*/
20+
public interface ICacheAreaDescriptor<K, V> extends INamed<Path> {
21+
/**
22+
* Storage abstraction for exceptions from {@link #getFunction()}. Exceptions are cached in order to improve performance. See
23+
* {@link com.g2forge.reassert.cache.store.ObjectSerializationCacheStore} which is strongly recommended as being one of the few ways to serialize and
24+
* deserialize exceptions in Java.
25+
*
26+
* @return Storage abstraction for the cache function exceptions.
27+
*/
28+
public ICacheStore<Exception> getExceptionConverter();
29+
30+
/**
31+
* Get the name of the on-disk exception within the cache entry.
32+
*
33+
* @return The name of the on-disk exception within the cache entry.
34+
*/
35+
public String getExceptionName();
36+
37+
/**
38+
* The function to cache. Accepts the key, and the on-disk path where the value should be cached. The path should only be used if this function is
39+
* downloading something, or otherwise making a copy of a large file structure. The path will not exist when this function is called, but it can be created
40+
* by this function. If <code>path</code> is <code>null</code> then the function should download any files to a temp directory instead. Returns the actual
41+
* value.
42+
*
43+
* @return The function whose results we are caching. A function from key and local path to actual value.
44+
*/
1045
public IFunction2<? super K, ? super Path, ? extends V> getFunction();
1146

47+
/**
48+
* Key hash function used to determine the on-disk location of the cache entry within the cache area. This function should return a (hopefully, but not
49+
* necessarily always) unique path for each key, which is valid on this operating system. <code>key -> Paths.get(key.toString())</code>, or something
50+
* equally simple, is acceptable if there are never OS-disallowed characters in that string.
51+
*
52+
* @return The key hash function.
53+
*/
1254
public IFunction1<? super K, ? extends Path> getHashFunction();
1355

56+
/**
57+
* Storage abstraction for the cache keys. See implementations of {@link ICacheStore}.
58+
*
59+
* @return Storage abstraction for the cache keys
60+
*/
1461
public ICacheStore<K> getKeyConverter();
1562

63+
/**
64+
* Get the name of the on-disk key within the cache entry.
65+
*
66+
* @return The name of the on-disk key within the cache entry.
67+
*/
1668
public String getKeyName();
1769

70+
/**
71+
* A name for this cache area. Should be unique to whatever is being cached and stable over time. <code>Paths.get(getClass().getSimpleName())</code> is
72+
* often a reasonable value.
73+
*/
1874
public Path getName();
1975

76+
/**
77+
* Storage abstraction for the cache values. See implementations of {@link ICacheStore}.
78+
*
79+
* @return Storage abstraction for the cache values.
80+
*/
2081
public ICacheStore<V> getValueConverter();
21-
22-
public ICacheStore<Exception> getExceptionConverter();
2382

83+
/**
84+
* Get the name of the on-disk value within the cache entry.
85+
*
86+
* @return The name of the on-disk value within the cache entry.
87+
*/
2488
public String getValueName();
25-
26-
public String getExceptionName();
2789
}

re-cache/src/main/java/com/g2forge/reassert/cache/store/DirectoryCacheStore.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
import com.g2forge.alexandria.java.io.RuntimeIOException;
1010
import com.g2forge.alexandria.java.io.file.HFile;
1111

12+
/**
13+
* A cache store for directories abstracted as java paths. They are moved into the cache when stored, or copied in if the move fails.
14+
*/
1215
public class DirectoryCacheStore implements ICacheStore<Path> {
1316
@Override
1417
public Path load(Path path) {

re-cache/src/main/java/com/g2forge/reassert/cache/store/FileCacheStore.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import com.g2forge.alexandria.java.io.RuntimeIOException;
99

10+
/**
11+
* A cache store for files abstracted as java paths. They are moved into the cache when stored.
12+
*/
1013
public class FileCacheStore implements ICacheStore<Path> {
1114
@Override
1215
public Path load(Path path) {

re-cache/src/main/java/com/g2forge/reassert/cache/store/ICacheStore.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,30 @@
22

33
import java.nio.file.Path;
44

5+
/**
6+
* A type-specific storage abstraction for cached keys and values.
7+
*
8+
* @param <T>
9+
* The type being stored.
10+
*/
511
public interface ICacheStore<T> {
12+
/**
13+
* Load a value from the specified path.
14+
*
15+
* @param path
16+
* The file or directory to load.
17+
* @return The loaded value.
18+
*/
619
public T load(Path path);
720

21+
/**
22+
* Store a value to the specified path.
23+
*
24+
* @param path
25+
* The file or directory to store a value to. This may or may not exist.
26+
* @param value
27+
* The value to store.
28+
* @return The value that was stored.
29+
*/
830
public T store(Path path, T value);
931
}

re-cache/src/main/java/com/g2forge/reassert/cache/store/JacksonCacheStore.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,19 @@
1111
import lombok.Getter;
1212
import lombok.RequiredArgsConstructor;
1313

14+
/**
15+
* A cache store for objects, which uses a jackson object mapper to serialize and deserialize them.
16+
*
17+
* @param <T>
18+
* The type of the values being stored.
19+
*/
1420
@Getter
1521
@RequiredArgsConstructor
1622
public class JacksonCacheStore<T> implements ICacheStore<T> {
23+
/** The jackson mapper to use during serdes. */
1724
protected final ObjectMapper mapper;
1825

26+
/** The type of the values being stored. */
1927
protected final ITypeRef<T> type;
2028

2129
@Override

re-cache/src/main/java/com/g2forge/reassert/cache/store/ObjectSerializationCacheStore.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,17 @@
1212
import lombok.Getter;
1313
import lombok.RequiredArgsConstructor;
1414

15+
/**
16+
* A cache store for objects, which uses java object serialization. This is particularly suited to storing exceptions on disk, and is often used for
17+
* {@link com.g2forge.reassert.cache.ICacheAreaDescriptor#getExceptionConverter()}.
18+
*
19+
* @param <T>
20+
* The type of the values being stored.
21+
*/
1522
@Getter
1623
@RequiredArgsConstructor
1724
public class ObjectSerializationCacheStore<T> implements ICacheStore<T> {
25+
/** The type of the values being stored. */
1826
protected final ITypeRef<T> type;
1927

2028
@Override

0 commit comments

Comments
 (0)