Skip to content

Commit bc663a8

Browse files
author
pellse
committed
mergeFunction is now passed directly to OneToOneCache and OneToManyCache
1 parent 70193af commit bc663a8

File tree

5 files changed

+67
-36
lines changed

5 files changed

+67
-36
lines changed

assembler/src/main/java/io/github/pellse/assembler/caching/OneToManyCache.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.pellse.assembler.caching;
22

33
import io.github.pellse.assembler.caching.factory.CacheContext.OneToManyCacheContext;
4+
import io.github.pellse.util.function.Function3;
45
import reactor.core.publisher.Mono;
56

67
import java.util.Collection;
@@ -31,14 +32,22 @@ static <ID, EID, R, RC extends Collection<R>> Cache<ID, RC> oneToManyCache(
3132
OneToManyCacheContext<ID, EID, R, RC> ctx,
3233
Cache<ID, RC> delegateCache) {
3334

35+
return oneToManyCache(removeDuplicate(ctx), ctx, delegateCache);
36+
}
37+
38+
static <ID, EID, R, RC extends Collection<R>> Cache<ID, RC> oneToManyCache(
39+
Function3<ID, RC, RC, RC> mergeFunction,
40+
OneToManyCacheContext<ID, EID, R, RC> ctx,
41+
Cache<ID, RC> delegateCache) {
42+
3443
final var optimizedCache = optimizedCache(delegateCache);
3544

3645
return adapterCache(
3746
optimizedCache::getAll,
3847
optimizedCache::computeAll,
3948
applyMergeStrategy(
4049
optimizedCache,
41-
(existingCacheItems, incomingChanges) -> ctx.mapMerger().apply(existingCacheItems, incomingChanges),
50+
(existingCacheItems, incomingChanges) -> mergeMaps(existingCacheItems, incomingChanges, mergeFunction),
4251
Cache::putAll),
4352
applyMergeStrategy(
4453
optimizedCache,
@@ -73,4 +82,8 @@ private static <ID, R, RC extends Collection<R>> Function<Map<ID, RC>, Mono<?>>
7382
return incomingChanges -> isEmpty(incomingChanges) ? just(of()) : delegateCache.getAll(incomingChanges.keySet())
7483
.flatMap(existingCacheItems -> cacheUpdater.updateCache(delegateCache, existingCacheItems, incomingChanges));
7584
}
85+
86+
private static <ID, EID, R, RC extends Collection<R>> Function3<ID, RC, RC, RC> removeDuplicate(OneToManyCacheContext<ID, EID, R, RC> ctx) {
87+
return (id, coll1, coll2) -> removeDuplicates(concat(coll1, coll2), ctx.idResolver(), rc -> convert(rc, ctx.collectionType(), ctx.collectionFactory()));
88+
}
7689
}

assembler/src/main/java/io/github/pellse/assembler/caching/OneToOneCache.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package io.github.pellse.assembler.caching;
22

3-
import io.github.pellse.assembler.caching.factory.CacheContext;
3+
import io.github.pellse.util.function.Function3;
44

55
import static io.github.pellse.assembler.caching.AdapterCache.adapterCache;
66
import static io.github.pellse.assembler.caching.OptimizedCache.optimizedCache;
7+
import static io.github.pellse.util.collection.CollectionUtils.mergeMaps;
78

89
public interface OneToOneCache {
910

11+
static <ID, R> Cache<ID, R> oneToOneCache(Cache<ID, R> delegateCache) {
12+
return oneToOneCache((k, r1, r2) -> r2 != null ? r2 : r1, delegateCache);
13+
}
14+
1015
static <ID, R> Cache<ID, R> oneToOneCache(
11-
CacheContext.OneToOneCacheContext<ID, R> ctx,
16+
Function3<ID, R, R, R> mergeFunction,
1217
Cache<ID, R> delegateCache) {
1318

1419
final var optimizedCache = optimizedCache(delegateCache);
@@ -17,7 +22,7 @@ static <ID, R> Cache<ID, R> oneToOneCache(
1722
optimizedCache::getAll,
1823
optimizedCache::computeAll,
1924
newMap -> optimizedCache.getAll(newMap.keySet())
20-
.map(existingMap -> ctx.mapMerger().apply(existingMap, newMap))
25+
.map(existingMap -> mergeMaps(existingMap, newMap, mergeFunction))
2126
.flatMap(optimizedCache::putAll),
2227
optimizedCache::removeAll);
2328
}

assembler/src/main/java/io/github/pellse/assembler/caching/factory/CacheContext.java

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.github.pellse.assembler.RuleMapperContext.OneToManyContext;
2020
import io.github.pellse.assembler.RuleMapperContext.OneToOneContext;
2121
import io.github.pellse.assembler.caching.factory.CacheFactory.CacheTransformer;
22+
import io.github.pellse.util.collection.CollectionUtils;
2223
import io.github.pellse.util.function.Function3;
2324

2425
import java.util.*;
@@ -30,7 +31,6 @@
3031

3132
import static io.github.pellse.assembler.caching.factory.ConcurrentCacheFactory.concurrent;
3233
import static io.github.pellse.util.collection.CollectionUtils.*;
33-
import static io.github.pellse.util.collection.CollectionUtils.mergeMaps;
3434

3535
public sealed interface CacheContext<ID, R, RRC, CTX extends CacheContext<ID, R, RRC, CTX>> {
3636

@@ -49,18 +49,18 @@ record OneToOneCacheContext<ID, R>(
4949
Function3<ID, R, R, R> mergeFunction,
5050
CacheTransformer<ID, R, R, OneToOneCacheContext<ID, R>> cacheTransformer) implements CacheContext<ID, R, R, OneToOneCacheContext<ID, R>> {
5151

52-
static<ID, R> OneToOneCacheContext<ID, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx) {
52+
static <ID, R> OneToOneCacheContext<ID, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx) {
5353
return oneToOneCacheContext(ctx, (k, r1, r2) -> r2 != null ? r2 : r1);
5454
}
5555

56-
static<ID, R> OneToOneCacheContext<ID, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx, Function3<ID, R, R, R> mergeFunction) {
56+
static <ID, R> OneToOneCacheContext<ID, R> oneToOneCacheContext(OneToOneContext<?, ?, ?, ID, R> ctx, Function3<ID, R, R, R> mergeFunction) {
5757
return new OneToOneCacheContext<>(ctx.mapCollector(),
5858
mergeFunction,
59-
concurrent());
59+
concurrent());
6060
}
6161
}
6262

63-
record OneToManyCacheContext<ID, EID, R, RC extends Collection<R>> (
63+
record OneToManyCacheContext<ID, EID, R, RC extends Collection<R>>(
6464
Function<R, EID> idResolver,
6565
IntFunction<Collector<R, ?, Map<ID, RC>>> mapCollector,
6666
Comparator<R> idComparator,
@@ -82,26 +82,16 @@ static <ID, EID, R, RC extends Collection<R>> OneToManyCacheContext<ID, EID, R,
8282
ctx.idComparator(),
8383
ctx.collectionFactory(),
8484
ctx.collectionType(),
85-
(id, coll1, coll2) ->
86-
isNotEmpty(coll1) || isNotEmpty(coll2) ? mergeFunction.apply(id, convert(coll1, ctx), convert(coll2, ctx)) : convert(List.of(), ctx),
85+
(id, coll1, coll2) -> isNotEmpty(coll1) || isNotEmpty(coll2) ? mergeFunction.apply(id, convert(coll1, ctx), convert(coll2, ctx)) : convert(List.of(), ctx),
8786
concurrent());
8887
}
8988

90-
private RC convert(Collection<R> collection) {
91-
return convert(collection, collectionType, collectionFactory);
92-
}
93-
94-
private static <ID, EID, R, RC extends Collection<R>> Function3<ID, RC, RC, RC> removeDuplicate(OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx) {
89+
public static <ID, EID, R, RC extends Collection<R>> Function3<ID, RC, RC, RC> removeDuplicate(OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx) {
9590
return (id, coll1, coll2) -> removeDuplicates(concat(coll1, coll2), ctx.idResolver(), rc -> convert(rc, ctx));
9691
}
9792

9893
private static <ID, EID, R, RC extends Collection<R>> RC convert(Collection<R> collection, OneToManyContext<?, ?, ?, ID, EID, R, RC> ctx) {
99-
return convert(collection, ctx.collectionType(), ctx.collectionFactory());
100-
}
101-
102-
@SuppressWarnings("unchecked")
103-
private static <R, RC extends Collection<R>> RC convert(Collection<R> collection, Class<RC> collectionType, Supplier<RC> collectionFactory) {
104-
return collectionType.isInstance(collection) ? (RC) collection : translate(collection, collectionFactory);
94+
return CollectionUtils.convert(collection, ctx.collectionType(), ctx.collectionFactory());
10595
}
10696
}
10797
}

