From 076f0ead084ba6f0b4724cedaa0eddc7b37fd553 Mon Sep 17 00:00:00 2001 From: Claus Stadler Date: Sun, 17 Sep 2023 22:21:38 +0200 Subject: [PATCH] Moved some utils related to limit and offset with guava Ranges from jenax' QueryUtils to RangeUtils. --- .../aksw/commons/rx/lookup/LookupService.java | 7 ++ .../rx/lookup/LookupServiceCacheMem.java | 20 +++-- .../aksw/commons/util/range/LongRanges.java | 73 +++++++++++++++++++ 3 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/range/LongRanges.java diff --git a/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java b/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java index 3f2afb11..c57cb729 100644 --- a/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java +++ b/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupService.java @@ -4,10 +4,13 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Predicate; +import com.google.common.cache.Cache; + import io.reactivex.rxjava3.core.Flowable; import io.reactivex.rxjava3.core.Single; @@ -55,6 +58,10 @@ default LookupService cache() { return LookupServiceCacheMem.create(this); } + default LookupService cache(Cache> hitCache, Cache missCache) { + return LookupServiceCacheMem.create(this, hitCache, missCache); + } + /** * Requests a map. diff --git a/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupServiceCacheMem.java b/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupServiceCacheMem.java index 82cad413..5688498e 100644 --- a/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupServiceCacheMem.java +++ b/aksw-commons-rx/src/main/java/org/aksw/commons/rx/lookup/LookupServiceCacheMem.java @@ -25,8 +25,8 @@ public class LookupServiceCacheMem implements LookupService { - protected Cache> cache;//new LRUMap(); - protected Cache missCache; + protected Cache> hitCache;//new LRUMap(); + protected Cache missCache; private LookupService base; @@ -41,9 +41,9 @@ public LookupServiceCacheMem(LookupService base, long maxCacheSize) { ); } - public LookupServiceCacheMem(LookupService base, Cache> cache, Cache missCache) { + public LookupServiceCacheMem(LookupService base, Cache> cache, Cache missCache) { this.base = base; - this.cache = cache; + this.hitCache = cache; this.missCache = missCache; } @@ -51,7 +51,7 @@ public LookupServiceCacheMem(LookupService base, Cache> cac public Map fetchMap(Iterable keys) { Set lookupKeys = Sets.newLinkedHashSet(keys); - Map> cachedEntries = cache.getAllPresent(keys); + Map> cachedEntries = hitCache.getAllPresent(keys); lookupKeys.removeAll(cachedEntries.keySet()); Set knownMissKeys = new LinkedHashSet<>(Sets.intersection(lookupKeys, missCache.asMap().keySet())); @@ -66,11 +66,11 @@ public Map fetchMap(Iterable keys) { V value; if(cachedEntries.containsKey(key)) { value = cachedEntries.get(key).orElse(null); - cache.put(key, Optional.ofNullable(value)); + hitCache.put(key, Optional.ofNullable(value)); result.put(key, value); } else if(fetchedMap.containsKey(key)) { value = fetchedMap.get(key); - cache.put(key, Optional.ofNullable(value)); + hitCache.put(key, Optional.ofNullable(value)); result.put(key, value); } else { missCache.put(key, true); @@ -97,6 +97,12 @@ public static LookupServiceCacheMem create(LookupService base return result; } + public static LookupServiceCacheMem create(LookupService base, Cache> hitCache, Cache missCache) { + LookupServiceCacheMem result = new LookupServiceCacheMem(base, hitCache, missCache); + return result; + } + + // public static LookupServiceCacheMem create(LookupService base, Map cache) { // LookupServiceCacheMem result = new LookupServiceCacheMem(base, cache); // return result; diff --git a/aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/range/LongRanges.java b/aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/range/LongRanges.java new file mode 100644 index 00000000..5568c322 --- /dev/null +++ b/aksw-commons-utils-parent/aksw-commons-utils/src/main/java/org/aksw/commons/util/range/LongRanges.java @@ -0,0 +1,73 @@ +package org.aksw.commons.util.range; + +import com.google.common.collect.BoundType; +import com.google.common.collect.DiscreteDomain; +import com.google.common.collect.Range; + +public class LongRanges { + /** + * Transform a range w.r.t. a discrete domain such that any lower bound is closed and the upper bound + * is open. As a result, a zero-length range is represented by [x..x) + * + * @param + * @param range + * @param domain + * @return + */ + // This seems to be the same as range.canonical(discreteDomain) +// public static > Range makeClosedOpen(Range range, DiscreteDomain domain) { +// ContiguousSet set = ContiguousSet.create(range, domain); +// range.canonical(domain) +// Range result = set.isEmpty() +// ? Range.closedOpen(range.lowerEndpoint(), range.lowerEndpoint()) +// : ContiguousSet.create(range, domain).range(BoundType.CLOSED, BoundType.OPEN); +// return result; +// } + + public static > T closedLowerEndpointOrNull(Range range, DiscreteDomain domain) { + T result = !range.hasLowerBound() + ? null + : range.lowerBoundType().equals(BoundType.CLOSED) + ? range.lowerEndpoint() + : domain.next(range.lowerEndpoint()); + + return result; + } + + public static > T openUpperEndpointOrNull(Range range, DiscreteDomain domain) { + T result = !range.hasUpperBound() + ? null + : range.upperBoundType().equals(BoundType.CLOSED) + ? domain.next(range.upperEndpoint()) + : range.upperEndpoint(); + + return result; + } + + public static Long rangeToOffset(Range range) { + Long tmp = range == null + ? null + : closedLowerEndpointOrNull(range, DiscreteDomain.longs()); + + Long result = tmp == null || tmp == 0 ? null : tmp; + return result; + } + + /** + * + * @param range + * @return + */ + public static Long rangeToLimit(Range range) { + // range = range == null ? null : makeClosedOpen(range, DiscreteDomain.longs()); + range = range == null ? null : range.canonical(DiscreteDomain.longs()); + + Long result = range == null || !range.hasUpperBound() + ? null + : DiscreteDomain.longs().distance(range.lowerEndpoint(), range.upperEndpoint()) + // If the upper bound is closed such as [x, x] then the result is the distance plus 1 + + (range.upperBoundType().equals(BoundType.CLOSED) ? 1 : 0); + + return result; + } +}