Search in sources :

Example 11 with EzyObject

use of com.tvd12.ezyfoxserver.client.entity.EzyObject 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 12 with EzyObject

use of com.tvd12.ezyfoxserver.client.entity.EzyObject in project dahlia by youngmonkeys.

the class CommandUpdateHandler method updateItem.

protected void updateItem(EzyObject item, EzyObject update) {
    for (Object key : update.keySet()) {
        Object updateValue = update.get(key);
        if (updateValue instanceof EzyObject) {
            EzyObject itemValue = item.get(key);
            updateItem(itemValue, (EzyObject) updateValue);
        } else {
            item.put(key, updateValue);
        }
    }
}
Also used : EzyObject(com.tvd12.ezyfox.entity.EzyObject) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 13 with EzyObject

use of com.tvd12.ezyfoxserver.client.entity.EzyObject 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)

Example 14 with EzyObject

use of com.tvd12.ezyfoxserver.client.entity.EzyObject in project dahlia by youngmonkeys.

the class FieldObjectWriter method writeValue.

@Override
protected void writeValue(FieldWriters writers, FileProxy file, FieldSetting setting, EzyObject value) throws IOException {
    FieldObjectSetting fs = (FieldObjectSetting) setting;
    Map<String, FieldSetting> fieldSettings = fs.getFields();
    writers.write(file, fieldSettings, value);
}
Also used : FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) FieldObjectSetting(com.tvd12.dahlia.core.setting.FieldObjectSetting)

Example 15 with EzyObject

use of com.tvd12.ezyfoxserver.client.entity.EzyObject in project dahlia by youngmonkeys.

the class SecondTest method main.

public static void main(String[] args) {
    deleteDataDir();
    LocalDahlia dahlia = new LocalDahlia("data");
    DatabaseSetting databaseSetting = new DatabaseSetting();
    databaseSetting.setDatabaseName("hello");
    IDatabase database = null;
    try {
        database = dahlia.createDatabase(databaseSetting);
    } catch (DatabaseExistedException e) {
        database = dahlia.getDatabase("hello");
    }
    ICollection collection = null;
    try {
        collection = database.createCollection("classpath:hello_test_setting2.json");
    } catch (CollectionExistedException e) {
        collection = database.getCollection("test");
    }
    EzyObject insertOneData = newObjectBuilder().append("value", 323L).append("name", "dungtv").build();
    try {
        EzyObject insertOneResult = collection.insert(insertOneData);
        System.out.println("insert one result: " + insertOneResult);
    } catch (DuplicatedIdException e) {
    } catch (Exception e) {
        e.printStackTrace();
    }
    long v1 = 1;
    double v2 = 1.1;
    System.out.println(v2 == v1);
    // EzyObject query1 = newObjectBuilder()
    // .append("_id", newObjectBuilder().append(Keywords.LESS_THAN_EQUAL, 3L))
    // .build();
    // CommandFindOne findOne = new CommandFindOne(collection.getId(), query1);
    // EzyObject findOneResult = commandExecutor.execute(findOne);
    // System.out.println("findOneResult: " + findOneResult);
    // EzyObject query2 = newObjectBuilder()
    // .append(Keywords.OR, newArrayBuilder()
    // .append(newObjectBuilder().append("_id", newObjectBuilder().append(Keywords.LESS_THAN_EQUAL, 3L)))
    // .append(newObjectBuilder().append("value", 223))
    // )
    // .build();
    EzyObject query3 = newObjectBuilder().append(Keywords.OR, newArrayBuilder().append(newObjectBuilder().append("value", 323L))).build();
    FindOptions options = new FindOptions().setSkip(0).setLimit(10);
    EzyArray findResult = collection.find(query3, options);
    System.out.println("findResult = " + findResult);
    Long size = collection.count();
    System.out.println("size: " + size);
}
Also used : FindOptions(com.tvd12.dahlia.query.FindOptions) IDatabase(com.tvd12.dahlia.IDatabase) CollectionExistedException(com.tvd12.dahlia.exception.CollectionExistedException) DatabaseExistedException(com.tvd12.dahlia.exception.DatabaseExistedException) CollectionExistedException(com.tvd12.dahlia.exception.CollectionExistedException) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException) DatabaseExistedException(com.tvd12.dahlia.exception.DatabaseExistedException) EzyArray(com.tvd12.ezyfox.entity.EzyArray) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) ICollection(com.tvd12.dahlia.ICollection) EzyObject(com.tvd12.ezyfox.entity.EzyObject) LocalDahlia(com.tvd12.dahlia.local.LocalDahlia) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException)

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