assembler/src/main/java/io/github/pellse/assembler/caching/factory/CacheFactory.java

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@
3636
import static io.github.pellse.assembler.QueryUtils.buildQueryFunction;
3737
import static io.github.pellse.assembler.RuleMapperSource.*;
3838
import static io.github.pellse.assembler.caching.DefaultCache.cache;
39-
import static io.github.pellse.assembler.caching.factory.CacheContext.OneToManyCacheContext.oneToManyCacheContext;
40-
import static io.github.pellse.assembler.caching.factory.CacheContext.OneToOneCacheContext.oneToOneCacheContext;
4139
import static io.github.pellse.assembler.caching.factory.DeferCacheFactory.defer;
4240
import static io.github.pellse.assembler.caching.OneToManyCache.oneToManyCache;
4341
import static io.github.pellse.assembler.caching.OneToOneCache.oneToOneCache;
@@ -167,7 +165,7 @@ static <T, TC extends Collection<T>, K, ID, R> RuleMapperSource<T, TC, K, ID, ID
167165
CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>> cacheFactory,
168166
Function<CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>>, CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>>>... delegateCacheFactories) {
169167

170-
return cached(OneToOneCacheContext::oneToOneCacheContext, ruleMapperSource, oneToOneCacheFactory(wrap(cacheFactory)), delegateCacheFactories);
168+
return cached((k, r1, r2) -> r2 != null ? r2 : r1, ruleMapperSource, cacheFactory, delegateCacheFactories);
171169
}
172170

