summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron <aaron@10gen.com>2009-02-09 17:14:18 -0500
committerAaron <aaron@10gen.com>2009-02-09 17:14:18 -0500
commit16652466ec7aeb9e17060ea95644eaf3e98bc78c (patch)
treeda876b47b919d9f69fed826dc0f63394b909863a
parent8476a12040ac53ad70388d00aabc32a4a1ea21c0 (diff)
downloadmongo-16652466ec7aeb9e17060ea95644eaf3e98bc78c.tar.gz
Refactor hint code
-rw-r--r--db/btree.h4
-rw-r--r--db/btreecursor.cpp2
-rw-r--r--db/namespace.h2
-rw-r--r--db/query.cpp37
4 files changed, 19 insertions, 26 deletions
diff --git a/db/btree.h b/db/btree.h
index c2652b26140..e82b38b6ce0 100644
--- a/db/btree.h
+++ b/db/btree.h
@@ -200,7 +200,7 @@ namespace mongo {
BSONObj endKey;
// BSONObj query; // the query we are working on in association with the cursor -- see noMoreMatches()
public:
- BtreeCursor(IndexDetails&, const BSONObj& startKey, int direction, const BSONObj& query);
+ BtreeCursor(const IndexDetails&, const BSONObj& startKey, int direction, const BSONObj& query);
virtual bool ok() {
return !bucket.isNull();
}
@@ -283,7 +283,7 @@ namespace mongo {
static string simpleRegexEnd( string regex );
- IndexDetails& indexDetails;
+ const IndexDetails& indexDetails;
BSONObj order;
DiskLoc bucket;
int keyOfs;
diff --git a/db/btreecursor.cpp b/db/btreecursor.cpp
index 62b73aa30cf..4e0c575dcd6 100644
--- a/db/btreecursor.cpp
+++ b/db/btreecursor.cpp
@@ -28,7 +28,7 @@ namespace mongo {
DiskLoc maxDiskLoc(0x7fffffff, 0x7fffffff);
DiskLoc minDiskLoc(0, 1);
- BtreeCursor::BtreeCursor(IndexDetails& _id, const BSONObj& k, int _direction, const BSONObj& _query) :
+ BtreeCursor::BtreeCursor(const IndexDetails& _id, const BSONObj& k, int _direction, const BSONObj& _query) :
// query(_query),
indexDetails(_id),
order(_id.keyPattern()),
diff --git a/db/namespace.h b/db/namespace.h
index 94af22cd94d..ea99c21a00c 100644
--- a/db/namespace.h
+++ b/db/namespace.h
@@ -142,7 +142,7 @@ namespace mongo {
{ x : 70, y : 3 } -> { x : 70 }
handles our embedded dot notation too.
*/
- BSONObj getKeyFromQuery(const BSONObj& query) {
+ BSONObj getKeyFromQuery(const BSONObj& query) const {
BSONObj k = keyPattern();
BSONObj res = query.extractFieldsUnDotted(k);
assert(res.objsize() != 0); // guard against a seg fault if details is 0
diff --git a/db/query.cpp b/db/query.cpp
index b88ae843692..33def1372ec 100644
--- a/db/query.cpp
+++ b/db/query.cpp
@@ -74,6 +74,19 @@ namespace mongo {
}
}
+ auto_ptr< Cursor > getHintCursor( const IndexDetails &ii, BSONObj query, BSONObj order, bool *simpleKeyMatch, bool *isSorted ) {
+ int direction = matchDirection( ii.keyPattern(), order );
+ if ( isSorted ) *isSorted = ( direction != 0 );
+ if ( direction == 0 )
+ direction = 1;
+ // NOTE This startKey is incorrect, preserving for the moment so
+ // we can preserve simpleKeyMatch behavior.
+ BSONObj startKey = ii.getKeyFromQuery(query);
+ if ( simpleKeyMatch )
+ *simpleKeyMatch = query.nFields() == startKey.nFields();
+ return auto_ptr<Cursor>(new BtreeCursor(ii, emptyObj, direction, query));
+ }
+
/* todo: _ use index on partial match with the query
parameters
@@ -95,17 +108,7 @@ namespace mongo {
for (int i = 0; i < d->nIndexes; i++ ) {
IndexDetails& ii = d->indexes[i];
if ( ii.indexName() == hintstr ) {
- int direction = matchDirection( ii.keyPattern(), order );
- if ( isSorted ) *isSorted = ( direction != 0 );
- if ( direction == 0 )
- direction = 1;
- // NOTE This startKey is incorrect, preserving for the moment so
- // we can preserve simpleKeyMatch behavior.
- BSONObj startKey = ii.getKeyFromQuery(query);
- if ( simpleKeyMatch )
- *simpleKeyMatch = query.nFields() == startKey.nFields();
- return auto_ptr<Cursor>(
- new BtreeCursor(ii, emptyObj, direction, query));
+ return getHintCursor( ii, query, order, simpleKeyMatch, isSorted );
}
}
}
@@ -114,17 +117,7 @@ namespace mongo {
for (int i = 0; i < d->nIndexes; i++ ) {
IndexDetails& ii = d->indexes[i];
if( ii.keyPattern().woCompare(hintobj) == 0 ) {
- int direction = matchDirection( ii.keyPattern(), order );
- if ( isSorted ) *isSorted = ( direction != 0 );
- if ( direction == 0 )
- direction = 1;
- // NOTE This startKey is incorrect, preserving for the moment so
- // we can preserve simpleKeyMatch behavior.
- BSONObj startKey = ii.getKeyFromQuery(query);
- if ( simpleKeyMatch )
- *simpleKeyMatch = query.nFields() == startKey.nFields();
- return auto_ptr<Cursor>(
- new BtreeCursor(ii, emptyObj, direction, query));
+ return getHintCursor( ii, query, order, simpleKeyMatch, isSorted );
}
}
}