MongoDB  2.6.0
ordering.h
1 // ordering.h
2 
3 /* Copyright 2009 10gen Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include "mongo/bson/bsonobj.h"
21 #include "mongo/bson/bsonobjiterator.h"
22 
23 namespace mongo {
24 
25  // todo: ideally move to db/ instead of bson/, but elim any dependencies first
26 
32  class Ordering {
33  unsigned bits;
34  Ordering(unsigned b) : bits(b) { }
35  public:
36  Ordering(const Ordering& r) : bits(r.bits) { }
37  void operator=(const Ordering& r) {
38  bits = r.bits;
39  }
40 
45  int get(int i) const {
46  return ((1 << i) & bits) ? -1 : 1;
47  }
48 
49  // for woCompare...
50  unsigned descending(unsigned mask) const { return bits & mask; }
51 
52  /*operator std::string() const {
53  StringBuilder buf;
54  for ( unsigned i=0; i<nkeys; i++)
55  buf.append( get(i) > 0 ? "+" : "-" );
56  return buf.str();
57  }*/
58 
59  static Ordering make(const BSONObj& obj) {
60  unsigned b = 0;
61  BSONObjIterator k(obj);
62  unsigned n = 0;
63  while( 1 ) {
64  BSONElement e = k.next();
65  if( e.eoo() )
66  break;
67  uassert( 13103, "too many compound keys", n <= 31 );
68  if( e.number() < 0 )
69  b |= (1 << n);
70  n++;
71  }
72  return Ordering(b);
73  }
74  };
75 
76 }
A precomputation of a BSON index or sort key pattern.
Definition: ordering.h:32