1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
|
#!/usr/local/bin/perl
package TiedHash;
use strict;
use warnings;
use DB_File;
use Carp;
=example
my $tied_hash = new TiedHash( { create => "$pfam_db.inx" } );
my $acc = "";
while (<$fh>) {
chomp;
my ($token, $rest) = split (/\s+/, $_, 2);
if ($token eq 'NAME') {
$acc = $rest;
}
elsif ($token =~ /^(NC|TC|DESC|ACC)$/) {
my $key = "$acc$;$token";
$tied_hash->store_key_value($key, $rest);
print STDERR "storing: $key, $rest\n";
}
=cut
sub new {
my $packagename = shift;
my $prefs_href = shift;
if ($prefs_href && ! ref $prefs_href) {
confess "Error, need hash reference with opts in constructor.\n";
}
my $self = {
index_filename => undef,
tied_index => {},
tie_invoked => 0,
};
bless ($self, $packagename);
if (ref $prefs_href eq "HASH") {
if (my $index_file = $prefs_href->{"create"}) {
$self->create_index_file($index_file);
}
elsif ($index_file = $prefs_href->{"use"}) {
$self->use_index_file($index_file);
}
}
return ($self);
}
####
sub tie_invoked {
my $self = shift;
return ($self->{tie_invoked});
}
####
sub DESTROY {
my $self = shift;
if ($self->{index_filename}) {
# hash must have been tied
# so, untie it
untie (%{$self->{tied_index}});
}
}
####
sub create_index_file {
my $self = shift;
return ($self->make_index_file(@_));
}
####
sub make_index_file {
my $self = shift;
my $filename = shift;
unless ($filename) {
confess "need filename as parameter";
}
if (-e $filename) {
unlink $filename or confess "cannot remove existing index filename $filename";
}
$self->{index_filename} = $filename;
tie (%{$self->{tied_index}}, 'DB_File', $filename, O_CREAT|O_RDWR, 0666, $DB_BTREE);
$self->{tie_invoked} = 1;
return;
}
####
sub use_index_file {
my $self = shift;
my $filename = shift;
unless ($filename) {
confess "need filename as parameter";
}
unless (-s $filename) {
confess "Error, cannot locate file: $filename\n";
}
$self->{index_filename} = $filename;
tie (%{$self->{tied_index}}, 'DB_File', $filename, O_RDONLY, 0, $DB_BTREE);
$self->{tie_invoked} = 1;
#my @keys = $self->get_keys();
#unless (@keys) {
# confess "Error, tried using $filename db, but couldn't perform retrievals.\n";
#}
return;
}
####
sub store_key_value {
my ($self, $identifier, $value) = @_;
#my $num_keys = scalar ($self->get_keys());
unless ($self->tie_invoked()) {
confess "Error, cannot store key/value pair since tied hash not created.\n";
}
my $found = 0;
while (! $found) {
$self->{tied_index}->{$identifier} = $value;
my $val = $self->get_value($identifier);
if (defined $val) {
$found = 1;
}
else {
warn "Berkeley DB had trouble storing ($identifier); trying again.\n";
}
}
return;
}
####
sub get_value {
my $self = shift;
my $identifier = shift;
unless ($self->tie_invoked()) {
confess "Error, cannot retrieve value from untied hash\n";
}
my $value = $self->{tied_index}->{$identifier};
return ($value);
}
##
sub get_keys {
my $self = shift;
unless ($self->tie_invoked()) {
confess "Error, cannot retrieve values from untied hash\n";
}
return (keys %{$self->{tied_index}});
}
1; #EOM
|