dddd
dddd

Reputation: 11

Can I use multiple keys in Berkeley DB?

I want to use in Berkeley DB the following Perl logic (for many millions records):

$hash{key1}{key2}{key3}{count1}++;
$hash{key1}{key2}{key3}{count2}++;

...

for (key1) {
   for (key2) {
     for (key3) {
        print $hash{key1}{key2}{key3}{count1}."\t".$hash{key1}{key2}{key2}{count2};
        }
     }
  }

Any example from multiple keys? I may of couse use "pseudo-multiple" key ( key1_key2_key3 ); but is there any other way?

Upvotes: 1

Views: 1025

Answers (3)

Jim Calfas
Jim Calfas

Reputation: 1

The Berkeley database supports multiple keys, although the API for doing this is not available in all languages. Take a look at the section "Secondary Keys" in the document "Berkeley DB Concepts"

https://docs.oracle.com/cd/E17076_05/html/gsg/JAVA/javadplconcepts.html#secondary

Upvotes: 0

Fengya Li
Fengya Li

Reputation: 4709

As this condition,to solve the problem you can just construct a unique-key like this:

$seperator = "_"; #seperator depends on your data,pick one never found in your key.
$key = join $seperator , ($key1,$key2,$key3);
$hash{$key}{$count1}++;
$hash{$key}{$count2}++;

...

for (key1) {
   for (key2) {
     for (key3) {
        $un_key = join $seperator , (key1,key2,key3);
        print $hash{$un_key}{count1}."\t".$hash{$un_key}{count2};
        }
     }
 }

Upvotes: 0

cjm
cjm

Reputation: 62109

Berkeley-db doesn't support multiple keys like that. Each record can have only one key.

You can concatenate the keys to form a single key, as you stated.

You can use MLDBM to give the appearance of nested keys. But that works by storing a serialized hash under key1, so it will be very inefficient if you have a lot of keys nested under the top-level key.

Or, you can give up on BDB and go with a real SQL database. DBD::SQLite is easy to install and includes the SQLite database engine along with its driver.

I'd go with either concatenating the keys or a real database, depending on what exactly you're trying to do.

Upvotes: 4

Related Questions