zxg588 发表于 2015-7-6 05:04:30

Mongodb源码分析--删除记录

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


   enum Operations {
      opReply = 1,   /* reply. responseTo is set. */
      dbMsg = 1000,    /* generic msg command followed by a string */
      dbUpdate = 2001, /* update object */
      dbInsert = 2002,
      //dbGetByOID = 2003,
      dbQuery = 2004,
      dbGetMore = 2005,
      dbDelete = 2006,
      dbKillCursors = 2007
    };
    可以看到dbDelete = 2002 为删除操作枚举值。当客户端将要删除的记录(或条件的document)发到服务端之后,mongodb通过消息封装方式将数据包中的字节流解析转成message类型,并进一步转换成dbmessage之后,mongodb就会根据消息类型进行判断,以决定接下来执行的操作),下面我们看一下assembleResponse在确定是删除操作时调用的方法,如下:
   


assembleResponse( Message &m, DbResponse &dbresponse, const SockAddr &client ) {
    .....
            try {
                if ( op == dbInsert ) {//添加记录操作
                  receivedInsert(m, currentOp);
                }
                else if ( op == dbUpdate ) { //更新记录
                  receivedUpdate(m, currentOp);
                }
                else if ( op == dbDelete ) { //删除记录
                  receivedDelete(m, currentOp);
                }
                else if ( op == dbKillCursors ) { //删除Cursors(游标)对象
                  currentOp.ensureStarted();
                  logThreshold = 10;
                  ss yieldSometimes() ) {//查看是否已到期(每个cc都会有一个读写操作时间,该值取决子获取读写锁时系统分配的时间,详见client.cpp 文件中的方法 int Client::recommendedYieldMicros( int * writers , int * readers ) {)
                cc.release(); // 时间已到则释放该对象(意味着已在别的地方被删除?)
                // TODO should we assert or something?
                break;
            }
            if ( !cc->ok() ) {
                break; // if we yielded, could have hit the end
            }
            // this way we can avoid calling updateLocation() every time (expensive)
            // as well as some other nuances handled
            cc->setDoingDeletes( true );
            DiskLoc rloc = cc->currLoc();//游标当前所指向的记录所在地址
            BSONObj key = cc->currKey();//游标当前所指向的记录的key
            // NOTE Calling advance() may change the matcher, so it's important
            // to try to match first.
            bool match = creal->matcher()->matches( key , rloc );//将当前游标指向的记录与游标中的where条件进行比较

            if ( ! cc->advance() )//游标移到下一个记录位置
                justOne = true;
            if ( ! match )
                continue;
            assert( !cc->c()->getsetdup(rloc) ); //不允许复本, 因为在多键值索引中可能会返回复本

            if ( !justOne ) {
                /* NOTE: this is SLOW.this is not good, noteLocation() was designed to be called across getMore
                  blocks.here we might call millions of times which would be bad.
                  */
                cc->c()->noteLocation();//记录当前游标移动到的位置
            }
            if ( logop ) {//是否保存操作日志
                BSONElement e;
                if( BSONObj( rloc.rec() ).getObjectID( e ) ) {
                  BSONObjBuilder b;
                  b.append( e );
                  bool replJustOne = true;
                  logOp( "d", ns, b.done(), 0, &replJustOne );//d表示delete
                }
                else {
                  problem() c->current()*/ );
            theDataFileMgr.deleteRecord(ns, rloc.rec(), rloc);//删除查询匹配到的记录
            nDeleted++;//累计删除信息数
            if ( justOne ) {
                break;
            }
            cc->c()->checkLocation();//因为删除完记录好,会造成缓存中相关索引信息过期,用该方法能确保索引有效
         
            if( !god )
                getDur().commitIfNeeded();
            if( debug && god && nDeleted == 100 ) //删除100条信息之后,显示内存使用预警信息
                log() capped && !cappedOK ) {//如果是capped collection类型,则不删除
            out() indexBuildInProgress ) { //对后台正在创建的索引进行_unindexRecord操作
            // always pass nowarn here, as this one may be missing for valid reasons as we are concurrently building it
            _unindexRecord(d->idx(n), obj, dl, false);//操作见下面代码段
      }
    }
    //pdfile.cpp文件 815行
    /* unindex all keys in index for this record. */
    static void _unindexRecord(IndexDetails& id, BSONObj& obj, const DiskLoc& dl, bool logMissing = true) {
      BSONObjSetDefaultOrder keys;
      id.getKeysFromObject(obj, keys);//通过记录获取键值信息
      for ( BSONObjSetDefaultOrder::iterator i=keys.begin(); i != keys.end(); i++ ) {
            BSONObj j = *i;
            if ( otherTraceLevel >= 5 ) {//otherTraceLevel为外部变量,定义在query.cpp中,目前作用不清楚
                out() lengthWithHeaders);//获取一个适合存储当前数据尺寸大小的bucket的序号, 参见当前文件的bucketSizes设置
            DiskLoc& list = deletedList;//该值会与上面的cappedLastDelRecLastExtent(获取deletedList)相关联
            DiskLoc oldHead = list;//取出第一条(head)记录
            getDur().writingDiskLoc(list) = dloc;//将旧的记录信息数据持久化,并将list首记录绑定成当前要删除的dloc
            d->nextDeleted = oldHead;//将(第一条)旧记录绑定到当前已删除记录的nextDeleted上,形成一个链表
      }
    }  

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



    //namespace.cpp 文件37行
    /* deleted lists -- linked lists of deleted records -- are placed in 'buckets' of various sizes
       so you can look for a deleterecord about the right size.
    */
    int bucketSizes[] = {
      32, 64, 128, 256, 0x200, 0x400, 0x800, 0x1000, 0x2000, 0x4000,
      0x8000, 0x10000, 0x20000, 0x40000, 0x80000, 0x100000, 0x200000,
      0x400000, 0x800000
    };
  
   
    最后,用一张时序图回顾一下删除记录时mongodb服务端代码的执行流程:

http://daizhj.iyunv.com/images/cnblogs_com/daizhj/mongodb_deleterecord.png
  
  好了,今天的内容到这里就告一段落了,在接下来的文章中,将会介绍客户端发起Update操作时,Mongodb的执行流程和相应实现部分。
    原文链接:http://www.iyunv.com/daizhj/archive/2011/04/06/mongodb_delete_recode_source_code.html
    作者: daizhj, 代震军   
    微博: http://t.sina.com.cn/daizhj
    Tags: mongodb,c++,source code
页: [1]
查看完整版本: Mongodb源码分析--删除记录