summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorhel <git@yorhel.nl>2019-05-15 16:48:57 +0200
committerYorhel <git@yorhel.nl>2019-05-15 16:52:06 +0200
commit02315a70c6604c6dc3f87649c387ddea5bc398de (patch)
tree2fb911ee48fa226d48c1eabed7d5e04ec7cdaee9
parentb4b299b6d6c3463f86e3980d5fd3116a82a24ff3 (diff)
lmdb.pl: Add an "extract" command
This makes lmdb.pl a useful tool for dynamically creating sorted dictionaries. `lmdb.pl create` can be used to add more passwords to an existing database and `lmdb.pl extract` will print all unique passwords in the right order for `btree.pl create`.
-rwxr-xr-xlmdb.pl35
1 files changed, 28 insertions, 7 deletions
diff --git a/lmdb.pl b/lmdb.pl
index 9daaaf1..1e5e11f 100755
--- a/lmdb.pl
+++ b/lmdb.pl
@@ -4,18 +4,25 @@
#
# ./lmdb.pl create dbfile <dictionary
# ./lmdb.pl bench dbfile
+# ./lmdb.pl extract dbfile >dictionary
use strict;
use warnings;
use v5.10;
-use LMDB_File ':flags';
+use LMDB_File ':flags', ':cursor_op';
my($cmd, $dbfile) = @ARGV;
+my($env, $txn, $dbi);
+
+sub db {
+ my $wr = shift;
+ $env = LMDB::Env->new($dbfile, { mapsize => 1<<36, maxdbs => 1, flags => MDB_NOSUBDIR|($wr?0:MDB_RDONLY) });
+ $txn = $env->BeginTxn();
+ $dbi = $txn->open('db', $wr ? MDB_CREATE : undef);
+}
sub encode {
- my $env = LMDB::Env->new($dbfile, { mapsize => 1<<36, maxdbs => 1, flags => MDB_NOSUBDIR });
- my $txn = $env->BeginTxn();
- my $dbi = $txn->open('db', MDB_CREATE);
+ db 1;
while(<STDIN>) {
chomp;
$txn->put($dbi => $_, '') if length($_) && length($_) < 510;
@@ -25,9 +32,7 @@ sub encode {
sub bench {
- my $env = LMDB::Env->new($dbfile, { mapsize => 1<<32, maxdbs => 1, flags => MDB_RDONLY|MDB_NOSUBDIR });
- my $txn = $env->BeginTxn();
- my $dbi = $txn->open('db');
+ db;
my $lookup = sub { eval { $txn->get($dbi => $_[0], my $d); 1 } };
use Benchmark 'timethis', ':hireswallclock';
@@ -36,5 +41,21 @@ sub bench {
timethis 200000, sub { $lookup->(rstr()) }, 'lmdb';
}
+
+sub extract {
+ db;
+ my $db = LMDB_File->new($txn, $dbi);
+ my $cur = $db->Cursor();
+ eval {
+ my($key, $value);
+ $cur->get($key, $value, MDB_FIRST);
+ while(1) {
+ print "$key\n";
+ $cur->get($key, $value, MDB_NEXT);
+ }
+ }
+}
+
encode if $cmd eq 'create';
bench if $cmd eq 'bench';
+extract if $cmd eq 'extract';