mlzboy 发表于 2013-1-13 18:29:55

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

<div class="cnblogs_code">http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gifhttp://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gifCode
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->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("finishedhttp://www.cnblogs.com/Images/dot.gif");
            }
            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);
        }
    }
}
页: [1]
查看完整版本: Berkeley Db Associate关联数据函数的使用