Skip to content

Save recursively #190

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -154,9 +154,9 @@ var response = await dietPlan.remove("listKeywords", ["a"]);
or using with save function

```dart
dietPlan.setAdd('listKeywords', ['a','a','d']);
dietPlan.setAddUnique('listKeywords', ['a','a','d']);
dietPlan.setRemove('listKeywords', ['a']);
dietPlan.setAddAll('listKeywords', ['a','a','d']);
dietPlan.setAddAllUnique('listKeywords', ['a','a','d']);
dietPlan.setRemoveAll('listKeywords', ['a']);
var response = dietPlan.save()
```

8 changes: 4 additions & 4 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -105,7 +105,7 @@ class _MyAppState extends State<MyApp> {

Future<void> initInstallation() async {
final ParseInstallation installation =
await ParseInstallation.currentInstallation();
await ParseInstallation.currentInstallation();
final ParseResponse response = await installation.create();
print(response);
}
@@ -244,13 +244,13 @@ class _MyAppState extends State<MyApp> {
/// Update current user from server - Best done to verify user is still a valid user
response = await ParseUser.getCurrentUserFromServer(
token: user?.get<String>(keyHeaderSessionToken));
if (response.success) {
if (response?.success ?? false) {
user = response.result;
}

/// log user out
response = await user.logout();
if (response.success) {
response = await user?.logout();
if (response?.success ?? false) {
user = response.result;
}

1 change: 1 addition & 0 deletions lib/parse_server_sdk.dart
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ library flutter_parse_sdk;
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'dart:math';
import 'dart:typed_data';
import 'package:sembast/sembast.dart';
import 'package:sembast/sembast_io.dart';
2 changes: 1 addition & 1 deletion lib/src/data/core_store_impl.dart
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ class CoreStoreImp implements CoreStore {

if (Platform.isIOS || Platform.isAndroid) {
dbDirectory = (await getApplicationDocumentsDirectory()).path;
final String dbPath = path.join('$dbDirectory+/parse', 'parse.db');
final String dbPath = path.join('$dbDirectory/parse', 'parse.db');
final Database db = await factory.openDatabase(dbPath, codec: codec);
_instance = CoreStoreImp._internal(db);
}
3 changes: 2 additions & 1 deletion lib/src/enums/parse_enum_api_rq.dart
Original file line number Diff line number Diff line change
@@ -31,5 +31,6 @@ enum ParseApiRQ {
decrement,
getConfigs,
addConfig,
liveQuery
liveQuery,
batch
}
2 changes: 1 addition & 1 deletion lib/src/objects/parse_installation.dart
Original file line number Diff line number Diff line change
@@ -206,7 +206,7 @@ class ParseInstallation extends ParseObject {
///Subscribes the device to a channel of push notifications.
void subscribeToChannel(String value) {
final List<dynamic> channel = <String>[value];
setAddUnique('channels', channel);
setAddAllUnique('channels', channel);
save();
}

208 changes: 185 additions & 23 deletions lib/src/objects/parse_object.dart
Original file line number Diff line number Diff line change
@@ -68,38 +68,196 @@ class ParseObject extends ParseBase implements ParseCloneable {
final String body = json.encode(toJson(forApiRQ: true));
final Response result = await _client.post(url, body: body);

//Set the objectId on the object after it is created.
//This allows you to perform operations on the object after creation
if (result.statusCode == 201) {
final Map<String, dynamic> map = json.decode(result.body);
objectId = map['objectId'].toString();
}

return handleResponse<ParseObject>(
this, result, ParseApiRQ.create, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.create, _debug, className);
}
}

Future<ParseResponse> update() async {
try {
final Uri url = getSanitisedUri(_client, '$_path/$objectId');
final String body = json.encode(toJson(forApiRQ: true));
final Response result = await _client.put(url, body: body);
return handleResponse<ParseObject>(
this, result, ParseApiRQ.save, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.save, _debug, className);
}
}

/// Saves the current object online
Future<ParseResponse> save() async {
if (getObjectData()[keyVarObjectId] == null) {
return create();
final ParseResponse response = await _saveChildren(this);
if (response.success) {
if (objectId == null) {
return create();
} else {
return update();
}
} else {
try {
final Uri url = getSanitisedUri(_client, '$_path/$objectId');
final String body = json.encode(toJson(forApiRQ: true));
final Response result = await _client.put(url, body: body);
return handleResponse<ParseObject>(
this, result, ParseApiRQ.save, _debug, className);
} on Exception catch (e) {
return handleException(e, ParseApiRQ.save, _debug, className);
return response;
}
}

Future<ParseResponse> _saveChildren(dynamic object) async {
final Set<ParseObject> uniqueObjects = Set<ParseObject>();
final Set<ParseFile> uniqueFiles = Set<ParseFile>();
if (!_collectionDirtyChildren(object, uniqueObjects, uniqueFiles,
Set<ParseObject>(), Set<ParseObject>())) {
final ParseResponse response = ParseResponse();
return response;
}
if (object is ParseObject) {
uniqueObjects.remove(object);
}
for (ParseFile file in uniqueFiles) {
final ParseResponse response = await file.save();
if (!response.success) {
return response;
}
}
List<ParseObject> remaining = uniqueObjects.toList();
final List<ParseObject> finished = List<ParseObject>();
final ParseResponse totalResponse = ParseResponse()
..success = true
..results = List<dynamic>()
..statusCode = 200;
while (remaining.isNotEmpty) {
/* Partition the objects into two sets: those that can be save immediately,
and those that rely on other objects to be created first. */
final List<ParseObject> current = List<ParseObject>();
final List<ParseObject> nextBatch = List<ParseObject>();
for (ParseObject object in remaining) {
if (object._canbeSerialized(finished)) {
current.add(object);
} else {
nextBatch.add(object);
}
}
remaining = nextBatch;
// TODO(yulingtianxia): lazy User
/* Batch requests have currently a limit of 50 packaged requests per single request
This splitting will split the overall array into segments of upto 50 requests
and execute them concurrently with a wrapper task for all of them. */
final List<List<ParseObject>> chunks = [];
for (int i = 0; i < current.length; i += 50) {
chunks.add(current.sublist(i, min(current.length, i + 50)));
}

for (List<ParseObject> chunk in chunks) {
final List<dynamic> requests = chunk.map<dynamic>((ParseObject obj) {
return obj.getRequestJson(obj.objectId == null ? 'POST' : 'PUT');
}).toList();
final ParseResponse response = await batchRequest(requests, chunk);
totalResponse.success &= response.success;
if (response.success) {
totalResponse.results.addAll(response.results);
totalResponse.count += response.count;
} else {
// TODO(yulingtianxia): If there was an error, we want to roll forward the save changes before rethrowing.
totalResponse.statusCode = response.statusCode;
totalResponse.error = response.error;
}
}
finished.addAll(current);
}
return totalResponse;
}

dynamic getRequestJson(String method) {
final Uri tempUri = Uri.parse(_client.data.serverUrl);
final String parsePath = tempUri.path;
final dynamic request = <String, dynamic>{
'method': method,
'path': '$parsePath$_path' + (objectId != null ? '/$objectId' : ''),
'body': toJson(forApiRQ: true)
};
return request;
}

bool _canbeSerialized(List<dynamic> aftersaving, {dynamic value}) {
if (value != null) {
if (value is ParseObject) {
if (value.objectId == null && !aftersaving.contains(value)) {
return false;
}
} else if (value is Map) {
for (dynamic child in value.values) {
if (!_canbeSerialized(aftersaving, value: child)) {
return false;
}
}
} else if (value is List) {
for (dynamic child in value) {
if (!_canbeSerialized(aftersaving, value: child)) {
return false;
}
}
}
} else if (!_canbeSerialized(aftersaving, value: getObjectData())) {
return false;
}
// TODO(yulingtianxia): handle ACL
return true;
}

/// Get the instance of ParseRelation class associated with the given key.
bool _collectionDirtyChildren(dynamic object, Set<ParseObject> uniqueObjects,
Set<ParseFile> uniqueFiles, Set<ParseObject> seen, Set<ParseObject> seenNew) {
if (object is List) {
for (dynamic child in object) {
if (!_collectionDirtyChildren(
child, uniqueObjects, uniqueFiles, seen, seenNew)) {
return false;
}
}
} else if (object is Map) {
for (dynamic child in object.values) {
if (!_collectionDirtyChildren(
child, uniqueObjects, uniqueFiles, seen, seenNew)) {
return false;
}
}
} else if (object is ParseACL) {
// TODO(yulingtianxia): handle ACL
} else if (object is ParseFile) {
if (object.url == null) {
uniqueFiles.add(object);
}
} else if (object is ParseObject) {
/* Check for cycles of new objects. Any such cycle means it will be
impossible to save this collection of objects, so throw an exception. */
if (object.objectId != null) {
seenNew = Set<ParseObject>();
} else {
if (seenNew.contains(object)) {
// TODO(yulingtianxia): throw an error?
return false;
}
seenNew.add(object);
}

/* Check for cycles of any object. If this occurs, then there's no
problem, but we shouldn't recurse any deeper, because it would be
an infinite recursion. */
if (seen.contains(object)) {
return true;
}
seen.add(object);

if (!_collectionDirtyChildren(
object.getObjectData(), uniqueObjects, uniqueFiles, seen, seenNew)) {
return false;
}

// TODO(yulingtianxia): Check Dirty
uniqueObjects.add(object);
}
return true;
}

/// Get the instance of ParseRelation class associated with the given key.
ParseRelation getRelation(String key) {
return ParseRelation(parent: this, key: key);
}
@@ -115,8 +273,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
}

/// Removes an element from an Array
void setRemove(String key, dynamic values) {
_arrayOperation('Remove', key, values);
void setRemove(String key, dynamic value) {
_arrayOperation('Remove', key, <dynamic>[value]);
}

/// Remove multiple elements from an array of an object
@@ -159,8 +317,11 @@ class ParseObject extends ParseBase implements ParseCloneable {
}
}

void setAddUnique(String key, dynamic value) {
_arrayOperation('AddUnique', key, <dynamic>[value]);
}
/// Add a multiple elements to an array of an object
void setAddUnique(String key, List<dynamic> values) {
void setAddAllUnique(String key, List<dynamic> values) {
_arrayOperation('AddUnique', key, values);
}

@@ -175,8 +336,8 @@ class ParseObject extends ParseBase implements ParseCloneable {
}

/// Add a single element to an array of an object
void setAdd(String key, dynamic values) {
_arrayOperation('Add', key, values);
void setAdd(String key, dynamic value) {
_arrayOperation('Add', key, <dynamic>[value]);
}

void addRelation(String key, List<dynamic> values) {
@@ -208,6 +369,7 @@ class ParseObject extends ParseBase implements ParseCloneable {

/// Used in array Operations in save() method
void _arrayOperation(String arrayAction, String key, List<dynamic> values) {
// TODO(yulingtianxia): Array operations should be incremental. Merge add and remove operation.
set<Map<String, dynamic>>(
key, <String, dynamic>{'__op': arrayAction, 'objects': values});
}
8 changes: 2 additions & 6 deletions lib/src/objects/parse_relation.dart
Original file line number Diff line number Diff line change
@@ -19,19 +19,15 @@ class ParseRelation<T extends ParseObject> {
if (object != null) {
_targetClass = object.getClassName();
_objects.add(object);
_parent.addRelation(_key, _objects.map((T value) {
return value.toPointer();
}).toList());
_parent.addRelation(_key, _objects.toList());
}
}

void remove(T object) {
if (object != null) {
_targetClass = object.getClassName();
_objects.remove(object);
_parent.removeRelation(_key, _objects.map((T value) {
return value.toPointer();
}).toList());
_parent.removeRelation(_key, _objects.toList());
}
}

Loading