Search in sources :

Example 6 with CollectionStorage

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

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

use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.

the class DahliaCoreLoader method newColectionsStorages.

protected List<CollectionStorage> newColectionsStorages(String databaseName) {
    List<CollectionStorage> collectionStorages = new ArrayList<>();
    File databaseDirectory = Paths.get(storageDirectory, DIRECTORY_DATABASES, databaseName).toFile();
    for (File collectionDirectory : databaseDirectory.listFiles()) {
        if (collectionDirectory.isFile())
            continue;
        String collectionName = collectionDirectory.getName();
        CollectionStorage storage = newCollectionStorage(databaseName, collectionName);
        collectionStorages.add(storage);
    }
    return collectionStorages;
}
Also used : ArrayList(java.util.ArrayList) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) File(java.io.File)

Example 9 with CollectionStorage

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

Example 10 with CollectionStorage

use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.

the class DahliaCoreLoader method doLoad.

protected DahliaCore doLoad() {
    Map<String, DatabaseStorage> databaseStoreages = newDatabaseStorages();
    Set<String> databaseNames = databaseStoreages.keySet();
    Map<String, List<CollectionStorage>> collectionStoragesMap = newDatabaseColectionsStorages(databaseNames);
    Map<String, DatabaseSetting> databaseSettings = readDatabaseSettings(databaseStoreages);
    Map<String, List<CollectionSetting>> collectionSettingsMap = readCollectionSettings(collectionStoragesMap);
    Storage storage = newStorage(databaseStoreages, collectionStoragesMap, databaseSettings, collectionSettingsMap);
    RuntimeSetting runtimeSetting = storage.readRuntimeSetting();
    DatabaseFactory databaseFactory = new DatabaseFactory(runtimeSetting.getMaxDatabaseId());
    CollectionFactory collectionFactory = new CollectionFactory(runtimeSetting.getMaxCollectionId());
    Databases databases = newDatabases(databaseFactory, collectionFactory, databaseSettings, collectionSettingsMap);
    loadAllCollections(databases, storage);
    DahliaCore dahlia = DahliaCore.builder().storage(storage).databases(databases).runtimeSetting(runtimeSetting).databaseFactory(databaseFactory).collectionFactory(collectionFactory).databaseStorageFactory(databaseStorageFactory).collectionStorageFactory(collectionStorageFactory).build();
    return dahlia;
}
Also used : CollectionFactory(com.tvd12.dahlia.core.factory.CollectionFactory) RuntimeSetting(com.tvd12.dahlia.core.setting.RuntimeSetting) Storage(com.tvd12.dahlia.core.storage.Storage) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) Databases(com.tvd12.dahlia.core.entity.Databases) DatabaseFactory(com.tvd12.dahlia.core.factory.DatabaseFactory) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage)

Aggregations

CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)15 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)13 Collection (com.tvd12.dahlia.core.entity.Collection)11 Record (com.tvd12.dahlia.core.entity.Record)10 EzyObject (com.tvd12.ezyfox.entity.EzyObject)9 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)8 ArrayList (java.util.ArrayList)6 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)5 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)5 EzyArray (com.tvd12.ezyfox.entity.EzyArray)5 DatabaseSetting (com.tvd12.dahlia.core.setting.DatabaseSetting)2 DatabaseStorage (com.tvd12.dahlia.core.storage.DatabaseStorage)2 Storage (com.tvd12.dahlia.core.storage.Storage)2 EzyPair (com.tvd12.ezyfox.util.EzyPair)2 EzyWrap (com.tvd12.ezyfox.util.EzyWrap)2 List (java.util.List)2 CommandCreateCollection (com.tvd12.dahlia.core.command.CommandCreateCollection)1 Database (com.tvd12.dahlia.core.entity.Database)1 Databases (com.tvd12.dahlia.core.entity.Databases)1 CollectionFactory (com.tvd12.dahlia.core.factory.CollectionFactory)1