.net下BerkeleyDB操作封裝C#版(附單元測試)

MaxIE發表於2014-05-23
 
 

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.Serialization.Formatters.Binary;

using System.Text;

using BerkeleyDb;

using Component;

namespace ToolManager
{
   public class BDBRecord
   {
       public object Key { get; set; }
       public string Value { get; set; }
   }    /**//// <summary>
   /// BDB資料庫操作類庫
   /// </summary>
   public class BDBHelper
   {

       private string DBFilePath { get; set; }
       private DBStoreType DBType { get; set; }
       public enum DBStoreType : byte
       {
           Auto=1,
           Queue,
           Hash
       }
       [Obsolete("該建構函式已廢棄 ,請使用BDBHelper(string dbfilePath)")]
       public BDBHelper()
       {
       }

       public BDBHelper(string dbfilePath)
       {
           this.DBFilePath = dbfilePath;
       }
       [Obsolete("該建構函式已廢棄 ,請使用BDBHelper(string dbfilePath)")]
       public BDBHelper(string dbfilePath, DBStoreType type)
       {
           this.DBFilePath = dbfilePath;
           this.DBType = type;
       }
       public BDBRecord FindOne()
       {
           return this.FindOne(null);
       }
       public BDBRecord FindOne(Func<object, string, bool> predicate)
       {
           //Dictionary<string, object> dict = new Dictionary<string, object>();
           try
           {
               Queue格式#region Queue格式
               //if (this.DBType == DBStoreType.Queue)
               //{
               using (Db db = new Db(DbCreateFlags.None))
               {
                   db.RecLen = 5000;
                   db.RecPad = '.';
                   DbQueue file = (DbQueue)db.Open(null, this.DBFilePath, null, DbType.Queue, Db.OpenFlags.Create, 0);
                   using (DbQueueCursor cursor = file.OpenCursor(null, DbFileCursor.CreateFlags.None))
                   {
                       foreach (KeyDataPair kvp in cursor)
                       {
                           BinaryFormatter bf = new BinaryFormatter();
                           MemoryStream stream = new MemoryStream();
                           stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
                           stream.Seek(0, SeekOrigin.Begin);
                           string k = BitConverter.ToInt32(kvp.Key.Buffer, 0).ToString();
                           object v = bf.Deserialize(stream);
                           if (predicate == null)
                           {
                               return new BDBRecord() { Key = v, Value = k };
                           }
                           else if (predicate(v, k))
                           {
                               return new BDBRecord() { Key = v, Value = k };
                           }
                       }
                   }
               }
               //}
               #endregion
           }
           catch (Exception ex)
           {
               Hash格式#region Hash格式
               //else if(this.DBType==DBStoreType.Hash)
               //{
               //遍歷資料
               using (Db db = new Db(DbCreateFlags.None))
               {
                   //這裡如果應用Db.OpenFlags.Create則在啟動後會覆蓋同名檔案,並新建同名檔案
                   //Db.OpenFlags.Truncate會清空資料庫
                   DbHash dbf = (DbHash)db.Open(null, this.DBFilePath, null, DbType.Hash,
       Db.OpenFlags.ThreadSafe, 0);

                   using (DbHashCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
                   {
                       foreach (KeyDataPair kvp in cursor)
                       {
                           BinaryFormatter bf = new BinaryFormatter();
                           MemoryStream stream = new MemoryStream();
                           stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
                           stream.Seek(0, SeekOrigin.Begin);
                           string k = Encoding.UTF8.GetString(kvp.Key.Buffer, 0, kvp.Key.Size);
                           object v = bf.Deserialize(stream);
                           if (predicate == null)
                           {
                               return new BDBRecord() { Key = v, Value = k };
                           }
                           else if (predicate(v, k))
                           {
                               return new BDBRecord() { Key = v, Value = k };
                           }
                       }
                   }
               }
               #endregion
               //}
           }
           //return dict;
           return null;
       }
       public Dictionary<object, string> FindAll(Func<object, string, bool> predicate)
       {

           Dictionary<object, string> dict = new Dictionary<object, string>();
           try
           {
               Queue格式#region Queue格式
               //if (this.DBType == DBStoreType.Queue)
               //{
               using (Db db = new Db(DbCreateFlags.None))
               {
                   db.RecLen = 5000;
                   db.RecPad = '.';
                   DbQueue file = (DbQueue)db.Open(null, this.DBFilePath, null, DbType.Queue, Db.OpenFlags.Create, 0);
                   using (DbQueueCursor cursor = file.OpenCursor(null, DbFileCursor.CreateFlags.None))
                   {
                       foreach (KeyDataPair kvp in cursor)
                       {
                           _Do2(kvp, predicate, dict);
                       }
                   }
               }
               //}
               #endregion
           }
           catch (Exception ex)
           {
               Hash格式#region Hash格式
               //else if(this.DBType==DBStoreType.Hash)
               //{
               //遍歷資料
               using (Db db = new Db(DbCreateFlags.None))
               {
                   //這裡如果應用Db.OpenFlags.Create則在啟動後會覆蓋同名檔案,並新建同名檔案
                   //Db.OpenFlags.Truncate會清空資料庫
                   DbHash dbf = (DbHash)db.Open(null, this.DBFilePath, null, DbType.Hash,
       Db.OpenFlags.ThreadSafe, 0);

                   using (DbHashCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
                   {
                       foreach (KeyDataPair kdp in cursor)
                       {
                           _Do(kdp, predicate, dict);
                       }
                   }
               }
               #endregion
               //}
           }
           return dict;
       }
       public Dictionary<object, string> FindAll()
       {
           //either below works fine
           //return this.FindAll((s, o) => true);
           return this.FindAll(null);
       }
       private static void _Do(KeyDataPair kvp, Func<object, string, bool> predicate, Dictionary<object, string> result)
       {
           BinaryFormatter bf = new BinaryFormatter();
           MemoryStream stream = new MemoryStream();
           stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
           stream.Seek(0, SeekOrigin.Begin);
           string k = Encoding.UTF8.GetString(kvp.Key.Buffer, 0, kvp.Key.Size);
           object v = bf.Deserialize(stream);
           if (predicate == null)
           {
               result.Add(v, k);
           }
           else if (predicate(v, k))
           {
               result.Add(v, k);
           }
       }
       private static void _Do2(KeyDataPair kvp, Func<object, string, bool> predicate, Dictionary<object, string> result)
       {
           BinaryFormatter bf = new BinaryFormatter();
           MemoryStream stream = new MemoryStream();
           stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
           stream.Seek(0, SeekOrigin.Begin);
           string k = BitConverter.ToInt32(kvp.Key.Buffer, 0).ToString();
           object v = bf.Deserialize(stream);
           if (predicate == null)
           {
               result.Add(v, k);
           }
           else if (predicate(v, k))
           {
               result.Add(v, k);
           }
       }
       /**//// <summary>
       /// 更新資料庫中的資料
       /// </summary>
       /// <param name="predicate">execute condition</param>
       /// <param name="isMatchOnlyOnce">is match only once</param>
       /// <returns>effect records</returns>
       public int UpdateInQueueMode(Func<int, object, bool> predicate, object value,bool isMatchOnlyOnce)
       {
                int count = 0;
                if (predicate == null)
                    return 0;
              //遍歷資料
               using (Db db = new Db(DbCreateFlags.None))
               {
                   db.RecLen = 5000;
                   db.RecPad = '.';
                   //這裡如果應用Db.OpenFlags.Create則在啟動後會覆蓋同名檔案,並新建同名檔案
                   //Db.OpenFlags.Truncate會清空資料庫
                   DbQueue dbf = (DbQueue)db.Open(null, this.DBFilePath, null, DbType.Queue,
       Db.OpenFlags.ThreadSafe|Db.OpenFlags.Create, 0);

                   using (DbQueueCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
                   {
                       BinaryFormatter bf = new BinaryFormatter();
                       MemoryStream stream = new MemoryStream();
                       foreach (KeyDataPair kdp in cursor)
                       {
                           int k = BitConverter.ToInt32(kdp.Key.Buffer, 0);
                           Console.WriteLine("k={0}", k.ToString());
                           stream.SetLength(0);
                           stream.Position = 0;
                           stream.Write(kdp.Data.Buffer, 0, kdp.Data.Size);
                           stream.Seek(0, SeekOrigin.Begin);
                           object v = bf.Deserialize(stream);
                           if(predicate(k,v))
                           {
                               count++;
                               //string d = Encoding.UTF8.GetString(kdp.Data.Buffer, 0, kdp.Data.Size);
                               //如何讀取Queue型別的主鍵值


                               //Console.WriteLine("{0},{1}", p2.State, p2.Os);
                               //p2.Os = "changed";
                               //stream = new MemoryStream();
                               stream.Position = 0;
                               stream.SetLength(0);
                               bf.Serialize(stream, value);
                               DbEntry data = DbEntry.InOut(stream.ToArray());
                               cursor.Put(ref data);
                               if (isMatchOnlyOnce)
                               {
                                   stream.Close();
                                   return count;
                               }
                           }
                       }
                           stream.Close();
                   }
           }
           return count;
       }

       public void CreateInQueueMode(object value)
       {
           Db PC = new Db(DbCreateFlags.None);
           PC.RecLen = 5000;
           PC.RecPad = '.';
           DbQueue file = (DbQueue)PC.Open(null, this.DBFilePath, null, DbType.Queue, Db.OpenFlags.Create|Db.OpenFlags.ThreadSafe, 0);
           //CreateSecondaryDB(file,"Id.PCs.s",new DbFile.KeyGeneratorFcn(Common.Id));
           //CreateSecondaryDB(file, "Id.PCs.s", new DbFile.KeyGeneratorFcn(Common.Id));
           //由於資料量不是很大,不考慮使用二級資料庫,直接使用遊標操作,降低複雜度
           //首先遍歷資料庫看有沒有已經存在,如果沒有,則新增一個,如果有,改變其狀態
           BinaryFormatter bf = new BinaryFormatter();
           MemoryStream stream= new MemoryStream();
           bf.Serialize(stream, value);
           DbEntry k = DbEntry.Out(new byte[1024]);
           DbEntry data = DbEntry.InOut(stream.ToArray());
           file.Append(null, ref k, ref data);
           stream.Close();
           file.Sync();
           PC.Close();
       }
       public int DeleteInQueueMode(Func<int, object, bool> predicate,bool isMatchOnlyOnce)
       {
           int count = 0;
           if (predicate == null)
               return 0;
           //遍歷資料
           using (Db db = new Db(DbCreateFlags.None))
           {
               db.RecLen = 5000;
               db.RecPad = '.';
               //這裡如果應用Db.OpenFlags.Create則在啟動後會覆蓋同名檔案,並新建同名檔案
               //Db.OpenFlags.Truncate會清空資料庫
               DbQueue dbf = (DbQueue)db.Open(null, this.DBFilePath, null, DbType.Queue,
   Db.OpenFlags.ThreadSafe|Db.OpenFlags.Create, 0);

               using (DbQueueCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
               {
                   BinaryFormatter bf = new BinaryFormatter();
                   MemoryStream stream = new MemoryStream();
                   foreach (KeyDataPair kdp in cursor)
                   {
                       int k = BitConverter.ToInt32(kdp.Key.Buffer, 0);
                       Console.WriteLine("k={0}", k.ToString());
                       stream.SetLength(0);
                       stream.Position = 0;
                       stream.Write(kdp.Data.Buffer, 0, kdp.Data.Size);
                       stream.Seek(0, SeekOrigin.Begin);
                       object v = bf.Deserialize(stream);
                       if (predicate(k, v))
                       {
                           count++;
                           //string d = Encoding.UTF8.GetString(kdp.Data.Buffer, 0, kdp.Data.Size);
                           //如何讀取Queue型別的主鍵值


                           //Console.WriteLine("{0},{1}", p2.State, p2.Os);
                           //p2.Os = "changed";
                           //stream = new MemoryStream();
                           //stream.Position = 0;
                           //stream.SetLength(0);
                           //bf.Serialize(stream, v);
                           //DbEntry data = DbEntry.InOut(stream.ToArray());
                           //cursor.Put(ref data);
                           cursor.Delete();
                           if (isMatchOnlyOnce)
                           {
                               stream.Close();
                               return count;
                           }
                       }
                   }stream.Close();
               }
           }
           return count;
       }

       /**//// <summary>
       /// 用於向支援重複鍵值的資料庫檔案新增資料
       /// </summary>
       /// <param name="key"></param>
       /// <param name="value"></param>
       public void CreateInHashModeWithDup(string key,object value)
       {
           //這裡只是更新了一條記錄,更新多條同key的情況沒有考慮
           Db db = new Db(DbCreateFlags.None);
           db.SetFlags(DbFlags.Dup);
           DbFile dbf = db.Open(null, this.DBFilePath, null, DbType.Hash, Db.OpenFlags.Create|Db.OpenFlags.ThreadSafe, 0);
           MemoryStream stream = new MemoryStream();
           BinaryFormatter formatter = new BinaryFormatter();
           formatter.Serialize(stream, value);
           DbEntry _key = DbEntry.InOut(Encoding.UTF8.GetBytes(key));
           DbEntry _data = DbEntry.InOut(stream.ToArray());
           if (dbf.Put(null, ref _key, ref _data) != 0)
               Console.Write("{0}:輸入錯誤", key);
           stream.Close();
           dbf.Sync();//資料更新
           db.Close();
       }
       /**//// <summary>
       /// 預設方式,如果已有鍵則進行更新操作
       /// </summary>
       /// <param name="key"></param>
       /// <param name="value"></param>
       public void CreateOrUpdateInHashModeWithoutDup(string key,object value)
       {
           //這裡只是更新了一條記錄,更新多條同key的情況沒有考慮
           Db db = new Db(DbCreateFlags.None);
           //db.SetFlags(DbFlags.Dup);
           DbFile dbf = db.Open(null, this.DBFilePath, null, DbType.Hash, Db.OpenFlags.Create|Db.OpenFlags.ThreadSafe, 0);
           MemoryStream stream = new MemoryStream();
           BinaryFormatter formatter = new BinaryFormatter();
           formatter.Serialize(stream, value);
           DbEntry _key = DbEntry.InOut(Encoding.UTF8.GetBytes(key));
           DbEntry _data = DbEntry.InOut(stream.ToArray());
           if (dbf.Put(null, ref _key, ref _data) != 0)
               Console.Write("{0}:輸入錯誤", key);
           stream.Close();
           dbf.Sync();//資料更新
           db.Close();
       }
       public int UpdateInHashMode(Func<string,object,bool> predicate,object value,bool isMatchOnlyOnce)
       {
           int count = 0;
           if (predicate == null)
               return count;
          //遍歷資料
           using (Db db = new Db(DbCreateFlags.None))
           {
               //這裡如果應用Db.OpenFlags.Create則在啟動後會覆蓋同名檔案,並新建同名檔案
               //Db.OpenFlags.Truncate會清空資料庫
               DbHash dbf = (DbHash)db.Open(null, this.DBFilePath, null, DbType.Hash,
   Db.OpenFlags.ThreadSafe|Db.OpenFlags.Create, 0);

               using (DbHashCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
               {
                   BinaryFormatter bf = new BinaryFormatter();
                   MemoryStream stream = new MemoryStream();
                  foreach (KeyDataPair kvp in cursor)
                   {
                       stream.SetLength(0);
                       stream.Position = 0;
                       stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
                       stream.Seek(0, SeekOrigin.Begin);
                       string k = Encoding.UTF8.GetString(kvp.Key.Buffer, 0, kvp.Key.Size);
                       object v = bf.Deserialize(stream);
                       if (predicate(k, v))
                       {
                           count++;
                           stream.SetLength(0);
                           stream.Position = 0;
                           bf.Serialize(stream, value);
                           DbEntry data = DbEntry.InOut(stream.ToArray());
                           cursor.Put(ref data, DbKeyCursor<DbHashCursor, DbHash>.PutMode.Current);
                           if (isMatchOnlyOnce)
                           {
                               stream.Close();
                               return count;
                           }
                       }
                   }
                   stream.Close();
               }
           }
           return count;
       }
       public int DeleteInHashMode(Func<string,object,bool> predicate,bool isMatchOnlyOnce)
       {
           int count = 0;
           if (predicate == null)
               return count;
           //遍歷資料
           using (Db db = new Db(DbCreateFlags.None))
           {
               //這裡如果應用Db.OpenFlags.Create則在啟動後會覆蓋同名檔案,並新建同名檔案
               //Db.OpenFlags.Truncate會清空資料庫
               DbHash dbf = (DbHash)db.Open(null, this.DBFilePath, null, DbType.Hash,
   Db.OpenFlags.ThreadSafe|Db.OpenFlags.Create, 0);

               using (DbHashCursor cursor = dbf.OpenCursor(null, DbFileCursor.CreateFlags.None))
               {
                   BinaryFormatter bf = new BinaryFormatter();
                   MemoryStream stream = new MemoryStream();
                   foreach (KeyDataPair kvp in cursor)
                   {
                       stream.SetLength(0);
                       stream.Position = 0;
                       stream.Write(kvp.Data.Buffer, 0, kvp.Data.Size);
                       stream.Seek(0, SeekOrigin.Begin);
                       string k = Encoding.UTF8.GetString(kvp.Key.Buffer, 0, kvp.Key.Size);
                       object v = bf.Deserialize(stream);
                       if (predicate(k, v))
                       {
                           count++;
                           cursor.Delete();
                           if (isMatchOnlyOnce)
                           {
                               stream.Close();
                               return count;
                           }
                       }
                   }
                   stream.Close();
               }
           }
           return count;
       }
   }
}

 

相關文章