熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C#使用BerkeleyDB操作簡介[2]

2022-06-13   來源: .NET編程 


  Berkeley DB是鍵值數據庫因此定義一個獲取鍵接口 

public interface IPut
   {
   string Key { get; }
   }

  數據庫的保存與更新 

public bool Set(IPut put)
   {
   Reset();
  
   keyStreamPosition = ;
   formatterSerialize(keyStream putKey);
   DbEntry key = DbEntryInOut(keyStreamGetBuffer() (int)keyStreamPosition);
   dataStreamPosition = ;
   formatterSerialize(dataStream put);
   DbEntry data = DbEntryInOut(dataStreamGetBuffer() (int)dataStreamPosition);
   WriteStatus status = btreePut(txn ref key ref data);
   switch (status)
   {
   case WriteStatusSuccess:
   return true;
   case WriteStatusNotFound:
   case WriteStatusKeyExist:
   default:
   return false;
   }
   }

  上述代碼就可以保存鍵值顯示對鍵值進行序列化然後再保存保存完有三個狀態可以一一處理

  數據庫的刪除

  刪除最為簡單 

  public bool Remove(IPut put)
   {
   keyStreamPosition = ;
   formatterSerialize(keyStream putKey);
   DbEntry key = DbEntryInOut(keyStreamGetBuffer() (int)keyStreamPosition);
   DeleteStatus status = btreeDelete(txn ref key);
   switch (status)
   {
   case DeleteStatusNotFound:
   case DeleteStatusSuccess:
   return true;
   case DeleteStatusKeyEmpty:
   default:
   return false;
   }
   }

  三關於添加和刪除 

  添加和刪除並沒有真正得進行添加和刪除必須執行Commit操作 

private bool iscomit = false;
   public void Commit()
   {
   txnCommit(TxnCommitModeNone);
   iscomit = true;
   }

  四尋找鍵

  用鍵查詢值和hash表一樣使用 

public bool Get(ref IPut put)
   {
   keyStreamPosition = ;
   formatterSerialize(keyStream putKey);
   DbEntry key = DbEntryInOut(keyStreamGetBuffer() (int)keyStreamPosition);
   dataStreamSetLength(dataStreamCapacity);
   DbEntry data = DbEntryOut(dataStreamGetBuffer());

[]  []  []  []  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/14803.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.