@Override public T apply(Result<T> input) { return input.now(); } }
@Override public <T> T get(Class<? extends T> clazz, long id) throws NotFoundException { return this.async.get(clazz, id).get(); }
@Override public <T> Result<T> find(final Key<? extends T> key) { Result<Map<Key<T>, T>> multi = this.get(Collections.singleton(key)); Future<T> wrapped = new SimpleFutureWrapper<Map<Key<T>, T>, T>(multi.getFuture()) { @Override protected T wrap(Map<Key<T>, T> base) throws Exception { return base.get(key); } }; return new ResultAdapter<T>(wrapped); }
@Override public <T> T find(Key<? extends T> key) { return this.async.find(key).get(); }
@Override public <T> Result<Key<T>> put(final T obj) { // let's just translate this to a put(iterable) call Result<Map<Key<T>, T>> result = this.put(Collections.singleton(obj)); Future<Key<T>> future = new SimpleFutureWrapper<Map<Key<T>, T>, Key<T>>(result.getFuture()) { @Override protected Key<T> wrap(Map<Key<T>, T> putted) throws Exception { return putted.keySet().iterator().next(); } }; return new ResultAdapter<Key<T>>(future); }
/** * Obtain the loaded value now. */ @Override public T now() { return result.now(); }
@Override public <T> T find(Class<? extends T> clazz, String name) { return this.async.find(clazz, name).get(); }
@Override public <T> Result<T> get(final Key<? extends T> key) { // The actual implementation is find(). Result<T> found = this.find(key); Future<T> wrapped = new SimpleFutureWrapper<T, T>(found.getFuture()) { @Override protected T wrap(T t) throws Exception { if (t != null) return t; else throw new NotFoundException(key); } }; return new ResultAdapter<T>(wrapped); }
@Override public Object invoke(Object obj, Method meth, Object[] params) throws Throwable { return meth.invoke(result.now(), params); }
@Override public void delete(Object... keysOrEntities) { this.async.delete(keysOrEntities).get(); }
@Override public <T> Result<Map<Key<T>, T>> put(final Iterable<? extends T> objs) { // Unfortunately we can't put the data in the session cache right away because // the entities might not have populated ids. They keys/ids only get populated // when the data is fetched. //for (T t: objs) // this.cache.put(this.factory.getKey(t), t); // //return super.put(objs); Result<Map<Key<T>, T>> orig = super.put(objs); Future<Map<Key<T>, T>> triggered = new TriggerSuccessFuture<Map<Key<T>, T>>(orig.getFuture()) { @Override protected void success(Map<Key<T>, T> result) { for (Map.Entry<Key<T>, T> entry: result.entrySet()) cache.put(entry.getKey(), entry.getValue()); } }; return new ResultAdapter<Map<Key<T>, T>>(triggered); }
@Override @SuppressWarnings("unchecked") public T nowUncached() { // Because clients could conceivably get() in the middle of our operations (see LoadCollectionRefsTest.specialListWorks()), // we need to check for early execution. This will perform poorly, but at least it will work. //assert Round.this.isExecuted(); loadEngine.execute(); return (T)translated.now().get(key); }
@Override public <T> T get(Key<? extends T> key) throws NotFoundException { return this.async.get(key).get(); }
Future<Map<S, T>> wrapped = new SimpleFutureWrapper<Map<Key<T>, T>, Map<S, T>>(fetched.getFuture()) { @Override protected Map<S, T> wrap(Map<Key<T>, T> base) throws Exception
@Override final protected T translate(final Result<F> from) { return wrap(from.now()); } }
@Override public <T> Key<T> put(T obj) { return this.async.put(obj).get(); }
Future<Map<Key<T>, T>> fromDatastore = super.get(keys).getFuture();
private Object writeReplace() throws ObjectStreamException { return new NowProxy<>(result.now()); }
@Override public <T> Map<Key<T>, T> put(T... objs) { return this.async.put(objs).get(); }
@Override public Response commit() { // Complete any enlisted operations so that the session becomes consistent. Note that some of the // enlisted load operations might result in further enlistment... so we have to do this in a loop // that protects against concurrent modification exceptions while (!enlisted.isEmpty()) { final List<Result<?>> last = enlisted; enlisted = new ArrayList<>(); for (final Result<?> result: last) result.now(); } final Response response = transaction.commit(); afterCommit.run(); return response; }