日期:2014-05-16 浏览次数:20527 次
        // 插入操作
        protected WriteResult insert(DBObject[] arr, boolean shouldApply , com.mongodb.WriteConcern concern )
        // 删除操作
        public WriteResult remove( DBObject o , com.mongodb.WriteConcern concern )
        // 查找操作
        Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize, int limit , int options )
         // 更新操作
        public WriteResult update( DBObject query , DBObject o , boolean upsert , boolean multi , com.mongodb.WriteConcern concern )
        // 插入操作
        protected WriteResult insert(DBObject[] arr, boolean shouldApply , com.mongodb.WriteConcern concern )
            throws MongoException {
            // 输出跟踪信息
            if ( willTrace() ) {
                for (DBObject o : arr) {
                    trace( "save:  " + _fullNameSpace + " " + JSON.serialize( o ) );
                }
            }
            // 是否更新对应的DBObject
            if ( shouldApply ){
                for ( int i=0; i<arr.length; i++ ){
                    DBObject o=arr[i];
                    apply( o );
                    _checkObject( o , false , false );
                    Object id = o.get( "_id" );
                    if ( id instanceof ObjectId ){
                        ((ObjectId)id).notNew();
                    }
                }
            }
            WriteResult last = null;
            // 输出 DBObject 到 Mongo 服务器
            int cur = 0;
            int maxsize = _mongo.getMaxBsonObjectSize();
            while ( cur < arr.length ){
                OutMessage om = new OutMessage( _mongo , 2002 );
 
                // 以 0 作为交互开始的信号
                om.writeInt( 0 ); // reserved
                // 输出完整的 namespace
                om.writeCString( _fullNameSpace );
                // 将要输出的对象写入 OutMessage 
                for ( ; cur<arr.length; cur++ ){
                    DBObject o = arr[cur];
                    om.putObject( o );
                    // 一次批量插入数据量的上限是 maxBsonObjectSize 的 4 倍
                    // 安全起见,这里使用 maxBsonObjectSize 的两倍
                    if ( om.size() > 2 * maxsize ){
                        // 超出一次批量插入的限制
                        // 停止构造 OutMessage,准备进入下一个循环
                        cur++;
                        break;
                    }
                }
 
                // 调用 DBTCPConnector 的 say 方法执行写入
                last = _connector.say( _db , om , concern );
            }
            
            return last;
        }
        
        // 查找操作
        Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize, int limit , int options )
            throws MongoException {
            
            if ( ref == null )
                ref = new BasicDBObject();
            
            // 输出跟踪信息
            if ( willTrace() ) trace( "find: " + _fullNameSpace + " " + JSON.serialize( ref ) );
 
            // 构造 OutMessage 
            OutMessage query = OutMessage.query( _mongo , options , _fullNameSpace , numToSkip , chooseBatchSize(batchSize, limit, 0) , ref , fields );
            // 调用 DBTCPConnector 的 call 方法获得查询结果
            Response res = _connector.call( _db , this , query , null , 2 );