summaryrefslogtreecommitdiff
path: root/src/mongo/db/update/document_diff_test_helpers.h
blob: 09d545cba47149cfafb86c0e79f0adcf8a17de5e (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
/**
 *    Copyright (C) 2020-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.
 */

#pragma once

#include <algorithm>

#include "mongo/bson/json.h"
#include "mongo/db/exec/document_value/document.h"
#include "mongo/db/exec/document_value/value.h"
#include "mongo/db/update/document_diff_applier.h"
#include "mongo/db/update/document_diff_calculator.h"
#include "mongo/platform/random.h"
#include "mongo/unittest/bson_test_util.h"
#include "mongo/unittest/unittest.h"

namespace mongo::doc_diff {

BSONObj createObjWithLargePrefix(const std::string& suffix) {
    const static auto largeObj = BSON("prefixLargeField" << std::string(200, 'a'));
    return largeObj.addFields(fromjson(suffix));
}

std::string getFieldName(int level, int fieldNum) {
    return str::stream() << "f" << level << fieldNum;
}

Value getScalarFieldValue(PseudoRandom* rng) {
    switch (rng->nextInt32(10)) {
        case 0:
            return Value("val"_sd);
        case 1:
            return Value(BSONNULL);
        case 2:
            return Value(-1LL);
        case 3:
            return Value(0);
        case 4:
            return Value(1.10);
        case 5:
            return Value(false);
        case 6:
            return Value(BSONRegEx("p"));
        case 7:
            return Value(Date_t());
        case 8:
            return Value(UUID::gen());
        case 9:
            return Value(BSONBinData("asdf", 4, BinDataGeneral));
        default:
            MONGO_UNREACHABLE;
    }
}

BSONObj generateDoc(PseudoRandom* rng, MutableDocument* doc, int depthLevel) {
    // Append a large field at each level so that the likelihood of generating a sub-diff is high.
    auto largeFieldObj = createObjWithLargePrefix("{}");
    doc->addField("prefixLargeField", Value(largeFieldObj.firstElement()));

    // Reduce the probabilty of generated nested objects as we go deeper. After depth level 6, we
    // should not be generating anymore nested objects.
    const double subObjProbability = 0.3 - (depthLevel * 0.05);
    const double subArrayProbability = 0.2 - (depthLevel * 0.05);

    const int numFields = (5 - depthLevel) + rng->nextInt32(4);
    for (int fieldNum = 0; fieldNum < numFields; ++fieldNum) {
        const auto fieldName = getFieldName(depthLevel, fieldNum);
        auto num = rng->nextCanonicalDouble();
        if (num <= subObjProbability) {
            MutableDocument subDoc;
            doc->addField(fieldName, Value(generateDoc(rng, &subDoc, depthLevel + 1)));
        } else if (num <= (subObjProbability + subArrayProbability)) {
            const auto arrayLength = rng->nextInt32(10);
            std::vector<Value> values;
            auto numSubObjs = 0;
            for (auto i = 0; i < arrayLength; i++) {
                // The probablilty of generating a sub-object goes down as we generate more
                // sub-objects and as the array position increases.
                if (!rng->nextInt32(2 + numSubObjs + i)) {
                    MutableDocument subDoc;

                    // Ensure that the depth is higher as we generate more sub-objects, to avoid
                    // bloating up the document size exponentially.
                    values.push_back(
                        Value(generateDoc(rng, &subDoc, depthLevel + 1 + numSubObjs * 2)));
                    ++numSubObjs;
                } else {
                    values.push_back(getScalarFieldValue(rng));
                }
            }

            doc->addField(fieldName, Value(values));
        } else {
            doc->addField(fieldName, getScalarFieldValue(rng));
        }
    }
    return doc->freeze().toBson();
}

}  // namespace mongo::doc_diff