Search in sources :

Example 6 with CollectionSetting

use of com.tvd12.dahlia.core.setting.CollectionSetting 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 7 with CollectionSetting

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

use of com.tvd12.dahlia.core.setting.CollectionSetting 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 9 with CollectionSetting

use of com.tvd12.dahlia.core.setting.CollectionSetting 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)

Example 10 with CollectionSetting

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

the class SettingCollectionSerializer method serialize.

@Override
public byte[] serialize(CollectionSetting setting) {
    EzyObject object = collectionToObject(setting);
    byte[] bytes = objectSerializer.serialize(object);
    return bytes;
}
Also used : EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)22 Collection (com.tvd12.dahlia.core.entity.Collection)16 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)13 EzyObject (com.tvd12.ezyfox.entity.EzyObject)13 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)11 Record (com.tvd12.dahlia.core.entity.Record)10 EzyArray (com.tvd12.ezyfox.entity.EzyArray)8 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)5 DatabaseSetting (com.tvd12.dahlia.core.setting.DatabaseSetting)5 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)5 ArrayList (java.util.ArrayList)5 CommandCreateCollection (com.tvd12.dahlia.core.command.CommandCreateCollection)4 Database (com.tvd12.dahlia.core.entity.Database)4 CollectionExistedException (com.tvd12.dahlia.exception.CollectionExistedException)3 DuplicatedIdException (com.tvd12.dahlia.exception.DuplicatedIdException)3 HashMap (java.util.HashMap)3 DahliaCore (com.tvd12.dahlia.core.DahliaCore)2 DahliaCoreLoader (com.tvd12.dahlia.core.DahliaCoreLoader)2 CommandCount (com.tvd12.dahlia.core.command.CommandCount)2 CommandCreateDatabase (com.tvd12.dahlia.core.command.CommandCreateDatabase)2