/** * Invokes a {@link Runnable} and translates {@link DatastoreException} and {@link RuntimeIOException} * to checked exceptions. * @throws DatastoreIOException if {@code f} threw {@link DatastoreException} * @throws IOException if {@code f} threw {@link RuntimeIOException} */ static void run(Runnable f) throws IOException { call(() -> { f.run(); return null; }); } }
/** * Optionally get an {@link Entity} from a {@link DatastoreReader}. * * @param datastore The reader to get from * @param key The key to get * @return an optional containing the entity if it existed, empty otherwise. */ static Optional<Entity> getOpt(CheckedDatastoreReaderWriter datastore, Key key) throws IOException { return Optional.ofNullable(datastore.get(key)); }
/** * @see DatastoreReaderWriter#update(Entity...) * @throws IOException if the underlying client throws {@link DatastoreException} */ void update(Entity... entites) throws IOException { run(() -> rw.update(entites)); }
/** * @see DatastoreReaderWriter#delete(Key...) * @throws IOException if the underlying client throws {@link DatastoreException} */ void delete(Key... keys) throws IOException { run(() -> rw.delete(keys)); }
/** * @see DatastoreReaderWriter#add(FullEntity) * @throws IOException if the underlying client throws {@link DatastoreException} */ Entity add(FullEntity<?> entity) throws IOException { return call(() -> rw.add(entity)); }
/** * Prefer this method over {@link #get(Iterable)} to avoid gathering large results in a list. * @see DatastoreReaderWriter#get(Key...) * @throws IOException if the underlying client throws {@link DatastoreException} * or if {@code f} throws {@link IOException}. */ void get(Iterable<Key> keys, IOConsumer<Entity> f) throws IOException { run(() -> rw.get(toArray(keys, Key.class)).forEachRemaining(IOConsumer.unchecked(f))); }
/** * Only use this method if the results are small enough that gathering them in list is acceptable. * Otherwise use {@link #get(Iterable, IOConsumer)}. * @see DatastoreReaderWriter#get(Key...) * @throws IOException if the underlying client throws {@link DatastoreException} */ List<Entity> get(Iterable<Key> keys) throws IOException { return call(() -> ImmutableList.copyOf(rw.get(toArray(keys, Key.class)))); }
/** * Prefer this method over {@link #query(Query)} to avoid gathering large results in a list. * @see DatastoreReaderWriter#run(Query) * @throws IOException if the underlying client throws {@link DatastoreException} * or if {@code f} throws {@link IOException}. */ <T> void query(Query<T> query, IOConsumer<T> f) throws IOException { run(() -> rw.run(query).forEachRemaining(IOConsumer.unchecked(f))); }
/** * Only use this method if the results are small enough that gathering them in list is acceptable. * Otherwise use {@link #query(Query, IOConsumer)}. * @see DatastoreReaderWriter#run(Query) * @throws IOException if the underlying client throws {@link DatastoreException} */ <T> List<T> query(Query<T> query) throws IOException { return call(() -> ImmutableList.copyOf(rw.run(query))); }
/** * @see DatastoreReaderWriter#get(Key) * @throws IOException if the underlying client throws {@link DatastoreException} */ Entity get(Key key) throws IOException { return call(() -> rw.get(key)); }
/** * @see DatastoreReaderWriter#put(FullEntity) * @throws IOException if the underlying client throws {@link DatastoreException} */ Entity put(FullEntity<?> entity) throws IOException { return call(() -> rw.put(entity)); }