summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDwight <dmerriman@gmail.com>2008-08-19 17:57:05 -0400
committerDwight <dmerriman@gmail.com>2008-08-19 17:57:05 -0400
commitd3099f01ab45dd8a91e57238695ca98f4659543e (patch)
tree29b864d83c35332142e0c2401365dacc1674e325
parent53c3f540b648880fc43a8b28bd2459a8f1868463 (diff)
downloadmongo-d3099f01ab45dd8a91e57238695ca98f4659543e.tar.gz
beginnings of some sorting stuff
-rw-r--r--db/db.vcproj4
-rw-r--r--db/query.cpp24
-rw-r--r--db/scanandorder.h27
3 files changed, 51 insertions, 4 deletions
diff --git a/db/db.vcproj b/db/db.vcproj
index 94d10bcc2a9..9be950141d0 100644
--- a/db/db.vcproj
+++ b/db/db.vcproj
@@ -361,6 +361,10 @@
>
</File>
<File
+ RelativePath=".\scanandorder.h"
+ >
+ </File>
+ <File
RelativePath="..\util\sock.h"
>
</File>
diff --git a/db/query.cpp b/db/query.cpp
index b6deac60b38..7e84fc9814a 100644
--- a/db/query.cpp
+++ b/db/query.cpp
@@ -28,6 +28,7 @@
#include "javajs.h"
#include "json.h"
#include "repl.h"
+#include "scanandorder.h"
/* We cut off further objects once we cross this threshold; thus, you might get
a little bit more than this, it is a threshold rather than a limit.
@@ -63,7 +64,7 @@ int runCount(const char *ns, JSObj& cmd, string& err);
simpleKeyMatch - set to true if the query is purely for a single key value
unchanged otherwise.
*/
-auto_ptr<Cursor> getIndexCursor(const char *ns, JSObj& query, JSObj& order, bool *simpleKeyMatch = 0) {
+auto_ptr<Cursor> getIndexCursor(const char *ns, JSObj& query, JSObj& order, bool *simpleKeyMatch = 0, bool *isSorted = 0) {
NamespaceDetails *d = nsdetails(ns);
if( d == 0 ) return auto_ptr<Cursor>();
@@ -90,6 +91,8 @@ auto_ptr<Cursor> getIndexCursor(const char *ns, JSObj& query, JSObj& order, bool
specified!
*/
DEV cout << " using index " << d->indexes[i].indexNamespace() << '\n';
+ if( isSorted )
+ *isSorted = true;
return auto_ptr<Cursor>(new BtreeCursor(d->indexes[i], reverse ? maxKey : emptyObj, reverse ? -1 : 1, false));
}
}
@@ -940,7 +943,7 @@ QueryResult* runQuery(Message& message, const char *ns, int ntoskip, int _ntoret
time_t t = time(0);
bool wantMore = true;
int ntoreturn = _ntoreturn;
- if( _ntoreturn < 0 ) {
+ if( _ntoreturn < 0 ) {
ntoreturn = -_ntoreturn;
wantMore = false;
}
@@ -979,13 +982,24 @@ QueryResult* runQuery(Message& message, const char *ns, int ntoskip, int _ntoret
JSMatcher &debug1 = *matcher;
assert( debug1.getN() < 5000 );
+ bool isSorted = false;
int nscanned = 0;
auto_ptr<Cursor> c = getSpecialCursor(ns);
if( c.get() == 0 )
- c = getIndexCursor(ns, query, order);
+ c = getIndexCursor(ns, query, order, 0, &isSorted);
if( c.get() == 0 )
c = findTableScan(ns, order);
+ auto_ptr<ScanAndOrder> so;
+ bool ordering = false;
+ if( !order.isEmpty() && !isSorted ) {
+ ordering = true;
+ ss << " scanAndOrder ";
+ so = auto_ptr<ScanAndOrder>(new ScanAndOrder());
+ wantMore = false;
+ // scanAndOrder(b, c.get(), order, ntoreturn);
+ }
+
while( c->ok() ) {
JSObj js = c->current();
if( queryTraceLevel >= 50 )
@@ -1005,7 +1019,9 @@ QueryResult* runQuery(Message& message, const char *ns, int ntoskip, int _ntoret
else {
bool ok = true;
assert( js.objsize() >= 0 ); //defensive for segfaults
- if( filter.get() ) {
+ /*if( ordering )
+ so->add(js);
+ else*/ if( filter.get() ) {
// we just want certain fields from the object.
JSObj x;
ok = x.addFields(js, *filter) > 0;
diff --git a/db/scanandorder.h b/db/scanandorder.h
new file mode 100644
index 00000000000..c950b8b11c5
--- /dev/null
+++ b/db/scanandorder.h
@@ -0,0 +1,27 @@
+// scanandorder.h
+
+#pragma once
+
+/* todo:
+ _ compound keys
+ _ reverse direction
+ _ limit amount of data
+*/
+/*
+void scanAndOrder(BufBuilder& b, Cursor *c, JSObj order, int ntoreturn) {
+ map<JSObj,DiskLoc> best; // key -> obj
+
+ set<string> orderFields;
+ order.getFieldNames(orderFields);
+
+
+}
+
+
+*/
+
+class ScanAndOrder {
+public:
+ void add(JSObj) { }
+};
+