Mongodb源码分析--删除记录

来源:互联网 发布:筑波大学 知乎 编辑:程序博客网 时间:2024/05/20 06:55

在之前的一篇文章 中,介绍了assembleResponse函数(位于instance.cpp第224行),它会根据op操作枚举类型来调用相应的crud操作,枚举类型定义如下:

 
view plaincopy to clipboardprint?
  1. enum Operations {  
  2.         opReply = 1,     /* reply. responseTo is set. */  
  3.         dbMsg = 1000,    /* generic msg command followed by a string */  
  4.         dbUpdate = 2001, /* update object */  
  5.         dbInsert = 2002,  
  6.         //dbGetByOID = 2003,  
  7.         dbQuery = 2004,  
  8.         dbGetMore = 2005,  
  9.         dbDelete = 2006,  
  10.         dbKillCursors = 2007  
  11.     };  


    可以看到dbDelete = 2002 为删除操作枚举值。当客户端将要删除的记录(或条件的document)发到服务端之后,mongodb通过消息封装方式将数据包中的字节流解析转成 message类型,并进一步转换成dbmessage之后,mongodb就会根据消息类型进行判断,以决定接下来执行的操作),下面我们看一下 assembleResponse在确定是删除操作时调用的方法,如下:

 
view plaincopy to clipboardprint?
  1. assembleResponse( Message &m, DbResponse &dbresponse, const SockAddr &client ) {  
  2.     .....  
  3.             try {  
  4.                 if ( op == dbInsert ) {  //添加记录操作  
  5.                     receivedInsert(m, currentOp);  
  6.                 }  
  7.                 else if ( op == dbUpdate ) { //更新记录  
  8.                     receivedUpdate(m, currentOp);  
  9.                 }  
  10.                 else if ( op == dbDelete ) { //删除记录  
  11.                     receivedDelete(m, currentOp);  
  12.                 }  
  13.                 else if ( op == dbKillCursors ) { //删除Cursors(游标)对象  
  14.                     currentOp.ensureStarted();  
  15.                     logThreshold = 10;  
  16.                     ss << "killcursors ";  
  17.                     receivedKillCursors(m);  
  18.                 }  
  19.                 else {  
  20.                     mongo::log() << "    operation isn't supported: " << op << endl;  
  21.                     currentOp.done();  
  22.                     log = true;  
  23.                 }  
  24.             }  
  25.           .....  
  26.         }  
  27.     }  
 


    从上面代码可以看出,系统在确定dbDelete操作时,调用了receivedDelete()方法(位于instance.cpp文件第323行),下面是该方法的定义:

view plaincopy to clipboardprint?
  1. void  receivedDelete(Message &  m, CurOp &  op) {  
  2.         DbMessage d(m); // 将Message消息转换成数据库消息格式  
  3.          const   char   * ns  =  d.getns(); // 获取相应名空间信息  
  4.         assert( * ns);  
  5.         uassert(  10056  ,   " not master " , isMasterNs( ns ) ); // 因为CUD操作在主库中操作,所以这里断言名空间包含的db信息中是不是主库,即"master"  
  6.         op.debug().str  <<  
  7.         ns  <<   '   ' ;  
  8.          // 获取"删除消息"结构体中的flags 标识位,如设置了该位,则仅删除查找到的第一条记录(document),否则删除所有匹配记录.  
  9.          // 关于消息结构体,参见我的这篇文章: http://www.cnblogs.com/daizhj/archive/2011/04/02/2003335.html  
  10.          int  flags  =  d.pullInt(); //  
  11.          bool  justOne  =  flags  &  RemoveOption_JustOne;  
  12.          bool  broadcast  =  flags  &  RemoveOption_Broadcast;  
  13.         assert( d.moreJSObjs() );  
  14.         BSONObj pattern  =  d.nextJsObj(); // 获取"删除消息"结构体中的selector(也就是要删数据条件where)  
  15.         {  
  16.              string  s  =  pattern.toString();  
  17.             op.debug().str  <<   "  query:  "   <<  s;  
  18.             op.setQuery(pattern);  
  19.         }  
  20.         writelock lk(ns);  
  21.          //  如果不更新所有节点(sharding)且当前物理结点是shard 状态时  
  22.          if  (  !  broadcast  &  handlePossibleShardedMessage( m ,  0  ) )  
  23.              return ;  
  24.          // if this ever moves to outside of lock, need to adjust check Client::Context::_finishInit  
  25.         Client::Context ctx(ns);  
  26.          long   long  n  =  deleteObjects(ns, pattern, justOne,  true ); // 删除对象信息  
  27.         lastError.getSafe() -> recordDelete( n );  
  28.     }  


    上面方法主要是对消息中的flag信息进行解析,以获取消息中的删除条件等信息,并最终调用 deleteObjects方法,该方法位于query.cpp文件中,如下:

 
