Skip to content

Commit 585d4e3

Browse files
committed
Refactors storage API for statelessness
Removes cache implementations, KVStorage interface, and direct implementations. Renames and refactors classes to clarify the stateless nature of the storage API. Updates Credentials to use a builder pattern.
1 parent 983d28a commit 585d4e3

31 files changed

Lines changed: 50 additions & 1510 deletions

src/main/java/wtf/casper/storageapi/Credentials.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package wtf.casper.storageapi;
22

3-
import lombok.AccessLevel;
4-
import lombok.AllArgsConstructor;
5-
import lombok.Getter;
6-
import lombok.RequiredArgsConstructor;
3+
import lombok.*;
74
import org.jetbrains.annotations.Nullable;
85

96
@Getter
107
@AllArgsConstructor(access = AccessLevel.PRIVATE)
118
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
9+
@Builder
1210
public final class Credentials {
1311

1412
private final StorageType type;
@@ -30,10 +28,6 @@ public final class Credentials {
3028
@Nullable
3129
private Integer port;
3230

33-
public static Credentials of(final StorageType type, @Nullable final String host, @Nullable final String username, @Nullable final String password, @Nullable final String database, @Nullable String collection, @Nullable String table, @Nullable final String uri, final int port) {
34-
return new Credentials(type, host, username, password, database, collection, table, uri, port);
35-
}
36-
3731
public StorageType getType(StorageType defaultValue) {
3832
return type == null ? defaultValue : type;
3933
}

src/main/java/wtf/casper/storageapi/StatelessFieldStorage.java renamed to src/main/java/wtf/casper/storageapi/FieldStorage.java

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
package wtf.casper.storageapi;
22

3-
import wtf.casper.storageapi.misc.ConstructableValue;
4-
import wtf.casper.storageapi.misc.KeyValue;
5-
import wtf.casper.storageapi.utils.ReflectionUtil;
63
import wtf.casper.storageapi.utils.StorageAPIConstants;
74

8-
import java.lang.reflect.ParameterizedType;
9-
import java.lang.reflect.Type;
105
import java.util.Collection;
116
import java.util.concurrent.CompletableFuture;
127

13-
public interface StatelessFieldStorage<K, V> {
8+
public interface FieldStorage<K, V> {
149

1510
/**
1611
* @param field the field to search for.
@@ -58,55 +53,6 @@ default CompletableFuture<Collection<V>> get(int limit, Filter... filters) {
5853
*/
5954
CompletableFuture<V> get(final K key);
6055

61-
/**
62-
* @param key the key to search for.
63-
* @return a future that will complete with the value that matches the given key or a generated value if not found.
64-
*/
65-
default CompletableFuture<V> getOrDefault(final K key) {
66-
return get(key).thenApply((v) -> {
67-
68-
if (v != null) {
69-
return v;
70-
}
71-
72-
if (this instanceof ConstructableValue<?, ?>) {
73-
v = ((ConstructableValue<K, V>) this).constructValue(key);
74-
if (v == null) {
75-
throw new RuntimeException("Failed to create default value for V with key " + key + ". " +
76-
"Please create a constructor in V for only the key");
77-
}
78-
return v;
79-
}
80-
81-
if (this instanceof KeyValue<?, ?>) {
82-
KeyValue<K, V> keyValueGetter = (KeyValue<K, V>) this;
83-
try {
84-
return ReflectionUtil.createInstance(keyValueGetter.value(), key);
85-
} catch (final Exception e) {
86-
e.printStackTrace();
87-
throw new RuntimeException("Failed to create default value for V with key " + key + ". " +
88-
"Please create a constructor in V for only the key.", e);
89-
}
90-
}
91-
92-
try {
93-
if (getClass().getGenericSuperclass() instanceof ParameterizedType parameterizedType) {
94-
Type type = parameterizedType.getActualTypeArguments()[1];
95-
Class<V> aClass = (Class<V>) Class.forName(type.getTypeName());
96-
return ReflectionUtil.createInstance(aClass, key);
97-
}
98-
99-
throw new RuntimeException("Failed to create default value for V with key " + key + ". " +
100-
"Please create a constructor in V for only the key.");
101-
102-
} catch (Exception e) {
103-
e.printStackTrace();
104-
throw new RuntimeException("Failed to create default value for V with key " + key + ". " +
105-
"Please create a constructor in V for only the key.");
106-
}
107-
});
108-
}
109-
11056
/**
11157
* @param field the field to search for.
11258
* @param value the value to search for.
@@ -178,7 +124,7 @@ default CompletableFuture<Void> close() {
178124
* @param storage the storage to migrate from. The data will be copied from the given storage to this storage.
179125
* @return a future that will complete with a boolean that represents whether the migration was successful.
180126
*/
181-
default CompletableFuture<Boolean> migrate(final StatelessFieldStorage<K, V> storage) {
127+
default CompletableFuture<Boolean> migrate(final FieldStorage<K, V> storage) {
182128
return CompletableFuture.supplyAsync(() -> {
183129
storage.allValues().thenAccept(this::saveAll).join();
184130
return true;

src/main/java/wtf/casper/storageapi/KVStorage.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

src/main/java/wtf/casper/storageapi/StatelessKVStorage.java renamed to src/main/java/wtf/casper/storageapi/KeyedStorage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import java.util.Map;
1212
import java.util.concurrent.CompletableFuture;
1313

14-
public interface StatelessKVStorage<K, V> {
14+
public interface KeyedStorage<K, V> {
1515

1616
/**
1717
* @param key the key to search for.
@@ -115,7 +115,7 @@ default CompletableFuture<Boolean> contains(K key) {
115115
* @param storage the storage to migrate from. The data will be copied from the given storage to this storage.
116116
* @return a future that will complete with a boolean that represents whether the migration was successful.
117117
*/
118-
default CompletableFuture<Boolean> migrate(final StatelessKVStorage<K, V> storage) {
118+
default CompletableFuture<Boolean> migrate(final KeyedStorage<K, V> storage) {
119119
return CompletableFuture.supplyAsync(() -> {
120120
Collection<V> vs = storage.allValues().join();
121121
saveAll(vs).join(); // save all will batch if the implementation supports it (mongo for example)

src/main/java/wtf/casper/storageapi/cache/Cache.java

Lines changed: 0 additions & 24 deletions
This file was deleted.

src/main/java/wtf/casper/storageapi/cache/CaffeineCache.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/main/java/wtf/casper/storageapi/cache/GoogleCache.java

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/main/java/wtf/casper/storageapi/cache/MapCache.java

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/main/java/wtf/casper/storageapi/impl/direct/kvstorage/DirectJsonKVStorage.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)