summaryrefslogtreecommitdiff
path: root/src/mongo/scripting/bson_template_evaluator.cpp
blob: 37a1bffec8497da2072b9ccfc456ae5f33fc9642 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/scripting/bson_template_evaluator.h"

#include <cstddef>
#include <cstdlib>

#include "mongo/base/static_assert.h"
#include "mongo/util/str.h"

namespace mongo {

using std::string;

void BsonTemplateEvaluator::initializeEvaluator() {
    addOperator("RAND_INT", &BsonTemplateEvaluator::evalRandInt);
    addOperator("RAND_INT_PLUS_THREAD", &BsonTemplateEvaluator::evalRandPlusThread);
    addOperator("SEQ_INT", &BsonTemplateEvaluator::evalSeqInt);
    addOperator("RAND_STRING", &BsonTemplateEvaluator::evalRandString);
    addOperator("CONCAT", &BsonTemplateEvaluator::evalConcat);
    addOperator("OID", &BsonTemplateEvaluator::evalObjId);
    addOperator("VARIABLE", &BsonTemplateEvaluator::evalVariable);
    addOperator("CUR_DATE", &BsonTemplateEvaluator::evalCurrentDate);
}

BsonTemplateEvaluator::BsonTemplateEvaluator(int64_t seed) : _id(0), rng(seed) {
    initializeEvaluator();
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::setId(size_t id) {
    if (id >= (1 << 7))
        // The id is too large--doesn't fit inside 7 bits.
        return StatusOpEvaluationError;
    this->_id = id;
    return StatusSuccess;
}

BsonTemplateEvaluator::~BsonTemplateEvaluator() {}

void BsonTemplateEvaluator::addOperator(const std::string& name, const OperatorFn& op) {
    _operatorFunctions[name] = op;
}

BsonTemplateEvaluator::OperatorFn BsonTemplateEvaluator::operatorEvaluator(
    const std::string& op) const {
    auto iter = _operatorFunctions.find(op);
    return iter == _operatorFunctions.end() ? nullptr : iter->second;
}

/* This is the top level method for using this library. It takes a BSON Object as input,
 * evaluates the templates and saves the result in the builder object.
 * The method returns appropriate Status on success/error condition.
 */
BsonTemplateEvaluator::Status BsonTemplateEvaluator::evaluate(const BSONObj& in,
                                                              BSONObjBuilder& builder) {
    BSONForEach(e, in) {
        Status st = _evalElem(e, builder);
        if (st != StatusSuccess)
            return st;
    }
    return StatusSuccess;
}

/* Sets the variable
 */
void BsonTemplateEvaluator::setVariable(const std::string& name, const BSONElement& elem) {
    this->_varMap[name] = elem.wrap();
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::_evalElem(const BSONElement in,
                                                               BSONObjBuilder& out) {
    if (in.type() == Array) {
        BSONArrayBuilder arrayBuilder(out.subarrayStart(in.fieldName()));
        std::vector<BSONElement> arrElems = in.Array();
        for (unsigned int i = 0; i < arrElems.size(); i++) {
            BSONElement element = arrElems[i];
            if (element.isABSONObj()) {
                BSONObjBuilder newBuilder(element.objsize());
                Status st = _evalElem(element, newBuilder);
                if (st != StatusSuccess)
                    return st;
                // Only want to append the field value, not the whole object
                arrayBuilder.append(newBuilder.done().getField(element.fieldName()));
            } else {
                arrayBuilder.append(element);
            }
        }
        arrayBuilder.done();
        return StatusSuccess;
    }

    if (in.type() != Object) {
        out.append(in);
        return StatusSuccess;
    }

    // continue evaluation on subObject
    BSONObj subObj = in.embeddedObject();

    BSONObjBuilder newBuilder(subObj.objsize() + 128);
    Status st = _evalObj(subObj, newBuilder);
    if (st != StatusSuccess)
        return st;

    BSONObj updatedObj = newBuilder.obj();

    // check if updated object needs template evaluation
    const char* opOrNot = updatedObj.firstElementFieldName();
    if (opOrNot[0] == '#') {
        const char* op = opOrNot + 1;
        OperatorFn fn = operatorEvaluator(op);
        if (!fn)
            return StatusBadOperator;
        Status st = fn(this, in.fieldName(), updatedObj, out);
        if (st != StatusSuccess)
            return st;
    } else {
        out.append(in.fieldName(), updatedObj);
    }
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::_evalObj(const BSONObj& in,
                                                              BSONObjBuilder& out) {
    BSONForEach(e, in) {
        Status st = _evalElem(e, out);
        if (st != StatusSuccess)
            return st;
    }
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalRandInt(BsonTemplateEvaluator* btl,
                                                                 const char* fieldName,
                                                                 const BSONObj& in,
                                                                 BSONObjBuilder& out) {
    // in = { #RAND_INT: [10, 20] }
    BSONObj range = in.firstElement().embeddedObject();
    if (!range["0"].isNumber() || !range["1"].isNumber())
        return StatusOpEvaluationError;
    const int min = range["0"].numberInt();
    const int max = range["1"].numberInt();
    if (max <= min)
        return StatusOpEvaluationError;
    // range of max-min
    int randomNum = min + (btl->rng.nextInt32(max - min));
    if (range.nFields() == 3) {
        if (!range[2].isNumber())
            return StatusOpEvaluationError;
        randomNum *= range[2].numberInt();
    }
    out.append(fieldName, randomNum);
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalRandPlusThread(BsonTemplateEvaluator* btl,
                                                                        const char* fieldName,
                                                                        const BSONObj& in,
                                                                        BSONObjBuilder& out) {
    // in = { #RAND_INT_PLUS_THREAD: [10, 20] }
    BSONObj range = in.firstElement().embeddedObject();
    if (!range["0"].isNumber() || !range["1"].isNumber())
        return StatusOpEvaluationError;
    const int min = range["0"].numberInt();
    const int max = range["1"].numberInt();
    if (max <= min)
        return StatusOpEvaluationError;
    int randomNum = min + (btl->rng.nextInt32(max - min));
    randomNum += ((max - min) * btl->_id);
    out.append(fieldName, randomNum);
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalSeqInt(BsonTemplateEvaluator* btl,
                                                                const char* fieldName,
                                                                const BSONObj& in,
                                                                BSONObjBuilder& out) {
    // in = { #SEQ_INT: { seq_id: 0, start: 10, step: -2 }
    BSONObj spec = in.firstElement().embeddedObject();
    if (spec.nFields() < 3)
        return StatusOpEvaluationError;
    if (spec["seq_id"].eoo() || !spec["seq_id"].isNumber())
        return StatusOpEvaluationError;
    if (spec["start"].eoo() || !spec["start"].isNumber())
        return StatusOpEvaluationError;
    if (spec["step"].eoo() || !spec["step"].isNumber())
        return StatusOpEvaluationError;

    // If we're here, then we have a well-formed SEQ_INT specification:
    // seq_id, start, and step fields, which are all numbers.

    int seq_id = spec["seq_id"].numberInt();
    long long curr_seqval = spec["start"].numberInt();

    // Handle the optional "unique" argument.
    //
    // If the test requires us to keep sequences unique between different
    // worker threads, then put the id number of this evaluator in the
    // high order byte.
    if (!spec["unique"].eoo() && spec["unique"].trueValue()) {
        long long workerid = btl->_id;
        curr_seqval += (workerid << ((sizeof(long long) - 1) * 8));
    }

    if (btl->_seqIdMap.end() != btl->_seqIdMap.find(seq_id)) {
        // We already have a sequence value. Add 'step' to get the next value.
        int step = spec["step"].numberInt();
        curr_seqval = btl->_seqIdMap[seq_id] + step;
    }

    // Handle the optional "mod" argument. This should be done after
    // handling all other options (currently just "unique").
    if (!spec["mod"].eoo()) {
        if (!spec["mod"].isNumber())
            return StatusOpEvaluationError;
        int modval = spec["mod"].numberInt();
        if (modval <= 0)
            return StatusOpEvaluationError;
        curr_seqval = (curr_seqval % modval);
    }

    // Store the sequence value.
    btl->_seqIdMap[seq_id] = curr_seqval;

    out.append(fieldName, curr_seqval);
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalRandString(BsonTemplateEvaluator* btl,
                                                                    const char* fieldName,
                                                                    const BSONObj& in,
                                                                    BSONObjBuilder& out) {
    // in = { #RAND_STRING: [10] }
    BSONObj range = in.firstElement().embeddedObject();
    if (range.nFields() != 1)
        return StatusOpEvaluationError;
    if (!range[0].isNumber())
        return StatusOpEvaluationError;
    const int length = range["0"].numberInt();
    if (length <= 0)
        return StatusOpEvaluationError;
    static const char alphanum[] =
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        "abcdefghijklmnopqrstuvwxyz"
        "0123456789+/";
    static const size_t alphaNumLength = sizeof(alphanum) - 1;
    MONGO_STATIC_ASSERT(alphaNumLength == 64);
    uint32_t currentRand = 0;
    std::string str;
    for (int i = 0; i < length; ++i, currentRand >>= 6) {
        if (i % 5 == 0)
            currentRand = btl->rng.nextInt32();
        str.push_back(alphanum[currentRand % alphaNumLength]);
    }
    out.append(fieldName, str);
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalConcat(BsonTemplateEvaluator* btl,
                                                                const char* fieldName,
                                                                const BSONObj& in,
                                                                BSONObjBuilder& out) {
    // in = { #CONCAT: ["hello", " ", "world"] }
    BSONObjBuilder objectBuilder;
    Status status = btl->evaluate(in.firstElement().embeddedObject(), objectBuilder);
    if (status != StatusSuccess)
        return status;
    BSONObj parts = objectBuilder.obj();
    if (parts.nFields() <= 1)
        return StatusOpEvaluationError;
    StringBuilder stringBuilder;
    BSONForEach(part, parts) {
        if (part.type() == String)
            stringBuilder << part.String();
        else
            part.toString(stringBuilder, false);
    }
    out.append(fieldName, stringBuilder.str());
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalObjId(BsonTemplateEvaluator* btl,
                                                               const char* fieldName,
                                                               const BSONObj& in,
                                                               BSONObjBuilder& out) {
    // in = { #OID: 1 }
    if (strcmp(fieldName, "_id") != 0)
        // Error: must be generating a value for the _id field.
        return StatusOpEvaluationError;
    out.genOID();
    return StatusSuccess;
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalVariable(BsonTemplateEvaluator* btl,
                                                                  const char* fieldName,
                                                                  const BSONObj& in,
                                                                  BSONObjBuilder& out) {
    // in = { #VARIABLE: "x" }
    BSONElement varEle = btl->_varMap[in["#VARIABLE"].String()].firstElement();
    if (!varEle.eoo()) {
        out.appendAs(varEle, fieldName);
        return StatusSuccess;
    } else {
        return StatusOpEvaluationError;
    }
}

BsonTemplateEvaluator::Status BsonTemplateEvaluator::evalCurrentDate(BsonTemplateEvaluator* btl,
                                                                     const char* fieldName,
                                                                     const BSONObj& in,
                                                                     BSONObjBuilder& out) {
    // in = { #CUR_DATE: 1 }
    auto offset = Milliseconds(in.firstElement().numberLong());
    out.append(fieldName, Date_t::now() + offset);
    return StatusSuccess;
}

}  // end namespace mongo