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

<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->   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                using (Db db = new Db(DbCreateFlags.None))
 93ExpandedSubBlockStart.gifContractedSubBlock.gif                {
 94                    //这里如果应用Db.OpenFlags.Create则在启动后会覆盖同名文件,并新建同名文件
 95                    //Db.OpenFlags.Truncate会清空数据库
 96                    DbHash dbf = (DbHash)db.Open(nullthis.DBFilePath, null, DbType.Hash,
 97        Db.OpenFlags.ThreadSafe, 0);
 98
 99                    using (DbHashCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
100ExpandedSubBlockStart.gifContractedSubBlock.gif                    {
101                        foreach (KeyDataPair kvp in cursor)
102ExpandedSubBlockStart.gifContractedSubBlock.gif                        {
103                            BinaryFormatter bf = new BinaryFormatter();
104                            MemoryStream stream = new MemoryStream();
105                            stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
106                            stream.Seek(0, SeekOrigin.Begin);
107                            string k = Encoding.UTF8.GetString(kvp.Key.Buffer, 0, kvp.Key.Size);
108                            object v = bf.Deserialize(stream);
109                            if (predicate == null)
110ExpandedSubBlockStart.gifContractedSubBlock.gif                            {
111ExpandedSubBlockStart.gifContractedSubBlock.gif                                return new BDBRecord() { Key = v, Value = k };
112                            }

113                            else if (predicate(v, k))
114ExpandedSubBlockStart.gifContractedSubBlock.gif                            {
115ExpandedSubBlockStart.gifContractedSubBlock.gif                                return new BDBRecord() { Key = v, Value = k };
116                            }

117                        }

118                    }

119                }

120                #endregion

121                //}
122            }

123            //return dict;
124            return null;
125        }

126        public Dictionary<objectstring> FindAll(Func<objectstringbool> predicate)
127ExpandedSubBlockStart.gifContractedSubBlock.gif        {
128
129            Dictionary<objectstring> dict = new Dictionary<objectstring>();
130            try
131ExpandedSubBlockStart.gifContractedSubBlock.gif            {
132ContractedSubBlock.gifExpandedSubBlockStart.gif                Queue格式#region Queue格式
133                //if (this.DBType == DBStoreType.Queue)
134                //{
135                using (Db db = new Db(DbCreateFlags.None))
136ExpandedSubBlockStart.gifContractedSubBlock.gif                {
137                    db.RecLen = 5000;
138                    db.RecPad = '.';
139                    DbQueue file = (DbQueue)db.Open(nullthis.DBFilePath, null, DbType.Queue, Db.OpenFlags.Create, 0);
140
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值