我写了一个小程序来探索使用Berkeley DB和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();
}
}
}
它创建一个用作表的数据库和一些辅助数据库作为该表的索引。问题是崩溃时出现以下错误:
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
在将一定数量的记录写入表后,它总是崩溃。此外,当我增加nIndexes的值时,崩溃前写入的记录数量会减少。如果值足够低(例如nIndexes = 5,nRecords = 50),程序将完成而不会出现问题。这让我觉得我正在耗尽一个有限的资源,但我只是看不出问题所在。