25
25
* exception statement from all source files in the program, then also delete
26
26
* it in the license file.
27
27
*/
28
+ /* _mdb_catalog.wt内容
29
+ {
30
+ ns: "test.test1",
31
+ md: {
32
+ ns: "test.test1",
33
+ options: {
34
+ uuid: UUID("520904ec-0432-4c00-a15d-788e2f5d707b")
35
+ },
36
+ indexes: [{
37
+ spec: {
38
+ v: 2,
39
+ key: {
40
+ _id: 1
41
+ },
42
+ name: "_id_",
43
+ ns: "test.test1"
44
+ },
45
+ ready: true,
46
+ multikey: false,
47
+ multikeyPaths: {
48
+ _id: BinData(0, 00)
49
+ },
50
+ head: 0,
51
+ prefix: -1
52
+ }, {
53
+ spec: {
54
+ v: 2,
55
+ key: {
56
+ name: 1.0,
57
+ age: 1.0
58
+ },
59
+ name: "name_1_age_1",
60
+ ns: "test.test1",
61
+ background: true
62
+ },
63
+ ready: true,
64
+ multikey: false,
65
+ multikeyPaths: {
66
+ name: BinData(0, 00),
67
+ age: BinData(0, 00)
68
+ },
69
+ head: 0,
70
+ prefix: -1
71
+ }, {
72
+ spec: {
73
+ v: 2,
74
+ key: {
75
+ zipcode: 1.0
76
+ },
77
+ name: "zipcode_1",
78
+ ns: "test.test1",
79
+ background: true
80
+ },
81
+ ready: true,
82
+ multikey: false,
83
+ multikeyPaths: {
84
+ zipcode: BinData(0, 00)
85
+ },
86
+ head: 0,
87
+ prefix: -1
88
+ }],
89
+ prefix: -1
90
+ },
91
+ idxIdent: {
92
+ _id_: "test/index/8-380857198902467499",
93
+ name_1_age_1: "test/index/0--6948813758302814892",
94
+ zipcode_1: "test/index/3--6948813758302814892"
95
+ },
96
+ ident: "test/collection/7-380857198902467499"
97
+ }
28
98
99
+ */
29
100
#include " mongo/platform/basic.h"
30
101
31
102
#include < memory>
@@ -228,7 +299,7 @@ CollectionCatalogEntry* KVDatabaseCatalogEntryBase::getCollectionCatalogEntry(St
228
299
return it->second ;
229
300
}
230
301
231
- // 获取对该表进行底层KV操作的KVCollectionCatalogEntry
302
+ // 获取对该表进行底层KV操作的WiredTigerRecordStore
232
303
RecordStore* KVDatabaseCatalogEntryBase::getRecordStore (StringData ns) const {
233
304
CollectionMap::const_iterator it = _collections.find (ns.toString ());
234
305
if (it == _collections.end ()) {
@@ -371,6 +442,12 @@ void KVDatabaseCatalogEntryBase::reinitCollectionAfterRepair(OperationContext* o
371
442
}
372
443
373
444
// DatabaseImpl::renameCollection调用,集合重命名
445
+ // 从该接口可以看出,一个表操作需要包含以下信息:
446
+ // 1. 更新sizeStorer.wt size元数据文件中对应的表,因为表名已经改变了
447
+ // 2. 表名修改后,_mdb_catalog.wt元数据也需要更新,包括表名和ident等,ident是通过表名生成的,表名改了,因此ident也需要修改
448
+ // 3. 表名改了后,ident也变了,因此操作该表的WiredTigerRecordStore也需要改变,需要重新生成
449
+ // 4. 在cache中根据上面新的表名、新的ident、新的WiredTigerRecordStore生成新的KVCollectionCatalogEntry,该entry在内存cache中缓存起来
450
+ // 疑问?为何没有对idxIdent(name_1_age_1: "test/index/0--6948813758302814892")改名,原因是idxIdent中对应的test是库,没有表信息
374
451
Status KVDatabaseCatalogEntryBase::renameCollection (OperationContext* opCtx,
375
452
StringData fromNS,
376
453
StringData toNS,
@@ -379,34 +456,45 @@ Status KVDatabaseCatalogEntryBase::renameCollection(OperationContext* opCtx,
379
456
380
457
RecordStore* originalRS = NULL ;
381
458
459
+ // 获取表信息
382
460
CollectionMap::const_iterator it = _collections.find (fromNS.toString ());
383
461
if (it == _collections.end ()) {
384
462
return Status (ErrorCodes::NamespaceNotFound, " rename cannot find collection" );
385
463
}
386
464
465
+ // 获取操作该表的WiredTigerRecordStore
387
466
originalRS = it->second ->getRecordStore ();
388
467
468
+ // 目的表明是否已经存在,存在说明重复了,直接报错
389
469
it = _collections.find (toNS.toString ());
390
470
if (it != _collections.end ()) {
391
471
return Status (ErrorCodes::NamespaceExists, " for rename to already exists" );
392
472
}
393
473
474
+ // 原表对应ident
394
475
const std::string identFrom = _engine->getCatalog ()->getCollectionIdent (fromNS);
395
476
477
+ // WiredTigerKVEngine::okToRename
478
+ // cache中记录的表数据大小,表重命名后,记录数元数据文件sizeStorer.wt也需要对应修改
396
479
Status status = _engine->getEngine ()->okToRename (opCtx, fromNS, toNS, identFrom, originalRS);
397
480
if (!status.isOK ())
398
481
return status;
399
482
400
- status = _engine->getCatalog ()->renameCollection (opCtx, fromNS, toNS, stayTemp);
483
+ // _mdb_catalog.wt元数据文件中的表名需要更新,元数据也需要更新,现在_mdb_catalog.wt中是新表的元数据信息了
484
+ // KVCatalog::renameCollection
485
+ status = _engine->getCatalog ()->renameCollection (opCtx, fromNS, toNS, stayTemp);
401
486
if (!status.isOK ())
402
487
return status;
403
488
489
+ // 源表名对应的ident也需要改名,因为表名对应ident必须根据指定算法生成,表名改了,indent肯定也就不一样了
404
490
const std::string identTo = _engine->getCatalog ()->getCollectionIdent (toNS);
405
491
406
492
invariant (identFrom == identTo);
407
493
494
+ // 获取新表的元数据信息文件_mdb_catalog.wt中的元数据信息md
408
495
BSONCollectionCatalogEntry::MetaData md = _engine->getCatalog ()->getMetaData (opCtx, toNS);
409
496
497
+ // 清除原表的内存cache信息
410
498
const CollectionMap::iterator itFrom = _collections.find (fromNS.toString ());
411
499
invariant (itFrom != _collections.end ());
412
500
opCtx->recoveryUnit ()->registerChange (
@@ -416,9 +504,11 @@ Status KVDatabaseCatalogEntryBase::renameCollection(OperationContext* opCtx,
416
504
opCtx->recoveryUnit ()->registerChange (
417
505
new AddCollectionChange (opCtx, this , toNS, identTo, false ));
418
506
507
+ // 获取操作新表的WiredTigerRecordStore
419
508
auto rs =
420
509
_engine->getEngine ()->getGroupedRecordStore (opCtx, toNS, identTo, md.options , md.prefix );
421
510
511
+ // 新表生成对应新的KVCollectionCatalogEntry cache信息
422
512
_collections[toNS.toString ()] = new KVCollectionCatalogEntry (
423
513
_engine->getEngine (), _engine->getCatalog (), toNS, identTo, std::move (rs));
424
514
0 commit comments