use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.
the class CommandInsertHandler method handle.
@Override
public Object handle(CommandInsert command) {
int collectionId = command.getCollectionId();
EzyArray data = command.getData();
Collection collection = databases.getCollection(collectionId);
CollectionSetting setting = collection.getSetting();
FieldSetting sId = setting.getId();
Map<String, FieldSetting> sFields = setting.getFields();
long dataSize = collection.getDataSize();
CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
EzyArray answerItems = EzyEntityFactory.newArray();
synchronized (collection) {
for (int i = 0; i < data.size(); ++i) {
EzyObject answerItem = EzyEntityFactory.newObject();
EzyObject item = data.get(i);
Comparable id = item.get(Constants.FIELD_ID);
if (id != null) {
Record existed = collection.findById(id);
if (existed != null) {
answerItem.put(Constants.RESULT_FIELD_EXISTED, true);
answerItems.add(id);
continue;
}
} else {
while (true) {
id = UUID.randomUUID();
Record existed = collection.findById(id);
if (existed == null)
break;
}
}
Record record = new Record(id, dataSize);
collection.insert(record);
collection.increaseDataSize();
collectionStorage.storeRecord(record, sId, sFields, item);
answerItem.put(Constants.FIELD_ID, id);
answerItems.add(id);
}
}
return answerItems;
}
use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.
the class CommandInsertOneHandler method handle.
@Override
public Object handle(CommandInsertOne command) {
int collectionId = command.getCollectionId();
EzyObject data = command.getData();
Collection collection = databases.getCollection(collectionId);
CollectionSetting setting = collection.getSetting();
CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
Comparable id = data.get(Constants.FIELD_ID);
synchronized (collection) {
if (id != null) {
Record existed = collection.findById(id);
if (existed != null)
throw new DuplicatedIdException(collection.getName(), id);
} else {
while (true) {
id = UUID.randomUUID();
Record existed = collection.findById(id);
if (existed == null)
break;
}
data.put(Constants.FIELD_ID, id);
}
Record record = new Record(id, collection.getDataSize());
collection.insert(record);
collection.increaseDataSize();
collectionStorage.storeRecord(record, setting.getId(), setting.getFields(), data);
}
EzyObject answer = EzyEntityFactory.newObject();
answer.put(Constants.FIELD_ID, id);
return answer;
}
use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.
the class CommandSaveOneHandler method handle.
@Override
public Object handle(CommandSaveOne command) {
int collectionId = command.getCollectionId();
EzyObject data = command.getData();
Collection collection = databases.getCollection(collectionId);
CollectionSetting setting = collection.getSetting();
CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
Comparable id = data.get(Constants.FIELD_ID);
EzyObject answer = EzyEntityFactory.newObject();
synchronized (collection) {
if (id != null) {
Record existed = collection.findById(id);
if (existed != null)
answer.put(Constants.RESULT_FIELD_EXISTED, true);
} else {
while (true) {
id = UUID.randomUUID();
Record existed = collection.findById(id);
if (existed == null)
break;
}
data.put(Constants.FIELD_ID, id);
}
Record record = new Record(id, collection.getDataSize());
collection.insert(record);
collection.increaseDataSize();
collectionStorage.storeRecord(record, setting.getId(), setting.getFields(), data);
}
answer.put(Constants.FIELD_ID, id);
return answer;
}
use of com.tvd12.dahlia.core.storage.CollectionStorage 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.storage.CollectionStorage 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;
}
Aggregations