<!--<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 {
12 public class BDBRecord
13 {
14 public object Key { get; set; }
15 public string Value { get; set; }
16 } /**//// <summary>
17 /// BDB数据库操作类库
18 /// </summary>
19 public class BDBHelper
20 {
21
22 private string DBFilePath { get; set; }
23 private DBStoreType DBType { get; set; }
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<object, string, bool> 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(null, this.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 using (Db db = new Db(DbCreateFlags.None))
93 {
94 //这里如果应用Db.OpenFlags.Create则在启动后会覆盖同名文件,并新建同名文件
95 //Db.OpenFlags.Truncate会清空数据库
96 DbHash dbf = (DbHash)db.Open(null, this.DBFilePath, null, DbType.Hash,
97 Db.OpenFlags.ThreadSafe, 0);
98
99 using (DbHashCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
100 {
101 foreach (KeyDataPair kvp in cursor)
102 {
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)
110 {
111 return new BDBRecord() { Key = v, Value = k };
112 }
113 else if (predicate(v, k))
114 {
115 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<object, string> FindAll(Func<object, string, bool> predicate)
127 {
128
129 Dictionary<object, string> dict = new Dictionary<object, string>();
130 try
131 {
132 Queue格式#region Queue格式
133 //if (this.DBType == DBStoreType.Queue)
134 //{
135 using (Db db = new Db(DbCreateFlags.None))
136 {
137 db.RecLen = 5000;
138 db.RecPad = '.';
139 DbQueue file = (DbQueue)db.Open(null, this.DBFilePath, null, DbType.Queue, Db.OpenFlags.Create, 0);
140
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 {
12 public class BDBRecord
13 {
14 public object Key { get; set; }
15 public string Value { get; set; }
16 } /**//// <summary>
17 /// BDB数据库操作类库
18 /// </summary>
19 public class BDBHelper
20 {
21
22 private string DBFilePath { get; set; }
23 private DBStoreType DBType { get; set; }
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<object, string, bool> 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(null, this.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 using (Db db = new Db(DbCreateFlags.None))
93 {
94 //这里如果应用Db.OpenFlags.Create则在启动后会覆盖同名文件,并新建同名文件
95 //Db.OpenFlags.Truncate会清空数据库
96 DbHash dbf = (DbHash)db.Open(null, this.DBFilePath, null, DbType.Hash,
97 Db.OpenFlags.ThreadSafe, 0);
98
99 using (DbHashCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
100 {
101 foreach (KeyDataPair kvp in cursor)
102 {
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)
110 {
111 return new BDBRecord() { Key = v, Value = k };
112 }
113 else if (predicate(v, k))
114 {
115 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<object, string> FindAll(Func<object, string, bool> predicate)
127 {
128
129 Dictionary<object, string> dict = new Dictionary<object, string>();
130 try
131 {
132 Queue格式#region Queue格式
133 //if (this.DBType == DBStoreType.Queue)
134 //{
135 using (Db db = new Db(DbCreateFlags.None))
136 {
137 db.RecLen = 5000;
138 db.RecPad = '.';
139 DbQueue file = (DbQueue)db.Open(null, this.DBFilePath, null, DbType.Queue, Db.OpenFlags.Create, 0);
140