@Override
public TemporaryStorage apply(Snapshot snapshot) {
final Storage storage = MemStorage.newEmptyStorage();
FakeClock clock = new FakeClock();
clock.setNowMillis(snapshot.getTimestamp());
final SnapshotStore<Snapshot> snapshotStore = new SnapshotStoreImpl(clock, storage);
snapshotStore.applySnapshot(snapshot);
return new TemporaryStorage() {
@Override
public void deleteTasks(final Query.Builder query) {
storage.write(new MutateWork.NoResult.Quiet() {
@Override
protected void execute(MutableStoreProvider storeProvider) {
Set<String> ids = FluentIterable.from(storeProvider.getTaskStore().fetchTasks(query))
.transform(Tasks.SCHEDULED_TO_ID)
.toSet();
storeProvider.getUnsafeTaskStore().deleteTasks(ids);
}
});
}
@Override
public Set<IScheduledTask> fetchTasks(final Query.Builder query) {
return storage.consistentRead(new Work.Quiet<Set<IScheduledTask>>() {
@Override
public Set<IScheduledTask> apply(StoreProvider storeProvider) {
return storeProvider.getTaskStore().fetchTasks(query);
}
});
}
@Override
public Snapshot toSnapshot() {
return snapshotStore.createSnapshot();
}
};
}