Search in sources :

Example 1 with CollectionSetting

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

the class LocalDatabase method createCollection.

@Override
public ICollection createCollection(Object setting) {
    CollectionSetting s = null;
    if (setting instanceof CollectionSetting) {
        s = (CollectionSetting) setting;
    } else if (setting instanceof File) {
        s = settingReader.readFileSetting((File) setting);
    } else if (setting instanceof InputStream) {
        s = settingReader.readInputStreamSetting((InputStream) setting);
    } else {
        String ss = (String) setting;
        if (ss.startsWith(PREFIX_FILE))
            s = settingReader.readFileSetting(ss.substring(PREFIX_FILE.length()));
        else if (ss.startsWith(PREFIX_CLASSPATH))
            s = settingReader.readFileSetting(ss.substring(PREFIX_CLASSPATH.length()));
        else
            s = settingReader.readJsonSetting(ss);
    }
    CommandCreateCollection command = new CommandCreateCollection(store.getId(), s);
    Collection collectionStore = commandExecutor.execute(command);
    LocalCollection collection = new LocalCollection(collectionStore, commandExecutor);
    collections.put(collectionStore.getName(), collection);
    return collection;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) InputStream(java.io.InputStream) CommandCreateCollection(com.tvd12.dahlia.core.command.CommandCreateCollection) CommandCreateCollection(com.tvd12.dahlia.core.command.CommandCreateCollection) Collection(com.tvd12.dahlia.core.entity.Collection) ICollection(com.tvd12.dahlia.ICollection) File(java.io.File)

Example 2 with CollectionSetting

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

the class CommandInsertHandler method handle.

@Override
public Object handle(CommandInsert 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);
                    answerItems.add(id);
                    continue;
                }
            } 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 3 with CollectionSetting

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

the class CommandInsertOneHandler method handle.

@Override
public Object handle(CommandInsertOne command) {
    int collectionId = command.getCollectionId();
    EzyObject data = command.getData();
    Collection collection = databases.getCollection(collectionId);
    CollectionSetting setting = collection.getSetting();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    Comparable id = data.get(Constants.FIELD_ID);
    synchronized (collection) {
        if (id != null) {
            Record existed = collection.findById(id);
            if (existed != null)
                throw new DuplicatedIdException(collection.getName(), id);
        } else {
            while (true) {
                id = UUID.randomUUID();
                Record existed = collection.findById(id);
                if (existed == null)
                    break;
            }
            data.put(Constants.FIELD_ID, id);
        }
        Record record = new Record(id, collection.getDataSize());
        collection.insert(record);
        collection.increaseDataSize();
        collectionStorage.storeRecord(record, setting.getId(), setting.getFields(), data);
    }
    EzyObject answer = EzyEntityFactory.newObject();
    answer.put(Constants.FIELD_ID, id);
    return answer;
}
Also used : 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) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException)

Example 4 with CollectionSetting

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

the class CommandSaveOneHandler method handle.

@Override
public Object handle(CommandSaveOne command) {
    int collectionId = command.getCollectionId();
    EzyObject data = command.getData();
    Collection collection = databases.getCollection(collectionId);
    CollectionSetting setting = collection.getSetting();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    Comparable id = data.get(Constants.FIELD_ID);
    EzyObject answer = EzyEntityFactory.newObject();
    synchronized (collection) {
        if (id != null) {
            Record existed = collection.findById(id);
            if (existed != null)
                answer.put(Constants.RESULT_FIELD_EXISTED, true);
        } else {
            while (true) {
                id = UUID.randomUUID();
                Record existed = collection.findById(id);
                if (existed == null)
                    break;
            }
            data.put(Constants.FIELD_ID, id);
        }
        Record record = new Record(id, collection.getDataSize());
        collection.insert(record);
        collection.increaseDataSize();
        collectionStorage.storeRecord(record, setting.getId(), setting.getFields(), data);
    }
    answer.put(Constants.FIELD_ID, id);
    return answer;
}
Also used : 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) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage)

Example 5 with CollectionSetting

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

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