Skip to content

Commit dd3766c

Browse files
committed
Refactors storage API for aggregation
Introduces a new query system and aggregation results. Removes filter from Aggregation object and adds it to query. Moves storage implementations to appropriate packages. Updates storage interface with aggregate, index, and unindex methods.
1 parent 788fc71 commit dd3766c

14 files changed

Lines changed: 314 additions & 206 deletions

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,13 @@
22

33
import lombok.AllArgsConstructor;
44
import lombok.Data;
5+
import lombok.experimental.Accessors;
56

67
@Data
8+
@Accessors(fluent = true)
79
@AllArgsConstructor
810
public class Aggregation {
911
private AggregateFunction function;
1012
private String field;
1113
private String alias;
12-
private Condition filter;
13-
14-
public Aggregation(AggregateFunction fn, String field, String alias) {
15-
this(fn, field, alias, null);
16-
}
17-
18-
public Aggregation withFilter(Condition filter) {
19-
this.filter = filter;
20-
return this;
21-
}
2214
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package wtf.casper.storageapi;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.experimental.Accessors;
6+
7+
@Data
8+
@Accessors(fluent = true)
9+
@AllArgsConstructor
10+
public class AggregationResult {
11+
private String alias;
12+
private Object value;
13+
}

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

Lines changed: 13 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,81 +3,29 @@
33
import wtf.casper.storageapi.utils.StorageAPIConstants;
44

55
import java.util.Collection;
6+
import java.util.List;
67
import java.util.concurrent.CompletableFuture;
78

89
public interface FieldStorage<K, V> {
910

1011
/**
11-
* @param field the field to search for.
12-
* @param value the value to search for.
12+
* @param query The query to execute
1313
* @return a future that will complete with a collection of all values that match the given field and value.
1414
*/
15-
default CompletableFuture<Collection<V>> get(final String field, final Object value) {
16-
return get(field, value, ConditionType.EQUALS, SortingType.NONE);
17-
}
18-
19-
/**
20-
* @param field the field to search for.
21-
* @param value the value to search for.
22-
* @param conditionType the filter type to use.
23-
* @param sortingType the sorting type to use.
24-
* @return a future that will complete with a collection of all values that match the given field and value.
25-
*/
26-
default CompletableFuture<Collection<V>> get(final String field, final Object value, final ConditionType conditionType, final SortingType sortingType) {
27-
return get(Condition.of(field, value, conditionType, sortingType));
28-
}
29-
30-
/**
31-
* @param conditions the filters to use.
32-
* @return a future that will complete with a collection of all value that match the given filters.
33-
*/
34-
default CompletableFuture<Collection<V>> get(Condition... conditions) {
35-
return get(-1, conditions);
36-
};
37-
38-
/**
39-
* @param limit the limit of values to return.
40-
* @param conditions the filters to use.
41-
* @return a future that will complete with a collection of all value that match the given filters.
42-
*/
43-
default CompletableFuture<Collection<V>> get(int limit, Condition... conditions) {
44-
return get(0, limit, conditions);
45-
}
46-
47-
CompletableFuture<Collection<V>> get(int skip, int limit, Condition... conditions);
15+
CompletableFuture<Collection<V>> get(Query query);
4816

4917
/**
50-
* @param key the key to search for.
51-
* @return a future that will complete with the value that matches the given key.
52-
* The value may be null if the key is not found.
18+
* @param query The query to remove
5319
*/
54-
CompletableFuture<V> get(final K key);
20+
CompletableFuture<Void> remove(final Query query);
5521

5622
/**
57-
* @param field the field to search for.
58-
* @param value the value to search for.
59-
* @return a future that will complete with the first value that matches the given field and value.
23+
* Executes an aggregation query on the storage and returns the result as a CompletableFuture.
24+
*
25+
* @param query The query containing aggregation details, such as the fields, functions, and filters to apply.
26+
* @return A CompletableFuture that completes with the result of the aggregation query as an AggregationResult object.
6027
*/
61-
default CompletableFuture<V> getFirst(final String field, final Object value) {
62-
return getFirst(field, value, ConditionType.EQUALS);
63-
}
64-
65-
/**
66-
* @param field the field to search for.
67-
* @param value the value to search for.
68-
* @param conditionType the filter type to use.
69-
* @return a future that will complete with the first value that matches the given field and value.
70-
*/
71-
default CompletableFuture<V> getFirst(final String field, final Object value, ConditionType conditionType) {
72-
return get(1, Condition.of(field, value, conditionType, SortingType.NONE)).thenApply((values) -> {
73-
if (values.isEmpty()) {
74-
return null;
75-
}
76-
77-
return values.iterator().next();
78-
});
79-
};
80-
28+
CompletableFuture<List<AggregationResult>> aggregate(final Query query);
8129

8230
/**
8331
* @param value the value to save.
@@ -88,14 +36,10 @@ default CompletableFuture<V> getFirst(final String field, final Object value, Co
8836
* @param values the values to save.
8937
*/
9038
default CompletableFuture<Void> saveAll(final Collection<V> values) {
39+
// designed to be naive approach that is overridden for batched impls
9140
return CompletableFuture.runAsync(() -> values.forEach(v -> save(v).join()), StorageAPIConstants.DB_THREAD_POOL);
9241
}
9342

94-
/**
95-
* @param key the key to remove.
96-
*/
97-
CompletableFuture<Void> remove(final V key);
98-
9943
/**
10044
* Writes the storage to disk.
10145
*/
@@ -113,13 +57,6 @@ default CompletableFuture<Void> close() {
11357
return CompletableFuture.completedFuture(null);
11458
}
11559

116-
/**
117-
* @param field the field to search for.
118-
* @param value the value to search for.
119-
* @return a future that will complete with a boolean that represents whether the storage contains a value that matches the given field and value.
120-
*/
121-
CompletableFuture<Boolean> contains(final String field, final Object value);
122-
12360
/**
12461
* @param storage the storage to migrate from. The data will be copied from the given storage to this storage.
12562
* @return a future that will complete with a boolean that represents whether the migration was successful.
@@ -141,12 +78,12 @@ default CompletableFuture<Boolean> migrate(final FieldStorage<K, V> storage) {
14178
* @param field the field to add an index for.
14279
* @return a future that will complete when the index has been added.
14380
*/
144-
CompletableFuture<Void> addIndex(String field);
81+
CompletableFuture<Void> index(String field);
14582

14683
/**
14784
* Removes an index from the storage.
14885
* @param field the field to remove the index for.
14986
* @return a future that will complete when the index has been removed.
15087
*/
151-
CompletableFuture<Void> removeIndex(String field);
88+
CompletableFuture<Void> unindex(String field);
15289
}
Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,55 @@
11
package wtf.casper.storageapi;
22

3-
import lombok.Builder;
3+
import lombok.Getter;
4+
import lombok.experimental.Accessors;
45

56
import java.util.ArrayList;
67
import java.util.List;
78

8-
@Builder
9+
@Getter
10+
@Accessors(fluent = true)
911
public class Query {
10-
private final List<Condition> conditions;
11-
private final List<Sort> sorts;
12-
private final boolean distinct;
13-
private final int limit;
14-
private final int offset;
12+
private List<Condition> conditions = List.of();
13+
private List<Sort> sorts = List.of();
14+
private List<Aggregation> aggregations = List.of();
15+
private boolean distinct = false;
16+
private int limit = -1;
17+
private int offset = 0;
1518

19+
public static Query of() {
20+
return new Query();
21+
}
22+
23+
public Query condition(Condition... conditions) {
24+
conditions = conditions == null ? new Condition[0] : conditions;
25+
this.conditions = new ArrayList<>(List.of(conditions));
26+
return this;
27+
}
28+
29+
public Query sort(Sort... sorts) {
30+
sorts = sorts == null ? new Sort[0] : sorts;
31+
this.sorts = new ArrayList<>(List.of(sorts));
32+
return this;
33+
}
34+
35+
public Query aggregation(Aggregation... aggregations) {
36+
aggregations = aggregations == null ? new Aggregation[0] : aggregations;
37+
this.aggregations = new ArrayList<>(List.of(aggregations));
38+
return this;
39+
}
40+
41+
public Query distinct(boolean distinct) {
42+
this.distinct = distinct;
43+
return this;
44+
}
45+
46+
public Query limit(int limit) {
47+
this.limit = limit;
48+
return this;
49+
}
50+
51+
public Query offset(int offset) {
52+
this.offset = offset;
53+
return this;
54+
}
1655
}

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

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

33
import lombok.Data;
4+
import lombok.experimental.Accessors;
45

56
@Data
7+
@Accessors(fluent = true)
68
public class Sort {
79
private final String field;
810
private final SortingType sortingType;

src/main/java/wtf/casper/storageapi/impl/direct/statelessfstorage/DirectMariaDBFStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package wtf.casper.storageapi.impl.direct.statelessfstorage;
22

33
import wtf.casper.storageapi.Credentials;
4-
import wtf.casper.storageapi.impl.statelessfstorage.MariaDBFStorage;
4+
import wtf.casper.storageapi.impl.fstorage.MariaDBFStorage;
55
import wtf.casper.storageapi.misc.ConstructableValue;
66

77
import java.util.function.Function;

src/main/java/wtf/casper/storageapi/impl/direct/statelessfstorage/DirectMongoFStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package wtf.casper.storageapi.impl.direct.statelessfstorage;
22

33
import wtf.casper.storageapi.Credentials;
4-
import wtf.casper.storageapi.impl.statelessfstorage.MongoFStorage;
4+
import wtf.casper.storageapi.impl.fstorage.MongoFStorage;
55
import wtf.casper.storageapi.misc.ConstructableValue;
66

77
import java.util.function.Function;

src/main/java/wtf/casper/storageapi/impl/direct/statelesskvstorage/DirectMongoKVStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package wtf.casper.storageapi.impl.direct.statelesskvstorage;
22

33
import wtf.casper.storageapi.Credentials;
4-
import wtf.casper.storageapi.impl.statelesskvstorage.MongoKVStorage;
4+
import wtf.casper.storageapi.impl.kvstorage.MongoKVStorage;
55
import wtf.casper.storageapi.misc.ConstructableValue;
66

77
import java.util.function.Function;

src/main/java/wtf/casper/storageapi/impl/direct/statelesskvstorage/DirectStatelessMariaDBKVStorage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package wtf.casper.storageapi.impl.direct.statelesskvstorage;
22

33
import wtf.casper.storageapi.Credentials;
4-
import wtf.casper.storageapi.impl.kvstorage.MariaDBKVStorage;
5-
import wtf.casper.storageapi.impl.statelesskvstorage.SqlKVStorage;
4+
import wtf.casper.storageapi.impl.kvstorage.SqlKVStorage;
65
import wtf.casper.storageapi.misc.ConstructableValue;
76

87
import java.util.function.Function;

0 commit comments

Comments
 (0)