views:

301

answers:

1

I wrote a small program to explore using Berkeley DB with C#.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

using BerkeleyDB;

namespace bdbtest {

    class Program {

        const string dbfile = @"c:\tmp\test.db";
        const int nIndexes = 5, nRecords = 500;

        static SecondaryKeyGenDelegate MakeKeyGen(int idx) {
            return (k, v) => new DatabaseEntry(Encoding.UTF8.GetBytes(Encoding.UTF8.GetString(v.Data) + string.Format("_{0}", idx)));
        }

        static void Main(string[] args) {

            File.Delete(dbfile);

            var environment = DatabaseEnvironment.Open(null,
                new DatabaseEnvironmentConfig() {
                    Create = true,
                    Private = true,
                    UseMPool = true,
                    ErrorPrefix = "Env: ",
                    ErrorFeedback = (pfx, msg) => Console.WriteLine(pfx + msg),
                });

            var table = RecnoDatabase.Open(dbfile, "table",
                new RecnoDatabaseConfig() {
                    Env = environment,
                    Creation = CreatePolicy.IF_NEEDED,
                    ErrorPrefix = "Table: ",
                    ErrorFeedback = (pfx, msg) => Console.WriteLine(pfx + msg),
                });

            var indexes = new List<SecondaryBTreeDatabase>();
            for (int idx = 0; idx < nIndexes; ++idx)
                indexes.Add(SecondaryBTreeDatabase.Open(dbfile, string.Format("index_{0}", idx),
                    new SecondaryBTreeDatabaseConfig(table, MakeKeyGen(idx)) {
                        Env = environment,
                        Creation = CreatePolicy.IF_NEEDED,
                        Compare = (k1, k2) => string.Compare(Encoding.UTF8.GetString(k1.Data), Encoding.UTF8.GetString(k2.Data)),
                        ErrorPrefix = "Index: ",
                        ErrorFeedback = (pfx, msg) => Console.WriteLine(pfx + msg),
                    }));

            for (int rn = 0; rn < nRecords; ++rn) {
                string s = string.Format("rec{0}", rn);
                table.Append(new DatabaseEntry(Encoding.UTF8.GetBytes(s)));
                Console.WriteLine(s);
            }

            foreach (var index in indexes)
                index.Close();
            indexes.Clear();
            table.Close();
            environment.Close();
        }
    }
}

It creates a database for use as table and a number of secondary databases as indexes for that table. The problem is that is crashes with the following error:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at BerkeleyDB.Internal.libdb_csharpPINVOKE.DB_put(HandleRef jarg1, HandleRef jarg2, HandleRef jarg3, HandleRef jarg4, UInt32 jarg5)
   at BerkeleyDB.Internal.DB.put(DB_TXN txn, DatabaseEntry key, DatabaseEntry data, UInt32 flags)
   at BerkeleyDB.Database.Put(DatabaseEntry key, DatabaseEntry data, Transaction txn, UInt32 flags)
   at BerkeleyDB.RecnoDatabase.Append(DatabaseEntry data, Transaction txn)
   at BerkeleyDB.RecnoDatabase.Append(DatabaseEntry data)
   at bdbtest.Program.Main(String[] args) in C:\Users\fgb\Documents\Visual Studio 2008\Projects\bdbtest\bdbtest\Program.cs:line 53

It always crashes after a certain number of records are written to table. Furthermore, as I increase the value of nIndexes, the number of records written before it crashes goes down. If the values are low enough (such as nIndexes = 5, nRecords = 50), the program completes without issues. This makes me think that I'm using up a finite resource, but I just can't see where the problem is.

A: 

Make sure C# code is targeting .NET CLR 3.5 .. Ive got these kind of AVL's with .NET 4.0 and berkeley DB.

Nirmal