@@ -178,12 +178,13 @@ CollectionImpl::CollectionImpl(Collection* _this_init,
178
178
179
179
void CollectionImpl::init (OperationContext* opCtx) {
180
180
_magic = kMagicNumber ;
181
- // IndexCatalogImpl::init
181
+ // IndexCatalogImpl::init 获取该表下面的所有索引信息
182
182
_indexCatalog.init (opCtx).transitional_ignore ();
183
183
if (isCapped ())
184
184
_recordStore->setCappedCallback (this );
185
185
186
186
// CollectionInfoCacheImpl::init
187
+ // 初始化该集合的planCache IndexEntry
187
188
_infoCache.init (opCtx);
188
189
}
189
190
@@ -207,6 +208,7 @@ CollectionImpl::~CollectionImpl() {
207
208
_magic = 0 ;
208
209
}
209
210
211
+ // 除了system.indexes system.namespaces system.profile外的其他表都需要id索引
210
212
bool CollectionImpl::requiresIdIndex () const {
211
213
if (_ns.isVirtualized () || _ns.isOplog ()) {
212
214
// No indexes on virtual collections or the oplog.
@@ -223,6 +225,7 @@ bool CollectionImpl::requiresIdIndex() const {
223
225
return true ;
224
226
}
225
227
228
+ // 获取SeekableRecordCursor游标
226
229
std::unique_ptr<SeekableRecordCursor> CollectionImpl::getCursor (OperationContext* opCtx,
227
230
bool forward) const {
228
231
dassert (opCtx->lockState ()->isCollectionLockedForMode (ns ().toString (), MODE_IS));
@@ -726,6 +729,7 @@ RecordId CollectionImpl::updateDocument(OperationContext* opCtx,
726
729
Status updateStatus = _recordStore->updateRecord (
727
730
opCtx, oldLocation, newDoc.objdata (), newDoc.objsize (), _enforceQuota (enforceQuota), this );
728
731
732
+ // mmap才会有该错误状态码
729
733
if (updateStatus == ErrorCodes::NeedsDocumentMove) {
730
734
return uassertStatusOK (_updateDocumentWithMove (
731
735
opCtx, oldLocation, oldDoc, newDoc, enforceQuota, opDebug, args, sid));
@@ -758,6 +762,7 @@ RecordId CollectionImpl::updateDocument(OperationContext* opCtx,
758
762
return {oldLocation};
759
763
}
760
764
765
+ // mmap才会用到
761
766
StatusWith<RecordId> CollectionImpl::_updateDocumentWithMove (OperationContext* opCtx,
762
767
const RecordId& oldLocation,
763
768
const Snapshotted<BSONObj>& oldDoc,
@@ -853,6 +858,7 @@ StatusWith<RecordData> CollectionImpl::updateDocumentWithDamages(
853
858
return newRecStatus;
854
859
}
855
860
861
+ // 针对mmapv1引擎,忽略
856
862
bool CollectionImpl::_enforceQuota (bool userEnforeQuota) const {
857
863
if (!userEnforeQuota)
858
864
return false ;
@@ -869,6 +875,7 @@ bool CollectionImpl::_enforceQuota(bool userEnforeQuota) const {
869
875
return true ;
870
876
}
871
877
878
+ // 是否固定集合
872
879
bool CollectionImpl::isCapped () const {
873
880
return _cappedNotifier.get ();
874
881
}
@@ -918,7 +925,9 @@ uint64_t CollectionImpl::getIndexSize(OperationContext* opCtx, BSONObjBuilder* d
918
925
* 2) drop indexes
919
926
* 3) truncate record store
920
927
* 4) re-write indexes
921
- */ // drop表数据,但是不drop索引
928
+ */
929
+ // EmptyCapped::run->mongo::emptyCapped调用
930
+ // drop表数据,但是不drop索引
922
931
Status CollectionImpl::truncate (OperationContext* opCtx) {
923
932
dassert (opCtx->lockState ()->isCollectionLockedForMode (ns ().toString (), MODE_X));
924
933
BackgroundOperation::assertNoBgOpInProgForNs (ns ());
@@ -953,6 +962,7 @@ Status CollectionImpl::truncate(OperationContext* opCtx) {
953
962
return Status::OK ();
954
963
}
955
964
965
+ // captrunc::run命令调用,测试使用
956
966
void CollectionImpl::cappedTruncateAfter (OperationContext* opCtx, RecordId end, bool inclusive) {
957
967
dassert (opCtx->lockState ()->isCollectionLockedForMode (ns ().toString (), MODE_X));
958
968
invariant (isCapped ());
@@ -1135,6 +1145,7 @@ namespace {
1135
1145
1136
1146
using ValidateResultsMap = std::map<std::string, ValidateResults>;
1137
1147
1148
+ // CollectionImpl::validate调用
1138
1149
void _validateRecordStore (OperationContext* opCtx,
1139
1150
RecordStore* recordStore,
1140
1151
ValidateCmdLevel level,
@@ -1155,6 +1166,7 @@ void _validateRecordStore(OperationContext* opCtx,
1155
1166
}
1156
1167
}
1157
1168
1169
+ // CollectionImpl::validate调用
1158
1170
void _validateIndexes (OperationContext* opCtx,
1159
1171
IndexCatalog* indexCatalog,
1160
1172
BSONObjBuilder* keysPerIndex,
@@ -1206,6 +1218,7 @@ void _validateIndexes(OperationContext* opCtx,
1206
1218
}
1207
1219
}
1208
1220
1221
+ // CollectionImpl::validate调用
1209
1222
void _markIndexEntriesInvalid (ValidateResultsMap* indexNsResultsMap, ValidateResults* results) {
1210
1223
1211
1224
// The error message can't be more specific because even though the index is
@@ -1221,6 +1234,7 @@ void _markIndexEntriesInvalid(ValidateResultsMap* indexNsResultsMap, ValidateRes
1221
1234
results->valid = false ;
1222
1235
}
1223
1236
1237
+ // CollectionImpl::validate调用
1224
1238
void _validateIndexKeyCount (OperationContext* opCtx,
1225
1239
IndexCatalog* indexCatalog,
1226
1240
RecordStore* recordStore,
@@ -1239,6 +1253,7 @@ void _validateIndexKeyCount(OperationContext* opCtx,
1239
1253
}
1240
1254
}
1241
1255
1256
+ // CollectionImpl::validate调用
1242
1257
void _reportValidationResults (OperationContext* opCtx,
1243
1258
IndexCatalog* indexCatalog,
1244
1259
ValidateResultsMap* indexNsResultsMap,
@@ -1286,6 +1301,9 @@ void _reportValidationResults(OperationContext* opCtx,
1286
1301
}
1287
1302
} // namespace
1288
1303
1304
+ // db.xx.validate()
1305
+ // https://docs.mongodb.com/v3.0/reference/command/validate/index.html
1306
+ // 该命令验证表中数据结构是否符合要求,因为建表的时候可能指定了validator
1289
1307
Status CollectionImpl::validate (OperationContext* opCtx,
1290
1308
ValidateCmdLevel level,
1291
1309
bool background,
@@ -1351,6 +1369,10 @@ Status CollectionImpl::validate(OperationContext* opCtx,
1351
1369
return Status::OK ();
1352
1370
}
1353
1371
1372
+ // touch::run touch命令调用
1373
+ // db.runCommand({ touch: "records", data: true, index: true })
1374
+ // 加载表的数据和索引到内存
1375
+ // 参考https://docs.mongodb.com/v3.0/reference/command/touch/index.html
1354
1376
Status CollectionImpl::touch (OperationContext* opCtx,
1355
1377
bool touchData,
1356
1378
bool touchIndexes,
0 commit comments