Search in sources :

Example 1 with Record

use of com.tvd12.dahlia.core.entity.Record in project dahlia by youngmonkeys.

the class LocalCollection method insert.

@Override
public EzyObject insert(EzyObject record) {
    CommandInsertOne command = new CommandInsertOne(store.getId(), record);
    EzyObject result = commandExecutor.execute(command);
    return result;
}
Also used : CommandInsertOne(com.tvd12.dahlia.core.command.CommandInsertOne) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 2 with Record

use of com.tvd12.dahlia.core.entity.Record 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;
}
Also used : FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) 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) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Example 3 with Record

use of com.tvd12.dahlia.core.entity.Record 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;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException)

Example 4 with Record

use of com.tvd12.dahlia.core.entity.Record 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;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Example 5 with Record

use of com.tvd12.dahlia.core.entity.Record in project dahlia by youngmonkeys.

the class QueryToPredicate method toDefaultPredicate.

protected Predicate<EzyObject> toDefaultPredicate(EzyObject query) {
    for (Entry kv : query.entrySet()) {
        String field = null;
        Object value = kv.getValue();
        String fieldOrOperation = (String) kv.getKey();
        Operation operation = Operation.valueOfKeyword(fieldOrOperation);
        if (operation != null) {
            EzyObject oValue = (EzyObject) value;
            for (Entry e : oValue.entrySet()) {
                field = (String) e.getKey();
                value = e.getValue();
                break;
            }
        } else if (value instanceof EzyObject) {
            EzyObject oValue = (EzyObject) value;
            for (Entry e : oValue.entrySet()) {
                String keyword = (String) e.getKey();
                operation = Operation.valueOfKeyword(keyword);
                value = e.getValue();
                break;
            }
        } else {
            field = fieldOrOperation;
            operation = Operation.EQ;
        }
        String qField = field;
        Object qValue = value;
        Operation qOperation = operation;
        return record -> {
            Object rValue = record.get(qField);
            int compareResult = compareValue(rValue, qValue);
            if (qOperation == Operation.GT)
                return compareResult > 0;
            if (qOperation == Operation.GTE)
                return compareResult >= 0;
            if (qOperation == Operation.LT)
                return compareResult < 0;
            if (qOperation == Operation.LTE)
                return compareResult <= 0;
            if (qOperation == Operation.NEQ)
                return compareResult != 0;
            return compareResult == 0;
        };
    }
    return record -> false;
}
Also used : EzyPredicates(com.tvd12.ezyfox.function.EzyPredicates) EzyArray(com.tvd12.ezyfox.entity.EzyArray) Predicate(java.util.function.Predicate) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) InvalidQueryException(com.tvd12.dahlia.exception.InvalidQueryException) Operation(com.tvd12.dahlia.math.Operation) List(java.util.List) Map(java.util.Map) Keywords(com.tvd12.dahlia.constant.Keywords) EzyObject(com.tvd12.ezyfox.entity.EzyObject) Entry(java.util.Map.Entry) Comparators(com.tvd12.dahlia.core.comparator.Comparators) Comparator(java.util.Comparator) Entry(java.util.Map.Entry) EzyObject(com.tvd12.ezyfox.entity.EzyObject) Operation(com.tvd12.dahlia.math.Operation) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

EzyObject (com.tvd12.ezyfox.entity.EzyObject)14 Record (com.tvd12.dahlia.core.entity.Record)11 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)10 Collection (com.tvd12.dahlia.core.entity.Collection)9 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)9 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)8 EzyArray (com.tvd12.ezyfox.entity.EzyArray)6 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)5 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)5 ArrayList (java.util.ArrayList)4 EzyPair (com.tvd12.ezyfox.util.EzyPair)2 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)2 IOException (java.io.IOException)2 Keywords (com.tvd12.dahlia.constant.Keywords)1 CommandInsertOne (com.tvd12.dahlia.core.command.CommandInsertOne)1 CommandSaveOne (com.tvd12.dahlia.core.command.CommandSaveOne)1 Comparators (com.tvd12.dahlia.core.comparator.Comparators)1 DuplicatedIdException (com.tvd12.dahlia.exception.DuplicatedIdException)1 InvalidQueryException (com.tvd12.dahlia.exception.InvalidQueryException)1 Operation (com.tvd12.dahlia.math.Operation)1