use of com.tvd12.ezyfox.entity.EzyObject in project dahlia by youngmonkeys.
the class CommandUpdateHandler method handle.
@Override
public Object handle(CommandUpdate 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 update = command.getUpdate();
List<EzyPair<Record, EzyObject>> updateItems = 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)
updateItems.add(new EzyPair<>(r, value));
}
});
for (EzyPair<Record, EzyObject> pair : updateItems) {
Record record = pair.getKey();
EzyObject updateItem = pair.getValue();
updateItem(updateItem, update);
collectionStorage.storeRecord(record, sId, sFields, updateItem);
}
}
EzyArray answer = EzyEntityFactory.newArray();
for (EzyPair<Record, EzyObject> pair : updateItems) {
EzyObject answerItem = EzyEntityFactory.newObject();
EzyObject updateItem = pair.getValue();
answerItem.put(Constants.FIELD_ID, updateItem.get(Constants.FIELD_ID));
}
return answer;
}
use of com.tvd12.ezyfox.entity.EzyObject in project dahlia by youngmonkeys.
the class RecordReader method read.
public EzyObject read(Record record, FieldSetting idSetting, Map<String, FieldSetting> settings) {
try {
file.seek(record.getPosition());
// header
file.readByte();
EzyObject output = EzyEntityFactory.newObject();
Object id = fieldReaders.read(file, idSetting);
output.put(FIELD_ID, id);
fieldReaders.read(file, settings, output);
return output;
} catch (IOException e) {
throw new IllegalStateException(e);
}
}
use of com.tvd12.ezyfox.entity.EzyObject in project dahlia by youngmonkeys.
the class QueryToPredicate method toPredicate0.
public Predicate<EzyObject> toPredicate0(EzyObject query) {
for (Entry kv : query.entrySet()) {
String op = (String) kv.getKey();
Function<Object, Predicate<EzyObject>> builder = builders.get(op);
if (builder != null)
return builder.apply(kv.getValue());
else
return toDefaultPredicate(query);
}
return EzyPredicates.ALWAY_TRUE;
}
use of com.tvd12.ezyfox.entity.EzyObject in project dahlia by youngmonkeys.
the class FieldObjectReader method readValue.
@Override
protected EzyObject readValue(FieldReaders readers, FileProxy file, FieldSetting setting) throws IOException {
FieldObjectSetting fs = (FieldObjectSetting) setting;
Map<String, FieldSetting> fieldSettings = fs.getFields();
EzyObject object = EzyEntityFactory.newObject();
readers.read(file, fieldSettings, object);
return object;
}
use of com.tvd12.ezyfox.entity.EzyObject in project dahlia by youngmonkeys.
the class FieldSimpleReaders method read.
@Override
public void read(FileProxy file, Map<String, FieldSetting> settings, EzyObject output) throws IOException {
int readFields = 0;
int totalFields = settings.size();
while (readFields < totalFields) {
String fieldName = readName(file);
FieldSetting setting = settings.get(fieldName);
Object value = readValue(file, setting);
output.put(fieldName, value);
++readFields;
}
}
Aggregations