Search in sources :

Example 51 with EzyObject

use of com.tvd12.ezyfoxserver.client.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;
}
Also used : ArrayList(java.util.ArrayList) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) RecordConsumer(com.tvd12.dahlia.core.function.RecordConsumer) CollectionNotFoundException(com.tvd12.dahlia.exception.CollectionNotFoundException) FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) EzyPair(com.tvd12.ezyfox.util.EzyPair) EzyArray(com.tvd12.ezyfox.entity.EzyArray) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 52 with EzyObject

use of com.tvd12.ezyfoxserver.client.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);
    }
}
Also used : EzyObject(com.tvd12.ezyfox.entity.EzyObject) EzyObject(com.tvd12.ezyfox.entity.EzyObject) IOException(java.io.IOException)

Example 53 with EzyObject

use of com.tvd12.ezyfoxserver.client.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;
}
Also used : Entry(java.util.Map.Entry) EzyObject(com.tvd12.ezyfox.entity.EzyObject) Predicate(java.util.function.Predicate)

Example 54 with EzyObject

use of com.tvd12.ezyfoxserver.client.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;
}
Also used : FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) FieldObjectSetting(com.tvd12.dahlia.core.setting.FieldObjectSetting) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 55 with EzyObject

use of com.tvd12.ezyfoxserver.client.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;
    }
}
Also used : FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

EzyObject (com.tvd12.ezyfox.entity.EzyObject)37 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)19 EzyArray (com.tvd12.ezyfox.entity.EzyArray)17 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)12 Collection (com.tvd12.dahlia.core.entity.Collection)11 Record (com.tvd12.dahlia.core.entity.Record)9 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)9 HashMap (java.util.HashMap)6 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)5 DatabaseSetting (com.tvd12.dahlia.core.setting.DatabaseSetting)5 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)5 DuplicatedIdException (com.tvd12.dahlia.exception.DuplicatedIdException)5 ArrayList (java.util.ArrayList)5 CollectionExistedException (com.tvd12.dahlia.exception.CollectionExistedException)4 DatabaseExistedException (com.tvd12.dahlia.exception.DatabaseExistedException)4 FindOptions (com.tvd12.dahlia.query.FindOptions)4 CommandCount (com.tvd12.dahlia.core.command.CommandCount)3 CommandFind (com.tvd12.dahlia.core.command.CommandFind)3 CommandInsertOne (com.tvd12.dahlia.core.command.CommandInsertOne)3 FieldLongSetting (com.tvd12.dahlia.core.setting.FieldLongSetting)3