173171
@SafeVarargs
@@ -177,7 +175,13 @@ static <T, TC extends Collection<T>, K, ID, R> RuleMapperSource<T, TC, K, ID, ID
177175
CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>> cacheFactory,
178176
Function<CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>>, CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>>>... delegateCacheFactories) {
179177

180-
return cached(ctx -> oneToOneCacheContext(ctx, mergeFunction), ruleMapperSource, oneToOneCacheFactory(wrap(cacheFactory)), delegateCacheFactories);
178+
final var wrappedCacheFactory = wrap(cacheFactory);
179+
180+
return cached(
181+
OneToOneCacheContext::oneToOneCacheContext,
182+
ruleMapperSource,
183+
ctx -> oneToOneCache(mergeFunction, wrappedCacheFactory.create(ctx)),
184+
delegateCacheFactories);
181185
}
182186

183187
@SafeVarargs
@@ -209,7 +213,7 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
209213
CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> cacheFactory,
210214
Function<CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>, CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>>... delegateCacheFactories) {
211215

212-
return cachedMany(mergeFunction, emptySource(), cacheFactory, delegateCacheFactories);
216+
return cachedMany(ctx -> mergeFunction, emptySource(), cacheFactory, delegateCacheFactories);
213217
}
214218

215219
@SafeVarargs
@@ -243,7 +247,7 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
243247
RuleMapperSource<T, TC, K, ID, EID, R, RC, OneToManyContext<T, TC, K, ID, EID, R, RC>> ruleMapperSource,
244248
Function<CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>, CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>>... delegateCacheFactories) {
245249

246-
return cachedMany(mergeFunction, ruleMapperSource, cache(), delegateCacheFactories);
250+
return cachedMany(ctx -> mergeFunction, ruleMapperSource, cache(), delegateCacheFactories);
247251
}
248252

249253
@SafeVarargs
@@ -262,7 +266,7 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
262266
CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> cacheFactory,
263267
Function<CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>, CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>>... delegateCacheFactories) {
264268

265-
return cachedMany(mergeFunction, from(queryFunction), cacheFactory, delegateCacheFactories);
269+
return cachedMany(ctx -> mergeFunction, from(queryFunction), cacheFactory, delegateCacheFactories);
266270
}
267271

268272
@SafeVarargs
@@ -271,29 +275,35 @@ static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> Ru
271275
CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> cacheFactory,
272276
Function<CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>, CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>>... delegateCacheFactories) {
273277

274-
return cached(OneToManyCacheContext::oneToManyCacheContext, ruleMapperSource, oneToManyCacheFactory(wrap(cacheFactory)), delegateCacheFactories);
278+
return cachedMany(CacheFactory::removeDuplicate, ruleMapperSource, cacheFactory, delegateCacheFactories);
275279
}
276280

