Your SlideShare is downloading. ×
  • Like
カジュアルにソースコードリーディング
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

カジュアルにソースコードリーディング

  • 4,511 views
Published

20120706 MongoDB Casual Talks

20120706 MongoDB Casual Talks

Published in Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,511
On SlideShare
0
From Embeds
0
Number of Embeds
6

Actions

Shares
Downloads
4
Comments
0
Likes
5

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

Transcript

  • 1. カジュアルにソースコードリーディング 2012/7/6 MongoDB Casual by @choplin
  • 2. 自己紹介•@choplin•Web/DBエンジニア(でした)•(MongoDBあまり使ってません)•Love SQL & PostgreSQL
  • 3. Agenda•追記型•In-place Update•Padding Factor
  • 4. 追記型
  • 5. 追記型とは•更新を削除と挿入の組み合せで実現する •PostgreSQL •SQLite •MongoDB •CouchDB •KVSいろいろ
  • 6. InsertTableの物理表現 Record A Record A Record B Record B Insert C Record C
  • 7. UpdateRecord A Record ARecord B Record B Update A Record A
  • 8. Pros. Cons.•Pros •同時実行制御の処理が簡潔に •MVCC•Cons •データ容量の肥大 •書き込み処理の負荷 •ガベージの発生 •Vacuum, Compact •(あれば)Indexの更新
  • 9. In-place Update
  • 10. In-place Update•既存のサイズを超えない場合は、ドキュメント丸ごとの追記を行わず、必要な値のみを書き換える
  • 11. Update{a:1} {a:1} {a:1,b:2} {$set: {b:2}}
  • 12. In-place Update{a:1} {a:2} {$inc: {a:1}}
  • 13. Padding Factor
  • 14. Padding Factor•In-place Updateに倒すために予めpaddingを含めた領域を確保する •どれだけのpaddingをとるかのfactor•設定はできない
  • 15. Padding Factor case. Padding Factor = 1.0Insert Header (16B) + BSON (12B)On disk 28B
  • 16. Padding Factor case. Padding Factor = 1.5Insert Header (16B) + BSON (12B) + Padding(14B)On disk 42B
  • 17. collStatsで確認> db.test.insert({a:1})> db.test.stats(){ "ns" : "test2.test", "count" : 1, "size" : 36, "avgObjSize" : 36, "storageSize" : 4096, "numExtents" : 1, "nindexes" : 1, "lastExtentSize" : 4096, "paddingFactor" : 1, "flags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1}>
  • 18. How?
  • 19. db/namespace.hclass NamespaceDetails { ... double paddingFactor; ... void paddingFits() { double x = paddingFactor - 0.01; if ( x >= 1.0 ) { *getDur().writing(&paddingFactor) = x; //getDur().setNoJournal(&paddingFactor,&x, sizeof(x)); } } void paddingTooSmall() { double x = paddingFactor + 0.6; if ( x <= 2.0 ) { *getDur().writing(&paddingFactor) = x; //getDur().setNoJournal(&paddingFactor,&x, sizeof(x)); } } ...}※ namespaceとはcollectionだと思ってもらってOK
  • 20. db/namespace.cppNamespaceDetails::NamespaceDetails( const DiskLoc &loc, bool _capped ) { ... paddingFactor = 1.0; ...} ※ namespaceとはcollectionだと思ってもらってOK
  • 21. db/pdfile.cppconst DiskLoc DataFileMgr::updateRecord(..., NamespaceDetails *d, ...){ ... if ( toupdate->netLength() < objNew.objsize() ) { ... d->paddingTooSmall(); ... deleteRecord(ns, toupdate, dl); return insert(ns, objNew.objdata(), objNew.objsize(), god); } ... d->paddingFits(); ...}DiskLoc DataFileMgr::insert(const char *ns, ...) { ... NamespaceDetails *d = nsdetails(ns); ... d->paddingFits(); ...} ※DataFileMgrはデータの物理表現を管理するクラス
  • 22. Update> db.test.update({a:1}, {$set: {b:1}})> db.test.stats(){ "ns" : "test2.test", "count" : 1, "size" : 80, "avgObjSize" : 80, "storageSize" : 4096, "numExtents" : 1, "nindexes" : 1, "lastExtentSize" : 4096, "paddingFactor" : 1.59, "flags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1}>
  • 23. Insert> db.test.insert({c:1})> db.test.stats(){ "ns" : "test2.test", "count" : 2, "size" : 144, "avgObjSize" : 72, "storageSize" : 4096, "numExtents" : 1, "nindexes" : 1, "lastExtentSize" : 4096, "paddingFactor" : 1.58, "flags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1}
  • 24. In-place Update> db.test.update({c:1}, {$set:{c:2}})> db.test.stats(){ "ns" : "test2.test", "count" : 2, "size" : 144, "avgObjSize" : 72, "storageSize" : 4096, "numExtents" : 1, "nindexes" : 1, ? "lastExtentSize" : 4096, "paddingFactor" : 1.58, "flags" : 1, "totalIndexSize" : 8176, "indexSizes" : { "_id_" : 8176 }, "ok" : 1}
  • 25. まとめ•MongoDBは追記型•追記を避けるためのIn-place Update•In-place UpdateのためのPadding Factor •default 1.0 •Min 1.0, Max 2.0 •Update +0.6 •Insert / (In-place Update?) -0.01