Berkeley DB是鍵值數據庫
因此定義一個獲取鍵接口
public interface IPut
{
string Key { get; }
}
一數據庫的保存與更新
public bool Set(IPut put)
{
Reset();
keyStream
Position =
;
formatter
Serialize(keyStream
put
Key);
DbEntry key = DbEntry
InOut(keyStream
GetBuffer()
(int)keyStream
Position);
dataStream
Position =
;
formatter
Serialize(dataStream
put);
DbEntry data = DbEntry
InOut(dataStream
GetBuffer()
(int)dataStream
Position);
WriteStatus status = btree
Put(txn
ref key
ref data);
switch (status)
{
case WriteStatus
Success:
return true;
case WriteStatus
NotFound:
case WriteStatus
KeyExist:
default:
return false;
}
}
上述代碼就可以保存鍵值顯示對鍵值進行序列化然後再保存保存完有三個狀態可以一一處理
二數據庫的刪除
刪除最為簡單
public bool Remove(IPut put)
{
keyStream
Position =
;
formatter
Serialize(keyStream
put
Key);
DbEntry key = DbEntry
InOut(keyStream
GetBuffer()
(int)keyStream
Position);
DeleteStatus status = btree
Delete(txn
ref key);
switch (status)
{
case DeleteStatus
NotFound:
case DeleteStatus
Success:
return true;
case DeleteStatus
KeyEmpty:
default:
return false;
}
}
三關於添加和刪除
添加和刪除並沒有真正得進行添加和刪除必須執行Commit操作
private bool iscomit = false;
public void Commit()
{
txn
Commit(Txn
CommitMode
None);
iscomit = true;
}
四尋找鍵
用鍵查詢值和hash表一樣使用
public bool Get(ref IPut put)
{
keyStream
Position =
;
formatter
Serialize(keyStream
put
Key);
DbEntry key = DbEntry
InOut(keyStream
GetBuffer()
(int)keyStream
Position);
dataStream
SetLength(dataStream
Capacity);
DbEntry data = DbEntry
Out(dataStream
GetBuffer());
[] [] [] [] [] [] []
From:http://tw.wingwit.com/Article/program/net/201311/14803.html