Search in sources :

Example 6 with DatabaseSetting

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

the class DahliaCoreLoader method doLoad.

protected DahliaCore doLoad() {
    Map<String, DatabaseStorage> databaseStoreages = newDatabaseStorages();
    Set<String> databaseNames = databaseStoreages.keySet();
    Map<String, List<CollectionStorage>> collectionStoragesMap = newDatabaseColectionsStorages(databaseNames);
    Map<String, DatabaseSetting> databaseSettings = readDatabaseSettings(databaseStoreages);
    Map<String, List<CollectionSetting>> collectionSettingsMap = readCollectionSettings(collectionStoragesMap);
    Storage storage = newStorage(databaseStoreages, collectionStoragesMap, databaseSettings, collectionSettingsMap);
    RuntimeSetting runtimeSetting = storage.readRuntimeSetting();
    DatabaseFactory databaseFactory = new DatabaseFactory(runtimeSetting.getMaxDatabaseId());
    CollectionFactory collectionFactory = new CollectionFactory(runtimeSetting.getMaxCollectionId());
    Databases databases = newDatabases(databaseFactory, collectionFactory, databaseSettings, collectionSettingsMap);
    loadAllCollections(databases, storage);
    DahliaCore dahlia = DahliaCore.builder().storage(storage).databases(databases).runtimeSetting(runtimeSetting).databaseFactory(databaseFactory).collectionFactory(collectionFactory).databaseStorageFactory(databaseStorageFactory).collectionStorageFactory(collectionStorageFactory).build();
    return dahlia;
}
Also used : CollectionFactory(com.tvd12.dahlia.core.factory.CollectionFactory) RuntimeSetting(com.tvd12.dahlia.core.setting.RuntimeSetting) Storage(com.tvd12.dahlia.core.storage.Storage) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) Databases(com.tvd12.dahlia.core.entity.Databases) DatabaseFactory(com.tvd12.dahlia.core.factory.DatabaseFactory) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) ArrayList(java.util.ArrayList) List(java.util.List) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage)

Example 7 with DatabaseSetting

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

the class DahliaCoreLoader method newStorage.

protected Storage newStorage(Map<String, DatabaseStorage> databaseStoreages, Map<String, List<CollectionStorage>> collectionStoragesMap, Map<String, DatabaseSetting> databaseSettings, Map<String, List<CollectionSetting>> collectionSettingsMap) {
    Storage storage = Storage.builder().directory(storageDirectory).objectSerializer(objectSerializer).objectDeserializer(objectDeserializer).build();
    for (String databaseName : databaseStoreages.keySet()) {
        DatabaseStorage databaseStorage = databaseStoreages.get(databaseName);
        DatabaseSetting databaseSetting = databaseSettings.get(databaseName);
        storage.addDatabaseStorage(databaseSetting.getDatabaseId(), databaseStorage);
    }
    for (String databaseName : databaseStoreages.keySet()) {
        DatabaseStorage databaseStorage = databaseStoreages.get(databaseName);
        List<CollectionSetting> collectionSettings = collectionSettingsMap.get(databaseName);
        List<CollectionStorage> collectionStorages = collectionStoragesMap.get(databaseName);
        for (int i = 0; i < collectionSettings.size(); ++i) {
            CollectionSetting collectionSetting = collectionSettings.get(i);
            CollectionStorage collectionStorage = collectionStorages.get(i);
            databaseStorage.addCollectionStorage(collectionSetting.getCollectionId(), collectionStorage);
            storage.addCollectionStorage(collectionSetting.getCollectionId(), collectionStorage);
        }
    }
    return storage;
}
Also used : CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) Storage(com.tvd12.dahlia.core.storage.Storage) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) DatabaseSetting(com.tvd12.dahlia.core.setting.DatabaseSetting) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) DatabaseStorage(com.tvd12.dahlia.core.storage.DatabaseStorage)

Example 8 with DatabaseSetting

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

the class DatabaseFactory method newDatabase.

public Database newDatabase(DatabaseSetting setting) {
    int databaseId = maxDatabaseId.incrementAndGet();
    setting.setDatabaseId(databaseId);
    Database database = new Database(setting);
    return database;
}
Also used : Database(com.tvd12.dahlia.core.entity.Database)

Example 9 with DatabaseSetting

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

the class SettingDatabaseSerializer method serialize.

@Override
public byte[] serialize(DatabaseSetting setting) {
    EzyObject object = databaseToObject(setting);
    byte[] bytes = objectSerializer.serialize(object);
    return bytes;
}
Also used : EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Example 10 with DatabaseSetting

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

Aggregations

DatabaseSetting (com.tvd12.dahlia.core.setting.DatabaseSetting)13 Database (com.tvd12.dahlia.core.entity.Database)6 EzyObject (com.tvd12.ezyfox.entity.EzyObject)6 DatabaseExistedException (com.tvd12.dahlia.exception.DatabaseExistedException)5 CommandCreateDatabase (com.tvd12.dahlia.core.command.CommandCreateDatabase)4 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)4 DatabaseStorage (com.tvd12.dahlia.core.storage.DatabaseStorage)4 CollectionExistedException (com.tvd12.dahlia.exception.CollectionExistedException)4 DuplicatedIdException (com.tvd12.dahlia.exception.DuplicatedIdException)4 FindOptions (com.tvd12.dahlia.query.FindOptions)4 EzyArray (com.tvd12.ezyfox.entity.EzyArray)4 IDatabase (com.tvd12.dahlia.IDatabase)3 Collection (com.tvd12.dahlia.core.entity.Collection)3 HashMap (java.util.HashMap)3 ICollection (com.tvd12.dahlia.ICollection)2 DahliaCore (com.tvd12.dahlia.core.DahliaCore)2 DahliaCoreLoader (com.tvd12.dahlia.core.DahliaCoreLoader)2 CommandCount (com.tvd12.dahlia.core.command.CommandCount)2 CommandCreateCollection (com.tvd12.dahlia.core.command.CommandCreateCollection)2 CommandExecutor (com.tvd12.dahlia.core.command.CommandExecutor)2