]> git.donarmstrong.com Git - bin.git/blob - db-hash
* add support for reverse
[bin.git] / db-hash
1 #! /usr/bin/perl
2 # , 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 by Don Armstrong <don@donarmstrong.com>.
6 # $Id: perl_script 1432 2009-04-21 02:42:41Z don $
7
8
9 use warnings;
10 use strict;
11
12 use Getopt::Long;
13 use Pod::Usage;
14
15 =head1 NAME
16
17 db-hash - create a database and query using it
18
19 =head1 SYNOPSIS
20
21  db-hash [options] dbname key
22  db-hash --create [options] dbname < key_value.txt
23
24  Options:
25   --create, -c create dbname
26   --update, -u update dbname, create if it doesn't exist
27   --dump, -D dump dbname
28   --missing-newline, -n output a newline for unhashed keys
29   --include-key, -k output the key as well as the value
30   --debug, -d debugging level (Default 0)
31   --help, -h display this help
32   --man, -m display manual
33
34 =head1 OPTIONS
35
36 =over
37
38 =item B<--create,-c>
39
40 Create a database
41
42 =item B<--update,-u>
43
44 Update a database; replaces all keys with the values loaded from the
45 file, but keeps existing keys which were not replaced
46
47 =item B<--dump,-D>
48
49 Dump database to stdout
50
51 =item B<--include-key,-k>
52
53 Output the key as well as the value. (Off by default)
54
55 =item B<--missing-newline,-n>
56
57 If a key doesn't exist in the database, output a newline. (Forced on
58 when --include-key set)
59
60 =item B<--reverse,-r>
61
62 Reverse the database (value looks up keys instead); only useful during
63 creation/update
64
65 =item B<--debug, -d>
66
67 Debug verbosity. (Default 0)
68
69 =item B<--help, -h>
70
71 Display brief usage information.
72
73 =item B<--man, -m>
74
75 Display this manual.
76
77 =back
78
79 =head1 EXAMPLES
80
81
82 =cut
83
84 use Fcntl qw/O_RDWR O_RDONLY O_CREAT O_TRUNC/;
85 use MLDBM qw(DB_File Storable);
86
87 use vars qw($DEBUG);
88
89 my %options = (debug           => 0,
90                help            => 0,
91                man             => 0,
92                create          => 0,
93                update          => 0,
94                dump            => 0,
95 #              include_key     => 0,
96                missing_newline => 1,
97                reverse         => 0,
98                );
99
100 GetOptions(\%options,
101            'create|c','update|u',
102            'dump|D',
103            'include_key|include-key|k!',
104            'reverse|r!',
105            'missing_newline|missing-newline|n!',
106            'debug|d+','help|h|?','man|m');
107
108 pod2usage() if $options{help};
109 pod2usage({verbose=>2}) if $options{man};
110
111 $DEBUG = $options{debug};
112
113 my @USAGE_ERRORS;
114 # if (1) {
115 #      push @USAGE_ERRORS,"You must pass something";
116 # }
117
118 pod2usage(join("\n",@USAGE_ERRORS)) if @USAGE_ERRORS;
119
120
121
122 my ($db,@keys) = @ARGV;
123
124 my %t_db;
125
126 if (not ($options{create} or $options{update})) {
127     tie %t_db, MLDBM => $db, O_RDONLY or
128         die "Unable to open $db for reading: $!";
129 }
130 else {
131     tie %t_db, MLDBM => $db, O_RDWR|O_CREAT|($options{update}?0:O_TRUNC), 0666 or
132         die "Unable to open $db for writing: $!";
133 }
134
135 if (not defined $options{include_key}) {
136     $options{include_key} = $options{dump} ? 0 : 1;
137 }
138
139
140 if ($options{update} or $options{create}) {
141     my %fast_db;
142     while (<STDIN>) {
143         chomp;
144         my ($key,@val) = split /\t/;
145         if ($options{reverse}) {
146             for my $val_key (@val) {
147                 $fast_db{$val_key} = [make_list($fast_db{$val_key})//[],$key];
148             }
149         }
150         else {
151             $fast_db{$key} = [make_list($fast_db{$key} // [],@val)];
152         }
153     }
154     for my $key (keys %fast_db) {
155         $t_db{$key} = $fast_db{$key};
156     }
157 }
158 elsif ($options{dump}) {
159     for my $key (keys %t_db) {
160         print "$key\t".join("\t",make_list($t_db{$key}))."\n";
161     }
162 }
163 else {
164     if (!@keys) {
165         output_keys(<STDIN>);
166     }
167     else {
168         output_keys(@keys);
169     }
170 }
171
172 sub output_keys {
173     my @temp = @_;
174     for my $key (@temp) {
175         if (not exists $t_db{$key}) {
176             chomp $key;
177         }
178         if ($options{include_key}) {
179             print $key."\t";
180         }
181         if (exists $t_db{$key}) {
182             print join("\t",make_list($t_db{$key}));
183             print "\n";
184         }
185         elsif ($options{missing_newline}) {
186             print "\n";
187         }
188     }
189 }
190
191 sub make_list {
192      return map {(ref($_) eq 'ARRAY')?@{$_}:$_} @_;
193 }
194
195
196
197
198 __END__