2 # db-hash creates databases and queries it, and is released
3 # under the terms of the GPL version 2, or any later version, at your
4 # option. See the file README and COPYING for more information.
5 # Copyright 2009-11 by Don Armstrong <don@donarmstrong.com>.
16 db-hash - create a database and query using it
20 db-hash [options] dbname key
21 db-hash --create [options] dbname < key_value.txt
24 --create, -c create dbname
25 --update, -u update dbname, create if it doesn't exist
26 --dump, -D dump dbname
27 --missing-newline, -n output a newline for unhashed keys
28 --include-key, -k output the key as well as the value
29 --reverse, -r reverse (values to key)
30 --key-to-itself map key to itself
31 --debug, -d debugging level (Default 0)
32 --help, -h display this help
33 --man, -m display manual
45 Update a database; replaces all keys with the values loaded from the
46 file, but keeps existing keys which were not replaced
50 Dump database to stdout
52 =item B<--include-key,-k>
54 Output the key as well as the value. (Off by default)
60 =item B<--key-to-itself>
62 Map the key to itself (On by default in reverse hashes)
64 =item B<--missing-newline,-n>
66 If a key doesn't exist in the database, output a newline. (Forced on
67 when --include-key set)
71 Reverse the database (value looks up keys instead); only useful during
76 Debug verbosity. (Default 0)
80 Display brief usage information.
93 use Fcntl qw/O_RDWR O_RDONLY O_CREAT O_TRUNC/;
94 use MLDBM qw(DB_File Storable);
98 my %options = (debug => 0,
105 missing_newline => 1,
109 GetOptions(\%options,
110 'create|c','update|u',
112 'include_key|include-key|k!',
114 'key_to_itself|key-to-itself|K',
115 'missing_newline|missing-newline|n!',
116 'debug|d+','help|h|?','man|m');
118 pod2usage() if $options{help};
119 pod2usage({verbose=>2}) if $options{man};
121 $DEBUG = $options{debug};
125 # push @USAGE_ERRORS,"You must pass something";
128 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
132 my ($db,@keys) = @ARGV;
136 if (not ($options{create} or $options{update})) {
137 tie %t_db, MLDBM => $db, O_RDONLY or
138 die "Unable to open $db for reading: $!";
141 tie %t_db, MLDBM => $db, O_RDWR|O_CREAT|($options{update}?0:O_TRUNC), 0666 or
142 die "Unable to open $db for writing: $!";
145 if ($options{reverse}) {
146 if (not exists $options{key_to_itself}) {
147 $options{key_to_itself} = 1;
151 if (not defined $options{include_key}) {
152 $options{include_key} = $options{dump} ? 0 : 1;
156 if ($options{update} or $options{create}) {
160 my ($key,@val) = split /\t/;
161 if ($options{reverse}) {
162 for my $val_key (@val,$options{key_to_itself}?$key:()) {
163 $fast_db{$val_key} = [make_list($fast_db{$val_key} // []),$key];
167 $fast_db{$key} = [make_list($fast_db{$key} // [],@val)];
170 for my $key (keys %fast_db) {
171 $t_db{$key} = $fast_db{$key};
174 elsif ($options{dump}) {
175 for my $key (keys %t_db) {
176 print "$key\t".join("\t",make_list($t_db{$key}))."\n";
181 output_keys(<STDIN>);
190 for my $key (@temp) {
191 if (not exists $t_db{$key}) {
194 if ($options{include_key}) {
197 if (exists $t_db{$key}) {
198 print join("\t",make_list($t_db{$key}));
201 elsif ($options{missing_newline}) {
208 return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;