Skip to content

Commit 1f6c597

Browse files
author
Steve Briskin
committed
JAVA-563: refactored queryopbuilder
1 parent e6198e5 commit 1f6c597

File tree

3 files changed

+117
-88
lines changed

3 files changed

+117
-88
lines changed

src/main/com/mongodb/DBCollection.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,12 @@ public DBObject findOne( DBObject o, DBObject fields ) {
676676
* Returns a single obejct from this collection matching the query.
677677
* @param o the query object
678678
* @param fields fields to return
679-
* @param orderby fields to order by
679+
* @param orderBy fields to order by
680680
* @return the object found, or <code>null</code> if no such object exists
681681
* @dochub find
682682
*/
683-
public DBObject findOne( DBObject o, DBObject fields, DBObject orderby){
684-
return findOne(o, fields, orderby, getReadPreference());
683+
public DBObject findOne( DBObject o, DBObject fields, DBObject orderBy){
684+
return findOne(o, fields, orderBy, getReadPreference());
685685
}
686686

687687
/**
@@ -699,15 +699,15 @@ public DBObject findOne( DBObject o, DBObject fields, ReadPreference readPref )
699699
* Returns a single object from this collection matching the query.
700700
* @param o the query object
701701
* @param fields fields to return
702-
* @param orderby fields to order by
702+
* @param orderBy fields to order by
703703
* @return the object found, or <code>null</code> if no such object exists
704704
* @dochub find
705705
*/
706-
public DBObject findOne( DBObject o, DBObject fields, DBObject orderby, ReadPreference readPref ) {
706+
public DBObject findOne( DBObject o, DBObject fields, DBObject orderBy, ReadPreference readPref ) {
707707

708708
DBObject queryop = new QueryOpBuilder()
709709
.addQuery(o)
710-
.addOrderBy(orderby)
710+
.addOrderBy(orderBy)
711711
.get();
712712

713713
Iterator<DBObject> i = __find( queryop, fields , 0 , -1 , 0, getOptions(), readPref, getDecoder() );

src/main/com/mongodb/DBCursor.java

Lines changed: 9 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -343,34 +343,17 @@ public int getOptions(){
343343

344344
// ---- internal stuff ------
345345

346-
private void _check()
347-
throws MongoException {
348-
if ( _it != null )
349-
return;
346+
private void _check() throws MongoException {
347+
if (_it != null)
348+
return;
350349

351-
_lookForHints();
352-
353-
DBObject foo = _query;
354-
if (hasSpecialQueryFields()) {
355-
QueryOpBuilder opbuilder = (_specialFields == null ? new QueryOpBuilder() : new QueryOpBuilder(_specialFields));
356-
357-
opbuilder.addQuery(_query)
358-
.addOrderBy(_orderBy)
359-
.addHint(_hint)
360-
.addHint(_hintDBObj);
361-
362-
if(_explain)
363-
opbuilder.addExplain();
364-
if(_snapshot)
365-
opbuilder.addSnapshot();
366-
367-
foo = opbuilder.get();
368-
}
369-
370-
_it = _collection.__find(foo, _keysWanted, _skip, _batchSize, _limit, _options, _readPref, getDecoder());
371-
}
350+
_lookForHints();
372351

373-
352+
DBObject queryOp = new QueryOpBuilder(_query, _orderBy, _hintDBObj, _hint, _explain, _snapshot, _specialFields).get();
353+
354+
_it = _collection.__find(queryOp, _keysWanted, _skip, _batchSize, _limit,
355+
_options, _readPref, getDecoder());
356+
}
374357

375358
// Only create a new decoder if there is a decoder factory explicitly set on the collection. Otherwise return null
376359
// so that the collection can use a cached decoder
@@ -403,19 +386,6 @@ private void _lookForHints(){
403386
}
404387
}
405388

406-
boolean hasSpecialQueryFields(){
407-
if ( _specialFields != null )
408-
return true;
409-
410-
if ( _orderBy != null && _orderBy.keySet().size() > 0 )
411-
return true;
412-
413-
if ( _hint != null || _hintDBObj != null || _snapshot )
414-
return true;
415-
416-
return _explain;
417-
}
418-
419389
void _checkType( CursorType type ){
420390
if ( _cursorType == null ){
421391
_cursorType = type;

src/main/com/mongodb/QueryOpBuilder.java

Lines changed: 102 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,52 @@
55

66

77
/**
8-
* Utility for constructing Query operation command with query, orderby, hint, explain, snapshot
8+
* Utility for constructing Query operation command with query, orderby, hint, explain, snapshot.
99
*/
10-
public class QueryOpBuilder {
11-
private DBObject queryop;
10+
class QueryOpBuilder {
11+
12+
private DBObject query;
13+
private DBObject orderBy;
14+
private DBObject hintObj;
15+
private String hintStr;
16+
private boolean explain;
17+
private boolean snapshot;
1218

13-
/**
14-
* Creates a builder with an empty query op
15-
*/
19+
private DBObject specialFields;
20+
1621
public QueryOpBuilder(){
17-
queryop = new BasicDBObject();
1822
}
1923

20-
/**
21-
* Creates a new builder
22-
* @param queryop
23-
*/
24-
public QueryOpBuilder(DBObject queryop){
25-
this.queryop = queryop;
24+
public QueryOpBuilder(DBObject query, DBObject orderBy, DBObject hintObj, String hintStr, boolean explain, boolean snapshot,
25+
DBObject specialFields) {
26+
this.query = query;
27+
this.orderBy = orderBy;
28+
this.hintObj = hintObj;
29+
this.hintStr = hintStr;
30+
this.explain = explain;
31+
this.snapshot = snapshot;
32+
this.specialFields = specialFields;
2633
}
27-
34+
35+
2836
/**
2937
* Adds the query clause to the operation
3038
* @param query
3139
* @return
3240
*/
3341
public QueryOpBuilder addQuery(DBObject query){
34-
return addToQueryObject("query", query, true);
42+
this.query = query;
43+
return this;
3544
}
3645

3746
/**
38-
* Adds the groupby clause to the operation
47+
* Adds the orderby clause to the operation
3948
* @param orderBy
4049
* @return
4150
*/
4251
public QueryOpBuilder addOrderBy(DBObject orderBy){
43-
return addToQueryObject("orderby", orderBy, false);
52+
this.orderBy = orderBy;
53+
return this;
4454
}
4555

4656
/**
@@ -49,75 +59,124 @@ public QueryOpBuilder addOrderBy(DBObject orderBy){
4959
* @return
5060
*/
5161
public QueryOpBuilder addHint(String hint){
52-
if(hint != null)
53-
addToQueryObject("$hint", hint);
54-
62+
this.hintStr = hint;
5563
return this;
5664
}
5765

66+
/**
67+
* Adds hint clause to the operation
68+
* @param hint
69+
* @return
70+
*/
5871
public QueryOpBuilder addHint(DBObject hint){
59-
if(hint != null)
60-
addToQueryObject("$hint", hint, false);
61-
72+
this.hintObj = hint;
73+
return this;
74+
}
75+
76+
77+
/**
78+
* Adds special fields to the operation
79+
* @param specialFields
80+
* @return
81+
*/
82+
public QueryOpBuilder addSpecialFields(DBObject specialFields){
83+
this.specialFields = specialFields;
6284
return this;
6385
}
6486

6587
/**
6688
* Adds the explain flag to the operation
89+
* @param explain
6790
* @return
6891
*/
69-
public QueryOpBuilder addExplain(){
70-
return addToQueryObject("$explain", true);
92+
public QueryOpBuilder addExplain(boolean explain){
93+
this.explain = explain;
94+
return this;
7195
}
7296

7397
/**
7498
* Adds the snapshot flag to the operation
99+
* @param snapshot
75100
* @return
76101
*/
77-
public QueryOpBuilder addSnapshot(){
78-
return addToQueryObject("$snapshot", true);
102+
public QueryOpBuilder addSnapshot(boolean snapshot){
103+
this.snapshot = snapshot;
104+
return this;
105+
}
106+
107+
108+
/**
109+
* Constructs the query operation DBObject
110+
* @return DBObject representing the query command to be sent to server
111+
*/
112+
public DBObject get(){
113+
if(hasSpecialQueryFields()){
114+
DBObject queryop = (specialFields == null ? new BasicDBObject() : specialFields);
115+
116+
addToQueryObject(queryop, "query", query, true);
117+
addToQueryObject(queryop, "orderby", orderBy, false);
118+
if (hintStr != null)
119+
addToQueryObject(queryop, "$hint", hintStr);
120+
if (hintObj != null)
121+
addToQueryObject(queryop, "$hint", hintObj);
122+
123+
if (explain)
124+
queryop.put("$explain", true);
125+
if (snapshot)
126+
queryop.put("$snapshot", true);
127+
128+
return queryop;
129+
}
79130

131+
return query;
80132
}
81133

134+
private boolean hasSpecialQueryFields(){
135+
if ( specialFields != null )
136+
return true;
137+
138+
if ( orderBy != null && orderBy.keySet().size() > 0 )
139+
return true;
140+
141+
if ( hintStr != null || hintObj != null || snapshot || explain)
142+
return true;
143+
144+
return false;
145+
}
146+
82147
/**
83148
* Adds DBObject to the operation
149+
* @param dbobj DBObject to add field to
84150
* @param field name of the field
85151
* @param obj object to add to the operation. Ignore if <code>null</code>.
86152
* @param sendEmpty if <code>true</code> adds obj even if it's empty. Ignore if <code>false</code> and obj is empty.
87153
* @return
88154
*/
89-
public QueryOpBuilder addToQueryObject(String field, DBObject obj, boolean sendEmpty) {
155+
private void addToQueryObject(DBObject dbobj, String field, DBObject obj, boolean sendEmpty) {
90156
if (obj == null)
91-
return this;
157+
return;
92158

93159
if (!sendEmpty && obj.keySet().size() == 0)
94-
return this;
160+
return;
95161

96-
return addToQueryObject(field, obj);
162+
addToQueryObject(dbobj, field, obj);
97163
}
98164

99165
/**
100166
* Adds an Object to the operation
167+
* @param dbobj DBObject to add field to
101168
* @param field name of the field
102169
* @param obj Object to be added. Ignore if <code>null</code>
103170
* @return
104171
*/
105-
public QueryOpBuilder addToQueryObject(String field, Object obj) {
172+
private void addToQueryObject(DBObject dbobj, String field, Object obj) {
106173

107174
if (obj == null)
108-
return this;
175+
return;
109176

110-
queryop.put(field, obj);
111-
112-
return this;
177+
dbobj.put(field, obj);
113178
}
114179

115-
/**
116-
* gets the constructed query operation object
117-
* @return
118-
*/
119-
public DBObject get(){
120-
return queryop;
121-
}
180+
122181

123182
}

0 commit comments

Comments
 (0)