Skip to content

Commit 27670c3

Browse files
committed
Added Query.orderKey()
Also removed QueryWrapper.
1 parent 9118705 commit 27670c3

7 files changed

Lines changed: 55 additions & 207 deletions

File tree

src/main/java/com/googlecode/objectify/cmd/Query.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,30 @@ public interface Query<T> extends SimpleQuery<T>
5454
*/
5555
public Query<T> filter(Filter filter);
5656

57+
/* (non-Javadoc)
58+
* @see com.googlecode.objectify.cmd.SimpleQuery#filterKey(java.lang.String, java.lang.Object)
59+
*/
60+
@Override
61+
public Query<T> filterKey(String condition, Object value);
62+
5763
/**
5864
* <p>Sorts based on a property. Examples:</p>
59-
*
65+
*
6066
* <ul>
6167
* <li>{@code order("age")}</li>
6268
* <li>{@code order("-age")} (descending sort)</li>
6369
* </ul>
64-
*
70+
*
6571
* <p>You can <strong>not</strong> sort on @Id or @Parent properties. Sort by __key__ or -__key__ instead.</p>
6672
*/
6773
public Query<T> order(String condition);
68-
69-
/* (non-Javadoc)
70-
* @see com.googlecode.objectify.cmd.SimpleQuery#filterKey(java.lang.String, java.lang.Object)
74+
75+
/**
76+
* Shorthand for {@code order("__key__")} or {@code order("-__key__")}
77+
* @param descending if true, specifies a descending (aka reverse) sort
7178
*/
72-
@Override
73-
public Query<T> filterKey(String condition, Object value);
74-
79+
public Query<T> orderKey(boolean descending);
80+
7581
/* (non-Javadoc)
7682
* @see com.googlecode.objectify.cmd.SimpleQuery#ancestor(java.lang.Object)
7783
*/

src/main/java/com/googlecode/objectify/cmd/SimpleQuery.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ public interface SimpleQuery<T> extends QueryExecute<T>
3434
*/
3535
public SimpleQuery<T> filterKey(Object value);
3636

37+
/**
38+
* Orders results by the key.
39+
* @param descending if true, specifies a descending (aka reverse) sort
40+
*/
41+
public SimpleQuery<T> orderKey(boolean descending);
42+
3743
/**
3844
* Restricts result set only to objects which have the given ancestor
3945
* somewhere in the chain. Doesn't need to be the immediate parent. The

src/main/java/com/googlecode/objectify/impl/SimpleQueryImpl.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public QueryImpl<T> filterKey(Object value)
5151
return filterKey("=", value);
5252
}
5353

54+
@Override
55+
public QueryImpl<T> orderKey(boolean descending) {
56+
String prefix = descending ? "-" : "";
57+
58+
QueryImpl<T> q = createQuery();
59+
q.addOrder(prefix + Entity.KEY_RESERVED_PROPERTY);
60+
return q;
61+
}
62+
5463
/* (non-Javadoc)
5564
* @see com.googlecode.objectify.cmd.Query#ancestor(java.lang.Object)
5665
*/

src/main/java/com/googlecode/objectify/util/cmd/QueryResultIteratorWrapper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public class QueryResultIteratorWrapper<T> implements QueryResultIterator<T>
1717
QueryResultIterator<T> base;
1818

1919
/** */
20-
public QueryResultIteratorWrapper(QueryResultIterator<T> base)
21-
{
20+
public QueryResultIteratorWrapper(QueryResultIterator<T> base) {
2221
this.base = base;
2322
}
2423

src/main/java/com/googlecode/objectify/util/cmd/QueryWrapper.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/main/java/com/googlecode/objectify/util/cmd/SimpleQueryWrapper.java

Lines changed: 0 additions & 158 deletions
This file was deleted.

src/test/java/com/googlecode/objectify/test/QueryTests.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,31 @@ public void testNormalReverseSorting() throws Exception {
117117
assert t2.getId().equals(triv2.getId());
118118
}
119119

120+
/** */
121+
@Test
122+
public void testKeySorting() throws Exception {
123+
Iterator<Trivial> it = ofy().load().type(Trivial.class).orderKey(false).iterator();
124+
125+
Trivial t1 = it.next();
126+
Trivial t2 = it.next();
127+
128+
assert t1.getId().equals(triv1.getId());
129+
assert t2.getId().equals(triv2.getId());
130+
}
131+
132+
/** */
133+
@Test
134+
public void testKeyReverseSorting() throws Exception {
135+
Iterator<Trivial> it = ofy().load().type(Trivial.class).orderKey(true).iterator();
136+
137+
// t2 first
138+
Trivial t2 = it.next();
139+
Trivial t1 = it.next();
140+
141+
assert t1.getId().equals(triv1.getId());
142+
assert t2.getId().equals(triv2.getId());
143+
}
144+
120145
/** Unfortunately we can only test one way without custom index file */
121146
@Test(expectedExceptions = IllegalArgumentException.class)
122147
public void doNotAllowSortingByIdField() throws Exception {

0 commit comments

Comments
 (0)