Search in sources :

Example 6 with FieldSetting

use of com.tvd12.dahlia.core.setting.FieldSetting 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();
}
Also used : 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 7 with FieldSetting

use of com.tvd12.dahlia.core.setting.FieldSetting 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;
}
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 8 with FieldSetting

use of com.tvd12.dahlia.core.setting.FieldSetting in project dahlia by youngmonkeys.

the class CommandFindHandler method handle.

@Override
public Object handle(CommandFind 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 options = command.getOptions();
    int skip = options.get(OptionFields.SKIP, int.class, 0);
    int limit = options.get(OptionFields.LIMIT, int.class, 25);
    EzyObject sortBy = options.get(OptionFields.SORT, EzyObject.class, EMPTY_OBJECT);
    if (sortBy.isEmpty()) {
        EzyArray answer = EzyEntityFactory.newArray();
        EzyWrap<Integer> count = new EzyWrap<>(0);
        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) {
                        int currentCount = count.getValue();
                        if (currentCount >= skip)
                            answer.add(value);
                        count.setValue(currentCount + 1);
                    }
                }

                @Override
                public boolean next() {
                    int currentSize = answer.size();
                    return currentSize < limit;
                }
            });
        }
        return answer;
    } else {
        List<EzyObject> found = 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)
                        found.add(value);
                }
            });
        }
        found.sort(sortByComparator(sortBy));
        return getResult(found, skip, limit);
    }
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) 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) 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 9 with FieldSetting

use of com.tvd12.dahlia.core.setting.FieldSetting in project dahlia by youngmonkeys.

the class CommandFindOneHandler method handle.

@Override
public Object handle(CommandFindOne 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();
    EzyWrap<EzyObject> ref = new EzyWrap<>();
    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)
                    ref.setValue(value);
            }

            @Override
            public boolean next() {
                return ref.hasNoValue();
            }
        });
    }
    return ref.getValue();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) 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) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 10 with FieldSetting

use of com.tvd12.dahlia.core.setting.FieldSetting in project dahlia by youngmonkeys.

the class SettingCollectionDeserializer method deserialize.

@Override
public CollectionSetting deserialize(byte[] bytes) {
    EzyObject object = objectDeserializer.deserialize(bytes);
    CollectionSetting setting = new CollectionSetting();
    setting.setCollectionId(object.get(SettingFields.ID, int.class));
    setting.setRecordSize(object.get(SettingFields.RECORD_SIZE, int.class));
    EzyArray fieldArray = object.get(SettingFields.FIELDS);
    Map<String, FieldSetting> fields = arrayToFields(fieldArray);
    setting.setFields(fields);
    return setting;
}
Also used : FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)25 EzyObject (com.tvd12.ezyfox.entity.EzyObject)20 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)11 EzyArray (com.tvd12.ezyfox.entity.EzyArray)10 Collection (com.tvd12.dahlia.core.entity.Collection)9 Record (com.tvd12.dahlia.core.entity.Record)9 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)7 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)5 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)5 FieldArraySetting (com.tvd12.dahlia.core.setting.FieldArraySetting)4 FieldObjectSetting (com.tvd12.dahlia.core.setting.FieldObjectSetting)4 HashMap (java.util.HashMap)4 DataType (com.tvd12.dahlia.core.data.DataType)3 ArrayList (java.util.ArrayList)3 JSONObject (org.json.JSONObject)3 DahliaCore (com.tvd12.dahlia.core.DahliaCore)2 DahliaCoreLoader (com.tvd12.dahlia.core.DahliaCoreLoader)2 CommandCount (com.tvd12.dahlia.core.command.CommandCount)2 CommandCreateCollection (com.tvd12.dahlia.core.command.CommandCreateCollection)2 CommandCreateDatabase (com.tvd12.dahlia.core.command.CommandCreateDatabase)2