summaryrefslogtreecommitdiff
path: root/src/mongo/db/commands/write_commands/batch_executor.h
blob: 0bab41d3ff828cf5420f1490621a3d1e8d176f40 (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
/**
 *    Copyright (C) 2013 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    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
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *    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 GNU Affero General 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 <string>

#include "mongo/base/disallow_copying.h"
#include "mongo/db/ops/update_request.h"
#include "mongo/s/write_ops/batched_command_request.h"
#include "mongo/s/write_ops/batched_command_response.h"
#include "mongo/s/write_ops/batched_delete_document.h"
#include "mongo/s/write_ops/batched_update_document.h"
#include "mongo/util/mongoutils/str.h"

namespace mongo {

    class BSONObjBuilder;
    class CurOp;
    class LastError;
    class OpCounters;
    class OperationContext;
    class WriteBatchStats;
    struct WriteOpStats;

    /**
     * An instance of WriteBatchExecutor is an object capable of issuing a write batch.
     */
    class WriteBatchExecutor {
        MONGO_DISALLOW_COPYING(WriteBatchExecutor);
    public:

        // State object used by private execInserts.  TODO: Do not expose this type.
        class ExecInsertsState;

        WriteBatchExecutor( OperationContext* txn,
                            OpCounters* opCounters,
                            LastError* le );

        /**
         * Issues writes with requested write concern.  Fills response with errors if problems
         * occur.
         */
        void executeBatch( const BatchedCommandRequest& request, BatchedCommandResponse* response );

        const WriteBatchStats& getStats() const;

        /**
         * Does basic validation of the batch request. Returns a non-OK status if
         * any problems with the batch are found.
         */
        static Status validateBatch( const BatchedCommandRequest& request );

    private:
        /**
         * Executes the writes in the batch and returns upserted _ids and write errors.
         * Dispatches to one of the three functions below for DBLock, CurOp, and stats management.
         */
        void bulkExecute( const BatchedCommandRequest& request,
                          std::vector<BatchedUpsertDetail*>* upsertedIds,
                          std::vector<WriteErrorDetail*>* errors );

        /**
         * Executes the inserts of an insert batch and returns the write errors.
         *
         * Internally uses the DBLock of the request namespace.
         * May execute multiple inserts inside the same DBLock, and/or take the DBLock multiple
         * times.
         */
        void execInserts( const BatchedCommandRequest& request,
                          std::vector<WriteErrorDetail*>* errors );

        /**
         * Executes a single insert from a batch, described in the opaque "state" object.
         */
        void execOneInsert( ExecInsertsState* state, WriteErrorDetail** error );

        /**
         * Executes an update item (which may update many documents or upsert), and returns the
         * upserted _id on upsert or error on failure.
         *
         * Internally uses the DBLock of the update namespace.
         * May take the DBLock multiple times.
         */
        void execUpdate( const BatchItemRef& updateItem,
                         BSONObj* upsertedId,
                         WriteErrorDetail** error );

        /**
         * Executes a delete item (which may remove many documents) and returns an error on failure.
         *
         * Internally uses the DBLock of the delete namespace.
         * May take the DBLock multiple times.
         */
        void execRemove( const BatchItemRef& removeItem, WriteErrorDetail** error );

        /**
         * Helper for incrementing stats on the next CurOp.
         *
         * No lock requirements.
         */
        void incOpStats( const BatchItemRef& currWrite );

        /**
         * Helper for incrementing stats after each individual write op.
         *
         * No lock requirements (though usually done inside write lock to make stats update look
         * atomic).
         */
        void incWriteStats( const BatchItemRef& currWrite,
                            const WriteOpStats& stats,
                            const WriteErrorDetail* error,
                            CurOp* currentOp );

        OperationContext* _txn;

        // OpCounters object to update - needed for stats reporting
        // Not owned here.
        OpCounters* _opCounters;

        // LastError object to use for preparing write results - needed for stats reporting
        // Not owned here.
        LastError* _le;

        // Stats
        std::unique_ptr<WriteBatchStats> _stats;
    };

    /**
     * Holds information about the result of a single write operation.
     */
    struct WriteOpStats {

        WriteOpStats() :
            n( 0 ), nModified( 0 ) {
        }

        void reset() {
            n = 0;
            nModified = 0;
            upsertedID = BSONObj();
        }

        // Num docs logically affected by this operation.
        int n;

        // Num docs actually modified by this operation, if applicable (update)
        int nModified;

        // _id of newly upserted document, if applicable (update)
        BSONObj upsertedID;
    };

    /**
     * Full stats accumulated by a write batch execution.  Note that these stats do not directly
     * correspond to the stats accumulated in opCounters and LastError.
     */
    class WriteBatchStats {
    public:

        WriteBatchStats() :
            numInserted( 0 ), numUpserted( 0 ), numMatched( 0 ), numModified( 0 ), numDeleted( 0 ) {
        }

        int numInserted;
        int numUpserted;
        int numMatched;
        int numModified;
        int numDeleted;
    };

} // namespace mongo