4
* Copyright (C) 2008 10gen Inc.
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Affero General Public License, version 3,
8
* as published by the Free Software Foundation.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU Affero General Public License for more details.
15
* You should have received a copy of the GNU Affero General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "../client/dbclient.h"
27
/* A ShardKeyPattern is a pattern indicating what data to extract from the object to make the shard key from.
28
Analogous to an index key pattern.
30
class ShardKeyPattern {
32
ShardKeyPattern( BSONObj p = BSONObj() );
35
global min is the lowest possible value for this key
38
BSONObj globalMin() const { return gMin; }
41
global max is the highest possible value for this key
43
BSONObj globalMax() const { return gMax; }
45
bool isGlobalMin( const BSONObj& k ){
46
return k.woCompare( globalMin() ) == 0;
49
bool isGlobalMax( const BSONObj& k ){
50
return k.woCompare( globalMax() ) == 0;
53
bool isGlobal( const BSONObj& k ){
54
return isGlobalMin( k ) || isGlobalMax( k );
57
/** compare shard keys from the objects specified
62
int compare( const BSONObj& l , const BSONObj& r );
65
@return whether or not obj has all fields in this shard key pattern
67
ShardKey({num:1}).hasShardKey({ name:"joe", num:3 }) is true
69
bool hasShardKey( const BSONObj& obj );
72
returns a query that filters results only for the range desired, i.e. returns
73
{ "field" : { $gte: keyval(min), $lt: keyval(max) } }
75
void getFilter( BSONObjBuilder& b , const BSONObj& min, const BSONObj& max );
77
/** @return true if shard s is relevant for query q.
85
bool relevantForQuery( const BSONObj& q , Chunk * s );
88
Returns if the given sort pattern can be ordered by the shard key pattern.
95
0 if sort either doesn't have all the fields or has extra fields
96
< 0 if sort is descending
97
> 1 if sort is ascending
99
int canOrder( const BSONObj& sort );
101
BSONObj key() { return pattern; }
103
string toString() const;
105
BSONObj extractKey(const BSONObj& from) const;
107
bool partOfShardKey(const string& key ) const {
108
return patternfields.count( key ) > 0;
111
operator string() const {
112
return pattern.toString();
119
/* question: better to have patternfields precomputed or not? depends on if we use copy contructor often. */
120
set<string> patternfields;
121
bool relevant(const BSONObj& query, const BSONObj& L, const BSONObj& R);
124
inline BSONObj ShardKeyPattern::extractKey(const BSONObj& from) const {
125
return from.extractFields(pattern);