Reputation: 351
Consider a sample application where we need to store Customer name as primary key and list of his transactions as value in Berkeley db. Do berkeley support storing list of values against a primary key? If so, how to approach designing the entity?
Upvotes: 0
Views: 406
Reputation: 136
The most common approach would be to save the records as a data storage type, such as XML (or JSON, if you prefer that sort of thing). Here's an example in C# that I came up with that stores the records as JSON objects in the database:
static void Main(string[] args)
{
var db = BTreeDatabase.Open("customers.db", new BTreeDatabaseConfig {Creation = CreatePolicy.ALWAYS});
for (int i = 0; i < 10000; i++)
{
var customer = new Customer
{
customerName = nameGenerator.GenerateRandomFirstAndLastName()
};
var list = new List<Transaction>();
for (int j = 0; j < 10; j++)
{
list.Add(new Transaction
{txAmount = random.NextDouble(), txDate = DateTime.Now, txId = randomString()});
}
customer.transactions = list;
db.Put(new DatabaseEntry(Encoding.Default.GetBytes(customer.customerName)),
new DatabaseEntry(Encoding.Default.GetBytes(JsonConvert.SerializeObject(customer))));
}
WriteLine("Completed!");
ReadLine();
}
Granted, this isn't the most efficient way, since I store the customer name twice, but it should be a good starting point.
Upvotes: 0