Search in sources :

Example 1 with CommandCreateCollection

use of com.tvd12.dahlia.core.command.CommandCreateCollection 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 CommandCreateCollection

use of com.tvd12.dahlia.core.command.CommandCreateCollection 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 3 with CommandCreateCollection

use of com.tvd12.dahlia.core.command.CommandCreateCollection in project dahlia by youngmonkeys.

the class DatabaseTest 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("test");
    Map<String, FieldSetting> fieldSettings = new HashMap<>();
    FieldLongSetting fieldIdSetting = new FieldLongSetting();
    fieldIdSetting.setNullable(true);
    fieldIdSetting.setDefaultValue(100L);
    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);
    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("test");
    }
    EzyObject insertOneData = newObjectBuilder().append("_id", 2L).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(Keywords.LESS_THAN_EQUAL, newObjectBuilder().append("_id", 3L))).append(newObjectBuilder().append("value", 223))).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) 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)

Example 4 with CommandCreateCollection

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

Aggregations

CommandCreateCollection (com.tvd12.dahlia.core.command.CommandCreateCollection)4 Collection (com.tvd12.dahlia.core.entity.Collection)4 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)4 Database (com.tvd12.dahlia.core.entity.Database)3 CollectionExistedException (com.tvd12.dahlia.exception.CollectionExistedException)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 CommandExecutor (com.tvd12.dahlia.core.command.CommandExecutor)2 CommandFind (com.tvd12.dahlia.core.command.CommandFind)2 CommandInsertOne (com.tvd12.dahlia.core.command.CommandInsertOne)2 DatabaseSetting (com.tvd12.dahlia.core.setting.DatabaseSetting)2 FieldLongSetting (com.tvd12.dahlia.core.setting.FieldLongSetting)2 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)2 FieldTextSetting (com.tvd12.dahlia.core.setting.FieldTextSetting)2 DatabaseExistedException (com.tvd12.dahlia.exception.DatabaseExistedException)2 DuplicatedIdException (com.tvd12.dahlia.exception.DuplicatedIdException)2 FindOptions (com.tvd12.dahlia.query.FindOptions)2 EzyArray (com.tvd12.ezyfox.entity.EzyArray)2