~ubuntu-branches/ubuntu/trusty/mongodb/trusty-proposed

« back to all changes in this revision

Viewing changes to s/shardkey.h

  • Committer: Bazaar Package Importer
  • Author(s): Antonin Kral
  • Date: 2010-01-29 19:48:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100129194845-8wbmkf626fwcavc9
Tags: upstream-1.3.1
ImportĀ upstreamĀ versionĀ 1.3.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// shardkey.h
 
2
 
 
3
/**
 
4
*    Copyright (C) 2008 10gen Inc.
 
5
*
 
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.
 
9
*
 
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.
 
14
*
 
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/>.
 
17
*/
 
18
 
 
19
#pragma once
 
20
 
 
21
#include "../client/dbclient.h"
 
22
 
 
23
namespace mongo {
 
24
    
 
25
    class Chunk;
 
26
 
 
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.
 
29
    */
 
30
    class ShardKeyPattern {
 
31
    public:
 
32
        ShardKeyPattern( BSONObj p = BSONObj() );
 
33
        
 
34
        /**
 
35
           global min is the lowest possible value for this key
 
36
                   e.g. { num : MinKey }
 
37
         */
 
38
        BSONObj globalMin() const { return gMin; }
 
39
 
 
40
        /**
 
41
           global max is the highest possible value for this key
 
42
         */
 
43
        BSONObj globalMax() const { return gMax; }
 
44
 
 
45
        bool isGlobalMin( const BSONObj& k ){
 
46
            return k.woCompare( globalMin() ) == 0;
 
47
        }
 
48
 
 
49
        bool isGlobalMax( const BSONObj& k ){
 
50
            return k.woCompare( globalMax() ) == 0;
 
51
        }
 
52
        
 
53
        bool isGlobal( const BSONObj& k ){
 
54
            return isGlobalMin( k ) || isGlobalMax( k );
 
55
        }
 
56
 
 
57
        /** compare shard keys from the objects specified
 
58
           l < r negative
 
59
           l == r 0
 
60
           l > r positive
 
61
         */
 
62
        int compare( const BSONObj& l , const BSONObj& r );
 
63
        
 
64
        /**
 
65
           @return whether or not obj has all fields in this shard key pattern
 
66
                   e.g. 
 
67
                     ShardKey({num:1}).hasShardKey({ name:"joe", num:3 }) is true
 
68
         */
 
69
        bool hasShardKey( const BSONObj& obj );
 
70
        
 
71
        /**
 
72
           returns a query that filters results only for the range desired, i.e. returns 
 
73
             { "field" : { $gte: keyval(min), $lt: keyval(max) } }
 
74
        */
 
75
        void getFilter( BSONObjBuilder& b , const BSONObj& min, const BSONObj& max );
 
76
        
 
77
        /** @return true if shard s is relevant for query q.
 
78
 
 
79
            Example:
 
80
              q:     { x : 3 }
 
81
              *this: { x : 1 }
 
82
              s:     x:2..x:7
 
83
               -> true
 
84
         */
 
85
        bool relevantForQuery( const BSONObj& q , Chunk * s );
 
86
        
 
87
        /**
 
88
           Returns if the given sort pattern can be ordered by the shard key pattern.
 
89
           Example
 
90
            sort:   { ts: -1 }
 
91
            *this:  { ts:1 }
 
92
              -> -1
 
93
 
 
94
              @return
 
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
 
98
         */
 
99
        int canOrder( const BSONObj& sort );
 
100
 
 
101
        BSONObj key() { return pattern; }
 
102
 
 
103
        string toString() const;
 
104
 
 
105
        BSONObj extractKey(const BSONObj& from) const;
 
106
        
 
107
        bool partOfShardKey(const string& key ) const {
 
108
            return patternfields.count( key ) > 0;
 
109
        }
 
110
 
 
111
        operator string() const {
 
112
            return pattern.toString();
 
113
        }
 
114
    private:
 
115
        BSONObj pattern;
 
116
        BSONObj gMin;
 
117
        BSONObj gMax;
 
118
 
 
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);
 
122
    };
 
123
 
 
124
    inline BSONObj ShardKeyPattern::extractKey(const BSONObj& from) const { 
 
125
        return from.extractFields(pattern);
 
126
    }
 
127
 
 
128