Search in sources :

Example 21 with CollectionSetting

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

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

Example 23 with CollectionSetting

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

the class DahliaCoreLoader method newDatabases.

protected Databases newDatabases(DatabaseFactory databaseFactory, CollectionFactory collectionFactory, Map<String, DatabaseSetting> databaseSettings, Map<String, List<CollectionSetting>> collectionSettingsMap) {
    Databases databases = new Databases();
    for (String databaseName : databaseSettings.keySet()) {
        DatabaseSetting databaseSetting = databaseSettings.get(databaseName);
        Database database = databaseFactory.createDatabase(databaseSetting);
        List<CollectionSetting> collectionSettings = collectionSettingsMap.get(databaseName);
        for (CollectionSetting collectionSetting : collectionSettings) {
            Collection collection = collectionFactory.createCollection(collectionSetting);
            database.addCollection(collection);
            databases.addCollection(collection);
        }
        databases.addDatabase(database);
    }
    return databases;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) Databases(com.tvd12.dahlia.core.entity.Databases) Database(com.tvd12.dahlia.core.entity.Database) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) Collection(com.tvd12.dahlia.core.entity.Collection)

Example 24 with CollectionSetting

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

the class DahliaCoreLoader method loadCollection.

protected void loadCollection(Collection collection, CollectionStorage storage) {
    CollectionSetting setting = collection.getSetting();
    FieldSetting idSetting = setting.getId();
    long recordPosition = collection.getDataSize();
    while (storage.hasMoreRecords(recordPosition)) {
        Record record = storage.readRecord(recordPosition, idSetting);
        if (record != null)
            collection.insert(record);
        recordPosition = collection.increaseDataSize();
    }
}
Also used : FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) Record(com.tvd12.dahlia.core.entity.Record)

Example 25 with CollectionSetting

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

the class CollectionSettingStorage method read.

public CollectionSetting read() {
    byte[] bytes = FileReaders.readBytes(filePath);
    CollectionSetting setting = settingDeserializer.deserialize(bytes);
    return setting;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting)

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