libmongocrypt
mc-range-mincover-generator.template.h
1 /*
2  * Copyright 2022-present MongoDB, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 // mc-range-mincover-generator.template.h is meant to be included in another
18 // source file.
19 
20 // TODO: replace `CONCAT` with `BSON_CONCAT` after libbson dependency is
21 // upgraded to 1.20.0 or higher.
22 #ifndef CONCAT
23 #define CONCAT_1(a, b) a##b
24 #define CONCAT(a, b) CONCAT_1(a, b)
25 #endif
26 // TODO: replace `CONCAT3` with `BSON_CONCAT3` after libbson dependency is
27 // upgraded to 1.20.0 or higher.
28 #ifndef CONCAT3
29 #define CONCAT3(a, b, c) CONCAT(a, CONCAT(b, c))
30 #endif
31 
32 #if !(defined(UINT_T) && defined(UINT_C) && defined(UINT_FMT_S) && defined(DECORATE_NAME))
33 #ifdef __INTELLISENSE__
34 #define UINT_T uint32_t
35 #define UINT_C UINT32_C
36 #define UINT_FMT_S PRIu32
37 #define DECORATE_NAME(Name) Name##_u32
38 #else
39 #error All of UINT_T, UINT_C, UINT_FMT_S, UINT_FMT_ARG, and DECORATE_NAME must be defined before #including this file
40 #endif
41 #endif
42 
43 #define BITS (sizeof(UINT_T) * CHAR_BIT)
44 
45 #define ZERO UINT_C(0)
46 
47 // Default for UINT_FMT_ARG
48 #ifndef UINT_FMT_ARG
49 #define UINT_FMT_ARG(X) X
50 #endif
51 
52 // Default comparison
53 #ifndef UINT_LESSTHAN
54 #define UINT_LESSTHAN(A, B) ((A) < (B))
55 #endif
56 
57 #ifndef MC_UINT_MAX
58 #define MC_UINT_MAX ~(UINT_C(0))
59 #endif
60 
61 // Default addition
62 #ifndef UINT_ADD
63 #define UINT_ADD(A, B) ((A) + (B))
64 #endif
65 #ifndef UINT_SUB
66 #define UINT_SUB(A, B) ((A) - (B))
67 #endif
68 
69 // Default lshift (also handles negatives as right-shift)
70 #ifndef UINT_LSHIFT
71 static inline UINT_T DECORATE_NAME(_mc_default_lshift)(UINT_T lhs, int off) {
72  if (off < 0) {
73  return lhs >> -off;
74  } else {
75  return lhs << off;
76  }
77 }
78 
79 #define UINT_LSHIFT DECORATE_NAME(_mc_default_lshift)
80 #endif
81 
82 #ifndef UINT_BITOR
83 #define UINT_BITOR(A, B) ((A) | (B))
84 #endif
85 
86 static inline int DECORATE_NAME(_mc_compare)(UINT_T lhs, UINT_T rhs) {
87  if (UINT_LESSTHAN(lhs, rhs)) {
88  return -1;
89  } else if (UINT_LESSTHAN(rhs, lhs)) {
90  return 1;
91  } else {
92  return 0;
93  }
94 }
95 
96 #define UINT_COMPARE DECORATE_NAME(_mc_compare)
97 
98 // MinCoverGenerator models the MinCoverGenerator type added in
99 // SERVER-68600.
100 typedef struct {
101  UINT_T _rangeMin;
102  UINT_T _rangeMax;
103  size_t _sparsity;
104  // _maxlen is the maximum bit length of edges in the mincover.
105  size_t _maxlen;
106 } DECORATE_NAME(MinCoverGenerator);
107 
108 static inline DECORATE_NAME(MinCoverGenerator)
109  * DECORATE_NAME(MinCoverGenerator_new)(UINT_T rangeMin,
110  UINT_T rangeMax,
111  UINT_T max,
112  size_t sparsity,
113  mongocrypt_status_t *status) {
114  BSON_ASSERT_PARAM(status);
115 
116  if (UINT_COMPARE(rangeMin, rangeMax) > 0) {
117  CLIENT_ERR("Range min (%" UINT_FMT_S ") must be less than or equal to range max (%" UINT_FMT_S
118  ") for range search",
119  UINT_FMT_ARG(rangeMin),
120  UINT_FMT_ARG(rangeMax));
121  return NULL;
122  }
123  if (UINT_COMPARE(rangeMax, max) > 0) {
124  CLIENT_ERR("Range max (%" UINT_FMT_S ") must be less than or equal to max (%" UINT_FMT_S ") for range search",
125  UINT_FMT_ARG(rangeMax),
126  UINT_FMT_ARG(max));
127  return NULL;
128  }
129 
130  if (sparsity == 0) {
131  CLIENT_ERR("Sparsity must be > 0");
132  return NULL;
133  }
134  DECORATE_NAME(MinCoverGenerator) *mcg = bson_malloc0(sizeof(DECORATE_NAME(MinCoverGenerator)));
135  mcg->_rangeMin = rangeMin;
136  mcg->_rangeMax = rangeMax;
137  mcg->_maxlen = (size_t)BITS - DECORATE_NAME(mc_count_leading_zeros)(max);
138  mcg->_sparsity = sparsity;
139  return mcg;
140 }
141 
142 static inline void DECORATE_NAME(MinCoverGenerator_destroy)(DECORATE_NAME(MinCoverGenerator) * mcg) {
143  bson_free(mcg);
144 }
145 
146 // applyMask applies a mask of 1 bits starting from the right.
147 // Bits 0 to bit-1 are replaced with 1. Other bits are left as-is.
148 static inline UINT_T DECORATE_NAME(applyMask)(UINT_T value, size_t maskedBits) {
149  const UINT_T ones = MC_UINT_MAX;
150 
151  BSON_ASSERT(maskedBits <= (size_t)BITS);
152  BSON_ASSERT(maskedBits >= 0);
153 
154  if (maskedBits == 0) {
155  return value;
156  }
157 
158  const size_t shift = ((size_t)BITS - maskedBits);
159  const UINT_T mask = UINT_LSHIFT(ones, -(int)shift);
160  return UINT_BITOR(value, mask);
161 }
162 
163 static inline bool DECORATE_NAME(MinCoverGenerator_isLevelStored)(DECORATE_NAME(MinCoverGenerator) * mcg,
164  size_t maskedBits) {
165  BSON_ASSERT_PARAM(mcg);
166  size_t level = mcg->_maxlen - maskedBits;
167  return 0 == maskedBits || 0 == (level % mcg->_sparsity);
168 }
169 
170 char *
171 DECORATE_NAME(MinCoverGenerator_toString)(DECORATE_NAME(MinCoverGenerator) * mcg, UINT_T start, size_t maskedBits) {
172  BSON_ASSERT_PARAM(mcg);
173  BSON_ASSERT(maskedBits <= mcg->_maxlen);
174  BSON_ASSERT(maskedBits <= (size_t)BITS);
175  BSON_ASSERT(maskedBits >= 0);
176 
177  if (maskedBits == mcg->_maxlen) {
178  return bson_strdup("root");
179  }
180 
181  UINT_T shifted = UINT_LSHIFT(start, -(int)maskedBits);
182  mc_bitstring valueBin = DECORATE_NAME(mc_convert_to_bitstring)(shifted);
183  char *ret = bson_strndup(valueBin.str + ((size_t)BITS - mcg->_maxlen + maskedBits), mcg->_maxlen + maskedBits);
184  return ret;
185 }
186 
187 static inline void DECORATE_NAME(MinCoverGenerator_minCoverRec)(DECORATE_NAME(MinCoverGenerator) * mcg,
188  mc_array_t *c,
189  UINT_T blockStart,
190  size_t maskedBits) {
191  BSON_ASSERT_PARAM(mcg);
192  BSON_ASSERT_PARAM(c);
193  const UINT_T blockEnd = DECORATE_NAME(applyMask)(blockStart, maskedBits);
194 
195  if (UINT_COMPARE(blockEnd, mcg->_rangeMin) < 0 || UINT_COMPARE(blockStart, mcg->_rangeMax) > 0) {
196  return;
197  }
198 
199  if (UINT_COMPARE(blockStart, mcg->_rangeMin) >= 0 && UINT_COMPARE(blockEnd, mcg->_rangeMax) <= 0
200  && DECORATE_NAME(MinCoverGenerator_isLevelStored)(mcg, maskedBits)) {
201  char *edge = DECORATE_NAME(MinCoverGenerator_toString)(mcg, blockStart, maskedBits);
202  _mc_array_append_val(c, edge);
203  return;
204  }
205 
206  BSON_ASSERT(maskedBits > 0);
207 
208  const size_t newBits = maskedBits - 1u;
209  DECORATE_NAME(MinCoverGenerator_minCoverRec)(mcg, c, blockStart, newBits);
210  DECORATE_NAME(MinCoverGenerator_minCoverRec)
211  (mcg, c, UINT_BITOR(blockStart, UINT_LSHIFT(UINT_C(1), (int)newBits)), newBits);
212 }
213 
214 static inline mc_mincover_t *DECORATE_NAME(MinCoverGenerator_minCover)(DECORATE_NAME(MinCoverGenerator) * mcg) {
215  BSON_ASSERT_PARAM(mcg);
216  mc_mincover_t *mc = mc_mincover_new();
217  DECORATE_NAME(MinCoverGenerator_minCoverRec)
218  (mcg, &mc->mincover, ZERO, mcg->_maxlen);
219  return mc;
220 }
221 
222 // adjustBounds increments *lowerBound if includeLowerBound is false and
223 // decrements *upperBound if includeUpperBound is false.
224 // lowerBound, min, upperBound, and max are expected to come from the result
225 // of mc_getTypeInfo.
226 static bool DECORATE_NAME(adjustBounds)(UINT_T *lowerBound,
227  bool includeLowerBound,
228  UINT_T min,
229  UINT_T *upperBound,
230  bool includeUpperBound,
231  UINT_T max,
232  mongocrypt_status_t *status) {
233  BSON_ASSERT_PARAM(lowerBound);
234  BSON_ASSERT_PARAM(upperBound);
235 
236  if (!includeLowerBound) {
237  if (UINT_COMPARE(*lowerBound, max) >= 0) {
238  CLIENT_ERR("Lower bound (%" UINT_FMT_S ") must be less than the range maximum (%" UINT_FMT_S
239  ") if lower bound is excluded from range.",
240  UINT_FMT_ARG(*lowerBound),
241  UINT_FMT_ARG(max));
242  return false;
243  }
244  *lowerBound = UINT_ADD(*lowerBound, UINT_C(1));
245  }
246  if (!includeUpperBound) {
247  if (UINT_COMPARE(*upperBound, min) <= 0) {
248  CLIENT_ERR("Upper bound (%" UINT_FMT_S ") must be greater than the range minimum (%" UINT_FMT_S
249  ") if upper bound is excluded from range.",
250  UINT_FMT_ARG(*upperBound),
251  UINT_FMT_ARG(min));
252  return false;
253  }
254  *upperBound = UINT_SUB(*upperBound, UINT_C(1));
255  }
256  return true;
257 }
258 
259 #undef UINT_T
260 #undef UINT_C
261 #undef UINT_FMT_S
262 #undef UINT_FMT_ARG
263 #undef DECORATE_NAME
264 #undef BITS
265 #undef UINT_COMPARE
266 #undef UINT_ADD
267 #undef UINT_SUB
268 #undef UINT_LSHIFT
269 #undef UINT_BITOR
270 #undef MC_UINT_MAX
271 #undef ZERO
272 #undef UINT_LESSTHAN
Definition: mc-range-mincover-generator.template.h:100
struct _mongocrypt_status_t mongocrypt_status_t
Definition: mongocrypt.h:152