Skip to content

Commit

Permalink
Moved some utils related to limit and offset with guava Ranges from j…
Browse files Browse the repository at this point in the history
…enax' QueryUtils to RangeUtils.
  • Loading branch information
Aklakan committed Sep 17, 2023
1 parent 11a8a48 commit 076f0ea
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -55,6 +58,10 @@ default LookupService<K, V> cache() {
return LookupServiceCacheMem.create(this);
}

default LookupService<K, V> cache(Cache<K, Optional<V>> hitCache, Cache<K, Object> missCache) {
return LookupServiceCacheMem.create(this, hitCache, missCache);
}


/**
* Requests a map.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
public class LookupServiceCacheMem<K, V>
implements LookupService<K, V>
{
protected Cache<K, Optional<V>> cache;//new LRUMap<K, V>();
protected Cache<K, Boolean> missCache;
protected Cache<K, Optional<V>> hitCache;//new LRUMap<K, V>();
protected Cache<K, Object> missCache;

private LookupService<K, V> base;

Expand All @@ -41,17 +41,17 @@ public LookupServiceCacheMem(LookupService<K, V> base, long maxCacheSize) {
);
}

public LookupServiceCacheMem(LookupService<K, V> base, Cache<K, Optional<V>> cache, Cache<K, Boolean> missCache) {
public LookupServiceCacheMem(LookupService<K, V> base, Cache<K, Optional<V>> cache, Cache<K, Object> missCache) {
this.base = base;
this.cache = cache;
this.hitCache = cache;
this.missCache = missCache;
}

@Override
public Map<K, V> fetchMap(Iterable<K> keys) {
Set<K> lookupKeys = Sets.newLinkedHashSet(keys);

Map<K, Optional<V>> cachedEntries = cache.getAllPresent(keys);
Map<K, Optional<V>> cachedEntries = hitCache.getAllPresent(keys);
lookupKeys.removeAll(cachedEntries.keySet());

Set<K> knownMissKeys = new LinkedHashSet<>(Sets.intersection(lookupKeys, missCache.asMap().keySet()));
Expand All @@ -66,11 +66,11 @@ public Map<K, V> fetchMap(Iterable<K> 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);
Expand All @@ -97,6 +97,12 @@ public static <K, V> LookupServiceCacheMem<K, V> create(LookupService<K, V> base
return result;
}

public static <K, V> LookupServiceCacheMem<K, V> create(LookupService<K, V> base, Cache<K, Optional<V>> hitCache, Cache<K, Object> missCache) {
LookupServiceCacheMem<K, V> result = new LookupServiceCacheMem<K, V>(base, hitCache, missCache);
return result;
}


// public static <K, V> LookupServiceCacheMem<K, V> create(LookupService<K, V> base, Map<K, V> cache) {
// LookupServiceCacheMem<K, V> result = new LookupServiceCacheMem<K, V>(base, cache);
// return result;
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <T>
* @param range
* @param domain
* @return
*/
// This seems to be the same as range.canonical(discreteDomain)
// public static <T extends Comparable<T>> Range<T> makeClosedOpen(Range<T> range, DiscreteDomain<T> domain) {
// ContiguousSet<T> set = ContiguousSet.create(range, domain);
// range.canonical(domain)
// Range<T> result = set.isEmpty()
// ? Range.closedOpen(range.lowerEndpoint(), range.lowerEndpoint())
// : ContiguousSet.create(range, domain).range(BoundType.CLOSED, BoundType.OPEN);
// return result;
// }

public static <T extends Comparable<T>> T closedLowerEndpointOrNull(Range<T> range, DiscreteDomain<T> domain) {
T result = !range.hasLowerBound()
? null
: range.lowerBoundType().equals(BoundType.CLOSED)
? range.lowerEndpoint()
: domain.next(range.lowerEndpoint());

return result;
}

public static <T extends Comparable<T>> T openUpperEndpointOrNull(Range<T> range, DiscreteDomain<T> domain) {
T result = !range.hasUpperBound()
? null
: range.upperBoundType().equals(BoundType.CLOSED)
? domain.next(range.upperEndpoint())
: range.upperEndpoint();

return result;
}

public static Long rangeToOffset(Range<Long> 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<Long> 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;
}
}

0 comments on commit 076f0ea

Please sign in to comment.