亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

Chinaunix

標(biāo)題: MongoDB Java Driver 源碼分析(3):com.mongodb.DBCollection [打印本頁(yè)]

作者: 凝望長(zhǎng)空    時(shí)間: 2011-11-13 17:24
標(biāo)題: MongoDB Java Driver 源碼分析(3):com.mongodb.DBCollection
MongoDB Java Driver 源碼分析(3):com.mongodb.DBCollection





DBCollection 是表示數(shù)據(jù)集合的抽象類(lèi),它的實(shí)現(xiàn)可以簡(jiǎn)單地分為兩類(lèi):
  一類(lèi)是抽象方法,由子類(lèi)(DBApiLayer.MyCollection)實(shí)現(xiàn);
  另一類(lèi)委托給類(lèi)型為 "DB" 的屬性 _db,_db 實(shí)際上是 DBApiLayer 類(lèi)的實(shí)例(DBApiLayer 繼承抽象類(lèi) DB);

  因此,DBCollection 類(lèi)是實(shí)現(xiàn)細(xì)節(jié)與 DBApiLayer 關(guān)系密切。
  DBApiLyer 的實(shí)現(xiàn)細(xì)節(jié)我們將在后續(xù)文章中進(jìn)行詳細(xì)的描述,本文主要探討兩者之間的聯(lián)系。

由子類(lèi)實(shí)現(xiàn)的方法

  以下方法是由子類(lèi)實(shí)現(xiàn)的,這些將在介紹 DBApiLayer 時(shí)再進(jìn)行詳細(xì)說(shuō)明:

Java代碼
  1. 1.// 插入記錄   
  2. 2.public abstract WriteResult insert(DBObject[] arr , WriteConcern concern )   
  3. 3.  
  4. 4.// 更新記錄   
  5. 5.public abstract WriteResult update( DBObject q , DBObject o , boolean upsert , boolean multi , WriteConcern concern )   
  6. 6.  
  7. 7.// 保存數(shù)據(jù)之前附加額外屬性   
  8. 8.protected abstract void doapply( DBObject o )   
  9. 9.  
  10. 10.// 刪除記錄   
  11. 11.public abstract WriteResult remove( DBObject o , WriteConcern concern )   
  12. 12.  
  13. 13.// 查詢(xún)記錄   
  14. 14.abstract Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit, int options )   
  15. 15.  
  16. 16.// 創(chuàng)建索引   
  17. 17.public abstract void createIndex( DBObject keys , DBObject options )   
  18. // 插入記錄
  19. public abstract WriteResult insert(DBObject[] arr , WriteConcern concern )

  20. // 更新記錄
  21. public abstract WriteResult update( DBObject q , DBObject o , boolean upsert , boolean multi , WriteConcern concern )

  22. // 保存數(shù)據(jù)之前附加額外屬性
  23. protected abstract void doapply( DBObject o )

  24. // 刪除記錄
  25. public abstract WriteResult remove( DBObject o , WriteConcern concern )

  26. // 查詢(xún)記錄
  27. abstract Iterator<DBObject> __find( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit, int options )

  28. // 創(chuàng)建索引
  29. public abstract void createIndex( DBObject keys , DBObject options )
復(fù)制代碼
  此外,以下方法是在這些方法的基礎(chǔ)上,在進(jìn)行包裝和組合得到的:

Java代碼
  1. 1.public final void ensureIndex( final DBObject keys , final DBObject optionsIN )   
  2. 2.public final DBObject findOne( DBObject o, DBObject fields )   
  3. 3.public final Object apply( DBObject jo , boolean ensureID )  
  4. public final void ensureIndex( final DBObject keys , final DBObject optionsIN )
  5. public final DBObject findOne( DBObject o, DBObject fields )
  6. public final Object apply( DBObject jo , boolean ensureID )
復(fù)制代碼
  ensurceIndex 方法用于創(chuàng)建索引,調(diào)用了抽象方法 createdIndex ,在此之前,先檢查數(shù)據(jù)庫(kù)是否為只讀,以及是否重復(fù)創(chuàng)建索引。
  具體實(shí)現(xiàn)如下:

