summaryrefslogtreecommitdiff
path: root/src/mongo/db/update/log_builder.h
blob: 4b0fb6663c1ee7743651825afb76a63ff79dc24a (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

/**
 *    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.
 */

#pragma once

#include "mongo/base/status.h"
#include "mongo/bson/mutable/document.h"

namespace mongo {

/**
 * Previously, there were multiple supported versions of the update language.
 */
enum class UpdateSemantics {
    // The update system introduced in v3.6, and is the only supported system. When a single update
    // adds multiple fields, those fields are added in lexicographic order by field name. This
    // system introduces support for arrayFilters and $[] syntax.
    kUpdateNode = 1,

    // Must be last.
    kNumUpdateSemantics
};

/** LogBuilder abstracts away some of the details of producing a properly constructed oplog
 *  update entry. It manages separate regions into which it accumulates $set and $unset
 *  operations, and distinguishes object replacement style oplog generation from
 *  $set/$unset style generation and prevents admixture.
 */
class LogBuilder {
public:
    static constexpr StringData kUpdateSemanticsFieldName = "$v"_sd;

    /** Construct a new LogBuilder. Log entries will be recorded as new children under the
     *  'logRoot' Element, which must be of type mongo::Object and have no children.
     */
    inline LogBuilder(mutablebson::Element logRoot)
        : _logRoot(logRoot),
          _objectReplacementAccumulator(_logRoot),
          _setAccumulator(_logRoot.getDocument().end()),
          _unsetAccumulator(_setAccumulator),
          _updateSemantics(_setAccumulator) {
        dassert(logRoot.isType(mongo::Object));
        dassert(!logRoot.hasChildren());
    }

    /** Return the Document to which the logging root belongs. */
    inline mutablebson::Document& getDocument() {
        return _logRoot.getDocument();
    }

    /** Add the given Element as a new entry in the '$set' section of the log. If a $set
     *  section does not yet exist, it will be created. If this LogBuilder is currently
     *  configured to contain an object replacement, the request to add to the $set section
     *  will return an Error.
     */
    Status addToSets(mutablebson::Element elt);

    /**
     * Convenience method which calls addToSets after
     * creating a new Element to wrap the SafeNum value.
     *
     * If any problem occurs then the operation will stop and return that error Status.
     */
    Status addToSets(StringData name, const SafeNum& val);

    /**
     * Convenience method which calls addToSets after
     * creating a new Element to wrap the old one.
     *
     * If any problem occurs then the operation will stop and return that error Status.
     */
    Status addToSetsWithNewFieldName(StringData name, const mutablebson::Element val);

    /**
     * Convenience method which calls addToSets after
     * creating a new Element to wrap the old one.
     *
     * If any problem occurs then the operation will stop and return that error Status.
     */
    Status addToSetsWithNewFieldName(StringData name, const BSONElement& val);

    /** Add the given path as a new entry in the '$unset' section of the log. If an
     *  '$unset' section does not yet exist, it will be created. If this LogBuilder is
     *  currently configured to contain an object replacement, the request to add to the
     *  $unset section will return an Error.
     */
    Status addToUnsets(StringData path);

    /**
     * Add a "$v" field to the log. Fails if there is already a "$v" field.
     */
    Status setUpdateSemantics(UpdateSemantics updateSemantics);

    /** Obtain, via the out parameter 'outElt', a pointer to the mongo::Object type Element
     *  to which the components of an object replacement should be recorded. It is an error
     *  to call this if any Elements have been added by calling either addToSets or
     *  addToUnsets, and attempts to do so will return a non-OK Status. Similarly, if there
     *  is already object replacement data recorded for this log, the call will fail.
     */
    Status getReplacementObject(mutablebson::Element* outElt);

private:
    // Returns true if the object replacement accumulator is valid and has children, false
    // otherwise.
    inline bool hasObjectReplacement() const;

    inline Status addToSection(mutablebson::Element newElt,
                               mutablebson::Element* section,
                               const char* sectionName);

    mutablebson::Element _logRoot;
    mutablebson::Element _objectReplacementAccumulator;
    mutablebson::Element _setAccumulator;
    mutablebson::Element _unsetAccumulator;
    mutablebson::Element _updateSemantics;
};

}  // namespace mongo