Search in sources :

Example 6 with Collection

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

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

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

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

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

the class SecondTest method main.

public static void main(String[] args) {
    deleteDataDir();
    LocalDahlia dahlia = new LocalDahlia("data");
    DatabaseSetting databaseSetting = new DatabaseSetting();
    databaseSetting.setDatabaseName("hello");
    IDatabase database = null;
    try {
        database = dahlia.createDatabase(databaseSetting);
    } catch (DatabaseExistedException e) {
        database = dahlia.getDatabase("hello");
    }
    ICollection collection = null;
    try {
        collection = database.createCollection("classpath:hello_test_setting2.json");
    } catch (CollectionExistedException e) {
        collection = database.getCollection("test");
    }
    EzyObject insertOneData = newObjectBuilder().append("value", 323L).append("name", "dungtv").build();
    try {
        EzyObject insertOneResult = collection.insert(insertOneData);
        System.out.println("insert one result: " + insertOneResult);
    } catch (DuplicatedIdException e) {
    } catch (Exception e) {
        e.printStackTrace();
    }
    long v1 = 1;
    double v2 = 1.1;
    System.out.println(v2 == v1);
    // EzyObject query1 = newObjectBuilder()
    // .append("_id", newObjectBuilder().append(Keywords.LESS_THAN_EQUAL, 3L))
    // .build();
    // CommandFindOne findOne = new CommandFindOne(collection.getId(), query1);
    // EzyObject findOneResult = commandExecutor.execute(findOne);
    // System.out.println("findOneResult: " + findOneResult);
    // EzyObject query2 = newObjectBuilder()
    // .append(Keywords.OR, newArrayBuilder()
    // .append(newObjectBuilder().append("_id", newObjectBuilder().append(Keywords.LESS_THAN_EQUAL, 3L)))
    // .append(newObjectBuilder().append("value", 223))
    // )
    // .build();
    EzyObject query3 = newObjectBuilder().append(Keywords.OR, newArrayBuilder().append(newObjectBuilder().append("value", 323L))).build();
    FindOptions options = new FindOptions().setSkip(0).setLimit(10);
    EzyArray findResult = collection.find(query3, options);
    System.out.println("findResult = " + findResult);
    Long size = collection.count();
    System.out.println("size: " + size);
}
Also used : FindOptions(com.tvd12.dahlia.query.FindOptions) IDatabase(com.tvd12.dahlia.IDatabase) CollectionExistedException(com.tvd12.dahlia.exception.CollectionExistedException) DatabaseExistedException(com.tvd12.dahlia.exception.DatabaseExistedException) CollectionExistedException(com.tvd12.dahlia.exception.CollectionExistedException) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException) DatabaseExistedException(com.tvd12.dahlia.exception.DatabaseExistedException) EzyArray(com.tvd12.ezyfox.entity.EzyArray) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) ICollection(com.tvd12.dahlia.ICollection) EzyObject(com.tvd12.ezyfox.entity.EzyObject) LocalDahlia(com.tvd12.dahlia.local.LocalDahlia) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException)

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