/** * Commits new version of record to the history and invalidate snapshot version. */ Set<String> commit(Record record) { history.add(history.size(), record.clone()); return snapshot.mergeWith(record); }
@NotNull @Override public Record apply(@NotNull Record record) { Record result = record.clone(); result.mergeWith(journal.snapshot); return result; } }).or(journal.snapshot.clone());
/** * Lookups record by mutation id, if it's found removes it from the history and invalidates snapshot record. * Snapshot record is superposition of all record versions in the history. */ Set<String> revert(UUID mutationId) { int recordIndex = -1; for (int i = 0; i < history.size(); i++) { if (mutationId.equals(history.get(i).mutationId())) { recordIndex = i; break; } } if (recordIndex == -1) { return Collections.emptySet(); } Set<String> changedKeys = new HashSet<>(); changedKeys.add(history.remove(recordIndex).key()); for (int i = Math.max(0, recordIndex - 1); i < history.size(); i++) { Record record = history.get(i); if (i == Math.max(0, recordIndex - 1)) { snapshot = record.clone(); } else { changedKeys.addAll(snapshot.mergeWith(record)); } } return changedKeys; } }
@Nullable @Override public Record loadRecord(@NotNull final String key, @NotNull final CacheHeaders cacheHeaders) { checkNotNull(key, "key == null"); checkNotNull(cacheHeaders, "cacheHeaders == null"); try { final Optional<Record> nonOptimisticRecord = nextCache() .flatMap(new Function<NormalizedCache, Optional<Record>>() { @NotNull @Override public Optional<Record> apply(@NotNull NormalizedCache cache) { return Optional.fromNullable(cache.loadRecord(key, cacheHeaders)); } }); final RecordJournal journal = lruCache.getIfPresent(key); if (journal != null) { return nonOptimisticRecord.map(new Function<Record, Record>() { @NotNull @Override public Record apply(@NotNull Record record) { Record result = record.clone(); result.mergeWith(journal.snapshot); return result; } }).or(journal.snapshot.clone()); } else { return nonOptimisticRecord.orNull(); } } catch (Exception ignore) { return null; } }
@Nonnull @Override public Record apply(@Nonnull Record record) { Record result = record.clone(); result.mergeWith(journal.snapshot); return result; } }).or(journal.snapshot.clone());
/** * Commits new version of record to the history and invalidate snapshot version. */ Set<String> commit(Record record) { history.addLast(record.clone()); return snapshot.mergeWith(record); }
@Nonnull @Override public Record apply(@Nonnull Record record) { Record result = record.clone(); result.mergeWith(journal.snapshot); return result; } }).or(journal.snapshot.clone());
/** * Commits new version of record to the history and invalidate snapshot version. */ Set<String> commit(Record record) { history.addLast(record.clone()); return snapshot.mergeWith(record); }
/** * Lookups record by mutation id, if it's found removes it from the history and invalidates snapshot record. * Snapshot record is superposition of all record versions in the history. */ Set<String> revert(UUID mutationId) { int recordIndex = -1; for (int i = 0; i < history.size(); i++) { if (mutationId.equals(history.get(i).mutationId())) { recordIndex = i; break; } } if (recordIndex == -1) { return Collections.emptySet(); } Set<String> changedKeys = new HashSet<>(); changedKeys.add(history.remove(recordIndex).key()); for (int i = Math.max(0, recordIndex - 1); i < history.size(); i++) { Record record = history.get(i); if (i == Math.max(0, recordIndex - 1)) { snapshot = record.clone(); } else { changedKeys.addAll(snapshot.mergeWith(record)); } } return changedKeys; } }
/** * Lookups record by mutation id, if it's found removes it from the history and invalidates snapshot record. * Snapshot record is superposition of all record versions in the history. */ Set<String> revert(UUID mutationId) { int recordIndex = -1; for (int i = 0; i < history.size(); i++) { if (mutationId.equals(history.get(i).mutationId())) { recordIndex = i; break; } } if (recordIndex == -1) { return Collections.emptySet(); } Set<String> changedKeys = new HashSet<>(); changedKeys.add(history.remove(recordIndex).key()); for (int i = Math.max(0, recordIndex - 1); i < history.size(); i++) { Record record = history.get(i); if (i == Math.max(0, recordIndex - 1)) { snapshot = record.clone(); } else { changedKeys.addAll(snapshot.mergeWith(record)); } } return changedKeys; } }
@Nullable @Override public Record loadRecord(@Nonnull final String key, @Nonnull final CacheHeaders cacheHeaders) { checkNotNull(key, "key == null"); checkNotNull(cacheHeaders, "cacheHeaders == null"); try { final Optional<Record> nonOptimisticRecord = nextCache() .flatMap(new Function<NormalizedCache, Optional<Record>>() { @Nonnull @Override public Optional<Record> apply(@Nonnull NormalizedCache cache) { return Optional.fromNullable(cache.loadRecord(key, cacheHeaders)); } }); final RecordJournal journal = lruCache.getIfPresent(key); if (journal != null) { return nonOptimisticRecord.map(new Function<Record, Record>() { @Nonnull @Override public Record apply(@Nonnull Record record) { Record result = record.clone(); result.mergeWith(journal.snapshot); return result; } }).or(journal.snapshot.clone()); } else { return nonOptimisticRecord.orNull(); } } catch (Exception ignore) { return null; } }
@Nullable @Override public Record loadRecord(@Nonnull final String key, @Nonnull final CacheHeaders cacheHeaders) { checkNotNull(key, "key == null"); checkNotNull(cacheHeaders, "cacheHeaders == null"); try { final Optional<Record> nonOptimisticRecord = nextCache() .flatMap(new Function<NormalizedCache, Optional<Record>>() { @Nonnull @Override public Optional<Record> apply(@Nonnull NormalizedCache cache) { return Optional.fromNullable(cache.loadRecord(key, cacheHeaders)); } }); final RecordJournal journal = lruCache.getIfPresent(key); if (journal != null) { return nonOptimisticRecord.map(new Function<Record, Record>() { @Nonnull @Override public Record apply(@Nonnull Record record) { Record result = record.clone(); result.mergeWith(journal.snapshot); return result; } }).or(journal.snapshot.clone()); } else { return nonOptimisticRecord.orNull(); } } catch (Exception ignore) { return null; } }