Search in sources :

Example 11 with CollectionStorage

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

the class DahliaCoreLoader method newStorage.

protected Storage newStorage(Map<String, DatabaseStorage> databaseStoreages, Map<String, List<CollectionStorage>> collectionStoragesMap, Map<String, DatabaseSetting> databaseSettings, Map<String, List<CollectionSetting>> collectionSettingsMap) {
    Storage storage = Storage.builder().directory(storageDirectory).objectSerializer(objectSerializer).objectDeserializer(objectDeserializer).build();
    for (String databaseName : databaseStoreages.keySet()) {
        DatabaseStorage databaseStorage = databaseStoreages.get(databaseName);
        DatabaseSetting databaseSetting = databaseSettings.get(databaseName);
        storage.addDatabaseStorage(databaseSetting.getDatabaseId(), databaseStorage);
    }
    for (String databaseName : databaseStoreages.keySet()) {
        DatabaseStorage databaseStorage = databaseStoreages.get(databaseName);
        List<CollectionSetting> collectionSettings = collectionSettingsMap.get(databaseName);
        List<CollectionStorage> collectionStorages = collectionStoragesMap.get(databaseName);
        for (int i = 0; i < collectionSettings.size(); ++i) {
            CollectionSetting collectionSetting = collectionSettings.get(i);
            CollectionStorage collectionStorage = collectionStorages.get(i);
            databaseStorage.addCollectionStorage(collectionSetting.getCollectionId(), collectionStorage);
            storage.addCollectionStorage(collectionSetting.getCollectionId(), collectionStorage);
        }
    }
    return storage;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) Storage(com.tvd12.dahlia.core.storage.Storage) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage)

Example 12 with CollectionStorage

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

the class DahliaCoreLoader method readCollectionSettings.

protected Map<String, List<CollectionSetting>> readCollectionSettings(Map<String, List<CollectionStorage>> storages) {
    Map<String, List<CollectionSetting>> settings = new HashMap<>();
    for (String databaseName : storages.keySet()) {
        List<CollectionSetting> collectionSettings = new ArrayList<>();
        List<CollectionStorage> collectionStorages = storages.get(databaseName);
        for (CollectionStorage storage : collectionStorages) {
            CollectionSetting setting = storage.readSetting();
            collectionSettings.add(setting);
        }
        settings.put(databaseName, collectionSettings);
    }
    return settings;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Example 13 with CollectionStorage

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

the class CommandCreateCollectionHandler method handle.

@Override
public Object handle(CommandCreateCollection command) {
    int databaseId = command.getDatabaseId();
    CollectionSetting setting = command.getSetting();
    String collectionName = setting.getCollectionName();
    Database database = databases.getDatabase(databaseId);
    if (database == null)
        throw new DatabaseNotFoundException(databaseId);
    Collection existedCollection = database.getCollection(collectionName);
    if (existedCollection != null)
        throw new CollectionExistedException(collectionName);
    int recordSize = recordSizeReader.read(setting.getAllFields());
    setting.setRecordSize(recordSize);
    Collection collection = collectionFactory.newCollection(setting);
    synchronized (runtimeSetting) {
        runtimeSetting.setMaxCollectionId(collection.getId());
        storage.storeRuntimeSetting(runtimeSetting);
    }
    database.addCollection(collection);
    databases.addCollection(collection);
    CollectionStorage collectionStorage = collectionStorageFactory.newCollectionStorage(collectionName, database.getName());
    storage.addCollectionStorage(collection.getId(), collectionStorage);
    collectionStorage.storeSetting(setting);
    return collection;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) DatabaseNotFoundException(com.tvd12.dahlia.exception.DatabaseNotFoundException) CollectionExistedException(com.tvd12.dahlia.exception.CollectionExistedException) Database(com.tvd12.dahlia.core.entity.Database) CommandCreateCollection(com.tvd12.dahlia.core.command.CommandCreateCollection) Collection(com.tvd12.dahlia.core.entity.Collection) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Example 14 with CollectionStorage

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

the class CommandSaveHandler method handle.

@Override
public Object handle(CommandSave command) {
    int collectionId = command.getCollectionId();
    EzyArray data = command.getData();
    Collection collection = databases.getCollection(collectionId);
    CollectionSetting setting = collection.getSetting();
    FieldSetting sId = setting.getId();
    Map<String, FieldSetting> sFields = setting.getFields();
    long dataSize = collection.getDataSize();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    EzyArray answerItems = EzyEntityFactory.newArray();
    synchronized (collection) {
        for (int i = 0; i < data.size(); ++i) {
            EzyObject answerItem = EzyEntityFactory.newObject();
            EzyObject item = data.get(i);
            Comparable id = item.get(Constants.FIELD_ID);
            if (id != null) {
                Record existed = collection.findById(id);
                if (existed != null)
                    answerItem.put(Constants.RESULT_FIELD_EXISTED, true);
            } else {
                while (true) {
                    id = UUID.randomUUID();
                    Record existed = collection.findById(id);
                    if (existed == null)
                        break;
                }
            }
            Record record = new Record(id, dataSize);
            collection.insert(record);
            collection.increaseDataSize();
            collectionStorage.storeRecord(record, sId, sFields, item);
            answerItem.put(Constants.FIELD_ID, id);
            answerItems.add(id);
        }
    }
    return answerItems;
}
Also used : 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) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Example 15 with CollectionStorage

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

the class CommandUpdateHandler method handle.

@Override
public Object handle(CommandUpdate 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 update = command.getUpdate();
    List<EzyPair<Record, EzyObject>> updateItems = 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)
                    updateItems.add(new EzyPair<>(r, value));
            }
        });
        for (EzyPair<Record, EzyObject> pair : updateItems) {
            Record record = pair.getKey();
            EzyObject updateItem = pair.getValue();
            updateItem(updateItem, update);
            collectionStorage.storeRecord(record, sId, sFields, updateItem);
        }
    }
    EzyArray answer = EzyEntityFactory.newArray();
    for (EzyPair<Record, EzyObject> pair : updateItems) {
        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)

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