view plaincopy to clipboardprint?
  1. // query.cpp文件 128行  
  2.    /*  ns:      要删除的表集合(namespace, e.g. <database>.<collection>) 
  3.     pattern: 删除条件,相当于 "where" 字语(clause / criteria) 
  4.     justOne: 是否仅删除第一个匹配对象信息 
  5.     god:     是否允许访问系统名空间(system namespaces) 
  6.   */  
  7.   long   long  deleteObjects( const   char   * ns, BSONObj pattern,  bool  justOneOrig,  bool  logop,  bool  god, RemoveSaver  *  rs ) {  
  8.       if (  ! god ) { // 如果不能访问system空间,但却删除该空间信息时  
  9.           if  ( strstr(ns,  " .system. " ) ) {  
  10.               /*  note a delete from system.indexes would corrupt the db. if done here, as there are pointers into those objects in NamespaceDetails. 
  11.               */  
  12.              uassert( 12050 ,  " cannot delete from system namespace " , legalClientSystemNS( ns ,  true  ) );  
  13.          }  
  14.           if  ( strchr( ns ,  ' $ '  ) ) {  
  15.              log()  <<   " cannot delete from collection with reserved $ in name:  "   <<  ns  <<  endl;  
  16.              uassert(  10100  ,   " cannot delete from collection with reserved $ in name " , strchr(ns,  ' $ ' )  ==   0  );  
  17.          }  
  18.      }  
  19.      NamespaceDetails  * d  =  nsdetails( ns ); // 获取名空间详细信息  
  20.       if  (  !  d )  
  21.           return   0 ;  
  22.      uassert(  10101  ,   " can't remove from a capped collection "  ,  !  d -> capped ); // 确保当前collection不是capped类型(该类型集合会自动删除旧数据)  
  23.       long   long  nDeleted  =   0 ;  
  24.       int  best  =   0 ;  
  25.      shared_ptr <  MultiCursor::CursorOp  >  opPtr(  new  DeleteOp( justOneOrig, best ) ); // 构造“删除操作”实例对象并用其构造游标操作(符)实例  
  26.      shared_ptr <  MultiCursor  >  creal(  new  MultiCursor( ns, pattern, BSONObj(), opPtr,  ! god ) ); // 构造MultiCursor查询游标(参见构造方法中的 nextClause()语句)  
  27.       if (  ! creal -> ok() ) // 如果查询游标指向地址是否正常(主要判断是否null),因为系统会根据上面游标初始信息决定使用什么样的方式进行信息查询(比如是否使用B树索引等)  
  28.           return  nDeleted;  
  29.      shared_ptr <  Cursor  >  cPtr  =  creal;  
  30.      auto_ptr < ClientCursor >  cc(  new  ClientCursor( QueryOption_NoCursorTimeout, cPtr, ns) ); // 将游标封装以便下面遍历使用  
  31.      cc -> setDoingDeletes(  true  ); // 设置_doingDeletes(删除中)标志  
  32.      CursorId id  =  cc -> cursorid();  
  33.       bool  justOne  =  justOneOrig;  
  34.       bool  canYield  =   ! god  &&   ! creal -> matcher() -> docMatcher().atomic();  
  35.       do  {  
  36.           if  ( canYield  &&   !  cc -> yieldSometimes() ) { // 查看是否已到期(每个cc都会有一个读写操作时间,该值取决子获取读写锁时系统分配的时间,详见client.cpp 文件中的方法 int Client::recommendedYieldMicros( int * writers , int * readers ) {)  
  37.              cc.release();  //  时间已到则释放该对象(意味着已在别的地方被删除?)  
  38.               //  TODO should we assert or something?  
  39.               break ;  
  40.          }  
  41.           if  (  ! cc -> ok() ) {  
  42.               break ;  //  if we yielded, could have hit the end  
  43.          }  
  44.           //  this way we can avoid calling updateLocation() every time (expensive)  
  45.           //  as well as some other nuances handled  
  46.          cc -> setDoingDeletes(  true  );  
  47.          DiskLoc rloc  =  cc -> currLoc(); // 游标当前所指向的记录所在地址  
  48.          BSONObj key  =  cc -> currKey(); // 游标当前所指向的记录的key  
  49.           //  NOTE Calling advance() may change the matcher, so it's important  
  50.           //  to try to match first.  
  51.           bool  match  =  creal -> matcher() -> matches( key , rloc ); // 将当前游标指向的记录与游标中的where条件进行比较  
  52.           if  (  !  cc -> advance() ) // 游标移到下一个记录位置  
  53.              justOne  =   true ;  
  54.           if  (  !  match )  
  55.               continue ;  
  56.          assert(  ! cc -> c() -> getsetdup(rloc) );  // 不允许复本, 因为在多键值索引中可能会返回复本  
  57.           if  (  ! justOne ) {  
  58.               /*  NOTE: this is SLOW.  this is not good, noteLocation() was designed to be called across getMore 
  59.                  blocks.  here we might call millions of times which would be bad. 
  60.                   */  
  61.              cc -> c() -> noteLocation(); // 记录当前游标移动到的位置  
  62.          }  
  63.           if  ( logop ) { // 是否保存操作日志  
  64.              BSONElement e;  
  65.               if ( BSONObj( rloc.rec() ).getObjectID( e ) ) {  
  66.                  BSONObjBuilder b;  
  67.                  b.append( e );  
  68.                   bool  replJustOne  =   true ;  
  69.                  logOp(  " d " , ns, b.done(),  0 ,  & replJustOne ); // d表示delete  
  70.              }  
  71.               else  {  
  72.                  problem()  <<   " deleted object without id, not logging "   <<  endl;  
  73.              }  
  74.          }  
  75.           if  ( rs ) // 将删除记录的bson objects 信息保存到磁盘文件上  
  76.              rs -> goingToDelete( rloc.obj()  /* cc->c->current() */  );  
  77.          theDataFileMgr.deleteRecord(ns, rloc.rec(), rloc); // 删除查询匹配到的记录  
  78.          nDeleted ++ ; // 累计删除信息数  
  79.           if  ( justOne ) {  
  80.               break ;  
  81.          }  
  82.          cc -> c() -> checkLocation(); // 因为删除完记录好,会造成缓存中相关索引信息过期,用该方法能确保索引有效  
  83.         
  84.           if (  ! god )  
  85.              getDur().commitIfNeeded();  
  86.           if ( debug  &&  god  &&  nDeleted  ==   100  )  // 删除100条信息之后,显示内存使用预警信息  
  87.              log()  <<   " warning high number of deletes with god=true which could use significant memory "   <<  endl;  
  88.      }  
  89.       while  ( cc -> ok() );  
  90.       if  ( cc. get ()  &&  ClientCursor::find( id ,  false  )  ==   0  ) { // 再次在btree bucket中查找,如没有找到,表示记录已全部被删除  
  91.          cc.release();  
  92.      }  
  93.       return  nDeleted; // 返回已删除的记录数  
  94.  }  


    上面的代码主要执行构造查询游标,并将游标指向地址的记录取出来与查询条件进行匹配,如果匹配命中,则进行删除。这里考虑到如果记录在内存时,如果删除 记录后,内存中的b树结构会有影响,所以在删除记录前/后分别执行noteLocation/checkLocation方法以校正 查询cursor的当前位置。因为这里是一个while循环,它会找到所有满足条件的记录,依次删除它们。因为这里使用了MultiCursor,该游标在我看来就是一个复合游标 ,它不仅包括了cursor 中所有功能,还支持or条件操作。而有关游标的构造和继承实现体系,mongodb做的有些复杂,很难几句说清,我会在本系列后面另用篇幅进行说明,敬请期待 
    注意上面代码段中的这行代码:

 

