use of com.tvd12.dahlia.core.command.CommandDelete in project dahlia by youngmonkeys.
the class CommandDeleteHandler method handle.
@Override
public Object handle(CommandDelete command) {
int collectionId = command.getCollectionId();
Collection collection = databases.getCollection(collectionId);
if (collection == null)
throw new CollectionNotFoundException(collectionId);
EzyObject query = command.getQuery();
Predicate<EzyObject> predicate = queryToPredicate.toPredicate(query);
CollectionSetting setting = collection.getSetting();
CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
FieldSetting sId = setting.getId();
Map<String, FieldSetting> sFields = setting.getFields();
List<EzyPair<Record, EzyObject>> deletedItems = new ArrayList<>();
synchronized (collection) {
collection.forEach(new RecordConsumer() {
@Override
public void accept(Record r) {
EzyObject value = collectionStorage.readRecord(r, sId, sFields);
boolean accepted = predicate.test(value);
if (accepted)
deletedItems.add(new EzyPair<>(r, value));
}
});
for (EzyPair<Record, EzyObject> pair : deletedItems) {
Record deletedRecord = pair.getKey();
EzyObject deletedValue = pair.getValue();
Comparable id = deletedRecord.getId();
collection.remove(id);
deletedRecord.setAlive(false);
collectionStorage.storeRecord(deletedRecord, sId, sFields, deletedValue);
}
}
EzyArray answer = EzyEntityFactory.newArray();
for (EzyPair<Record, EzyObject> pair : deletedItems) {
EzyObject answerItem = EzyEntityFactory.newObject();
EzyObject updateItem = pair.getValue();
answerItem.put(Constants.FIELD_ID, updateItem.get(Constants.FIELD_ID));
}
return answer;
}
use of com.tvd12.dahlia.core.command.CommandDelete in project dahlia by youngmonkeys.
the class LocalCollection method delete.
@Override
public EzyArray delete(EzyObject query) {
CommandDelete command = new CommandDelete(store.getId(), query);
EzyArray result = commandExecutor.execute(command);
return result;
}
Aggregations