use of com.tvd12.dahlia.core.entity.Collection in project dahlia by youngmonkeys.
the class CommandCountHandler method handle.
@Override
public Object handle(CommandCount 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);
if (predicate == EzyPredicates.ALWAY_TRUE)
return collection.size();
CollectionSetting setting = collection.getSetting();
CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
FieldSetting sId = setting.getId();
Map<String, FieldSetting> sFields = setting.getFields();
AtomicInteger count = new AtomicInteger();
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)
count.incrementAndGet();
}
});
}
return count.get();
}
use of com.tvd12.dahlia.core.entity.Collection 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.entity.Collection in project dahlia by youngmonkeys.
the class CommandFindHandler method handle.
@Override
public Object handle(CommandFind 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();
EzyObject options = command.getOptions();
int skip = options.get(OptionFields.SKIP, int.class, 0);
int limit = options.get(OptionFields.LIMIT, int.class, 25);
EzyObject sortBy = options.get(OptionFields.SORT, EzyObject.class, EMPTY_OBJECT);
if (sortBy.isEmpty()) {
EzyArray answer = EzyEntityFactory.newArray();
EzyWrap<Integer> count = new EzyWrap<>(0);
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) {
int currentCount = count.getValue();
if (currentCount >= skip)
answer.add(value);
count.setValue(currentCount + 1);
}
}
@Override
public boolean next() {
int currentSize = answer.size();
return currentSize < limit;
}
});
}
return answer;
} else {
List<EzyObject> found = 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)
found.add(value);
}
});
}
found.sort(sortByComparator(sortBy));
return getResult(found, skip, limit);
}
}
use of com.tvd12.dahlia.core.entity.Collection in project dahlia by youngmonkeys.
the class CommandFindOneHandler method handle.
@Override
public Object handle(CommandFindOne 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();
EzyWrap<EzyObject> ref = new EzyWrap<>();
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)
ref.setValue(value);
}
@Override
public boolean next() {
return ref.hasNoValue();
}
});
}
return ref.getValue();
}
use of com.tvd12.dahlia.core.entity.Collection in project dahlia by youngmonkeys.
the class DahliaCoreLoader method loadCollections.
protected void loadCollections(Database database, DatabaseStorage storage) {
for (Collection collection : database.getCollectionList()) {
CollectionStorage collectionStorage = storage.getCollectionStorage(collection.getId());
loadCollection(collection, collectionStorage);
}
}
Aggregations