MongoDB C++ Driver legacy-1.1.2
Loading...
Searching...
No Matches
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
23namespace mongo {
24
25// todo: ideally move to db/ instead of bson/, but elim any dependencies first
26
32class Ordering {
33 unsigned bits;
34 Ordering(unsigned b) : bits(b) {}
35
36public:
37 Ordering(const Ordering& r) : bits(r.bits) {}
38 void operator=(const Ordering& r) {
39 bits = r.bits;
40 }
41
46 int get(int i) const {
47 return ((1 << i) & bits) ? -1 : 1;
48 }
49
50 // for woCompare...
51 unsigned descending(unsigned mask) const {
52 return bits & mask;
53 }
54
55 /*operator std::string() const {
56 StringBuilder buf;
57 for ( unsigned i=0; i<nkeys; i++)
58 buf.append( get(i) > 0 ? "+" : "-" );
59 return buf.str();
60 }*/
61
62 static Ordering make(const BSONObj& obj) {
63 unsigned b = 0;
64 BSONObjIterator k(obj);
65 unsigned n = 0;
66 while (1) {
67 BSONElement e = k.next();
68 if (e.eoo())
69 break;
70 uassert(13103, "too many compound keys", n <= 31);
71 if (e.number() < 0)
72 b |= (1 << n);
73 n++;
74 }
75 return Ordering(b);
76 }
77};
78}
A precomputation of a BSON index or sort key pattern.
Definition ordering.h:32
int get(int i) const
so, for key pattern { a : 1, b : -1 } get(0) == 1 get(1) == -1
Definition ordering.h:46
Utility functions for parsing numbers from strings.
Definition compare_numbers.h:20