Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Help: Read Berkeley Key,Value using C#

29 views
Skip to first unread message

ggkr...@gmail.com

unread,
Mar 4, 2013, 1:03:54 PM3/4/13
to
I'm new to C#.
I have installed Berkeley 5.3.21 using the distribution msi.

Can someone please tell me how to correct my C# program to decode a recNo Berkeley database. I can do it using C++; but I'm having problems understanding how to retrieve/decode the key,value pairs using C#.

TIA
Garry

-------------------- Code -------------------------
//
//define Btree false
//

// Berkeley database is a recNo database with key,value pair defined as:
// key - unsigned int (1-4095)
// value - structure _bdbcomponentrecord (same as class defined below)
//


using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using BerkeleyDB;
using System.Runtime.InteropServices;

namespace AccessingBerkeley
{

//
// Replaces struct _bdbcomponentrecord
//
public class _bdbcomponentrecord
{
public int maintenanceaction;
public int componentstate;
public uint componentcode;
public int componenttype;
public string componentname = new string(new char[51]);
public byte rsvd; // Alignment
public int enginetype;

// public static string CopyFrom(DatabaseEntry dbt)
// {
// System.Text.ASCIIEncoding decode = new ASCIIEncoding();
// return decode.GetString(dbt.Data);
// }
}

class Program
{
static void Main(string[] args)
{
#if Btree
BTreeDatabase btreeDB;
BTreeDatabaseConfig btreeConfig;
#else
RecnoDatabase recnoDB;
RecnoDatabaseConfig recnoConfig;
#endif

Cursor dbc;
DatabaseEntry key;
DatabaseEntry data;
string buff, dbFileName, keyString, keyStr;
int keyVal;
int compNo;
int recNo;

_bdbcomponentrecord rec = new _bdbcomponentrecord();

try
{
String pwd = Environment.CurrentDirectory;
pwd = Path.Combine(pwd, "..");
pwd = Path.Combine(pwd, "..");
if (IntPtr.Size == 4)
pwd = Path.Combine(pwd, "Win32");
else
pwd = Path.Combine(pwd, "x64");
#if DEBUG
pwd = Path.Combine(pwd, "Debug");
#else
pwd = Path.Combine(pwd, "Release");
#endif
pwd += ";" + Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", pwd);
}
catch (Exception e)
{
Console.WriteLine(
"Unable to set the PATH environment variable.");
Console.WriteLine(e.Message);
return;
}

try
{
#if Btree
dbFileName = @"C:\BDB\componen.bdb";
#else
dbFileName = @"C:\BDB\componen.bdb";
#endif
if (args.Length > 0)
{
dbFileName = args[0];
}
}
catch
{
usage();
return;
}

#if false
if (File.Exists(dbFileName))
{
while (true)
{
Console.Write
("{0} already exists. Delete it? (y/n) ", dbFileName);
buff = Console.ReadLine().ToLower();
if (buff == "y" || buff == "n")
break;
}

if (buff == "y")
{
try
{
File.Delete(dbFileName);
}
catch
{
Console.WriteLine("Unable to delete {0}.", dbFileName);
return;
}
}
}

#endif
#if Btree
btreeConfig = new BTreeDatabaseConfig();
btreeConfig.Duplicates = DuplicatesPolicy.SORTED;
btreeConfig.ErrorPrefix = "excs_access";
btreeConfig.Creation = CreatePolicy.IF_NEEDED;
btreeConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1);
btreeConfig.PageSize = 8 * 1024;
#else
recnoConfig = new RecnoDatabaseConfig();
recnoConfig.ErrorPrefix = "excs_access";
recnoConfig.Creation = CreatePolicy.IF_NEEDED;
recnoConfig.CacheSize = new CacheInfo(0, 32 * 1024, 1);
recnoConfig.PageSize = 1024;
#endif

try
{
#if Btree
btreeDB = BTreeDatabase.Open(dbFileName, btreeConfig);
#else
recnoDB = RecnoDatabase.Open(dbFileName, recnoConfig);
#endif
}
catch (Exception e)
{
Console.WriteLine("Error opening {0}.", dbFileName);
Console.WriteLine(e.Message);
Console.Write("\nPress any key to exit >");
Console.ReadKey(true);
return;
}

key = new DatabaseEntry();
data = new DatabaseEntry();

#if false
while (true)
{
Console.Write("key [blank line to quit] > ");
keyString = Console.ReadLine();
if (keyString == "")
break;

keyVal = Convert.ToInt32(keyString);
keyStr = Convert.ToString(keyVal);

dbtFromString(key, keyStr);
dbtFromString(data, reverse(keyString));

try
{
#if Btree
btreeDB.Put(key, data);
#else
recnoDB.PutNoOverwrite(key, data);
#endif
}
catch
{
return;
}

}

#endif

#if Btree
using (dbc = btreeDB.Cursor())
{

Console.WriteLine("All key : data pairs:");
foreach (KeyValuePair<DatabaseEntry, DatabaseEntry> p in dbc) {
Console.WriteLine("{0}::{1}",
strFromDBT(p.Key), strFromDBT(p.Value));
}
}
}
#else
using (dbc = recnoDB.Cursor())
{

Console.WriteLine("All key : data pairs:");
foreach (KeyValuePair<DatabaseEntry, DatabaseEntry> p in dbc)
{
//bdbcomponentrecvar.CopyFrom(p.Value);
compNo = Convert.ToInt32(p.Key);
Console.WriteLine("{0}", compNo);
}
}
#endif
Console.Write("Press any key to exit >");
Console.ReadKey(true);
#if Btree
btreeDB.Close();
#else
recnoDB.Close();
#endif
}


#region Utilities
public static void usage()
{
Console.WriteLine(
"Usage: excs_access [database]");
}

static void dbtFromString(DatabaseEntry dbt, string s)
{
dbt.Data = System.Text.Encoding.ASCII.GetBytes(s);
}

public static string strFromDBT(DatabaseEntry dbt)
{

System.Text.ASCIIEncoding decode =
new ASCIIEncoding();
return decode.GetString(dbt.Data);
}

public static string reverse(string s)
{
StringBuilder tmp = new StringBuilder(s.Length);
for (int i = s.Length - 1; i >= 0; i--)
tmp.Append(s[i]);
return tmp.ToString();
}
#endregion Utilities

}
}
-------------------- End Code -------------------------
0 new messages