Skip to content
Merged
Show file tree
Hide file tree
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
25 changes: 15 additions & 10 deletions Parse/src/main/java/com/parse/OfflineQueryLogic.java
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ private static boolean matchesWithinConstraint(Object constraint, Object value)

/**
* Returns true iff the given value matches the given operator and constraint.
*
*
* @throws UnsupportedOperationException
* if the operator is not one this function can handle.
*/
Expand Down Expand Up @@ -955,8 +955,11 @@ public int compare(T lhs, T rhs) {
/**
* Makes sure that the object specified by path, relative to container, is fetched.
*/
private Task<Void> fetchIncludeAsync(
final Object container, final String path, final ParseSQLiteDatabase db)
private static Task<Void> fetchIncludeAsync(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit:

  private Task<Void> fetchIncludeAsync(
      final OfflineStore store
      final Object container,
      final String path,
      final ParseSQLiteDatabase db)

final OfflineStore store,
final Object container,
final String path,
final ParseSQLiteDatabase db)
throws ParseException {
// If there's no object to include, that's fine.
if (container == null) {
Expand All @@ -972,7 +975,7 @@ private Task<Void> fetchIncludeAsync(
task = task.onSuccessTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
return fetchIncludeAsync(item, path, db);
return fetchIncludeAsync(store, item, path, db);
}
});
}
Expand All @@ -986,7 +989,7 @@ public Task<Void> then(Task<Void> task) throws Exception {
task = task.onSuccessTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
return fetchIncludeAsync(array.get(index), path, db);
return fetchIncludeAsync(store, array.get(index), path, db);
}
});
}
Expand Down Expand Up @@ -1020,7 +1023,7 @@ public Task<Void> then(Task<Void> task) throws Exception {
public Task<Object> then(Task<Void> task) throws Exception {
if (container instanceof ParseObject) {
// Make sure this object is fetched before descending into it.
return fetchIncludeAsync(container, null, db).onSuccess(new Continuation<Void, Object>() {
return fetchIncludeAsync(store, container, null, db).onSuccess(new Continuation<Void, Object>() {
@Override
public Object then(Task<Void> task) throws Exception {
return ((ParseObject) container).get(key);
Expand All @@ -1041,25 +1044,27 @@ public Object then(Task<Void> task) throws Exception {
}).onSuccessTask(new Continuation<Object, Task<Void>>() {
@Override
public Task<Void> then(Task<Object> task) throws Exception {
return fetchIncludeAsync(task.getResult(), rest, db);
return fetchIncludeAsync(store, task.getResult(), rest, db);
}
});
}

/**
* Makes sure all of the objects included by the given query get fetched.
*/
/* package */ <T extends ParseObject> Task<Void> fetchIncludesAsync(
/* package */ static <T extends ParseObject> Task<Void> fetchIncludesAsync(
final OfflineStore store,
final T object,
ParseQuery.State<T> state, final ParseSQLiteDatabase db) {
ParseQuery.State<T> state,
final ParseSQLiteDatabase db) {
Set<String> includes = state.includes();
// We do the fetches in series because it makes it easier to fail on the first error.
Task<Void> task = Task.forResult(null);
for (final String include : includes) {
task = task.onSuccessTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
return fetchIncludeAsync(object, include, db);
return fetchIncludeAsync(store, object, include, db);
}
});
}
Expand Down
2 changes: 1 addition & 1 deletion Parse/src/main/java/com/parse/OfflineStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ public Task<List<T>> then(Task<Void> task) throws Exception {
fetchedIncludesTask = fetchedIncludesTask.onSuccessTask(new Continuation<Void, Task<Void>>() {
@Override
public Task<Void> then(Task<Void> task) throws Exception {
return queryLogic.fetchIncludesAsync(object, query, db);
return OfflineQueryLogic.fetchIncludesAsync(OfflineStore.this, object, query, db);
}
});
}
Expand Down
35 changes: 10 additions & 25 deletions Parse/src/test/java/com/parse/OfflineQueryLogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,6 @@ public void testFetchIncludesParseObject() throws ParseException {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);
ParseSQLiteDatabase db = mock(ParseSQLiteDatabase.class);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
Expand All @@ -839,7 +838,7 @@ public void testFetchIncludesParseObject() throws ParseException {
ParseObject unfetchedObject = new ParseObject("TestObject");
object.put("foo", unfetchedObject);

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, db));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, db));
verify(store).fetchLocallyAsync(object, db);
verify(store).fetchLocallyAsync(unfetchedObject, db);
verifyNoMoreInteractions(store);
Expand All @@ -851,7 +850,6 @@ public void testFetchIncludesCollection() throws ParseException {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);
ParseSQLiteDatabase db = mock(ParseSQLiteDatabase.class);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
Expand All @@ -864,7 +862,7 @@ public void testFetchIncludesCollection() throws ParseException {
objects.add(unfetchedObject);
when(object.get("foo")).thenReturn(objects);

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, db));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, db));
verify(store).fetchLocallyAsync(object, db);
verify(store).fetchLocallyAsync(unfetchedObject, db);
verifyNoMoreInteractions(store);
Expand All @@ -876,7 +874,6 @@ public void testFetchIncludesJSONArray() throws ParseException {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);
ParseSQLiteDatabase db = mock(ParseSQLiteDatabase.class);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
Expand All @@ -889,7 +886,7 @@ public void testFetchIncludesJSONArray() throws ParseException {
objects.put(unfetchedObject);
when(object.get("foo")).thenReturn(objects);

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, db));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, db));
verify(store).fetchLocallyAsync(object, db);
verify(store).fetchLocallyAsync(unfetchedObject, db);
verifyNoMoreInteractions(store);
Expand All @@ -901,7 +898,6 @@ public void testFetchIncludesMap() throws ParseException {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);
ParseSQLiteDatabase db = mock(ParseSQLiteDatabase.class);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
Expand All @@ -914,7 +910,7 @@ public void testFetchIncludesMap() throws ParseException {
objects.put("bar", unfetchedObject);
when(object.get("foo")).thenReturn(objects);

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, db));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, db));
verify(store).fetchLocallyAsync(object, db);
verify(store).fetchLocallyAsync(unfetchedObject, db);
verifyNoMoreInteractions(store);
Expand All @@ -926,7 +922,6 @@ public void testFetchIncludesJSONObject() throws Exception {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);
ParseSQLiteDatabase db = mock(ParseSQLiteDatabase.class);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
Expand All @@ -939,7 +934,7 @@ public void testFetchIncludesJSONObject() throws Exception {
objects.put("bar", unfetchedObject);
when(object.get("foo")).thenReturn(objects);

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, db));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, db));
verify(store).fetchLocallyAsync(object, db);
verify(store).fetchLocallyAsync(unfetchedObject, db);
verifyNoMoreInteractions(store);
Expand All @@ -951,16 +946,14 @@ public void testFetchIncludesNull() throws ParseException {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
.include("foo")
.build();

ParseObject object = new ParseObject("TestObject");
object.put("foo", JSONObject.NULL);

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, null));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, null));
// only itself
verify(store, times(1))
.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class));
Expand All @@ -972,8 +965,6 @@ public void testFetchIncludesNonParseObject() throws ParseException {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
.include("foo")
.build();
Expand All @@ -982,7 +973,7 @@ public void testFetchIncludesNonParseObject() throws ParseException {
object.put("foo", 1);

thrown.expect(ParseException.class);
ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, null));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, null));
// only itself
verify(store, times(1))
.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class));
Expand All @@ -994,15 +985,13 @@ public void testFetchIncludesDoesNotExist() throws ParseException {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
.include("foo")
.build();

ParseObject object = new ParseObject("TestObject");

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, null));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, null));
// only itself
verify(store, times(1))
.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class));
Expand All @@ -1014,16 +1003,14 @@ public void testFetchIncludesNestedNull() throws Exception {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
.include("foo.bar")
.build();

ParseObject object = new ParseObject("TestObject");
object.put("foo", JSONObject.NULL);

ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, null));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, null));
// only itself
verify(store, times(1))
.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class));
Expand All @@ -1035,8 +1022,6 @@ public void testFetchIncludesNestedNonParseObject() throws Exception {
when(store.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class)))
.thenReturn(Task.<ParseObject>forResult(null));

OfflineQueryLogic logic = new OfflineQueryLogic(store);

ParseQuery.State<ParseObject> query = new ParseQuery.State.Builder<>("TestObject")
.include("foo.bar")
.build();
Expand All @@ -1045,7 +1030,7 @@ public void testFetchIncludesNestedNonParseObject() throws Exception {
object.put("foo", 1);

thrown.expect(IllegalStateException.class);
ParseTaskUtils.wait(logic.fetchIncludesAsync(object, query, null));
ParseTaskUtils.wait(OfflineQueryLogic.fetchIncludesAsync(store, object, query, null));
// only itself
verify(store, times(1))
.fetchLocallyAsync(any(ParseObject.class), any(ParseSQLiteDatabase.class));
Expand Down