use of com.tvd12.ezyfox.entity.EzyArray in project dahlia by youngmonkeys.
the class LocalCollection method save.
@Override
public EzyArray save(EzyArray records) {
CommandSave command = new CommandSave(store.getId(), records);
EzyArray result = commandExecutor.execute(command);
return result;
}
use of com.tvd12.ezyfox.entity.EzyArray 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;
}
use of com.tvd12.ezyfox.entity.EzyArray 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);
}
use of com.tvd12.ezyfox.entity.EzyArray in project dahlia by youngmonkeys.
the class CommandDeleteHandler method handle.
@Override
public Object handle(CommandDelete 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();
List<EzyPair<Record, EzyObject>> deletedItems = 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)
deletedItems.add(new EzyPair<>(r, value));
}
});
for (EzyPair<Record, EzyObject> pair : deletedItems) {
Record deletedRecord = pair.getKey();
EzyObject deletedValue = pair.getValue();
Comparable id = deletedRecord.getId();
collection.remove(id);
deletedRecord.setAlive(false);
collectionStorage.storeRecord(deletedRecord, sId, sFields, deletedValue);
}
}
EzyArray answer = EzyEntityFactory.newArray();
for (EzyPair<Record, EzyObject> pair : deletedItems) {
EzyObject answerItem = EzyEntityFactory.newObject();
EzyObject updateItem = pair.getValue();
answerItem.put(Constants.FIELD_ID, updateItem.get(Constants.FIELD_ID));
}
return answer;
}
use of com.tvd12.ezyfox.entity.EzyArray 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);
}
}
Aggregations