00001 // shardkey.h 00002 00019 #pragma once 00020 00021 #include "../client/dbclient.h" 00022 00023 namespace mongo { 00024 00025 class Chunk; 00026 00027 /* A ShardKeyPattern is a pattern indicating what data to extract from the object to make the shard key from. 00028 Analogous to an index key pattern. 00029 */ 00030 class ShardKeyPattern { 00031 public: 00032 ShardKeyPattern( BSONObj p = BSONObj() ); 00033 00038 BSONObj globalMin() const { return gMin; } 00039 00043 BSONObj globalMax() const { return gMax; } 00044 00045 bool isGlobalMin( const BSONObj& k ) const { 00046 return k.woCompare( globalMin() ) == 0; 00047 } 00048 00049 bool isGlobalMax( const BSONObj& k ) const { 00050 return k.woCompare( globalMax() ) == 0; 00051 } 00052 00053 bool isGlobal( const BSONObj& k ) const { 00054 return isGlobalMin( k ) || isGlobalMax( k ); 00055 } 00056 00062 int compare( const BSONObj& l , const BSONObj& r ) const; 00063 00069 bool hasShardKey( const BSONObj& obj ) const; 00070 00071 BSONObj key() const { return pattern; } 00072 00073 string toString() const; 00074 00075 BSONObj extractKey(const BSONObj& from) const; 00076 00077 bool partOfShardKey(const char* key ) const { 00078 return pattern.hasField(key); 00079 } 00080 bool partOfShardKey(const string& key ) const { 00081 return pattern.hasField(key.c_str()); 00082 } 00083 00088 bool isPrefixOf( const BSONObj& otherPattern ) const; 00089 00093 BSONObj moveToFront(const BSONObj& obj) const; 00094 00095 private: 00096 BSONObj pattern; 00097 BSONObj gMin; 00098 BSONObj gMax; 00099 00100 /* question: better to have patternfields precomputed or not? depends on if we use copy constructor often. */ 00101 set<string> patternfields; 00102 }; 00103 00104 inline BSONObj ShardKeyPattern::extractKey(const BSONObj& from) const { 00105 BSONObj k = from.extractFields(pattern); 00106 uassert(13334, "Shard Key must be less than 512 bytes", k.objsize() < 512); 00107 return k; 00108 } 00109 00110 }