小明思考

          Just a software engineer
          posts - 124, comments - 36, trackbacks - 0, articles - 0
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          leveldb研究5- Snapshot

          Posted on 2012-03-13 16:54 小明 閱讀(4495) 評(píng)論(0)  編輯  收藏 所屬分類: 分布式計(jì)算
          所謂snapshot就是一個(gè)快照,我們可以從快照中讀到舊的數(shù)據(jù)。

          先寫一個(gè)測(cè)試程序來(lái)看看snapshot的使用:

          #include <iostream>
          #include 
          "leveldb/db.h"

          using namespace std;
          using namespace leveldb;


          int main() {
              DB 
          *db ;
              Options op;
              op.create_if_missing 
          = true;
              Status s 
          = DB::Open(op,"/tmp/testdb",&db);

              
          if(s.ok()){
                  cout 
          << "create successfully" << endl;
                  s 
          = db->Put(WriteOptions(),"abcd","1234");
                  
          if(s.ok()){
                      cout 
          << "put successfully" << endl;
                      
          string value;
                      s 
          = db->Get(ReadOptions(),"abcd",&value);
                      
          if(s.ok()){
                          cout 
          << "get successfully,value:" << value << endl;
                      }
                  }
                  
          if(s.ok()){
                      
          string value;
                      
          const Snapshot * ss =db->GetSnapshot();
                      ReadOptions rop;
                      db
          ->Put(WriteOptions(),"abcd","123456");
                      db
          ->Get(rop,"abcd",&value);
                      
          if(s.ok()){
                              cout 
          << "get successfully,value:" << value << endl;
                      }
                      rop.snapshot 
          = ss;
                      db
          ->Get(rop,"abcd",&value);
                      
          if(s.ok()){
                              cout 
          << "get from snapshot successfully,value:" << value << endl;
                      }
                      db
          ->ReleaseSnapshot(ss);
                  }
              }
              delete db;
              
          return 0;
          }

          程序運(yùn)行的輸出結(jié)果是:
          create successfully
          put successfully
          get successfully,value:1234
          get successfully,value:123456
          get from snapshot successfully,value:1234

          可以看出,即使在數(shù)據(jù)更新后,我們?nèi)匀豢梢詮膕napshot中讀到舊的數(shù)據(jù)。

          下面我們來(lái)分析leveldb中snapshot的實(shí)現(xiàn)。

          SequenceNumber(db/dbformat.h)
          SequenceNumber是leveldb很重要的東西,每次對(duì)數(shù)據(jù)庫(kù)進(jìn)行更新操作,都會(huì)生成一個(gè)新的SequenceNumber,64bits,其中高8位為0,可以跟key的類型(8bits)進(jìn)行合并成64bits。
          typedef uint64_t SequenceNumber;

          // We leave eight bits empty at the bottom so a type and sequence#
          // can be packed together into 64-bits.
          static const SequenceNumber kMaxSequenceNumber =
              ((0x1ull << 56) - 1);

          SnapShot(db/snapshot.h),,可以看出snapshot其實(shí)就是一個(gè)sequence number
          class SnapshotImpl : public Snapshot {
           
          public:
            
          //創(chuàng)建后保持不變
            SequenceNumber number_;  

           
          private:
            friend 
          class SnapshotList; 

            
          //雙向循環(huán)鏈表
            SnapshotImpl* prev_;
            SnapshotImpl
          * next_;

            SnapshotList
          * list_;                 // just for sanity checks
          };

          創(chuàng)建snapshot:
          const Snapshot* DBImpl::GetSnapshot() {
            MutexLock l(
          &mutex_);
            
          return snapshots_.New(versions_->LastSequence());
          }

          刪除snapshot:
          void DBImpl::ReleaseSnapshot(const Snapshot* s) {
            MutexLock l(
          &mutex_);
            snapshots_.Delete(reinterpret_cast
          <const SnapshotImpl*>(s));
          }




          主站蜘蛛池模板: 汤阴县| 淮南市| 合川市| 溧阳市| 青田县| 锡林浩特市| 莆田市| 高陵县| 会东县| 岚皋县| 古浪县| 科技| 兴海县| 沽源县| 和静县| 哈巴河县| 天津市| 门头沟区| 女性| 宜章县| 濮阳市| 静乐县| 二手房| 建始县| 望江县| 沙湾县| 青浦区| 日喀则市| 瑞金市| 石渠县| 岳普湖县| 蒙城县| 梅州市| 繁峙县| 东台市| 平江县| 璧山县| 茶陵县| 广汉市| 台江县| 饶平县|