Search in sources :

Example 11 with Collection

use of com.tvd12.dahlia.core.entity.Collection 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 12 with Collection

use of com.tvd12.dahlia.core.entity.Collection 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 13 with Collection

use of com.tvd12.dahlia.core.entity.Collection 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 14 with Collection

use of com.tvd12.dahlia.core.entity.Collection 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 15 with Collection

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

the class DahliaCoreLoader method loadCollections.

protected void loadCollections(Database database, DatabaseStorage storage) {
    for (Collection collection : database.getCollectionList()) {
        CollectionStorage collectionStorage = storage.getCollectionStorage(collection.getId());
        loadCollection(collection, collectionStorage);
    }
}
Also used : Collection(com.tvd12.dahlia.core.entity.Collection) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Aggregations

Collection (com.tvd12.dahlia.core.entity.Collection)17 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)16 EzyObject (com.tvd12.ezyfox.entity.EzyObject)13 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)11 Record (com.tvd12.dahlia.core.entity.Record)10 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)10 EzyArray (com.tvd12.ezyfox.entity.EzyArray)10 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)5 DatabaseSetting (com.tvd12.dahlia.core.setting.DatabaseSetting)5 CollectionExistedException (com.tvd12.dahlia.exception.CollectionExistedException)5 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)5 DuplicatedIdException (com.tvd12.dahlia.exception.DuplicatedIdException)5 CommandCreateCollection (com.tvd12.dahlia.core.command.CommandCreateCollection)4 Database (com.tvd12.dahlia.core.entity.Database)4 DatabaseExistedException (com.tvd12.dahlia.exception.DatabaseExistedException)4 FindOptions (com.tvd12.dahlia.query.FindOptions)4 EzyConstant (com.tvd12.ezyfox.constant.EzyConstant)4 EzySession (com.tvd12.ezyfoxserver.entity.EzySession)4 ArrayList (java.util.ArrayList)4 ICollection (com.tvd12.dahlia.ICollection)3