// Collection of code snippets by Arne Vajhøj // (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) using System; using System.Collections.Generic; using System.IO; using System.Text; using BerkeleyDB; namespace BDB { public class Data { public int Iv { get; set; } public double Xv { get; set; } public string Sv { get; set; } public override string ToString() { return string.Format("(iv: {0}, xv: {1}, sv: {2})", Iv, Xv, Sv); } } public class Program { private static DatabaseEntry Prep(string key) { return new DatabaseEntry(Encoding.UTF8.GetBytes(key)); } private static DatabaseEntry Serialize(Data d) { MemoryStream ms = new MemoryStream(); BinaryWriter bw = new BinaryWriter(ms); bw.Write(d.Iv); bw.Write(d.Xv); bw.Write((short)d.Sv.Length); bw.Write(Encoding.UTF8.GetBytes(d.Sv)); bw.Flush(); return new DatabaseEntry(ms.ToArray()); } private static Data Deserialize(DatabaseEntry de) { MemoryStream ms = new MemoryStream(de.Data); BinaryReader br = new BinaryReader(ms); Data d = new Data(); d.Iv = br.ReadInt32(); d.Xv = br.ReadDouble(); int len = br.ReadInt16(); byte[] temp = new byte[len]; br.Read(temp, 0, temp.Length); d.Sv = Encoding.UTF8.GetString(temp); return d; } private static void Dump(Database db, string key) { Console.WriteLine("Key={0}", key); if(db.Exists(Prep(key))) { Data value = Deserialize(db.Get(Prep(key)).Value); Console.WriteLine(value); } else { Console.WriteLine("Not found"); } } private const int NREC = 1000; private const int DN_OFFSET = 3 * NREC; public static void Main(string[] args) { using(Database db = BTreeDatabase.Open("/work/BDB/test.db", new BTreeDatabaseConfig())) { // put data for (int i = 0; i < NREC; i++) { string putkey = "Key#" + (DN_OFFSET + i + 1); Data value = new Data { Iv = i + 1, Xv = i + 1.0, Sv = string.Format("This is value {0}", i + 1) }; db.Put(Prep(putkey), Serialize(value)); } string key; key = "Key#" + (DN_OFFSET + 77); // get Dump(db, key); // delete db.Delete(Prep(key)); // get non existing Dump(db, key); // key = "Key#" + (DN_OFFSET + 88); // update and get Dump(db, key); Data updvalue = (Data)Deserialize(db.Get(Prep(key)).Value); updvalue.Iv = updvalue.Iv + 1; updvalue.Xv = updvalue.Xv + 0.1; updvalue.Sv = updvalue.Sv + " updated"; db.Put(Prep(key), Serialize(updvalue)); Dump(db, key); // list all using (Cursor it = db.Cursor()) { int n = 0; bool more = it.MoveFirst(); while (more) { string itkey = Encoding.UTF8.GetString(it.Current.Key.Data); if(!itkey.StartsWith("Key#")) { Console.WriteLine("Unexpected key: " + itkey); } Data itvalue = Deserialize(it.Current.Value); if (itvalue.Iv < 1 || NREC < itvalue.Iv) { Console.WriteLine("Unexpected value :" + itvalue); } n++; more = it.MoveNext(); } Console.WriteLine(n); } // list all - alternative using (Cursor it1 = db.Cursor()) { int n1 = 0; foreach(KeyValuePair kvp in it1) { String it1key = Encoding.UTF8.GetString(kvp.Key.Data); if(!it1key.StartsWith("Key#")) { Console.WriteLine("Unexpected key: " + it1key); } Data it1value = Deserialize(kvp.Value); if (it1value.Iv < 1 || NREC < it1value.Iv) { Console.WriteLine("Unexpected value :" + it1value); } n1++; } Console.WriteLine(n1); } // list keys where "Key#n075" <= key < "Key#n085" using (Cursor it2 = db.Cursor()) { int n2 = 0; bool more = it2.Move(Prep("Key#" + (DN_OFFSET + 75)), true); while (more) { string it2key = Encoding.UTF8.GetString(it2.Current.Key.Data); if(string.Compare(it2key, "Key#" + (DN_OFFSET + 85)) >= 0) break; n2++; more = it2.MoveNext(); } Console.WriteLine(n2); } } } } }