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());
[] [] [] [] [] [] []