view plaincopy to clipboardprint?
  1. theDataFileMgr.deleteRecord(ns, rloc.rec(), rloc); // 删除查询匹配到的记录  
  2.     该行代码执行了最终的删除记录操作,其定义如下:  
  3.      // pdfile.cpp文件 912行  
  4.      // 删除查询匹配查询到的记录  
  5.      void  DataFileMgr::deleteRecord( const   char   * ns, Record  * todelete,  const  DiskLoc &  dl,  bool  cappedOK,  bool  noWarn) {  
  6.         dassert( todelete  ==  dl.rec() ); // debug断言,检查要删除的Record信息与传入的dl是否一致(避免函数调用过程中被修改?)  
  7.         NamespaceDetails *  d  =  nsdetails(ns);  
  8.          if  ( d -> capped  &&   ! cappedOK ) { // 如果是capped collection类型,则不删除  
  9.              out ()  <<   " failing remove on a capped ns  "   <<  ns  <<  endl;  
  10.             uassert(  10089  ,   " can't remove from a capped collection "  ,  0  );  
  11.              return ;  
  12.         }  
  13.          // 如果还有别的游标指向当前dl(并发情况下),则提升它们  
  14.         ClientCursor::aboutToDelete(dl);  
  15.          // 将要删除的记录信息从索引b村中移除  
  16.         unindexRecord(d, todelete, dl, noWarn);  
  17.          // 删除指定记录信息  
  18.         _deleteRecord(d, ns, todelete, dl);  
  19.         NamespaceDetailsTransient::get_w( ns ).notifyOfWriteOp();  
  20.     }  

 


    上面删除记录方法deleteRecord中,执行的删除顺序与我之前写的那篇插入记录方式正好相反(那篇文章中是选在内存中分配记录然后将地址放到b树 中),这里是先将要删除记录的索引信息删除,然后再删除指定记录(更新内存中的记录信息而不是真的删除,稍后会进行解释)。

    首先我们先看一下上面代码段的unindexRecord方法:

  
