use of com.tvd12.ezyfox.entity.EzyArray in project dahlia by youngmonkeys.
the class CommandSaveHandler method handle.
@Override
public Object handle(CommandSave 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);
} 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 CommandUpdateHandler method handle.
@Override
public Object handle(CommandUpdate 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 update = command.getUpdate();
List<EzyPair<Record, EzyObject>> updateItems = 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)
updateItems.add(new EzyPair<>(r, value));
}
});
for (EzyPair<Record, EzyObject> pair : updateItems) {
Record record = pair.getKey();
EzyObject updateItem = pair.getValue();
updateItem(updateItem, update);
collectionStorage.storeRecord(record, sId, sFields, updateItem);
}
}
EzyArray answer = EzyEntityFactory.newArray();
for (EzyPair<Record, EzyObject> pair : updateItems) {
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 FieldArrayReader method readValue.
@Override
protected EzyArray readValue(FieldReaders readers, FileProxy file, FieldSetting setting) throws IOException {
FieldArraySetting fs = (FieldArraySetting) setting;
FieldSetting itemSetting = fs.getItem();
int size = file.readShort();
EzyArray array = EzyEntityFactory.newArray();
for (int i = 0; i < size; ++i) {
Object item = readers.readValue(file, itemSetting);
array.add(item);
}
return array;
}
use of com.tvd12.ezyfox.entity.EzyArray in project dahlia by youngmonkeys.
the class FieldArrayWriter method writeValue.
@Override
protected void writeValue(FieldWriters writers, FileProxy file, FieldSetting setting, EzyArray value) throws IOException {
int size = value.size();
file.writeShort((short) size);
FieldArraySetting fs = (FieldArraySetting) setting;
FieldSetting itemSetting = fs.getItem();
for (int i = 0; i < size; ++i) writers.write(file, itemSetting, value.get(i));
}
use of com.tvd12.ezyfox.entity.EzyArray in project ezyfox-server by youngmonkeys.
the class EzyRequestPluginControllerTest method test.
@Test
public void test() {
EzyRequestPluginController controller = new EzyRequestPluginController();
EzyServerContext serverContext = mock(EzyServerContext.class);
EzyZoneContext zoneContext = mock(EzyZoneContext.class);
when(serverContext.getZoneContext(1)).thenReturn(zoneContext);
EzyPluginContext pluginContext = mock(EzyPluginContext.class);
when(zoneContext.getPluginContext(1)).thenReturn(pluginContext);
EzyPlugin plugin = mock(EzyPlugin.class);
when(pluginContext.getPlugin()).thenReturn(plugin);
EzyPluginRequestController requestController = mock(EzyPluginRequestController.class);
when(plugin.getRequestController()).thenReturn(requestController);
EzySimpleRequestPluginRequest request = new EzySimpleRequestPluginRequest();
EzyAbstractSession session = spy(EzyAbstractSession.class);
EzySimpleUser user = new EzySimpleUser();
user.setZoneId(1);
user.setId(1);
user.setName("test");
request.setSession(session);
request.setUser(user);
EzyArray array = EzyEntityFactory.newArrayBuilder().append(1).append(EzyEntityFactory.newArrayBuilder()).build();
request.deserializeParams(array);
controller.handle(serverContext, request);
}
Aggregations