Search in sources :

Example 16 with Collection

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

the class CollectionFactory method createCollection.

public Collection createCollection(CollectionSetting setting) {
    Collection collection = new Collection(setting);
    Map<String, IndexSetting> indexes = setting.getIndexes();
    for (String indexName : indexes.keySet()) {
        IndexSetting indexSetting = indexes.get(indexName);
        Index index = new Index(indexSetting);
        collection.addIndex(index);
    }
    return collection;
}
Also used : Collection(com.tvd12.dahlia.core.entity.Collection) Index(com.tvd12.dahlia.core.entity.Index) IndexSetting(com.tvd12.dahlia.core.setting.IndexSetting)

Example 17 with Collection

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

the class CollectionFactory method newCollection.

public Collection newCollection(CollectionSetting setting) {
    int collectionId = maxCollectionId.incrementAndGet();
    setting.setCollectionId(collectionId);
    Collection collection = createCollection(setting);
    return collection;
}
Also used : Collection(com.tvd12.dahlia.core.entity.Collection)

Example 18 with Collection

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

the class DatabaseTest2 method main.

public static void main(String[] args) {
    deleteDataDir();
    DahliaCoreLoader loader = new DahliaCoreLoader().storageDirectory("data");
    DahliaCore dahlia = loader.load();
    CommandExecutor commandExecutor = dahlia.getCommandExecutor();
    DatabaseSetting databaseSetting = new DatabaseSetting();
    databaseSetting.setDatabaseName("hello");
    CommandCreateDatabase commandCreateDatabase = new CommandCreateDatabase(databaseSetting);
    Database database = null;
    try {
        database = commandExecutor.execute(commandCreateDatabase);
    } catch (DatabaseExistedException e) {
        database = dahlia.getDatabases().getDatabase("hello");
    }
    CollectionSetting collectionSetting = new CollectionSetting();
    collectionSetting.setCollectionId(1);
    collectionSetting.setCollectionName("test2");
    Map<String, FieldSetting> fieldSettings = new HashMap<>();
    FieldUuidSetting fieldIdSetting = new FieldUuidSetting();
    fieldIdSetting.setNullable(true);
    fieldSettings.put("_id", fieldIdSetting);
    FieldLongSetting fieldValueSetting = new FieldLongSetting();
    fieldValueSetting.setNullable(true);
    fieldValueSetting.setDefaultValue(300L);
    fieldSettings.put("value", fieldValueSetting);
    FieldTextSetting fieldNameSetting = new FieldTextSetting();
    fieldNameSetting.setNullable(false);
    fieldSettings.put("name", fieldNameSetting);
    collectionSetting.setFields(fieldSettings);
    IndexSetting nameIndexSetting = new IndexSetting("nameIndex", EzyMaps.newHashMap("name", true));
    collectionSetting.setIndexes(Lists.newArrayList(nameIndexSetting));
    System.out.println(collectionSetting.toMap());
    CommandCreateCollection commandCreateCollection = new CommandCreateCollection(database.getId(), collectionSetting);
    Collection collection = null;
    try {
        collection = commandExecutor.execute(commandCreateCollection);
    } catch (CollectionExistedException e) {
        collection = database.getCollection("test2");
    }
    EzyObject insertOneData = newObjectBuilder().append("value", 323L).append("name", "dungtv").build();
    CommandInsertOne commandInsertOne = new CommandInsertOne(collection.getId(), insertOneData);
    try {
        EzyObject insertOneResult = commandExecutor.execute(commandInsertOne);
        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", 323))).build();
    FindOptions options = new FindOptions().setSkip(0).setLimit(10);
    CommandFind commandFind = new CommandFind(collection.getId(), query3, options.toObject());
    EzyArray findResult = commandExecutor.execute(commandFind);
    System.out.println("findResult = " + findResult);
    Long size = dahlia.execute(new CommandCount(collection.getId()));
    System.out.println("size: " + size);
}
Also used : FindOptions(com.tvd12.dahlia.query.FindOptions) HashMap(java.util.HashMap) CollectionExistedException(com.tvd12.dahlia.exception.CollectionExistedException) CommandExecutor(com.tvd12.dahlia.core.command.CommandExecutor) CommandFind(com.tvd12.dahlia.core.command.CommandFind) FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CommandInsertOne(com.tvd12.dahlia.core.command.CommandInsertOne) DahliaCore(com.tvd12.dahlia.core.DahliaCore) CommandCreateDatabase(com.tvd12.dahlia.core.command.CommandCreateDatabase) Database(com.tvd12.dahlia.core.entity.Database) FieldLongSetting(com.tvd12.dahlia.core.setting.FieldLongSetting) EzyArray(com.tvd12.ezyfox.entity.EzyArray) EzyObject(com.tvd12.ezyfox.entity.EzyObject) CommandCreateCollection(com.tvd12.dahlia.core.command.CommandCreateCollection) CommandCreateDatabase(com.tvd12.dahlia.core.command.CommandCreateDatabase) DatabaseExistedException(com.tvd12.dahlia.exception.DatabaseExistedException) DahliaCoreLoader(com.tvd12.dahlia.core.DahliaCoreLoader) FieldTextSetting(com.tvd12.dahlia.core.setting.FieldTextSetting) CollectionExistedException(com.tvd12.dahlia.exception.CollectionExistedException) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException) DatabaseExistedException(com.tvd12.dahlia.exception.DatabaseExistedException) FieldUuidSetting(com.tvd12.dahlia.core.setting.FieldUuidSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) CommandCreateCollection(com.tvd12.dahlia.core.command.CommandCreateCollection) Collection(com.tvd12.dahlia.core.entity.Collection) CommandCount(com.tvd12.dahlia.core.command.CommandCount) DuplicatedIdException(com.tvd12.dahlia.exception.DuplicatedIdException) IndexSetting(com.tvd12.dahlia.core.setting.IndexSetting)

