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

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 3616 | 回復: 1
打印 上一主題 下一主題

[MongoDB] MongoDB Java Driver 源碼分析(3):com.mongodb.DBCollection [復制鏈接]

論壇徽章:
0
跳轉到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2011-11-13 17:24 |只看該作者 |倒序瀏覽
MongoDB Java Driver 源碼分析(3):com.mongodb.DBCollection





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

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

由子類實現(xiàn)的方法

  以下方法是由子類實現(xiàn)的,這些將在介紹 DBApiLayer 時再進行詳細說明:

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.// 查詢記錄   
  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. // 查詢記錄
  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 )
復制代碼
  此外,以下方法是在這些方法的基礎上,在進行包裝和組合得到的:

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 )
復制代碼
  ensurceIndex 方法用于創(chuàng)建索引,調用了抽象方法 createdIndex ,在此之前,先檢查數(shù)據(jù)庫是否為只讀,以及是否重復創(chuàng)建索引。
  具體實現(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ù)   
  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ù)中的索引名稱   
  13. 13.    final String name = options.get( "name" ).toString();   
  14. 14.  
  15. 15.    // 如果索引已經(jīng)創(chuàng)建,則返回,避免重復創(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ù)
  28.         final DBObject options = defaultOptions( keys );
  29.         for ( String k : optionsIN.keySet() )
  30.             options.put( k , optionsIN.get( k ) );

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

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

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

Java代碼
  1. 1.public final DBObject findOne( DBObject o, DBObject fields ) {   
  2. 2.    // 調用 _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.         // 調用 _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.     }
復制代碼
  apply 方法用于在保存對象之前,為對象添加額外的屬性。
  除了調用抽象方法 doapply,還可以根據(jù) ensureId 的值,生成 id。
  具體實現(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.    // 調用抽象方法 doapply,為對象附加額外屬性。   
  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.         // 調用抽象方法 doapply,為對象附加額外屬性。
  22.         doapply( jo );

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


Java代碼
  1. 1.// 查詢并修改對象   
  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)計數(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.// 查詢對象并去除重復   
  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. // 查詢并修改對象
  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)計數(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. // 查詢對象并去除重復
  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 )
復制代碼
  以 findAndModify 為例。
  findAndModify 方法用于查詢并修改對象,實際調用的是 _db.command( cmd ) 方法;
  在調用 _db 的方法之前,先對 cmd 進行組裝,添加查詢條件、查詢字段、排序條件等;
  然后調用 _db.command( cmd ),返回結果或拋出異常。
  具體實現(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 對象   
  4. 4.    BasicDBObject cmd = new BasicDBObject( "findandmodify", _name);   
  5. 5.  
  6. 6.    // 添加查詢條件   
  7. 7.    if (query != null && !query.keySet().isEmpty())   
  8. 8.        cmd.append( "query", query );   
  9. 9.  
  10. 10.    // 添加查詢字段   
  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)建的對象   
  31. 31.        if (returnNew)   
  32. 32.            cmd.append( "new", returnNew );   
  33. 33.  
  34. 34.       // 被查詢的對象不存在時是否創(chuàng)建一個   
  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.    // 返回結果或拋異常   
  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.}
復制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2011-11-14 09:38 |只看該作者
謝謝分享
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復

  

北京盛拓優(yōu)訊信息技術有限公司. 版權所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關心和支持過ChinaUnix的朋友們 轉載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP