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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
package VCP::DB_File::sdbm;
=head1 NAME
VCP::DB_File::sdbm - Subclass providing SDBM_File storage
=head1 SYNOPSIS
use VCP::DB_File;
VCP::DB_File->new;
=head1 DESCRIPTION
To write your own DB_File filetype, copy this file and alter it. Then
ask us to add an option to the .vcp file parsing to enable it.
=over
=for test_script t/01db_file_sdbm.t
=cut
$VERSION = 1 ;
@ISA = qw( VCP::DB_File );
use strict ;
use VCP::Debug qw( :debug );
use Fcntl;
use File::Spec;
use SDBM_File;
use VCP::DB_File;
use VCP::Debug qw( :debug );
use VCP::Logger qw( BUG );
#use base qw( VCP::DB_File );
#use fields (
# 'Hash', ## The hash we tie
#);
sub db_file {
my $self = shift;
return File::Spec->catfile(
$self->store_loc,
"db"
);
}
sub close_db {
my $self = shift;
return unless $self->{Hash};
$self->SUPER::close_db;
$self->{Hash} = undef;
}
sub delete_db {
my $self = shift;
my $store_files_pattern = $self->store_loc . "/*";
my $has_store_files = -e $self->store_loc;
if ( $has_store_files ) {
require File::Glob;
my @store_files = File::Glob::glob( $store_files_pattern );
$has_store_files &&= @store_files;
}
return
unless $has_store_files;
$self->SUPER::delete_db;
$self->rmdir_store_loc unless $ENV{VCPNODELETE};
}
sub open_db {
my $self = shift;
$self->SUPER::open_db;
$self->mkdir_store_loc;
$self->{Hash} = {};
my $fn = $self->db_file;
tie %{$self->{Hash}}, "SDBM_File", $fn, O_RDWR|O_CREAT, 0660
or die "$! while opening DB_File SDBM file '$fn'";
}
sub open_existing_db {
my $self = shift;
$self->SUPER::open_db;
$self->mkdir_store_loc;
$self->{Hash} = {};
my $fn = $self->db_file;
tie %{$self->{Hash}}, "SDBM_File", $fn, O_RDWR, 0
or die "$! while opening DB_File SDBM file '$fn'";
}
sub raw_set { ## so big_records.pm can call us with prepacked stuff
my $self = shift;
my $key = shift;
$self->{Hash}->{$key} = shift;
}
sub set {
my $self = shift;
my $key_parts = shift;
BUG "key must be an ARRAY reference"
unless ref $key_parts eq "ARRAY";
debug "setting ",
ref $self, " ",
join( ",", @$key_parts ), " => ",
join( ",", @_ )
if debugging;
$self->raw_set(
$self->pack_values( @$key_parts ),
$self->pack_values( @_ )
);
}
sub raw_get {
my $self = shift;
my $key = shift;
$self->{Hash}->{$key};
}
sub get {
my $self = shift;
my $key_parts = shift;
BUG "key must be an ARRAY reference"
unless ref $key_parts eq "ARRAY";
BUG "extra args found"
if @_;
BUG "called in scalar context"
if defined wantarray && !wantarray;
my $key = $self->pack_values( @$key_parts );
my $v = $self->raw_get( $key );
return unless defined $v;
$self->unpack_values( $v );
}
sub exists {
my $self = shift;
my $key_parts = shift;
BUG "key must be an ARRAY reference"
unless ref $key_parts eq "ARRAY";
my $key = $self->pack_values( @$key_parts );
return $self->{Hash}->{$key} ? 1 : 0;
}
sub keys {
my $self = shift;
map [ $self->unpack_values( $_ ) ], keys %{$self->{Hash}};
}
=item dump
$db->dump( \*STDOUT );
my $s = $db->dump;
my @l = $db->dump;
Dumps keys and values from a DB, in lexically sorted key order.
If a filehandle reference is provided, prints to that filehandle.
Otherwise, returns a string or array containing the entire dump,
depending on context.
=cut
sub dump {
my $self = shift;
my $fh = @_ ? shift : undef;
my( @keys, %vals );
my @w;
while ( my ( $k, $v ) = each %{$self->{Hash}} ) {
my @key = $self->unpack_values( $k );
for ( my $i = 0; $i <= $#key; ++$i ) {
$w[$i] = length $key[$i]
if ! defined $w[$i] || length $key[$i] > $w[$i];
}
push @keys, $k;
$vals{$k} = [ $self->unpack_values( $v ) ];
}
## This does not take file separators in to account, but that's ok
## for a debugging tool and the ids that are used as key values
## are supposed to be opaque anyway
@keys = sort @keys;
# build format string
my $f = join( " ", map "%-${w[$_]}s", 0..$#w ) . " => %s\n";
my @lines;
while ( @keys ) {
my $k = shift @keys;
my @v = map { "'$_'" } @{$vals{$k}};
my $s = sprintf $f,
$self->unpack_values( $k ),
@v == 1 ? $v[0] : join join( ",", @v ), "(", ")";
if( defined $fh ) {
print $fh $s;
}
else {
push @lines, $s;
}
}
unless( defined $fh ) {
if( wantarray ) {
chomp @lines;
return @lines;
}
return join "", @lines;
}
}
=back
=head1 LIMITATIONS
There is no way (yet) of telling the mapper to continue processing the
rules list. We could implement labels like C< <<I<label>>> > to be
allowed before pattern expressions (but not between pattern and result),
and we could then impelement C< <<goto I<label>>> >. And a C< <<next>>
> could be used to fall through to the next label. All of which is
wonderful, but I want to gain some real world experience with the
current system and find a use case for gotos and fallthroughs before I
implement them. This comment is here to solicit feedback :).
=head1 AUTHOR
Barrie Slaymaker <barries@slaysys.com>
=head1 COPYRIGHT
Copyright (c) 2000, 2001, 2002 Perforce Software, Inc.
All rights reserved.
See L<VCP::License|VCP::License> (C<vcp help license>) for the terms of use.
=cut
1
|