@Override public void destroy() throws Exception { this.adapter.clear(); }
@Override public long count(Class<?> type) { Assert.notNull(type, "Type for count must not be null!"); return adapter.count(resolveKeySpace(type)); }
@Override public Collection<?> execute(@Nullable SpelCriteria criteria, @Nullable Comparator<?> sort, long offset, int rows, String keyspace) { return sortAndFilterMatchingRange(getRequiredAdapter().getAllOf(keyspace), criteria, sort, offset, rows); }
@Override public <T> T insert(Object id, T objectToInsert) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToInsert, "Object to be inserted must not be null!"); String keyspace = resolveKeySpace(objectToInsert.getClass()); potentiallyPublishEvent(KeyValueEvent.beforeInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); execute((KeyValueCallback<Void>) adapter -> { if (adapter.contains(id, keyspace)) { throw new DuplicateKeyException( String.format("Cannot insert existing object with id %s!. Please use update.", id)); } adapter.put(id, objectToInsert, keyspace); return null; }); potentiallyPublishEvent(KeyValueEvent.afterInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); return objectToInsert; }
@Override public void delete(Class<?> type) { Assert.notNull(type, "Type to delete must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDropKeySpace(keyspace, type)); execute((KeyValueCallback<Void>) adapter -> { adapter.deleteAllOf(keyspace); return null; }); potentiallyPublishEvent(KeyValueEvent.afterDropKeySpace(keyspace, type)); }
@Override public <T> T delete(Object id, Class<T> type) { Assert.notNull(id, "Id for object to be deleted must not be null!"); Assert.notNull(type, "Type to delete must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDelete(id, keyspace, type)); T result = execute(adapter -> adapter.delete(id, keyspace, type)); potentiallyPublishEvent(KeyValueEvent.afterDelete(id, keyspace, type, result)); return result; }
@Override public <T> T update(Object id, T objectToUpdate) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToUpdate, "Object to be updated must not be null!"); String keyspace = resolveKeySpace(objectToUpdate.getClass()); potentiallyPublishEvent(KeyValueEvent.beforeUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate)); Object existing = execute(adapter -> adapter.put(id, objectToUpdate, keyspace)); potentiallyPublishEvent( KeyValueEvent.afterUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate, existing)); return objectToUpdate; }
@Override public <T> Iterable<T> find(KeyValueQuery<?> query, Class<T> type) { return executeRequired((KeyValueCallback<Iterable<T>>) adapter -> { Iterable<?> result = adapter.find(query, resolveKeySpace(type), type); List<T> filtered = new ArrayList<>(); for (Object candidate : result) { if (typeCheck(type, candidate)) { filtered.add(type.cast(candidate)); } } return filtered; }); }
@Override public <T> Optional<T> findById(Object id, Class<T> type) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(type, "Type to fetch must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeGet(id, keyspace, type)); T result = execute(adapter -> { Object value = adapter.get(id, keyspace, type); if (value == null || typeCheck(type, value)) { return type.cast(value); } return null; }); potentiallyPublishEvent(KeyValueEvent.afterGet(id, keyspace, type, result)); return Optional.ofNullable(result); }
@Override public <T> T insert(Object id, T objectToInsert) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToInsert, "Object to be inserted must not be null!"); String keyspace = resolveKeySpace(objectToInsert.getClass()); potentiallyPublishEvent(KeyValueEvent.beforeInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); execute((KeyValueCallback<Void>) adapter -> { if (adapter.contains(id, keyspace)) { throw new DuplicateKeyException( String.format("Cannot insert existing object with id %s!. Please use update.", id)); } adapter.put(id, objectToInsert, keyspace); return null; }); potentiallyPublishEvent(KeyValueEvent.afterInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); return objectToInsert; }
@Override public void delete(Class<?> type) { Assert.notNull(type, "Type to delete must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDropKeySpace(keyspace, type)); execute((KeyValueCallback<Void>) adapter -> { adapter.deleteAllOf(keyspace); return null; }); potentiallyPublishEvent(KeyValueEvent.afterDropKeySpace(keyspace, type)); }
@Override public <T> T delete(Object id, Class<T> type) { Assert.notNull(id, "Id for object to be deleted must not be null!"); Assert.notNull(type, "Type to delete must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDelete(id, keyspace, type)); T result = execute(adapter -> adapter.delete(id, keyspace, type)); potentiallyPublishEvent(KeyValueEvent.afterDelete(id, keyspace, type, result)); return result; }
@Override public <T> T update(Object id, T objectToUpdate) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToUpdate, "Object to be updated must not be null!"); String keyspace = resolveKeySpace(objectToUpdate.getClass()); potentiallyPublishEvent(KeyValueEvent.beforeUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate)); Object existing = execute(adapter -> adapter.put(id, objectToUpdate, keyspace)); potentiallyPublishEvent( KeyValueEvent.afterUpdate(id, keyspace, objectToUpdate.getClass(), objectToUpdate, existing)); return objectToUpdate; }
@Override public <T> Iterable<T> find(KeyValueQuery<?> query, Class<T> type) { return executeRequired((KeyValueCallback<Iterable<T>>) adapter -> { Iterable<?> result = adapter.find(query, resolveKeySpace(type), type); List<T> filtered = new ArrayList<>(); for (Object candidate : result) { if (typeCheck(type, candidate)) { filtered.add(type.cast(candidate)); } } return filtered; }); }
@Override public <T> Optional<T> findById(Object id, Class<T> type) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(type, "Type to fetch must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeGet(id, keyspace, type)); T result = execute(adapter -> { Object value = adapter.get(id, keyspace, type); if (value == null || typeCheck(type, value)) { return type.cast(value); } return null; }); potentiallyPublishEvent(KeyValueEvent.afterGet(id, keyspace, type, result)); return Optional.ofNullable(result); }
@Override public <T> T insert(Object id, T objectToInsert) { Assert.notNull(id, "Id for object to be inserted must not be null!"); Assert.notNull(objectToInsert, "Object to be inserted must not be null!"); String keyspace = resolveKeySpace(objectToInsert.getClass()); potentiallyPublishEvent(KeyValueEvent.beforeInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); execute((KeyValueCallback<Void>) adapter -> { if (adapter.contains(id, keyspace)) { throw new DuplicateKeyException( String.format("Cannot insert existing object with id %s!. Please use update.", id)); } adapter.put(id, objectToInsert, keyspace); return null; }); potentiallyPublishEvent(KeyValueEvent.afterInsert(id, keyspace, objectToInsert.getClass(), objectToInsert)); return objectToInsert; }
@Override public Collection<?> execute(@Nullable SpelCriteria criteria, @Nullable Comparator<?> sort, long offset, int rows, String keyspace) { return sortAndFilterMatchingRange(getRequiredAdapter().getAllOf(keyspace), criteria, sort, offset, rows); }
@Override public long count(Class<?> type) { Assert.notNull(type, "Type for count must not be null!"); return adapter.count(resolveKeySpace(type)); }
@Override public void delete(Class<?> type) { Assert.notNull(type, "Type to delete must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDropKeySpace(keyspace, type)); execute((KeyValueCallback<Void>) adapter -> { adapter.deleteAllOf(keyspace); return null; }); potentiallyPublishEvent(KeyValueEvent.afterDropKeySpace(keyspace, type)); }
@Override public <T> T delete(Object id, Class<T> type) { Assert.notNull(id, "Id for object to be deleted must not be null!"); Assert.notNull(type, "Type to delete must not be null!"); String keyspace = resolveKeySpace(type); potentiallyPublishEvent(KeyValueEvent.beforeDelete(id, keyspace, type)); T result = execute(adapter -> adapter.delete(id, keyspace, type)); potentiallyPublishEvent(KeyValueEvent.afterDelete(id, keyspace, type, result)); return result; }