view plaincopy to clipboardprint?
  1. // pdfile.cpp文件 845行  
  2.   /*  在所有索引中去掉当前记录信息中的相关索引键(包括多键值索用)信息 */  
  3.   static   void  unindexRecord(NamespaceDetails  * d, Record  * todelete,  const  DiskLoc &  dl,  bool  noWarn  =   false ) {  
  4.      BSONObj obj(todelete);  
  5.       int  n  =  d -> nIndexes;  
  6.       for  (  int  i  =   0 ; i  <  n; i ++  )  
  7.          _unindexRecord(d -> idx(i), obj, dl,  ! noWarn); // 操作见下面代码段  
  8.       if ( d -> indexBuildInProgress ) {  // 对后台正在创建的索引进行_unindexRecord操作  
  9.           //  always pass nowarn here, as this one may be missing for valid reasons as we are concurrently building it  
  10.          _unindexRecord(d -> idx(n), obj, dl,  false ); // 操作见下面代码段  
  11.      }  
  12.  }  
  13.   // pdfile.cpp文件 815行  
  14.   /*  unindex all keys in index for this record.  */  
  15.   static   void  _unindexRecord(IndexDetails &  id, BSONObj &  obj,  const  DiskLoc &  dl,  bool  logMissing  =   true ) {  
  16.      BSONObjSetDefaultOrder keys;  
  17.      id.getKeysFromObject(obj, keys); // 通过记录获取键值信息  
  18.       for  ( BSONObjSetDefaultOrder::iterator i = keys.begin(); i  !=  keys.end(); i ++  ) {  
  19.          BSONObj j  =   * i;  
  20.           if  ( otherTraceLevel  >=   5  ) { // otherTraceLevel为外部变量,定义在query.cpp中,目前作用不清楚  
  21.               out ()  <<   " _unindexRecord()  "   <<  obj.toString();  
  22.               out ()  <<   " /n  unindex: "   <<  j.toString()  <<  endl;  
  23.          }  
  24.          nUnindexes ++ ; // 累加索引数  
  25.           bool  ok  =   false ;  
  26.           try  {  
  27.              ok  =  id.head.btree() -> unindex(id.head, id, j, dl); // 在btree bucket中删除记录的索引信息  
  28.          }  
  29.           catch  (AssertionException &  e) {  
  30.              problem()  <<   " Assertion failure: _unindex failed  "   <<  id.indexNamespace()  <<  endl;  
  31.               out ()  <<   " Assertion failure: _unindex failed:  "   <<  e.what()  <<   ' /n ' ;  
  32.               out ()  <<   "   obj: "   <<  obj.toString()  <<   ' /n ' ;  
  33.               out ()  <<   "   key: "   <<  j.toString()  <<   ' /n ' ;  
  34.               out ()  <<   "   dl: "   <<  dl.toString()  <<  endl;  
  35.              sayDbContext();  
  36.          }  
  37.           if  (  ! ok  &&  logMissing ) {  
  38.               out ()  <<   " unindex failed (key too big?)  "   <<  id.indexNamespace()  <<   ' /n ' ;  
  39.          }  
  40.      }  
  41.  }  
 


     上面代码主要是把要删除的记录的B树键值信息取出,然后通过循环(可能存在多键索引,具体参见我之前插入记录那篇文章中B树索引构造的相关内容)删除相应B树索引信息,下面代码段就是在B树中查找(locate)并最终删除(delKeyAtPos)的逻辑:

