`
mlzboy
  • 浏览: 701733 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

目前来说比较.net下最好的bdb操作封装(附单元测试)

阅读更多
<!---->  1using System;
  2using System.Collections.Generic;
  3using System.IO;
  4using System.Linq;
  5using System.Runtime.Serialization.Formatters.Binary;
  6using System.Text;
  7using BerkeleyDb;
  8using Component;
  9
 10namespace ToolManager
 11{
 12    public class BDBRecord
 13    {
 14        public object Key getset; }
 15        public string Value getset; }
 16    }
    /**//// <summary>
 17    /// BDB数据库操作类库
 18    /// </summary>

 19    public class BDBHelper
 20    {
 21
 22        private string DBFilePath getset; }
 23        private DBStoreType DBType getset; }
 24        public enum DBStoreType : byte
 25        {
 26            Auto=1,
 27            Queue,
 28            Hash
 29        }

 30        [Obsolete("该构造函数已废弃 ,请使用BDBHelper(string dbfilePath)")]
 31        public BDBHelper()
 32        {
 33        }

 34
 35        public BDBHelper(string dbfilePath)
 36        {
 37            this.DBFilePath = dbfilePath;
 38        }

 39        [Obsolete("该构造函数已废弃 ,请使用BDBHelper(string dbfilePath)")]
 40        public BDBHelper(string dbfilePath, DBStoreType type)
 41        {
 42            this.DBFilePath = dbfilePath;
 43            this.DBType = type;
 44        }

 45        public BDBRecord FindOne()
 46        {
 47            return this.FindOne(null);
 48        }

 49        public BDBRecord FindOne(Func<objectstringbool> predicate)
 50        {
 51            //Dictionary<string, object> dict = new Dictionary<string, object>();
 52            try
 53            {
 54                Queue格式#region Queue格式
 55                //if (this.DBType == DBStoreType.Queue)
 56                //{
 57                using (Db db = new Db(DbCreateFlags.None))
 58                {
 59                    db.RecLen = 5000;
 60                    db.RecPad = '.';
 61                    DbQueue file = (DbQueue)db.Open(nullthis.DBFilePath, null, DbType.Queue, Db.OpenFlags.Create, 0);
 62                    using (DbQueueCursor cursor = file.OpenCursor(null, DbFileCursor.CreateFlags.None))
 63                    {
 64                        foreach (KeyDataPair kvp in cursor)
 65                        {
 66                            BinaryFormatter bf = new BinaryFormatter();
 67                            MemoryStream stream = new MemoryStream();
 68                            stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
 69                            stream.Seek(0, SeekOrigin.Begin);
 70                            string k = BitConverter.ToInt32(kvp.Key.Buffer, 0).ToString();
 71                            object v = bf.Deserialize(stream);
 72                            if (predicate == null)
 73                            {
 74                                return new BDBRecord() { Key = v, Value = k };
 75                            }

 76                            else if (predicate(v, k))
 77                            {
 78                                return new BDBRecord() { Key = v, Value = k };
 79                            }

 80                        }

 81                    }

 82                }

 83                //}
 84                #endregion

 85            }

 86            catch (Exception ex)
 87            {
 88                Hash格式#region Hash格式
 89                //else if(this.DBType==DBStoreType.Hash)
 90                //{
 91                //遍历数据
 92 &nbs
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics