Skip to content

Commit d1ca7b3

Browse files
committed
closed: #142
1 parent 53601e6 commit d1ca7b3

File tree

3 files changed

+205
-26
lines changed

3 files changed

+205
-26
lines changed

core/src/main/java/cn/leancloud/AVQuery.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public enum CachePolicy {
3030
private java.lang.Boolean isRunning;
3131
private CachePolicy cachePolicy = CachePolicy.IGNORE_CACHE;
3232
private long maxCacheAge = -1;
33-
private boolean includeACL = false;
3433

3534
QueryConditions conditions;
3635

@@ -216,6 +215,11 @@ public void clearCachedResult() {
216215
Map<String, String> query = assembleParameters();
217216
String cacheKey = QueryResultCache.generateKeyForQueryCondition(getClassName(), query);
218217
QueryResultCache.getInstance().clearCachedFile(cacheKey);
218+
219+
// clear result for getFirstInBackground().
220+
query.put("limit", "1");
221+
cacheKey = QueryResultCache.generateKeyForQueryCondition(getClassName(), query);
222+
QueryResultCache.getInstance().clearCachedFile(cacheKey);
219223
}
220224

221225
/**
@@ -337,7 +341,7 @@ public AVQuery<T> addDescendingOrder(String key) {
337341
* @return include flag.
338342
*/
339343
public boolean isIncludeACL() {
340-
return includeACL;
344+
return conditions.isIncludeACL();
341345
}
342346

343347
/**
@@ -346,11 +350,10 @@ public boolean isIncludeACL() {
346350
* @return this query.
347351
*/
348352
public AVQuery<T> includeACL(boolean includeACL) {
349-
this.includeACL = includeACL;
353+
conditions.includeACL(includeACL);
350354
return this;
351355
}
352356

353-
354357
/**
355358
* Include nested AVObjects for the provided key. You can use dot notation to specify which fields
356359
* in the included object that are also fetched.
@@ -924,9 +927,6 @@ public Observable<List<T>> findInBackground() {
924927

925928
protected Observable<List<T>> findInBackground(int explicitLimit) {
926929
Map<String, String> query = assembleParameters();
927-
if (this.includeACL && null != query) {
928-
query.put("returnACL", "true");
929-
}
930930
if (explicitLimit > 0) {
931931
query.put("limit", Integer.toString(explicitLimit));
932932
}

core/src/main/java/cn/leancloud/query/QueryConditions.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ public class QueryConditions implements Cloneable {
1717
private int skip = -1;
1818
private String order;
1919
private Map<String, String> parameters;
20+
private boolean includeACL = false;
2021

2122
public QueryConditions() {
2223
where = new HashMap<String, List<QueryOperation>>();
2324
include = new LinkedList<String>();
25+
includeACL = false;
2426
parameters = new HashMap<String, String>();
2527
}
2628

@@ -36,6 +38,7 @@ public QueryConditions clone() {
3638
condition.setTrace(this.trace);
3739
condition.setSkip(this.skip);
3840
condition.setOrder(this.order);
41+
condition.includeACL(this.includeACL);
3942
return condition;
4043
}
4144

@@ -142,6 +145,23 @@ public void selectKeys(Collection<String> keys) {
142145
}
143146
}
144147

148+
/**
149+
* Flag to indicate need ACL returned in result.
150+
* @return include flag.
151+
*/
152+
public boolean isIncludeACL() {
153+
return includeACL;
154+
}
155+
156+
/**
157+
* set include ACL or not.
158+
* @param includeACL Flag to indicate need ACL returned in result.
159+
* @return this query.
160+
*/
161+
public void includeACL(boolean includeACL) {
162+
this.includeACL = includeACL;
163+
}
164+
145165
public Map<String, Object> compileWhereOperationMap() {
146166
Map<String, Object> result = new HashMap<String, Object>();
147167
for (Map.Entry<String, List<QueryOperation>> entry : where.entrySet()) {
@@ -223,6 +243,9 @@ public Map<String, Object> assembleJsonParam() {
223243
if (skip >= 0) {
224244
result.put("skip", skip);
225245
}
246+
if (includeACL) {
247+
result.put("returnACL", "true");
248+
}
226249
if (!StringUtil.isEmpty(order)) {
227250
result.put("order", order);
228251
}
@@ -249,6 +272,9 @@ public Map<String, String> assembleParameters() {
249272
if (skip >= 0) {
250273
parameters.put("skip", Integer.toString(skip));
251274
}
275+
if (includeACL) {
276+
parameters.put("returnACL", "true");
277+
}
252278
if (!StringUtil.isEmpty(order)) {
253279
parameters.put("order", order);
254280
}
@@ -260,6 +286,7 @@ public Map<String, String> assembleParameters() {
260286
String keys = StringUtil.join(",", selectedKeys);
261287
parameters.put("keys", keys);
262288
}
289+
263290
return parameters;
264291
}
265292

@@ -460,23 +487,4 @@ public void whereContainsAll(String key, Collection<?> values) {
460487
public void whereDoesNotExist(String key) {
461488
addWhereItem(key, "$exists", false);
462489
}
463-
464-
// public String generateQueryString() {
465-
// assembleParameters();
466-
// Map<String, String> params = getParameters();
467-
// StringBuilder sb = new StringBuilder();
468-
// int index = 0;
469-
// for (Map.Entry<String, String> param: params.entrySet()) {
470-
// if (index > 0) {
471-
// sb.append("&");
472-
// }
473-
// index++;
474-
// sb.append(param.getKey());
475-
// sb.append("=");
476-
// sb.append(param.getValue());
477-
// }
478-
// return sb.toString();
479-
// }
480-
481-
482490
}

core/src/test/java/cn/leancloud/AVQueryTest.java

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import junit.framework.Test;
99
import junit.framework.TestCase;
1010
import junit.framework.TestSuite;
11+
import org.jetbrains.annotations.NotNull;
1112

1213
import java.util.*;
1314
import java.util.concurrent.CountDownLatch;
@@ -460,6 +461,176 @@ public void onComplete() {
460461
assertTrue(testSucceed);
461462
}
462463

464+
public void testQueryAllAfterClearCache() throws Exception {
465+
final AVQuery query = new AVQuery("Student");
466+
query.orderByDescending(AVObject.KEY_CREATED_AT);
467+
query.limit(5);
468+
query.skip(1);
469+
query.findInBackground().subscribe(new Observer<List<AVObject>>() {
470+
public void onSubscribe(Disposable disposable) {
471+
472+
}
473+
474+
public void onNext(List<AVObject> o) {
475+
System.out.println("succeed to query at first time, result size: " + o.size());
476+
query.clearCachedResult();
477+
query.findInBackground().subscribe(new Observer<List<AVObject>>() {
478+
@Override
479+
public void onSubscribe(@NotNull Disposable disposable) {
480+
481+
}
482+
483+
@Override
484+
public void onNext(@NotNull List<AVObject> o) {
485+
System.out.println("succeed to query at second time, result size: " + o.size());
486+
testSucceed = true;
487+
latch.countDown();
488+
}
489+
490+
@Override
491+
public void onError(@NotNull Throwable throwable) {
492+
System.out.println("failed to query at second time");
493+
throwable.printStackTrace();
494+
latch.countDown();
495+
}
496+
497+
@Override
498+
public void onComplete() {
499+
500+
}
501+
});
502+
503+
}
504+
505+
public void onError(Throwable throwable) {
506+
System.out.println("failed to query at first time");
507+
throwable.printStackTrace();
508+
latch.countDown();
509+
}
510+
511+
public void onComplete() {
512+
513+
}
514+
});
515+
latch.await();
516+
assertTrue(testSucceed);
517+
518+
}
519+
520+
public void testQueryFirstAfterClearCache() throws Exception {
521+
final AVQuery query = new AVQuery("Student");
522+
query.orderByDescending(AVObject.KEY_CREATED_AT);
523+
query.limit(5);
524+
query.skip(1);
525+
query.getFirstInBackground().subscribe(new Observer<AVObject>() {
526+
public void onSubscribe(Disposable disposable) {
527+
528+
}
529+
530+
public void onNext(AVObject o) {
531+
System.out.println("succeed to query at first time, result objectId: " + o.getObjectId());
532+
query.clearCachedResult();
533+
query.setCachePolicy(AVQuery.CachePolicy.CACHE_ELSE_NETWORK);
534+
query.getFirstInBackground().subscribe(new Observer<AVObject>() {
535+
@Override
536+
public void onSubscribe(@NotNull Disposable disposable) {
537+
538+
}
539+
540+
@Override
541+
public void onNext(@NotNull AVObject o) {
542+
System.out.println("succeed to query at second time, result objectId: " + o.getObjectId());
543+
testSucceed = true;
544+
latch.countDown();
545+
}
546+
547+
@Override
548+
public void onError(@NotNull Throwable throwable) {
549+
System.out.println("failed to query at second time");
550+
throwable.printStackTrace();
551+
latch.countDown();
552+
}
553+
554+
@Override
555+
public void onComplete() {
556+
557+
}
558+
});
559+
560+
}
561+
562+
public void onError(Throwable throwable) {
563+
System.out.println("failed to query at first time");
564+
throwable.printStackTrace();
565+
latch.countDown();
566+
}
567+
568+
public void onComplete() {
569+
570+
}
571+
});
572+
latch.await();
573+
assertTrue(testSucceed);
574+
575+
}
576+
577+
public void testQueryCountAfterClearCache() throws Exception {
578+
final AVQuery query = new AVQuery("Student");
579+
query.orderByDescending(AVObject.KEY_CREATED_AT);
580+
query.limit(5);
581+
query.skip(1);
582+
query.countInBackground().subscribe(new Observer<Integer>() {
583+
public void onSubscribe(Disposable disposable) {
584+
585+
}
586+
587+
public void onNext(Integer o) {
588+
System.out.println("succeed to query at first time, count: " + o);
589+
query.clearCachedResult();
590+
query.setCachePolicy(AVQuery.CachePolicy.CACHE_ELSE_NETWORK);
591+
query.countInBackground().subscribe(new Observer<Integer>() {
592+
@Override
593+
public void onSubscribe(@NotNull Disposable disposable) {
594+
595+
}
596+
597+
@Override
598+
public void onNext(@NotNull Integer o) {
599+
System.out.println("succeed to query at second time, count: " + o);
600+
testSucceed = true;
601+
latch.countDown();
602+
}
603+
604+
@Override
605+
public void onError(@NotNull Throwable throwable) {
606+
System.out.println("failed to query at second time");
607+
throwable.printStackTrace();
608+
latch.countDown();
609+
}
610+
611+
@Override
612+
public void onComplete() {
613+
614+
}
615+
});
616+
617+
}
618+
619+
public void onError(Throwable throwable) {
620+
System.out.println("failed to query at first time");
621+
throwable.printStackTrace();
622+
latch.countDown();
623+
}
624+
625+
public void onComplete() {
626+
627+
}
628+
});
629+
latch.await();
630+
assertTrue(testSucceed);
631+
632+
}
633+
463634
public void testDeepIncludeQuery() throws Exception {
464635
AVQuery<AVObject> queryLikeRed = new AVQuery<>("FileUnitTest");
465636
queryLikeRed.include("taskId");

0 commit comments

Comments
 (0)