By Birdshover@ 博客園
下載到Berkeley DB for
BerkeleyDB的數據庫操作需要借助DbBTree類
下面代碼就是初始化得到DbBTree實例的一個過程
/// <summary>
/// 數據庫目錄
/// </summary>
private string directory;
/// <summary>
/// 數據庫文件名
/// </summary>
private string dbName;
private DbBTree btree;
private Txn txn;
private Db db;
private Env env;
/// <summary>
/// 初始化
/// </summary>
private void Init()
{
env = new Env(EnvCreateFlags
Env
Env
Env
Env
Env
Env
Env
env
txn = env
db = env
btree = (DbBTree)db
}
另外Berkeley DB數據庫的操作需要借助於序列化
/// <summary>
/// 二進制序列化
/// </summary>
private BinaryFormatter formatter;
/// <summary>
/// 鍵內存流
/// </summary>
private MemoryStream keyStream;
/// <summary>
/// 內容內存流
/// </summary>
private MemoryStream dataStream;
private void StreamInit()
{
formatter = new BinaryFormatter();
keyStream = new MemoryStream();
dataStream = new MemoryStream();
}
Berkeley DB是鍵值數據庫
public interface IPut
{
string Key { get; }
}
一
public bool Set(IPut put)
{
Reset();
keyStream
formatter
DbEntry key = DbEntry
dataStream
formatter
DbEntry data = DbEntry
WriteStatus status = btree
switch (status)
{
case WriteStatus
return true;
case WriteStatus
case WriteStatus
default:
return false;
}
}
上述代碼就可以保存鍵值
二
刪除最為簡單
public bool Remove(IPut put)
{
keyStream
formatter
DbEntry key = DbEntry
DeleteStatus status = btree
switch (status)
{
case DeleteStatus
case DeleteStatus
return true;
case DeleteStatus
default:
return false;
}
}
三
添加和刪除並沒有真正得進行添加和刪除
private bool iscomit = false;
public void Commit()
{
txn
iscomit = true;
}
四
用鍵查詢值
public bool Get(ref IPut put)
{
keyStream
formatter
DbEntry key = DbEntry
dataStream
DbEntry data = DbEntry
while (true)
{
ReadStatus status = btree
switch (status)
{
case ReadStatus
dataStream
dataStream
put = (IPut)formatter
return true;
case ReadStatus
if (key
{
keyStream
key = DbEntry
}
if (data
{
dataStream
data = DbEntry
}
continue;
case ReadStatus
case ReadStatus
default:
return false;
}
}
}
五
public List<IPut> Find()
{
List<IPut> custList = new List<IPut>();
using (DbBTreeCursor cursor = btree
{
IPut cust = null;
while (GetNextRecord(cursor
custList
}
return custList;
}
private bool GetNextRecord(DbBTreeCursor cursor
{
ReadStatus status;
keyStream
dataStream
DbEntry key = DbEntry
DbEntry data = DbEntry
do
{
status = cursor
switch (status)
{
case ReadStatus
case ReadStatus
case ReadStatus
if (key
{
keyStream
key = DbEntry
}
if (data
{
dataStream
data = DbEntry
}
continue;
case ReadStatus
dataStream
dataStream
cust = (IPut)formatter
return true;
default:
return false;
}
} while (true);
}
六
public interface IPut
{
string Key { get; }
}
public class BDBManager : IDisposable
{
/// <summary>
/// 數據庫目錄
/// </summary>
private string directory;
/// <summary>
/// 數據庫文件名
/// </summary>
private string dbName;
private DbBTree btree;
private Txn txn;
private Db db;
private Env env;
/// <summary>
/// 二進制序列化
/// </summary>
private BinaryFormatter formatter;
/// <summary>
/// 鍵內存流
/// </summary>
private MemoryStream keyStream;
/// <summary>
/// 內容內存流
/// </summary>
private MemoryStream dataStream;
public BDBManager(string directory
{
this
this
Init();
StreamInit();
}
public bool Set(IPut put)
{
Reset();
keyStream
formatter
DbEntry key = DbEntry
dataStream
formatter
DbEntry data = DbEntry
WriteStatus status = btree
switch (status)
{
case WriteStatus
return true;
case WriteStatus
case WriteStatus
default:
return false;
}
}
private bool iscomit = false;
public void Commit()
{
txn
iscomit = true;
}
public List<IPut> Find()
{
List<IPut> custList = new List<IPut>();
using (DbBTreeCursor cursor = btree
{
IPut cust = null;
while (GetNextRecord(cursor
custList
}
return custList;
}
public bool Get(ref IPut put)
{
keyStream
formatter
DbEntry key = DbEntry
dataStream
DbEntry data = DbEntry
while (true)
{
ReadStatus status = btree
switch (status)
{
case ReadStatus
dataStream
dataStream
put = (IPut)formatter
return true;
case ReadStatus
if (key
{
keyStream
key = DbEntry
}
if (data
{
dataStream
data = DbEntry
}
continue;
case ReadStatus
case ReadStatus
default:
return false;
}
}
}
public bool Remove(IPut put)
{
Reset();
keyStream
formatter
DbEntry key = DbEntry
DeleteStatus status = btree
switch (status)
{
case DeleteStatus
case DeleteStatus
return true;
case DeleteStatus
default:
return false;
}
}
public void Dispose()
{
if (!iscomit)
Commit();
db
db
}
private void Reset()
{
iscomit = false;
}
private void Init()
{
env = new Env(EnvCreateFlags
Env
Env
Env
Env
Env
Env
Env
env
txn = env
db = env
btree = (DbBTree)db
}
private void StreamInit()
{
formatter = new BinaryFormatter();
keyStream = new MemoryStream();
dataStream = new MemoryStream();
}
private bool GetNextRecord(DbBTreeCursor cursor
{
ReadStatus status;
keyStream
dataStream
DbEntry key = DbEntry
DbEntry data = DbEntry
do
{
status = cursor
switch (status)
{
case ReadStatus
case ReadStatus
case ReadStatus
if (key
{
keyStream
key = DbEntry
}
if (data
{
dataStream
data = DbEntry
}
continue;
case ReadStatus
dataStream
dataStream
cust = (IPut)formatter
return true;
default:
return false;
}
} while (true);
}
}
調用方法
首先要有一個寫入的實體類
[Serializable()]
class Item : IPut
{
public string Name { get; set; }
public string Text { get; set; }
public int ID { get; set; }
public override string ToString()
{
return string
}
public string Key
{
get { return Name; }
}
}
操作
using (BDBManager manager = new BDBManager(
{
bool success = manager
Console
}
using (BDBManager manager = new BDBManager(
{
IPut put = new Item() { Name =
bool success = manager
Console
}
using (BDBManager manager = new BDBManager(
{
IPut put = new Item() { Name =
bool success = manager
Console
}
using (BDBManager manager = new BDBManager(
{
List<IPut> list = manager
foreach (var item in list)
{
Console
}
}
Console
Console
From:http://tw.wingwit.com/Article/program/net/201311/12343.html