Java代碼
  1. 1.public final void ensureIndex( final DBObject keys , final DBObject optionsIN )   
  2. 2.    throws MongoException {   
  3. 3.  
  4. 4.    // 檢查是否為只讀   
  5. 5.    if ( checkReadOnly( false ) ) return;   
  6. 6.  
  7. 7.    // 設(shè)置參數(shù)   
  8. 8.    final DBObject options = defaultOptions( keys );   
  9. 9.    for ( String k : optionsIN.keySet() )   
  10. 10.        options.put( k , optionsIN.get( k ) );   
  11. 11.  
  12. 12.    // 取得參數(shù)中的索引名稱(chēng)   
  13. 13.    final String name = options.get( "name" ).toString();   
  14. 14.  
  15. 15.    // 如果索引已經(jīng)創(chuàng)建,則返回,避免重復(fù)創(chuàng)建   
  16. 16.    if ( _createdIndexes.contains( name ) )   
  17. 17.        return;   
  18. 18.  
  19. 19.    // 創(chuàng)建索引,并添加到 createdIndexes   
  20. 20.    createIndex( keys , options );   
  21. 21.    _createdIndexes.add( name );   
  22. 22.}  
  23.     public final void ensureIndex( final DBObject keys , final DBObject optionsIN )
  24.         throws MongoException {

  25.         // 檢查是否為只讀
  26.         if ( checkReadOnly( false ) ) return;

  27.         // 設(shè)置參數(shù)
  28.         final DBObject options = defaultOptions( keys );
  29.         for ( String k : optionsIN.keySet() )
  30.             options.put( k , optionsIN.get( k ) );

  31.         // 取得參數(shù)中的索引名稱(chēng)
  32.         final String name = options.get( "name" ).toString();

  33.         // 如果索引已經(jīng)創(chuàng)建,則返回,避免重復(fù)創(chuàng)建
  34.         if ( _createdIndexes.contains( name ) )
  35.             return;

  36.         // 創(chuàng)建索引,并添加到 createdIndexes
  37.         createIndex( keys , options );
  38.         _createdIndexes.add( name );
  39.     }
復(fù)制代碼
  findOne 方法用于查詢(xún)一個(gè)對(duì)象,第一個(gè)參數(shù)是表示查詢(xún)條件的 DBObject,第二個(gè)參數(shù)表示返回結(jié)果包含的字段。
  調(diào)用了抽象方法 _find,并取第一條。
  具體實(shí)現(xiàn)如下:

Java代碼
  1. 1.public final DBObject findOne( DBObject o, DBObject fields ) {   
  2. 2.    // 調(diào)用 _find 方法   
  3. 3.    Iterator<DBObject> i = __find( o , fields , 0 , -1 , 0, getOptions() );   
  4. 4.  
  5. 5.    // 讀取第一條記錄   
  6. 6.    if ( i == null || ! i.hasNext() )   
  7. 7.        return null;   
  8. 8.    return i.next();   
  9. 9.}  
  10.     public final DBObject findOne( DBObject o, DBObject fields ) {
  11.         // 調(diào)用 _find 方法
  12.         Iterator<DBObject> i = __find( o , fields , 0 , -1 , 0, getOptions() );

  13.         // 讀取第一條記錄
  14.         if ( i == null || ! i.hasNext() )
  15.             return null;
  16.         return i.next();
  17.     }
復(fù)制代碼
  apply 方法用于在保存對(duì)象之前,為對(duì)象添加額外的屬性。
  除了調(diào)用抽象方法 doapply,還可以根據(jù) ensureId 的值,生成 id。
  具體實(shí)現(xiàn)如下:

Java代碼
  1. 1.public final Object apply( DBObject jo , boolean ensureID ){   
  2. 2.    // 根據(jù) ensureId 的值,確定是否生成 id   
  3. 3.    Object id = jo.get( "_id" );   
  4. 4.    if ( ensureID && id == null ){   
  5. 5.        id = ObjectId.get();   
  6. 6.        jo.put( "_id" , id );   
  7. 7.    }   
  8. 8.  
  9. 9.    // 調(diào)用抽象方法 doapply,為對(duì)象附加額外屬性。   
  10. 10.    doapply( jo );   
  11. 11.  
  12. 12.    return id;   
  13. 13.}  
  14.     public final Object apply( DBObject jo , boolean ensureID ){
  15.         // 根據(jù) ensureId 的值,確定是否生成 id
  16.         Object id = jo.get( "_id" );
  17.         if ( ensureID && id == null ){
  18.             id = ObjectId.get();
  19.             jo.put( "_id" , id );
  20.         }

  21.         // 調(diào)用抽象方法 doapply,為對(duì)象附加額外屬性。
  22.         doapply( jo );

  23.         return id;
  24.     }
復(fù)制代碼
委托給 _db 的方法


