Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using BerkeleyDb;
namespace BerkeleyDBDemo
{
public class Class4
{
static void Main(string[] args)
{
using (Db db = new Db(DbCreateFlags.None))
{
db.RecLen = 5000;
db.RecPad = '.';
DbQueue file = (DbQueue)db.Open(null, @"task.aaa", null, DbType.Queue, Db.OpenFlags.Create, 0);
using(Db db2=new Db(DbCreateFlags.None))
{
db2.SetFlags(DbFlags.Dup|DbFlags.DupSort);
DbFile secondfile = db2.Open(null, @"task.bbb", null, DbType.BTree, Db.OpenFlags.Create, 0);
file.Associate(secondfile,
delegate(DbFile _secondary, ref DbEntry _key, ref DbEntry _data, out DbEntry _k)
{
//_secondary二级库
//_key一级库
//_data一级库
//_result被创造的二级库key值
MemoryStream _stream = new MemoryStream();
BinaryFormatter _bf=new BinaryFormatter();
_stream.Write(_data.Buffer, 0, _data.Size);
_stream.Seek(0, SeekOrigin.Begin);
Task task2 = (Task)_bf.Deserialize(_stream);
_stream.Close();
_k = DbEntry.InOut(Encoding.UTF8.GetBytes(task2.Author));
//_secondary.Put(null, ref _k, ref _key);
//_secondary.Sync();
return DbFile.KeyGenStatus.Success;
}, DbFile.AssociateFlags.Create);
////创建一个任务的一条数据
Task task = new Task()
{
Id = Guid.NewGuid().ToString(),
Author = "lexus",
CreatedDate = DateTime.Now
};
AddData(file, task);
AddData(file, task);
AddData(file, task);
task.Author = "bbb";
AddData(file, task);
task.Author = "ccc";
AddData(file, task);
file.Sync();
}
Console.WriteLine("finished");
}
Console.ReadLine();
}
private static void AddData(DbQueue file, Task task)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bf.Serialize(stream, task);
DbEntry key = DbEntry.Out(new byte[1024]);
DbEntry data = DbEntry.InOut(stream.ToArray());
stream.Close();
file.Append(null, ref key, ref data);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using BerkeleyDb;
namespace BerkeleyDBDemo
{
public class Class4
{
static void Main(string[] args)
{
using (Db db = new Db(DbCreateFlags.None))
{
db.RecLen = 5000;
db.RecPad = '.';
DbQueue file = (DbQueue)db.Open(null, @"task.aaa", null, DbType.Queue, Db.OpenFlags.Create, 0);
using(Db db2=new Db(DbCreateFlags.None))
{
db2.SetFlags(DbFlags.Dup|DbFlags.DupSort);
DbFile secondfile = db2.Open(null, @"task.bbb", null, DbType.BTree, Db.OpenFlags.Create, 0);
file.Associate(secondfile,
delegate(DbFile _secondary, ref DbEntry _key, ref DbEntry _data, out DbEntry _k)
{
//_secondary二级库
//_key一级库
//_data一级库
//_result被创造的二级库key值
MemoryStream _stream = new MemoryStream();
BinaryFormatter _bf=new BinaryFormatter();
_stream.Write(_data.Buffer, 0, _data.Size);
_stream.Seek(0, SeekOrigin.Begin);
Task task2 = (Task)_bf.Deserialize(_stream);
_stream.Close();
_k = DbEntry.InOut(Encoding.UTF8.GetBytes(task2.Author));
//_secondary.Put(null, ref _k, ref _key);
//_secondary.Sync();
return DbFile.KeyGenStatus.Success;
}, DbFile.AssociateFlags.Create);
////创建一个任务的一条数据
Task task = new Task()
{
Id = Guid.NewGuid().ToString(),
Author = "lexus",
CreatedDate = DateTime.Now
};
AddData(file, task);
AddData(file, task);
AddData(file, task);
task.Author = "bbb";
AddData(file, task);
task.Author = "ccc";
AddData(file, task);
file.Sync();
}
Console.WriteLine("finished");
}
Console.ReadLine();
}
private static void AddData(DbQueue file, Task task)
{
BinaryFormatter bf = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
bf.Serialize(stream, task);
DbEntry key = DbEntry.Out(new byte[1024]);
DbEntry data = DbEntry.InOut(stream.ToArray());
stream.Close();
file.Append(null, ref key, ref data);
}
}
}
由于bdb无法使用sql,因此关联数据库的运用将使你减轻负担,要理解关联数据库可以将它理解成关系数据库中一对多关系,一端指primary db,多端指secondary db,具体可以参看其doc