use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.
the class CommandFindHandler method handle.
@Override
public Object handle(CommandFind 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);
CollectionSetting setting = collection.getSetting();
CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
FieldSetting sId = setting.getId();
Map<String, FieldSetting> sFields = setting.getFields();
EzyObject options = command.getOptions();
int skip = options.get(OptionFields.SKIP, int.class, 0);
int limit = options.get(OptionFields.LIMIT, int.class, 25);
EzyObject sortBy = options.get(OptionFields.SORT, EzyObject.class, EMPTY_OBJECT);
if (sortBy.isEmpty()) {
EzyArray answer = EzyEntityFactory.newArray();
EzyWrap<Integer> count = new EzyWrap<>(0);
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) {
int currentCount = count.getValue();
if (currentCount >= skip)
answer.add(value);
count.setValue(currentCount + 1);
}
}
@Override
public boolean next() {
int currentSize = answer.size();
return currentSize < limit;
}
});
}
return answer;
} else {
List<EzyObject> found = new ArrayList<>();
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)
found.add(value);
}
});
}
found.sort(sortByComparator(sortBy));
return getResult(found, skip, limit);
}
}
use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.
the class CommandFindOneHandler method handle.
@Override
public Object handle(CommandFindOne 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);
CollectionSetting setting = collection.getSetting();
CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
FieldSetting sId = setting.getId();
Map<String, FieldSetting> sFields = setting.getFields();
EzyWrap<EzyObject> ref = new EzyWrap<>();
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)
ref.setValue(value);
}
@Override
public boolean next() {
return ref.hasNoValue();
}
});
}
return ref.getValue();
}
use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.
the class DahliaCoreLoader method newColectionsStorages.
protected List<CollectionStorage> newColectionsStorages(String databaseName) {
List<CollectionStorage> collectionStorages = new ArrayList<>();
File databaseDirectory = Paths.get(storageDirectory, DIRECTORY_DATABASES, databaseName).toFile();
for (File collectionDirectory : databaseDirectory.listFiles()) {
if (collectionDirectory.isFile())
continue;
String collectionName = collectionDirectory.getName();
CollectionStorage storage = newCollectionStorage(databaseName, collectionName);
collectionStorages.add(storage);
}
return collectionStorages;
}
use of com.tvd12.dahlia.core.storage.CollectionStorage in project dahlia by youngmonkeys.
the class DahliaCoreLoader method loadCollections.
protected void loadCollections(Database database, DatabaseStorage storage) {
for (Collection collection : database.getCollectionList()) {
CollectionStorage collectionStorage = storage.getCollectionStorage(collection.getId());
loadCollection(collection, collectionStorage);
}
}
use of com.tvd12.dahlia.core.storage.CollectionStorage 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;
}
Aggregations