Example 19 with Collection

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

the class LocalCollectionSettingReader method readJsonSetting.

public CollectionSetting readJsonSetting(String json) {
    JSONObject rawSetting = new JSONObject(json);
    CollectionSetting setting = new CollectionSetting();
    String collectionName = null;
    if (rawSetting.has(SettingFields.NAME))
        collectionName = rawSetting.getString(SettingFields.NAME);
    else
        throw new IllegalArgumentException("collection 'name' is required");
    setting.setCollectionName(collectionName);
    JSONObject fieldSettings = null;
    if (rawSetting.has(SettingFields.FIELDS))
        fieldSettings = rawSetting.getJSONObject(SettingFields.FIELDS);
    else
        throw new IllegalArgumentException("'fields' is required");
    setting.setFields(readFieldSettings(fieldSettings));
    if (rawSetting.has(SettingFields.INDEXES)) {
        JSONObject indexSettings = rawSetting.getJSONObject(SettingFields.INDEXES);
        setting.setIndexes(readIndexSettings(indexSettings));
    }
    System.out.println("settings: " + setting.toMap());
    return setting;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) JSONObject(org.json.JSONObject)

Example 20 with Collection

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

the class FirstTest 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_setting.json");
    } catch (CollectionExistedException e) {
        collection = database.getCollection("test");
    }
    EzyObject insertOneData1 = newObjectBuilder().append(SettingFields.ID, 2).append("value", 323L).append("name", "dungtv").build();
    try {
        EzyObject insertOneResult = collection.insert(insertOneData1);
        System.out.println("insert one result 1: " + insertOneResult);
    } catch (DuplicatedIdException e) {
    } catch (Exception e) {
        e.printStackTrace();
    }
    EzyObject insertOneData2 = newObjectBuilder().append(SettingFields.ID, 3).append("value", 325L).append("name", "dungtv").build();
    try {
        EzyObject insertOneResult = collection.insert(insertOneData2);
        System.out.println("insert one result 2: " + insertOneResult);
    } catch (DuplicatedIdException e) {
    } catch (Exception e) {
        e.printStackTrace();
    }
    EzyObject insertOneData3 = newObjectBuilder().append(SettingFields.ID, 4).append("value", 321L).append("name", "dungtv").build();
    try {
        EzyObject insertOneResult = collection.insert(insertOneData3);
        System.out.println("insert one result 3: " + 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(Keywords.LESS_THAN_EQUAL, newObjectBuilder().append("_id", 100L))).append(newObjectBuilder().append("value", 323L))).build();
    FindOptions options = new FindOptions().setSkip(0).setLimit(10).sortBy("value");
    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