summaryrefslogtreecommitdiff
path: root/src/mongo/db/catalog/collection_write_path.h
blob: 482c59116340f13aceefe09dc5166f6886cd82b7 (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
/**
 *    Copyright (C) 2022-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/bsonobj.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/curop.h"
#include "mongo/db/namespace_string.h"

namespace mongo {
namespace collection_internal {

using OnRecordInsertedFn = std::function<Status(const RecordId& loc)>;

enum class StoreDeletedDoc { Off, On };

enum class RetryableWrite { kYes, kNo };

/**
 * Constants used for the opDiff argument in updateDocument and updateDocumentWithDamages.
 */
constexpr const BSONObj* kUpdateAllIndexes = nullptr;
constexpr const BSONObj* kUpdateNoIndexes = &BSONObj::kEmptyObject;

/**
 * Inserts a document into the record store for a bulk loader that manages the index building. The
 * bulk loader is notified with the RecordId of the document inserted into the RecordStore through
 * the 'OnRecordInsertedFn' callback.
 *
 * NOTE: It is up to caller to commit the indexes.
 */
Status insertDocumentForBulkLoader(OperationContext* opCtx,
                                   const CollectionPtr& collection,
                                   const BSONObj& doc,
                                   const OnRecordInsertedFn& onRecordInserted);

/**
 * Inserts all documents inside one WUOW.
 * Caller should ensure vector is appropriately sized for this.
 * If any errors occur (including WCE), caller should retry documents individually.
 *
 * 'opDebug' Optional argument. When not null, will be used to record operation statistics.
 */
Status insertDocuments(OperationContext* opCtx,
                       const CollectionPtr& collection,
                       std::vector<InsertStatement>::const_iterator begin,
                       std::vector<InsertStatement>::const_iterator end,
                       OpDebug* opDebug,
                       bool fromMigrate = false);

/**
 * Does NOT modify the doc before inserting (i.e. will not add an _id field for documents that are
 * missing it)
 *
 * 'opDebug' Optional argument. When not null, will be used to record operation statistics.
 */
Status insertDocument(OperationContext* opCtx,
                      const CollectionPtr& collection,
                      const InsertStatement& doc,
                      OpDebug* opDebug,
                      bool fromMigrate = false);

/**
 * Checks the 'failCollectionInserts' fail point at the beginning of an insert operation to see if
 * the insert should fail. Returns Status::OK if The function should proceed with the insertion.
 * Otherwise, the function should fail and return early with the error Status.
 */
Status checkFailCollectionInsertsFailPoint(const NamespaceString& ns, const BSONObj& firstDoc);

/**
 * Updates the document @ oldLocation with newDoc.
 *
 * If the document fits in the old space, it is put there; if not, it is moved.
 * Sets 'args.updatedDoc' to the updated version of the document with damages applied, on success.
 * 'opDiff' Optional argument. If set to kUpdateAllIndexes, all the indexes are updated. If it is
 * set to kUpdateNoIndexes, no indexes are updated. Otherwise, it is the precomputed difference
 * between 'oldDoc' and 'newDoc', used to determine which indexes need to be updated.
 * 'opDebug' Optional argument. When not null, will be used to record operation statistics.
 * @return the post update location of the doc (may or may not be the same as oldLocation)
 */
RecordId updateDocument(OperationContext* opCtx,
                        const CollectionPtr& collection,
                        const RecordId& oldLocation,
                        const Snapshotted<BSONObj>& oldDoc,
                        const BSONObj& newDoc,
                        const BSONObj* opDiff,
                        OpDebug* opDebug,
                        CollectionUpdateArgs* args);

/**
 * Illegal to call if collection->updateWithDamagesSupported() returns false.
 * Sets 'args.updatedDoc' to the updated version of the document with damages applied, on success.
 * Returns the contents of the updated document.
 */
StatusWith<BSONObj> updateDocumentWithDamages(OperationContext* opCtx,
                                              const CollectionPtr& collection,
                                              const RecordId& loc,
                                              const Snapshotted<BSONObj>& oldDoc,
                                              const char* damageSource,
                                              const mutablebson::DamageVector& damages,
                                              const BSONObj* opDiff,
                                              OpDebug* opDebug,
                                              CollectionUpdateArgs* args);

/**
 * Deletes the document with the given RecordId from the collection. For a description of the
 * parameters, see the overloaded function below.
 */
void deleteDocument(OperationContext* opCtx,
                    const CollectionPtr& collection,
                    StmtId stmtId,
                    const RecordId& loc,
                    OpDebug* opDebug,
                    bool fromMigrate = false,
                    bool noWarn = false,
                    StoreDeletedDoc storeDeletedDoc = StoreDeletedDoc::Off,
                    CheckRecordId checkRecordId = CheckRecordId::Off,
                    RetryableWrite retryableWrite = RetryableWrite::kNo);

/**
 * Deletes the document from the collection.
 *
 * @param doc: the document to be deleted.
 * @param fromMigrate: indicates whether the delete was induced by a chunk migration, and so should
 * be ignored by the user as an internal maintenance operation and not a real delete.
 * @param loc: key to uniquely identify a record in a collection.
 * @param opDebug: Optional argument. When not null, will be used to record operation statistics.
 * @param noWarn: if unindexing the record causes an error, if noWarn is true the error will not be
 * logged.
 * @param storeDeletedDoc: whether to store the document deleted in the oplog.
 * @param checkRecordId: whether to confirm the recordId matches the record we are removing when
 * unindexing.
 * @param retryableWrite: whether it's a retryable write, @see write_stage_common::isRetryableWrite
 */
void deleteDocument(OperationContext* opCtx,
                    const CollectionPtr& collection,
                    Snapshotted<BSONObj> doc,
                    StmtId stmtId,
                    const RecordId& loc,
                    OpDebug* opDebug,
                    bool fromMigrate = false,
                    bool noWarn = false,
                    StoreDeletedDoc storeDeletedDoc = StoreDeletedDoc::Off,
                    CheckRecordId checkRecordId = CheckRecordId::Off,
                    RetryableWrite retryableWrite = RetryableWrite::kNo);

}  // namespace collection_internal
}  // namespace mongo