diff options
author | Mathias Stearn <mathias@10gen.com> | 2010-09-22 16:39:23 -0400 |
---|---|---|
committer | Mathias Stearn <mathias@10gen.com> | 2010-09-22 17:50:50 -0400 |
commit | 456493083e22b6e1e81f25f57a8962d9716fcf82 (patch) | |
tree | 35b625d326d4b3731d2abfb7e54cbe53d6fc29e0 /s/strategy_shard.cpp | |
parent | 461aa87d0cf278da8017d79e0c56a97b44d251cf (diff) | |
download | mongo-456493083e22b6e1e81f25f57a8962d9716fcf82.tar.gz |
move code into ShardKeyPattern::moveToFront() and add unit tests. SERVER-1540
Diffstat (limited to 's/strategy_shard.cpp')
-rw-r--r-- | s/strategy_shard.cpp | 60 |
1 files changed, 2 insertions, 58 deletions
diff --git a/s/strategy_shard.cpp b/s/strategy_shard.cpp index f269e9c4bdb..9124d28ddc4 100644 --- a/s/strategy_shard.cpp +++ b/s/strategy_shard.cpp @@ -146,65 +146,9 @@ namespace mongo { } - { - // Move Shard Key to front of object - // Current implementation only works on top-level keys - const ShardKeyPattern& sk = manager->getShardKey(); - - vector<const char*> keysToMove; - keysToMove.push_back("_id"); - BSONForEach(e, sk.key()){ - if (strchr(e.fieldName(), '.') == NULL) - keysToMove.push_back(e.fieldName()); - } - - if (keysToMove.size() > 1){ - BufBuilder buf (o.objsize()); - buf.appendNum(o.objsize()); - - vector<pair<const char*, size_t> > copies; - pair<const char*, size_t> toCopy (NULL, 0); - - BSONForEach(e, o){ - bool moveToFront = false; - for (vector<const char*>::const_iterator it(keysToMove.begin()), end(keysToMove.end()); it!=end; ++it){ - if (strcmp(e.fieldName(), *it) == 0){ - moveToFront = true; - break; - } - } - - if (moveToFront){ - buf.appendBuf(e.fieldName()-1, e.size()); - if (toCopy.first){ - copies.push_back(toCopy); - toCopy.first = NULL; - } - } else { - if (!toCopy.first){ - toCopy.first = e.fieldName()-1; - toCopy.second = e.size(); - } else { - toCopy.second += e.size(); - } - } - } - - for (vector<pair<const char*, size_t> >::const_iterator it(copies.begin()), end(copies.end()); it!=end; ++it){ - buf.appendBuf(it->first, it->second); - } + // Many operations benefit from having the shard key early in the object + o = manager->getShardKey().moveToFront(o); - if (toCopy.first){ - buf.appendBuf(toCopy.first, toCopy.second); - } - - buf.appendChar('\0'); - - o = BSONObj(buf.buf(), true); - buf.decouple(); - } - } - bool gotThrough = false; for ( int i=0; i<10; i++ ){ try { |