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

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

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

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

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

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

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

 49        public BDBRecord FindOne(Func<objectstringbool> predicate)
 50ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 51            //Dictionary<string, object> dict = new Dictionary<string, object>();
 52            try
 53ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 54ContractedSubBlock.gifExpandedSubBlockStart.gif                Queue格式#region Queue格式
 55                //if (this.DBType == DBStoreType.Queue)
 56                //{
 57                using (Db db = new Db(DbCreateFlags.None))
 58ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 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))
 63ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
 64                        foreach (KeyDataPair kvp in cursor)
 65ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
 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)
 73ExpandedSubBlockStart.gifContractedSubBlock.gif                            {
 74ExpandedSubBlockStart.gifContractedSubBlock.gif                                return new BDBRecord() { Key = v, Value = k };
 75                            }

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

 80                        }

 81                    }

 82                }

 83                //}
 84                #endregion

 85            }

 86            catch (Exception ex)
 87ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 88ContractedSubBlock.gifExpandedSubBlockStart.gif                Hash格式#region Hash格式
 89                //else if(this.DBType==DBStoreType.Hash)
 90                //{
 91                //遍历数据
 92 &nbs
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值