view plaincopy to clipboardprint?
  1. // btree.cpp文件 1116行  
  2.    /* 从索引中移除键值 */  
  3.    bool  BtreeBucket::unindex( const  DiskLoc thisLoc, IndexDetails &  id,  const  BSONObj &  key,  const  DiskLoc recordLoc )  const  {  
  4.        if  ( key.objsize()  >  KeyMax ) { // 判断键值是否大于限制  
  5.           OCCASIONALLY problem()  <<   " unindex: key too large to index, skipping  "   <<  id.indexNamespace()  <<   /*  ' ' << key.toString() <<  */  endl;  
  6.            return   false ;  
  7.       }  
  8.        int  pos;  
  9.        bool  found;  
  10.       DiskLoc loc  =  locate(id, thisLoc, key, Ordering::make(id.keyPattern()), pos, found, recordLoc,  1 ); // 从btree bucket中查找指定记录并获得位置信息(pos)  
  11.        if  ( found ) {  
  12.           loc.btreemod() -> delKeyAtPos(loc, id, pos, Ordering::make(id.keyPattern())); // 删除指定位置的记录信息  
  13.            return   true ;  
  14.       }  
  15.        return   false ;  
  16.   }  


      在删除b树索引之后,接着就是“删除内存(或磁盘,因为mmap机制)中的记录”了,也就是之前DataFileMgr::deleteRecord()方法的下面代码:

  
view plaincopy to clipboardprint?
  1. _deleteRecord(d, ns, todelete, dl)  

    
    其定义如下:

view plaincopy to clipboardprint?
  1. //pdfile.cpp文件 859行  
  2.      /* deletes a record, just the pdfile portion -- no index cleanup, no cursor cleanup, etc. 
  3.        caller must check if capped 
  4.     */  
  5.     void DataFileMgr::_deleteRecord(NamespaceDetails *d, const char *ns, Record *todelete, const DiskLoc& dl) {  
  6.         /* remove ourself from the record next/prev chain */  
  7.         {  
  8.             if ( todelete->prevOfs != DiskLoc::NullOfs )//如果要删除记录的前面有信息则记录到日志中  
  9.                 getDur().writingInt( todelete->getPrev(dl).rec()->nextOfs ) = todelete->nextOfs;  
  10.             if ( todelete->nextOfs != DiskLoc::NullOfs )//如果要删除记录的前面有信息则记录到日志中  
  11.                 getDur().writingInt( todelete->getNext(dl).rec()->prevOfs ) = todelete->prevOfs;  
  12.         }  
  13.         //extents是一个数据文件区域,该区域有所有记录(records)均属于同一个名空间namespace  
  14.         /* remove ourself from extent pointers */  
  15.         {  
  16.             Extent *e = getDur().writing( todelete->myExtent(dl) );  
  17.             if ( e->firstRecord == dl ) {//如果要删除记录为该extents区域第一条记录时  
  18.                 if ( todelete->nextOfs == DiskLoc::NullOfs )//且为唯一记录时  
  19.                     e->firstRecord.Null();//则该空间第一元素为空  
  20.                 else//将当前空间第一条(有效)记录后移一位  
  21.                     e->firstRecord.set(dl.a(), todelete->nextOfs);  
  22.             }  
  23.             if ( e->lastRecord == dl ) {//如果要删除记录为该extents区域最后一条记录时  
  24.                 if ( todelete->prevOfs == DiskLoc::NullOfs )//如果要删除记录的前一条信息位置为空时  
  25.                     e->lastRecord.Null();//该空间最后一条记录清空  
  26.                 else //设置该空间最后一条(有效)记录位置前移一位  
  27.                     e->lastRecord.set(dl.a(), todelete->prevOfs);  
  28.             }  
  29.         }  
  30.         /* 添加到释放列表中 */  
  31.         {  
  32.             {//更新空间统计信息  
  33.                 NamespaceDetails::Stats *s = getDur().writing(&d->stats);  
  34.                 s->datasize -= todelete->netLength();  
  35.                 s->nrecords--;  
  36.             }  
  37.             if ( strstr(ns, ".system.indexes") ) {//如果为索引空间,则把要删除记录在内存中的信息标识为0  
  38.                 /* temp: if in system.indexes, don't reuse, and zero out: we want to be 
  39.                    careful until validated more, as IndexDetails has pointers 
  40.                    to this disk location.  so an incorrectly done remove would cause 
  41.                    a lot of problems. 
  42.                 */  
  43.                 memset(getDur().writingPtr(todelete, todelete->lengthWithHeaders), 0, todelete->lengthWithHeaders);  
  44.             }  
  45.             else {  
  46.                 DEV {  
  47.                     unsigned long long *p = (unsigned long long *) todelete->data;  
  48.                     *getDur().writing(p) = 0;  
  49.                     //DEV memset(todelete->data, 0, todelete->netLength()); // attempt to notice invalid reuse.  
  50.                 }  
  51.                 d->addDeletedRec((DeletedRecord*)todelete, dl);//向当前空间的“要删除记录链表”中添加当前要删除的记录信息  
  52.             }  
  53.         }  
  54.     }  
 


     这里有一个数据结构要先解析一下,因为mongodb在删除记录时并不是真把记录从内存中remove出来,而是将该删除记录数据置空(写0或特殊数字加 以标识)同时将该记录所在地址放到一个list列表中,也就是上面代码注释中所说的“释放列表”,这样做的好就是就是如果有用户要执行插入记录操作 时,mongodb会首先从该“释放列表”中获取size合适的“已删除记录”地址返回,这种废物利用 的 方法会提升性能(避免了malloc内存操作),同时mongodb也使用了bucket size数组来定义多个大小size不同的列表,用于将要删除的记录根据其size大小放到合适的“释放列表”中(deletedList),有关该 deletedList内容,详见namespace.h文件中的注释内容。
    上面代码中如果记录的ns 在索引中则进行使用memset方法重置该记录数据,否则才执行将记录添加到“释放列表”操作,如下:

   