Java代碼
  1. 1.// 查詢(xún)并修改對(duì)象   
  2. 2.public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort,   
  3. 3.    boolean remove, DBObject update, boolean returnNew, boolean upsert)   
  4. 4.  
  5. 5.// 刪除索引   
  6. 6.public void dropIndexes( String name )   
  7. 7.  
  8. 8.// 刪除該數(shù)據(jù)集   
  9. 9.public void drop()   
  10. 10.  
  11. 11.// 統(tǒng)計(jì)數(shù)量   
  12. 12.public long getCount(DBObject query, DBObject fields, long limit, long skip )   
  13. 13.  
  14. 14.// 重命名該集合   
  15. 15.public DBCollection rename( String newName, boolean dropTarget )   
  16. 16.  
  17. 17.// 執(zhí)行 Group 操作   
  18. 18.public DBObject group( GroupCommand cmd )   
  19. 19.  
  20. 20.// 查詢(xún)對(duì)象并去除重復(fù)   
  21. 21.public List distinct( String key , DBObject query )   
  22. 22.  
  23. 23.// 執(zhí)行 Map Reduce 操作   
  24. 24.public MapReduceOutput mapReduce( MapReduceCommand command )   
  25. 25.  
  26. 26.// 獲取索引信息   
  27. 27.public List<DBObject> getIndexInfo()   
  28. 28.  
  29. 29.// 獲取子數(shù)據(jù)集   
  30. 30.public DBCollection getCollection( String n )  
  31. // 查詢(xún)并修改對(duì)象
  32. public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort,
  33.     boolean remove, DBObject update, boolean returnNew, boolean upsert)

  34. // 刪除索引
  35. public void dropIndexes( String name )

  36. // 刪除該數(shù)據(jù)集
  37. public void drop()

  38. // 統(tǒng)計(jì)數(shù)量
  39. public long getCount(DBObject query, DBObject fields, long limit, long skip )

  40. // 重命名該集合
  41. public DBCollection rename( String newName, boolean dropTarget )

  42. // 執(zhí)行 Group 操作
  43. public DBObject group( GroupCommand cmd )

  44. // 查詢(xún)對(duì)象并去除重復(fù)
  45. public List distinct( String key , DBObject query )

  46. // 執(zhí)行 Map Reduce 操作
  47. public MapReduceOutput mapReduce( MapReduceCommand command )

  48. // 獲取索引信息
  49. public List<DBObject> getIndexInfo()

  50. // 獲取子數(shù)據(jù)集
  51. public DBCollection getCollection( String n )
復(fù)制代碼
  以 findAndModify 為例。
  findAndModify 方法用于查詢(xún)并修改對(duì)象,實(shí)際調(diào)用的是 _db.command( cmd ) 方法;
  在調(diào)用 _db 的方法之前,先對(duì) cmd 進(jìn)行組裝,添加查詢(xún)條件、查詢(xún)字段、排序條件等;
  然后調(diào)用 _db.command( cmd ),返回結(jié)果或拋出異常。
  具體實(shí)現(xiàn)如下:

Java代碼
  1. 1.public DBObject findAndModify(DBObject query, DBObject fields, DBObject sort,   
  2. 2.    boolean remove, DBObject update, boolean returnNew, boolean upsert) {   
  3. 3.    // 創(chuàng)建 cmd 對(duì)象   
  4. 4.    BasicDBObject cmd = new BasicDBObject( "findandmodify", _name);   
  5. 5.  
  6. 6.    // 添加查詢(xún)條件   
  7. 7.    if (query != null && !query.keySet().isEmpty())   
  8. 8.        cmd.append( "query", query );   
  9. 9.  
  10. 10.    // 添加查詢(xún)字段   
  11. 11.    if (fields != null && !fields.keySet().isEmpty())   
  12. 12.        cmd.append( "fields", fields );   
  13. 13.  
  14. 14.    // 添加排序字段   
  15. 15.    if (sort != null && !sort.keySet().isEmpty())   
  16. 16.        cmd.append( "sort", sort );   
  17. 17.  
  18. 18.    // 是否執(zhí)行刪除   
  19. 19.    if (remove)   
  20. 20.        cmd.append( "remove", remove );   
  21. 21.    else {   
  22. 22.        // 添加更新的字段   
  23. 23.        if (update != null && !update.keySet().isEmpty()) {   
  24. 24.            // if 1st key doesnt start with $, then object will be inserted as is, need to check it   
  25. 25.            String key = update.keySet().iterator().next();   
  26. 26.            if (key.charAt(0) != '$')   
  27. 27.                _checkObject(update, false, false);   
  28. 28.            cmd.append( "update", update );   
  29. 29.        }   
  30. 30.        // 是否返回新創(chuàng)建的對(duì)象   
  31. 31.        if (returnNew)   
  32. 32.            cmd.append( "new", returnNew );   
  33. 33.  
  34. 34.       // 被查詢(xún)的對(duì)象不存在時(shí)是否創(chuàng)建一個(gè)   
  35. 35.       if (upsert)   
  36. 36.            cmd.append( "upsert", upsert );   
  37. 37.    }   
  38. 38.  
  39. 39.    // 刪除操作與更新操作不能混合,拋異常   
  40. 40.    if (remove && !(update == null || update.keySet().isEmpty() || returnNew))   
  41. 41.        throw new MongoException("FindAndModify: Remove cannot be mixed with the Update, or returnNew params!");   
  42. 42.  
  43. 43.    // 執(zhí)行 _db.command( cmd )   
  44. 44.    CommandResult res = this._db.command( cmd );   
  45. 45.  
  46. 46.    // 返回結(jié)果或拋異常   
  47. 47.    if (res.ok() || res.getErrorMessage().equals( "No matching object found" ))   
  48. 48.        return (DBObject) res.get( "value" );   
  49. 49.    res.throwOnError();   
  50. 50.    return null;   
  51. 51.}
復(fù)制代碼

作者: 寂寞沖咖啡    時(shí)間: 2011-11-14 09:38
謝謝分享




歡迎光臨 Chinaunix (http://72891.cn/) Powered by Discuz! X3.2