Skip to content

Commit

Permalink
Sorted cache objects (#25)
Browse files Browse the repository at this point in the history
* Ensure cache is in a deterministic order

Sorts cache by checksum.

* Fixed spec.

* Added streamSliceSince
  • Loading branch information
Asgeir Storesund Nilsen authored May 22, 2020
1 parent 9a84e36 commit 14130b9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/main/java/no/fint/cache/CacheService.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ public Stream<T> streamSince(String orgId, long sinceTimestamp) {
return getCache(orgId).orElseThrow(() -> new CacheNotFoundException(orgId)).streamSince(sinceTimestamp).map(CacheObject::getObject);
}

public Stream<T> streamSliceSince(String orgId, long sinceTimestamp, int skip, int limit) {
return getCache(orgId)
.orElseThrow(() -> new CacheNotFoundException(orgId))
.streamSince(sinceTimestamp)
.skip(skip)
.limit(limit)
.map(CacheObject::getObject);
}

public Optional<T> getOne(String orgId, Predicate<T> idFunction) {
Cache<T> cache = getCache(orgId).orElseThrow(() -> new CacheNotFoundException(orgId));
return cache.filter(idFunction).max(Comparator.comparingLong(CacheObject::getLastUpdated)).map(CacheObject::getObject);
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/no/fint/cache/FintCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ private void updateInternal(Map<String, CacheObject<T>> cacheObjectMap) {
});

cacheObjectsCopy.addAll(cacheObjectMap.values());
cacheObjects = ImmutableList.copyOf(cacheObjectsCopy);
cacheObjects = ImmutableList.sortedCopyOf(Comparator.comparing(CacheObject::getChecksum), cacheObjectsCopy);
}

updateMetaData();
Expand All @@ -82,7 +82,7 @@ public void add(List<T> objects) {
private void addInternal(Map<String, CacheObject<T>> newObjects) {
List<CacheObject<T>> cacheObjectsCopy = new ArrayList<>(cacheObjects);
cacheObjectsCopy.addAll(newObjects.values());
cacheObjects = ImmutableList.copyOf(cacheObjectsCopy);
cacheObjects = ImmutableList.sortedCopyOf(Comparator.comparing(CacheObject::getChecksum), cacheObjectsCopy);
updateMetaData();
}

Expand Down Expand Up @@ -133,7 +133,7 @@ private void updateMetaData() {
int i = iterator.nextIndex();
CacheObject<T> it = iterator.next();
digest.update(it.rawChecksum());
IntStream.of(it.getHashCodes()).forEach(key -> newIndex.compute(key, (k,v) -> {
IntStream.of(it.getHashCodes()).forEach(key -> newIndex.compute(key, (k, v) -> {
if (v == null) {
return new SingleIndex(i);
}
Expand Down Expand Up @@ -171,7 +171,9 @@ public int size() {
}

@Override
public long volume() { return cacheMetaData.getSize(); }
public long volume() {
return cacheMetaData.getSize();
}

@Override
public Stream<CacheObject<T>> filter(Predicate<T> predicate) {
Expand Down
17 changes: 14 additions & 3 deletions src/test/groovy/no/fint/cache/FintCacheIntegrationSpec.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -174,14 +174,14 @@ class FintCacheIntegrationSpec extends Specification {

def "On received event"() {
given:
def event = new Event(orgId: 'rogfk.no', data: ['test1', 'test2', 'test3'])
def event = new Event(orgId: 'rogfk.no', data: ['test1', 'test2', 'test3', 'test4'])

when:
testCacheService.onAction(event)
def values = testCacheService.getAll('rogfk.no')

then:
values.size() == 3
values.size() == 4
}

def 'Update cache with hashcodes'() {
Expand Down Expand Up @@ -216,6 +216,17 @@ class FintCacheIntegrationSpec extends Specification {
def result = testCacheService.streamSlice('rogfk.no', 1, 1).collect(Collectors.toList())

then:
result == ['test2']
result == ['test4']
}

def 'Stream cache slice since'() {
given:
testCacheService.update('rogfk.no', ['test1', 'test2', 'test3', 'test4'])

when:
def result = testCacheService.streamSliceSince('rogfk.no', System.currentTimeMillis() - 500, 1, 1).collect(Collectors.toList())

then:
result == ['test4']
}
}

0 comments on commit 14130b9

Please sign in to comment.