view plaincopy to clipboardprint?
  1. void  NamespaceDetails::addDeletedRec(DeletedRecord  * d, DiskLoc dloc) {  
  2.         BOOST_STATIC_ASSERT(  sizeof (NamespaceDetails::Extra)  <=   sizeof (NamespaceDetails) );  
  3.         {  
  4.             Record  * r  =  (Record  * ) getDur().writingPtr(d,  sizeof (Record));  
  5.             d  =   & r -> asDeleted(); // 转换成DeletedRecord类型  
  6.              // 防止引用已删除的记录  
  7.             (unsigned & ) (r -> data)  =   0xeeeeeeee ; // 修改要删除记录的数据信息  
  8.         }  
  9.         DEBUGGING log()  <<   " TEMP: add deleted rec  "   <<  dloc.toString()  <<   '   '   <<  hex  <<  d -> extentOfs  <<  endl;  
  10.          if  ( capped ) { // 如果是cap集合方式,则会将记录放到该集全中  
  11.              if  (  ! cappedLastDelRecLastExtent().isValid() ) {  
  12.                  //  Initial extent allocation.  Insert at end.  
  13.                 d -> nextDeleted  =  DiskLoc();  
  14.                  if  ( cappedListOfAllDeletedRecords().isNull() ) // deletedList[0] 是否为空,该值指向一个被删除的记录列表  
  15.                     getDur().writingDiskLoc( cappedListOfAllDeletedRecords() )  =  dloc; // 持久化该删除记录  
  16.                  else  {  
  17.                     DiskLoc i  =  cappedListOfAllDeletedRecords(); // 如果为空向该列表中添加删除记录  
  18.                      for  (;  ! i.drec() -> nextDeleted.isNull(); i  =  i.drec() -> nextDeleted ) // 遍历到最后一条记录  
  19.                         ;  
  20.                     i.drec() -> nextDeleted.writing()  =  dloc; // 将要删除的记录信息追加到链接尾部  
  21.                 }  
  22.             }  
  23.              else  {  
  24.                 d -> nextDeleted  =  cappedFirstDeletedInCurExtent(); // 将deletedList[0]放到“删除记录”的后面  
  25.                 getDur().writingDiskLoc( cappedFirstDeletedInCurExtent() )  =  dloc; // 持久化deletedList[0]信息并将当前要删除的dloc绑定到deletedList[0]位置  
  26.                  //  always compact() after this so order doesn't matter  
  27.             }  
  28.         }  
  29.          else  {  
  30.              int  b  =  bucket(d -> lengthWithHeaders); // 获取一个适合存储当前数据尺寸大小的bucket的序号, 参见当前文件的bucketSizes设置  
  31.             DiskLoc &  list  =  deletedList[b]; // 该值会与上面的cappedLastDelRecLastExtent(获取deletedList[0])相关联  
  32.             DiskLoc oldHead  =  list; // 取出第一条(head)记录  
  33.             getDur().writingDiskLoc(list)  =  dloc; // 将旧的记录信息数据持久化,并将list首记录绑定成当前要删除的dloc  
  34.             d -> nextDeleted  =  oldHead; // 将(第一条)旧记录绑定到当前已删除记录的nextDeleted上,形成一个链表  
  35.         }  
  36.     }  
 

     这样,就完成了将记录放到“释放列表”中的操作,上面的bucket中提供的大小款式 如下:

  
view plaincopy to clipboardprint?
  1. // namespace.cpp 文件37行  
  2.    /*  deleted lists -- linked lists of deleted records -- are placed in 'buckets' of various sizes 
  3.      so you can look for a deleterecord about the right size. 
  4.    */  
  5.    int  bucketSizes[]  =  {  
  6.        32 ,  64 ,  128 ,  256 ,  0x200 ,  0x400 ,  0x800 ,  0x1000 ,  0x2000 ,  0x4000 ,  
  7.        0x8000 ,  0x10000 ,  0x20000 ,  0x40000 ,  0x80000 ,  0x100000 ,  0x200000 ,  
  8.        0x400000 ,  0x800000  
  9.   };   


     
    最后,用一张时序图回顾一下删除记录时mongodb服务端代码的执行流程:

  

     好了,今天的内容到这里就告一段落了,在接下来的文章中,将会介绍客户端发起Update操作时,Mongodb的执行流程和相应实现部分。

    原文链接:http://www.cnblogs.com/daizhj/archive/2011/04/06/2006740.html
    作者: daizhj, 代震军   
    微博: http://t.sina.com.cn/daizhj
    Tags: mongodb,c++,source code