Berkeley Db Associate关联数据函数的使用

ContractedBlock.gif ExpandedBlockStart.gif 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(
nullref key, ref data);
        }
    }
}

 

由于bdb无法使用sql,因此关联数据库的运用将使你减轻负担,要理解关联数据库可以将它理解成关系数据库中一对多关系,一端指primary db,多端指secondary db,具体可以参看其doc

posted on 2008-07-31 21:42  lexus 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/lexus/archive/2008/07/31/1257644.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值