277281
@SafeVarargs
278282
static <T, TC extends Collection<T>, K, ID, EID, R, RC extends Collection<R>> RuleMapperSource<T, TC, K, ID, EID, R, RC, OneToManyContext<T, TC, K, ID, EID, R, RC>> cachedMany(
279-
Function3<ID, RC, RC, RC> mergeFunction,
283+
Function<OneToManyCacheContext<ID, EID, R, RC>, Function3<ID, RC, RC, RC>> mergeFunctionProvider,
280284
RuleMapperSource<T, TC, K, ID, EID, R, RC, OneToManyContext<T, TC, K, ID, EID, R, RC>> ruleMapperSource,
281285
CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> cacheFactory,
282286
Function<CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>, CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>>>... delegateCacheFactories) {
283287

284-
return cached(ctx -> oneToManyCacheContext(ctx, mergeFunction), ruleMapperSource, oneToManyCacheFactory(wrap(cacheFactory)), delegateCacheFactories);
288+
final var wrappedCacheFactory = wrap(cacheFactory);
289+
290+
return cached(
291+
OneToManyCacheContext::oneToManyCacheContext,
292+
ruleMapperSource,
293+
ctx -> oneToManyCache(mergeFunctionProvider.apply(ctx), ctx, wrappedCacheFactory.create(ctx)),
294+
delegateCacheFactories);
285295
}
286296

287297
static <ID, RRC> Function<Map<ID, RRC>, Mono<?>> toMono(Consumer<Map<ID, RRC>> consumer) {
288298
return map -> just(also(map, consumer));
289299
}
290300

291-
private static <ID, R> CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>> oneToOneCacheFactory(CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>> cacheFactory) {
292-
return cacheContext -> oneToOneCache(cacheContext, cacheFactory.create(cacheContext));
301+
private static <ID, R> CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>> oneToOneCacheFactory(Function3<ID, R, R, R> mergeFunction, CacheFactory<ID, R, R, OneToOneCacheContext<ID, R>> cacheFactory) {
302+
return ctx -> oneToOneCache(mergeFunction, cacheFactory.create(ctx));
293303
}
294304

295-
private static <ID, EID, R, RC extends Collection<R>> CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> oneToManyCacheFactory(CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> cacheFactory) {
296-
return cacheContext -> oneToManyCache(cacheContext, cacheFactory.create(cacheContext));
305+
private static <ID, EID, R, RC extends Collection<R>> CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> oneToManyCacheFactory(Function3<ID, RC, RC, RC> mergeFunction, CacheFactory<ID, R, RC, OneToManyCacheContext<ID, EID, R, RC>> cacheFactory) {
306+
return ctx -> oneToManyCache(mergeFunction, ctx, cacheFactory.create(ctx));
297307
}
298308

299309
@SafeVarargs
@@ -375,4 +385,12 @@ private static <T, TC extends Collection<T>, K, ID, EID, R, RRC> Map<ID, RRC> bu
375385
diff(ids, map.keySet())
376386
.forEach(id -> ifNotNull(ctx.defaultResultProvider().apply(id), value -> map.put(id, value))));
377387
}
388+
389+
private static <ID, EID, R, RC extends Collection<R>> Function3<ID, RC, RC, RC> removeDuplicate(OneToManyCacheContext<ID, EID, R, RC> ctx) {
390+
return (id, coll1, coll2) -> removeDuplicates(concat(coll1, coll2), ctx.idResolver(), rc -> convert(rc, ctx));
391+
}
392+
393+
private static <ID, EID, R, RC extends Collection<R>> RC convert(Collection<R> collection, OneToManyCacheContext<ID, EID, R, RC> ctx) {
394+
return CollectionUtils.convert(collection, ctx.collectionType(), ctx.collectionFactory());
395+
}
378396
}

assembler/src/main/java/io/github/pellse/util/collection/CollectionUtils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,11 @@ static <K, V> Collection<V> removeDuplicates(
198198
.values();
199199
}
200200

201+
@SuppressWarnings("unchecked")
202+
static <T, C extends Collection<T>> C convert(Collection<T> collection, Class<C> collectionType, Supplier<C> collectionFactory) {
203+
return collectionType.isInstance(collection) ? (C) collection : translate(collection, collectionFactory);
204+
}
205+
201206
static <K, V> LinkedHashMap<K, V> toLinkedHashMap(Map<K, V> map) {
202207
return map instanceof LinkedHashMap<K, V> lhm ? lhm : new LinkedHashMap<>(map);
203208
}

0 commit comments

Comments
 (0)