| 1 | package Store::Digest::Driver::FileSystem; | ||
| 2 | |||
| 3 | 1 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use utf8; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use Moose; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | |||
| 12 | 1 | ▶ | use MooseX::Types::Moose qw(HashRef ArrayRef); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 13 | 1 | ▶ | use Store::Digest::Types qw(Directory DateTimeType Token); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | |||
| 15 | 1 | ▶ | use Store::Digest::Object; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 16 | 1 | ▶ | use Store::Digest::Stats; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 17 | |||
| 18 | 0 | ▶ | use BerkeleyDB qw(DB_CREATE DB_GET_BOTH DB_INIT_CDB DB_INIT_LOCK |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 19 | DB_INIT_LOG DB_INIT_TXN DB_INIT_MPOOL DB_NEXT | ||
| 20 | DB_SET_RANGE DB_GET_BOTH DB_GET_BOTH_RANGE | ||
| 21 | DB_RECOVER DB_RECOVER_FATAL DB_DONOTINDEX | ||
| 22 | 0 | DB_DUP DB_DUPSORT DB_DIRTY_READ DB_AUTO_COMMIT DB_HASH); | |
| 23 | |||
| 24 | use Path::Class (); | ||
| 25 | use File::Copy (); | ||
| 26 | use MIME::Base32 (); | ||
| 27 | use URI::ni (); | ||
| 28 | use Math::BigInt (); | ||
| 29 | use Scalar::Util (); | ||
| 30 | use List::Util (); | ||
| 31 | |||
| 32 | # directories | ||
| 33 | use constant STORE => 'store'; | ||
| 34 | use constant TEMP => 'tmp'; | ||
| 35 | use constant CONTROL => 'control'; | ||
| 36 | use constant INDEX => 'index'; | ||
| 37 | use constant BUFSIZE => 2**13; | ||
| 38 | |||
| 39 | with 'Role::MimeInfo'; | ||
| 40 | with 'Store::Digest::Driver'; | ||
| 41 | |||
| 42 | |||
| 43 | # digests with byte lengths | ||
| 44 | my %DIGESTS = ( | ||
| 45 | 'md5' => 16, | ||
| 46 | # 'ripemd-160' => 20, | ||
| 47 | 'sha-1' => 20, | ||
| 48 | 'sha-256' => 32, | ||
| 49 | 'sha-384' => 48, | ||
| 50 | 'sha-512' => 64, | ||
| 51 | ); | ||
| 52 | |||
| 53 | # values for empty object | ||
| 54 | my %EMPTY = ( | ||
| 55 | 'md5' => 'd41d8cd98f00b204e9800998ecf8427e', | ||
| 56 | 'sha-1' => 'da39a3ee5e6b4b0d3255bfef95601890afd80709', | ||
| 57 | 'sha-256' => 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855', | ||
| 58 | 'sha-384' => '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b', | ||
| 59 | 'sha-512' => 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e', | ||
| 60 | ); | ||
| 61 | |||
| 62 | # pack these down | ||
| 63 | %EMPTY = map { $_ => pack('H*', $EMPTY{$_}) } keys %EMPTY; | ||
| 64 | |||
| 65 | sub _pack_n { | ||
| 66 | my $val = shift; | ||
| 67 | return unless defined $val; | ||
| 68 | return unless Scalar::Util::looks_like_number($val); | ||
| 69 | return unless $val > 0; | ||
| 70 | return unless $val == int $val; | ||
| 71 | |||
| 72 | pack 'N', $val; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _pack_z { | ||
| 76 | my $val = shift; | ||
| 77 | return unless defined $val; | ||
| 78 | $val =~ s/^\s*(.*?)\s*$/$1/sm; | ||
| 79 | return if $val eq ''; | ||
| 80 | |||
| 81 | pack 'Z*', $val; | ||
| 82 | } | ||
| 83 | |||
| 84 | my %META = ( | ||
| 85 | ctime => \&_pack_n, | ||
| 86 | mtime => \&_pack_n, | ||
| 87 | dtime => \&_pack_n, | ||
| 88 | ptime => \&_pack_n, | ||
| 89 | type => \&_pack_z, | ||
| 90 | language => \&_pack_z, | ||
| 91 | charset => \&_pack_z, | ||
| 92 | encoding => \&_pack_z, | ||
| 93 | ); | ||
| 94 | |||
| 95 | sub _meta_keyfunc { | ||
| 96 | my ($self, $field) = @_; | ||
| 97 | my $pri = $self->_primary; | ||
| 98 | my $algos = [@{$self->_algorithms}]; | ||
| 99 | my $func = $META{$field}; | ||
| 100 | |||
| 101 | return sub { | ||
| 102 | my %p = _inflate_rec($pri, $algos, $_[0], $_[1], 1); | ||
| 103 | if (defined $p{$field} and defined (my $val = $func->($p{$field}))) { | ||
| 104 | $_[2] = $val; | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | return DB_DONOTINDEX; | ||
| 108 | }; | ||
| 109 | } | ||
| 110 | |||
| 111 | =head1 NAME | ||
| 112 | |||
| 113 | Store::Digest::Driver::FileSystem - File system driver for Store::Digest | ||
| 114 | |||
| 115 | =head1 VERSION | ||
| 116 | |||
| 117 | Version 0.06 | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | our $VERSION = '0.06'; | ||
| 122 | |||
| 123 | # target directory | ||
| 124 | has dir => ( | ||
| 125 | is => 'ro', | ||
| 126 | isa => Directory, | ||
| 127 | required => 1, | ||
| 128 | coerce => 1, | ||
| 129 | ); | ||
| 130 | |||
| 131 | # file permissions | ||
| 132 | has umask => ( | ||
| 133 | is => 'ro', | ||
| 134 | isa => 'Int', | ||
| 135 | lazy => 1, | ||
| 136 | default => 077, | ||
| 137 | ); | ||
| 138 | |||
| 139 | # transaction environment | ||
| 140 | has _env => ( | ||
| 141 | is => 'rw', | ||
| 142 | isa => 'BerkeleyDB::Env', | ||
| 143 | ); | ||
| 144 | |||
| 145 | # metadata for the actual database | ||
| 146 | has _control => ( | ||
| 147 | is => 'rw', | ||
| 148 | isa => 'BerkeleyDB::Btree', | ||
| 149 | ); | ||
| 150 | |||
| 151 | # the payload data itself | ||
| 152 | has _entries => ( | ||
| 153 | is => 'ro', | ||
| 154 | isa => HashRef, | ||
| 155 | lazy => 1, | ||
| 156 | default => sub { {} }, | ||
| 157 | ); | ||
| 158 | |||
| 159 | # metadata -> object mapping | ||
| 160 | has _indices => ( | ||
| 161 | is => 'ro', | ||
| 162 | isa => HashRef, | ||
| 163 | lazy => 1, | ||
| 164 | default => sub { {} }, | ||
| 165 | ); | ||
| 166 | |||
| 167 | |||
| 168 | =head1 SYNOPSIS | ||
| 169 | |||
| 170 | my $store = Store::Digest->new( | ||
| 171 | driver => 'FileSystem', | ||
| 172 | options => { dir => '/var/db/store-digest' }, # or wherever | ||
| 173 | ); | ||
| 174 | |||
| 175 | =head1 METHODS | ||
| 176 | |||
| 177 | =head2 new | ||
| 178 | |||
| 179 | =cut | ||
| 180 | |||
| 181 | sub _create_dirs { | ||
| 182 | my $self = shift; | ||
| 183 | |||
| 184 | my $d = $self->dir; | ||
| 185 | my @dirs = ($d, $d->subdir(STORE), $d->subdir(TEMP)); | ||
| 186 | for my $dir (@dirs) { | ||
| 187 | my $stat = $dir->stat; | ||
| 188 | if ($stat) { | ||
| 189 | Carp::croak("Can't read and/or write $dir") | ||
| 190 | unless (-r $stat and -w $stat); | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | eval { | ||
| 194 | my $mode = 0777 & ~$self->umask | 02000; | ||
| 195 | $dir->mkpath(0, $mode); | ||
| 196 | }; | ||
| 197 | if ($@) { | ||
| 198 | Carp::croak("Can't create $dir: $@"); | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | |||
| 204 | # attempt to create a btree control file; rescue from the stupid | ||
| 205 | # stupid stupid stupid idea that this should be a hash | ||
| 206 | sub _init_control { | ||
| 207 | my $self = shift; | ||
| 208 | |||
| 209 | my $env = $self->_env; | ||
| 210 | my $mode = 0666 & ~$self->umask; | ||
| 211 | my $file = $self->dir->absolute->file(CONTROL); | ||
| 212 | |||
| 213 | my $control; | ||
| 214 | |||
| 215 | if (-e $file) { | ||
| 216 | $control = BerkeleyDB::Unknown->new( | ||
| 217 | -Env => $env, | ||
| 218 | -Flags => DB_AUTO_COMMIT, | ||
| 219 | -Mode => $mode, | ||
| 220 | -Filename => CONTROL, | ||
| 221 | ) or Carp::croak | ||
| 222 | ("Could not initialize control database: $BerkeleyDB::Error"); | ||
| 223 | |||
| 224 | if ($control->type == DB_HASH) { | ||
| 225 | my $c2 = BerkeleyDB::Btree->new( | ||
| 226 | -Env => $env, | ||
| 227 | -Flags => DB_CREATE|DB_AUTO_COMMIT, | ||
| 228 | -Mode => $mode, | ||
| 229 | -Filename => CONTROL . 2, | ||
| 230 | ) or Carp::croak | ||
| 231 | ("Could not create replacement control: $BerkeleyDB::Error"); | ||
| 232 | |||
| 233 | my $txn = $env->txn_begin; | ||
| 234 | $txn->Txn($control, $c2); | ||
| 235 | |||
| 236 | my $cur = $control->db_cursor; | ||
| 237 | my ($k, $v, $ret) = ('', '', 0); | ||
| 238 | while ($cur->c_get($k, $v, DB_NEXT) == 0) { | ||
| 239 | #warn "wat $k $v"; | ||
| 240 | $ret += $c2->db_put($k, $v); | ||
| 241 | } | ||
| 242 | Carp::croak("Could not copy database: $BerkeleyDB::Error") if $ret; | ||
| 243 | |||
| 244 | $cur->c_close; | ||
| 245 | undef $cur; | ||
| 246 | |||
| 247 | unless ($txn->txn_commit == 0) { | ||
| 248 | die 'Could not copy contents from old control database to new!'; | ||
| 249 | } | ||
| 250 | |||
| 251 | $env->txn_checkpoint(0, 0); | ||
| 252 | |||
| 253 | # require Data::Dumper; | ||
| 254 | # warn Data::Dumper::Dumper($env->txn_stat); | ||
| 255 | |||
| 256 | undef $txn; | ||
| 257 | |||
| 258 | $control->db_close; | ||
| 259 | $c2->db_close; | ||
| 260 | undef $control; | ||
| 261 | undef $c2; | ||
| 262 | |||
| 263 | # note OK is zero hence `and` | ||
| 264 | BerkeleyDB::db_remove(-Env => $env, -Filename => CONTROL) | ||
| 265 | and die 'Could not delete old hash-based control database!: ' | ||
| 266 | . $BerkeleyDB::Error; | ||
| 267 | BerkeleyDB::db_rename | ||
| 268 | (-Env => $env, -Filename => CONTROL . 2, -Newname => CONTROL) | ||
| 269 | and die 'Could not rename new B+Tree control database!: ' | ||
| 270 | . $BerkeleyDB::Error; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | $control ||= BerkeleyDB::Btree->new( | ||
| 275 | -Env => $env, | ||
| 276 | -Flags => DB_CREATE|DB_AUTO_COMMIT, | ||
| 277 | -Mode => $mode, | ||
| 278 | -Filename => CONTROL, | ||
| 279 | ) or Carp::croak | ||
| 280 | ("Could not initialize (B+Tree) control database: $BerkeleyDB::Error"); | ||
| 281 | |||
| 282 | $control; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub BUILD { | ||
| 286 | my $self = shift; | ||
| 287 | |||
| 288 | $self->_create_dirs; | ||
| 289 | |||
| 290 | my $mode = 0666 & ~$self->umask; | ||
| 291 | |||
| 292 | #warn $self->dir->absolute; | ||
| 293 | my $flags = DB_CREATE|DB_INIT_MPOOL|DB_INIT_TXN| | ||
| 294 | DB_INIT_LOCK|DB_INIT_LOG|DB_RECOVER; | ||
| 295 | my $env = BerkeleyDB::Env->new( | ||
| 296 | -Home => $self->dir->absolute->stringify, | ||
| 297 | -Mode => $mode, | ||
| 298 | -Flags => $flags, | ||
| 299 | -ErrFile => $self->dir->absolute->file('error.log')->stringify, | ||
| 300 | ) or Carp::croak | ||
| 301 | ("Can't create transaction environment: $BerkeleyDB::Error"); | ||
| 302 | |||
| 303 | $self->_env($env); | ||
| 304 | |||
| 305 | # create control | ||
| 306 | # my $control = BerkeleyDB::Hash->new( | ||
| 307 | # -Env => $env, | ||
| 308 | # -Flags => DB_CREATE|DB_AUTO_COMMIT, | ||
| 309 | # -Mode => $mode, | ||
| 310 | # #-Txn => $txn, | ||
| 311 | # -Filename => 'control', | ||
| 312 | # ) or Carp::croak | ||
| 313 | # ("Can't create/bind control database: $BerkeleyDB::Error"); | ||
| 314 | my $control = $self->_init_control; | ||
| 315 | $self->_control($control); | ||
| 316 | |||
| 317 | my $txn = $env->txn_begin; | ||
| 318 | # add to transaction | ||
| 319 | $control->Txn($txn); | ||
| 320 | |||
| 321 | # control stores: | ||
| 322 | |||
| 323 | # algorithms used | ||
| 324 | if ($control->db_get(algorithms => my $v) == 0) { | ||
| 325 | my @alg = sort split(/\s*,+\s*/, $v); | ||
| 326 | |||
| 327 | for my $a (@alg) { | ||
| 328 | Carp::croak | ||
| 329 | ("CONTROL DATABASE CORRUPT: $a is not a valid algorithm") | ||
| 330 | unless defined $DIGESTS{$a}; | ||
| 331 | } | ||
| 332 | |||
| 333 | # make sure this is sorted for comparison below | ||
| 334 | $v = join ',', @alg; | ||
| 335 | |||
| 336 | if (defined (my $w = $self->_algorithms)) { | ||
| 337 | my $x = join ',', sort map { lc $_ } @$w; | ||
| 338 | Carp::croak("Store is already initialized with: $v") | ||
| 339 | unless $x eq $v; | ||
| 340 | } | ||
| 341 | |||
| 342 | $self->_algorithms(\@alg); | ||
| 343 | } | ||
| 344 | else { | ||
| 345 | my $w = $self->_algorithms || [keys %DIGESTS]; | ||
| 346 | for my $a (@$w) { | ||
| 347 | Carp::croak | ||
| 348 | ("Driver initialization failed: $a is not a valid algorithm") | ||
| 349 | unless defined $DIGESTS{$a}; | ||
| 350 | } | ||
| 351 | # XXX DO NOT FORGET TO SORT THIS | ||
| 352 | $w = [sort map { lc $_ } @$w]; | ||
| 353 | my $v = join ',', @$w; | ||
| 354 | |||
| 355 | $self->_algorithms($w); | ||
| 356 | unless ($control->db_put(algorithms => $v) == 0) { | ||
| 357 | die 'INTERNAL ERROR: could not store control data (algorithms): ' . | ||
| 358 | $BerkeleyDB::Error; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | # primary algorithm | ||
| 363 | if ($control->db_get(primary => my $v) == 0) { | ||
| 364 | Carp::croak("CONTROL DATABASE CORRUPT: $v is not a valid algorithm") | ||
| 365 | unless defined $DIGESTS{$v}; | ||
| 366 | if (defined (my $w = $self->_primary)) { | ||
| 367 | Carp::croak | ||
| 368 | ("Store is already initialized with primary algorithm $v") | ||
| 369 | unless $v eq lc $w; | ||
| 370 | } | ||
| 371 | $self->_primary($v); | ||
| 372 | } | ||
| 373 | else { | ||
| 374 | my $v = lc($self->_primary || 'sha-256'); | ||
| 375 | Carp::croak("Driver initialization failed: $v is not a valid algorithm") | ||
| 376 | unless defined $DIGESTS{$v}; | ||
| 377 | |||
| 378 | Carp::croak | ||
| 379 | ("Driver initialization failed: $v must be in algorithm list") | ||
| 380 | unless grep { $_ eq $v } @{$self->_algorithms}; | ||
| 381 | |||
| 382 | $self->_primary($v); | ||
| 383 | unless ($control->db_put(primary => $v) == 0) { | ||
| 384 | die 'INTERNAL ERROR: could not store control data (primary): ' . | ||
| 385 | $BerkeleyDB::Error; | ||
| 386 | } | ||
| 387 | } | ||
| 388 | |||
| 389 | # total number of known hash entries | ||
| 390 | unless ($control->db_get(objects => my $v) == 0) { | ||
| 391 | unless ($control->db_put(objects => 0) == 0) { | ||
| 392 | die 'INTERNAL ERROR: could not store control data (objects): ' . | ||
| 393 | $BerkeleyDB::Error; | ||
| 394 | } | ||
| 395 | } | ||
| 396 | |||
| 397 | # total number of hash entries where the payloads were removed | ||
| 398 | unless ($control->db_get(deleted => my $v) == 0) { | ||
| 399 | unless ($control->db_put(deleted => 0) == 0) { | ||
| 400 | die 'INTERNAL ERROR: could not store control data (deleted): ' . | ||
| 401 | $BerkeleyDB::Error; | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | # total number of bytes stored | ||
| 406 | unless ($control->db_get(bytes => my $v) == 0) { | ||
| 407 | unless ($control->db_put(bytes => 0) == 0) { | ||
| 408 | die 'INTERNAL ERROR: could not store control data (bytes): ' . | ||
| 409 | $BerkeleyDB::Error; | ||
| 410 | } | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | # store-wide creation and modification times | ||
| 415 | my $now = time; | ||
| 416 | unless ($control->db_get(ctime => my $v) == 0) { | ||
| 417 | unless ($control->db_put(ctime => $now) == 0) { | ||
| 418 | die 'INTERNAL ERROR: could not store control data (ctime): ' . | ||
| 419 | $BerkeleyDB::Error; | ||
| 420 | } | ||
| 421 | } | ||
| 422 | |||
| 423 | unless ($control->db_get(mtime => my $v) == 0) { | ||
| 424 | unless ($control->db_put(mtime => $now) == 0) { | ||
| 425 | die 'INTERNAL ERROR: could not store control data (mtime): ' . | ||
| 426 | $BerkeleyDB::Error; | ||
| 427 | } | ||
| 428 | } | ||
| 429 | |||
| 430 | unless ($txn->txn_commit == 0) { | ||
| 431 | die 'INTERNAL ERROR: Could not commit transaction on control data: ' . | ||
| 432 | $BerkeleyDB::Error; | ||
| 433 | } | ||
| 434 | |||
| 435 | # renew transaction | ||
| 436 | $txn = $env->txn_begin; | ||
| 437 | |||
| 438 | # create algo btrees, these enable partial matches | ||
| 439 | my $pri = $self->_primary; | ||
| 440 | my $ent = $self->_entries; | ||
| 441 | my $primary = $ent->{$pri} = BerkeleyDB::Btree->new( | ||
| 442 | -Env => $env, | ||
| 443 | -Flags => DB_CREATE|DB_AUTO_COMMIT, | ||
| 444 | -Mode => $mode, | ||
| 445 | -Filename => $pri, | ||
| 446 | ) or Carp::croak | ||
| 447 | ("Can't create/bind primary database $pri: $BerkeleyDB::Error"); | ||
| 448 | |||
| 449 | # add primary database to transaction | ||
| 450 | $primary->Txn($txn); | ||
| 451 | |||
| 452 | my @rest = grep { $_ ne $pri } @{$self->_algorithms}; | ||
| 453 | for my $i (0..$#rest) { | ||
| 454 | my $k = $rest[$i]; | ||
| 455 | my $secondary = BerkeleyDB::Btree->new( | ||
| 456 | -Env => $env, | ||
| 457 | -Flags => DB_CREATE|DB_AUTO_COMMIT, | ||
| 458 | -Mode => $mode, | ||
| 459 | -Filename => $k, | ||
| 460 | #-Txn => $txn, | ||
| 461 | ) or Carp::croak | ||
| 462 | ("Can't create/bind $k database: $BerkeleyDB::Error"); | ||
| 463 | |||
| 464 | $self->_entries->{$k} = $secondary; | ||
| 465 | |||
| 466 | # now we associate secondary to primary | ||
| 467 | my $len = $DIGESTS{$k}; | ||
| 468 | my $off = List::Util::sum0(@DIGESTS{@rest[0..$i]}) - $len; | ||
| 469 | my $sub = sub { $_[2] = substr($_[1], $off, $len); return 0; }; | ||
| 470 | |||
| 471 | $primary->associate($secondary, $sub); | ||
| 472 | } | ||
| 473 | |||
| 474 | # now do metadata indices | ||
| 475 | my @meta = qw(ctime mtime dtime ptime type language charset encoding); | ||
| 476 | my $meta = $self->_indices; | ||
| 477 | my %init; | ||
| 478 | for my $k (@meta) { | ||
| 479 | my $index = $meta->{$k} = BerkeleyDB::Btree->new( | ||
| 480 | -Env => $env, | ||
| 481 | -Flags => DB_CREATE|DB_AUTO_COMMIT, | ||
| 482 | -Property => DB_DUP|DB_DUPSORT, | ||
| 483 | -Mode => $mode, | ||
| 484 | -Filename => INDEX, | ||
| 485 | -Subname => $k, | ||
| 486 | ) or Carp::croak | ||
| 487 | ("Can't create/bind $k index database: $BerkeleyDB::Error"); | ||
| 488 | |||
| 489 | # add index db to transaction | ||
| 490 | $index->Txn($txn); | ||
| 491 | |||
| 492 | # add this to initialization queue | ||
| 493 | my $st = $index->db_stat; | ||
| 494 | if ($st->{bt_nkeys} == 0) { | ||
| 495 | #warn "adding $k to init queue"; | ||
| 496 | $init{$k} = $META{$k}; | ||
| 497 | } | ||
| 498 | else { | ||
| 499 | $primary->associate($index, $self->_meta_keyfunc($k)); | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | # now run init queue | ||
| 504 | if (keys %init) { | ||
| 505 | my $cursor = $primary->db_cursor; | ||
| 506 | my ($pk, $rec) = (0, 0); | ||
| 507 | while ($cursor->c_get($pk, $rec, DB_NEXT) == 0) { | ||
| 508 | #warn unpack 'H*', $pk; | ||
| 509 | #warn unpack 'H*', $rec; | ||
| 510 | |||
| 511 | my %p = _inflate_rec($pri, [@rest], $pk, $rec, 1); | ||
| 512 | while (my ($ix, $func) = each %init) { | ||
| 513 | if (defined $p{$ix} and defined (my $k = $func->($p{$ix}))) { | ||
| 514 | #warn "adding $ix => $p{$ix}"; | ||
| 515 | my $wat = $meta->{$ix}->db_put($k, $pk); | ||
| 516 | #warn "wat $wat: $BerkeleyDB::Error" if $wat; | ||
| 517 | } | ||
| 518 | } | ||
| 519 | } | ||
| 520 | |||
| 521 | $cursor->c_close; | ||
| 522 | undef $cursor; | ||
| 523 | |||
| 524 | for my $ix (keys %init) { | ||
| 525 | $primary->associate($meta->{$ix}, $self->_meta_keyfunc($ix)); | ||
| 526 | } | ||
| 527 | } | ||
| 528 | |||
| 529 | unless ($txn->txn_commit == 0) { | ||
| 530 | die "Could not commit initialization transaction: $BerkeleyDB::Error"; | ||
| 531 | } | ||
| 532 | |||
| 533 | # hashes ctime atime dtime type language encoding charset | ||
| 534 | #$self->_entries($entries); | ||
| 535 | } | ||
| 536 | |||
| 537 | sub DEMOLISH { | ||
| 538 | my $self = shift; | ||
| 539 | my $p = $self->_primary; | ||
| 540 | my $e = $self->_entries; | ||
| 541 | |||
| 542 | # close non-primary entries | ||
| 543 | for my $k (keys %$e) { | ||
| 544 | next if $k eq $p; | ||
| 545 | my $db = $e->{$k}; | ||
| 546 | $db->db_close if $db; | ||
| 547 | } | ||
| 548 | |||
| 549 | # now close primary | ||
| 550 | $e->{$p}->db_close if $e->{$p}; | ||
| 551 | |||
| 552 | # now close control | ||
| 553 | $self->_control->db_close if $self->_control; | ||
| 554 | } | ||
| 555 | |||
| 556 | =head2 add | ||
| 557 | |||
| 558 | Add an object to the store. | ||
| 559 | |||
| 560 | =cut | ||
| 561 | |||
| 562 | # # File::MimeInfo::mimetype_isa only tells you if the child type is an | ||
| 563 | # # immediate descendant of its parent, which is practically useless. | ||
| 564 | # sub _mimetype_isa_really { | ||
| 565 | # my ($child, $ancestor) = @_; | ||
| 566 | # return unless defined $child && defined $ancestor; | ||
| 567 | # my @q = ($child); | ||
| 568 | # my %t; | ||
| 569 | # do { | ||
| 570 | # for my $t (File::MimeInfo::mimetype_isa(shift @q)) { | ||
| 571 | # $t = lc $t; # JIC | ||
| 572 | # push @q, $t unless defined $t{$t}; | ||
| 573 | # $t{$t}++; | ||
| 574 | # } | ||
| 575 | # } while @q; | ||
| 576 | |||
| 577 | # if (defined $ancestor) { | ||
| 578 | # return !!$t{$ancestor}; | ||
| 579 | # } | ||
| 580 | # else { | ||
| 581 | # return sort keys %t; | ||
| 582 | # } | ||
| 583 | # } | ||
| 584 | |||
| 585 | sub _file_for { | ||
| 586 | my ($self, $digest) = @_; | ||
| 587 | # digest is assumed to be binary | ||
| 588 | my $primary = $self->_primary; | ||
| 589 | Carp::croak("Expecting binary $primary digest") | ||
| 590 | unless length $digest eq $DIGESTS{$primary}; | ||
| 591 | |||
| 592 | my $b32 = lc MIME::Base32::encode_rfc3548($digest); | ||
| 593 | my @parts = unpack('a4a4a4a*', $b32); | ||
| 594 | |||
| 595 | $self->dir->file(STORE, @parts); | ||
| 596 | } | ||
| 597 | |||
| 598 | # take an object and return a record | ||
| 599 | sub _deflate { | ||
| 600 | my ($self, $obj) = @_; | ||
| 601 | |||
| 602 | # find the primary algorithm | ||
| 603 | my @alg = @{$self->_algorithms}; | ||
| 604 | my $pri = $self->_primary; | ||
| 605 | my $key = $obj->digest($pri)->digest; | ||
| 606 | # create concatenated string of binary digests | ||
| 607 | my $rec = ''; | ||
| 608 | utf8::downgrade($rec); | ||
| 609 | $rec .= join '', map { $obj->digest($_)->digest } grep { $_ ne $pri } @alg; | ||
| 610 | |||
| 611 | # get the rest of the object's contents | ||
| 612 | my @rec = map { | ||
| 613 | $obj->$_ ? $obj->$_->epoch : 0 } qw(ctime mtime ptime dtime); | ||
| 614 | push @rec, $obj->_flags; | ||
| 615 | push @rec, map { $obj->$_ || '' } qw(type language charset encoding); | ||
| 616 | # add them to the record | ||
| 617 | $rec .= pack('NNNNCZ*Z*Z*Z*', @rec); | ||
| 618 | |||
| 619 | #warn length $rec; | ||
| 620 | |||
| 621 | # optionally return the key | ||
| 622 | return wantarray ? ($rec, $key) : $rec; | ||
| 623 | } | ||
| 624 | |||
| 625 | # inflate just the record part; not an instance method | ||
| 626 | sub _inflate_rec { | ||
| 627 | my ($pri, $algos, $digest, $record, $skip) = @_; | ||
| 628 | |||
| 629 | #warn $pri, ' ', join ',', @$algos; | ||
| 630 | |||
| 631 | my (%p, %rec); | ||
| 632 | |||
| 633 | unless ($skip) { | ||
| 634 | %rec = ($pri => URI::ni->from_digest($digest, $pri, undef, 256)); | ||
| 635 | $p{digests} = \%rec; | ||
| 636 | } | ||
| 637 | |||
| 638 | # run the digests off the front | ||
| 639 | for my $algo (grep { $_ ne $pri } @$algos) { | ||
| 640 | unless ($skip) { | ||
| 641 | my $b = substr $record, 0, $DIGESTS{$algo}; | ||
| 642 | $rec{$algo} = URI::ni->from_digest($b, $algo, undef, 256); | ||
| 643 | } | ||
| 644 | |||
| 645 | # set the offset | ||
| 646 | $record = substr($record, $DIGESTS{$algo}); | ||
| 647 | } | ||
| 648 | |||
| 649 | # now unpack the rest of the record | ||
| 650 | @p{qw(ctime mtime ptime dtime flags type language charset encoding)} | ||
| 651 | = unpack('NNNNCZ*Z*Z*Z*', $record); | ||
| 652 | |||
| 653 | # delete the cruft | ||
| 654 | for my $k (qw(dtime type language charset encoding)) { | ||
| 655 | delete $p{$k} unless $p{$k}; | ||
| 656 | } | ||
| 657 | |||
| 658 | wantarray ? %p : \%p; | ||
| 659 | } | ||
| 660 | |||
| 661 | # take a record and return an object | ||
| 662 | sub _inflate { | ||
| 663 | # digest is binary | ||
| 664 | my ($self, $digest, $record, $file) = @_; | ||
| 665 | |||
| 666 | unless ($file) { | ||
| 667 | my $f = $self->_file_for($digest); | ||
| 668 | $file = $f if -f $f; | ||
| 669 | } | ||
| 670 | |||
| 671 | #warn length $record; | ||
| 672 | |||
| 673 | my $pri = $self->_primary; | ||
| 674 | my %rec = ($pri => URI::ni->from_digest($digest, $pri, undef, 256)); | ||
| 675 | |||
| 676 | for my $algo (grep { $_ ne $pri } @{$self->_algorithms}) { | ||
| 677 | #warn $algo; | ||
| 678 | my $b = substr $record, 0, $DIGESTS{$algo}; | ||
| 679 | $rec{$algo} = URI::ni->from_digest($b, $algo, undef, 256); | ||
| 680 | |||
| 681 | # set the offset | ||
| 682 | $record = substr($record, $DIGESTS{$algo}); | ||
| 683 | } | ||
| 684 | #warn unpack "H*", $record; | ||
| 685 | |||
| 686 | my %p; | ||
| 687 | @p{qw(ctime mtime ptime dtime flags type language charset encoding)} | ||
| 688 | = unpack('NNNNCZ*Z*Z*Z*', $record); | ||
| 689 | |||
| 690 | # delete the cruft | ||
| 691 | for my $k (qw(dtime type language charset encoding)) { | ||
| 692 | delete $p{$k} unless $p{$k}; | ||
| 693 | } | ||
| 694 | |||
| 695 | if ($file) { | ||
| 696 | my $stat = $file->stat; | ||
| 697 | $p{size} = $stat->size; | ||
| 698 | $p{content} = sub { $file->openr }; | ||
| 699 | } | ||
| 700 | |||
| 701 | $p{digests} = \%rec; | ||
| 702 | |||
| 703 | return Store::Digest::Object->new(%p); | ||
| 704 | } | ||
| 705 | |||
| 706 | sub add { | ||
| 707 | my ($self, %p) = @_; | ||
| 708 | my $pri = $self->_primary; | ||
| 709 | my $ctl = $self->_control; | ||
| 710 | my $env = $self->_env; | ||
| 711 | |||
| 712 | # open transactions on all databases | ||
| 713 | my $txn = $env->txn_begin or Carp::croak | ||
| 714 | ("Could not open transaction: $BerkeleyDB::Error " . $env->status); | ||
| 715 | $txn->Txn($ctl, values %{$self->_entries}); | ||
| 716 | |||
| 717 | # Step 1: record digests as we read the content handle into a | ||
| 718 | # temporary file | ||
| 719 | |||
| 720 | # To prevent aborted writes, we don't write files directly. We | ||
| 721 | # write tempfiles and then rename them, like rsync does. | ||
| 722 | my $tempdir = $self->dir->subdir(TEMP); | ||
| 723 | my ($tempfh, $tempname) = $tempdir->tempfile; | ||
| 724 | $tempname = Path::Class::File->new($tempname); | ||
| 725 | |||
| 726 | # create Digest objects for each algorithm | ||
| 727 | my %state = map { $_ => Digest->new(uc $_) } @{$self->_algorithms}; | ||
| 728 | |||
| 729 | # remove this; it's confusing | ||
| 730 | my $content = delete $p{content}; | ||
| 731 | |||
| 732 | # XXX make block size tunable? | ||
| 733 | while ($content->read(my $buf, BUFSIZE)) { | ||
| 734 | utf8::downgrade($buf); | ||
| 735 | # as the block is read, pass it into each digest algorithm. | ||
| 736 | for my $k (keys %state) { | ||
| 737 | #warn sprintf '%s %d', $k, length $buf; | ||
| 738 | $state{$k}->add($buf); | ||
| 739 | #warn $state{$k}->clone->hexdigest if $k eq 'md5'; | ||
| 740 | } | ||
| 741 | # and write it back out to the tempfile | ||
| 742 | $tempfh->syswrite($buf); | ||
| 743 | } | ||
| 744 | |||
| 745 | # We will have to nuke this tempfile on our own. | ||
| 746 | $tempfh->close; | ||
| 747 | chmod 0444 & ~$self->umask, $tempname; # XXX tunable perms? | ||
| 748 | |||
| 749 | # Convert these into digests now | ||
| 750 | $p{digests} ||= {}; | ||
| 751 | for my $k (keys %state) { | ||
| 752 | $p{digests}{$k} = URI::ni->from_digest($state{$k}, $k); | ||
| 753 | } | ||
| 754 | |||
| 755 | # Step 2: move the file into position unless there is already a copy | ||
| 756 | |||
| 757 | my $bin = $p{digests}{$pri}->digest; | ||
| 758 | # don't let perl mistake the binary digest for utf8 | ||
| 759 | utf8::downgrade($bin); | ||
| 760 | my $target = $self->_file_for($bin); | ||
| 761 | |||
| 762 | if (-f $target) { | ||
| 763 | unlink $tempname; | ||
| 764 | } | ||
| 765 | else { | ||
| 766 | my $mode = 0777 & ~$self->umask | 02000; | ||
| 767 | my $parent = $target->parent; | ||
| 768 | eval { $parent->mkpath(0, $mode) }; | ||
| 769 | Carp::croak("Can't create container path $parent: $@") if $@; | ||
| 770 | |||
| 771 | # ONLY IF MISSING DO YOU ADD THE FILE | ||
| 772 | File::Copy::move($tempname, $target); | ||
| 773 | } | ||
| 774 | |||
| 775 | # Step 3: alter database entries | ||
| 776 | |||
| 777 | my $db = $self->_entries->{$pri}; | ||
| 778 | my $now = time; | ||
| 779 | |||
| 780 | # if a record is present then we're just replacing the file | ||
| 781 | my $obj; | ||
| 782 | my $rec = ''; | ||
| 783 | utf8::downgrade($rec); | ||
| 784 | $db->db_get($bin, $rec); | ||
| 785 | |||
| 786 | if ($rec) { | ||
| 787 | # create an object because it's easier to deal with | ||
| 788 | $obj = $self->_inflate($bin, $rec); | ||
| 789 | # note this will automatically load the file even if it was | ||
| 790 | # marked deleted, since we already moved it into the tree. | ||
| 791 | |||
| 792 | # if there is a dtime, clear it | ||
| 793 | if ($obj->dtime and $obj->dtime->epoch > 0) { | ||
| 794 | $obj->dtime(undef); | ||
| 795 | |||
| 796 | my $nok = 0; | ||
| 797 | |||
| 798 | # don't forget to decrement the deleted count by 1 | ||
| 799 | $ctl->db_get(deleted => my $deleted); | ||
| 800 | $nok += $ctl->db_put | ||
| 801 | (deleted => ($deleted && $deleted > 0 ? $deleted - 1 : 0)); | ||
| 802 | # and no matter what, we don't want this number to be negative | ||
| 803 | |||
| 804 | # set the modification time as well | ||
| 805 | $nok += $ctl->db_put(mtime => $now); | ||
| 806 | |||
| 807 | if ($nok) { | ||
| 808 | $txn->txn_abort; | ||
| 809 | die 'INTERNAL ERROR: Could not modify control database: ' . | ||
| 810 | $BerkeleyDB::Error; | ||
| 811 | } | ||
| 812 | |||
| 813 | # overwrite the record | ||
| 814 | $rec = $self->_deflate($obj); | ||
| 815 | |||
| 816 | # XXX ???? WTF how many times do i have to re-downgrade | ||
| 817 | utf8::downgrade($rec); | ||
| 818 | |||
| 819 | unless ($db->db_put($bin, $rec) == 0) { | ||
| 820 | die 'INTERNAL ERROR: Could not modify digest database: ' . | ||
| 821 | $BerkeleyDB::Error; | ||
| 822 | } | ||
| 823 | } | ||
| 824 | } | ||
| 825 | else { | ||
| 826 | # tie up loose ends re the file | ||
| 827 | my $stat = $target->stat; | ||
| 828 | $p{size} = $stat->size; | ||
| 829 | $p{content} = $target->openr; | ||
| 830 | |||
| 831 | # get the type by 'magic' if it's missing or incorrect | ||
| 832 | my $type = $self->mimetype($target->stringify); | ||
| 833 | $p{type} = $type | ||
| 834 | unless ($p{type} && $self->mimetype_isa($p{type}, $type)); | ||
| 835 | |||
| 836 | # ctime is required | ||
| 837 | $p{ctime} = $now; | ||
| 838 | |||
| 839 | # now for the control db | ||
| 840 | my $nok = 0; | ||
| 841 | |||
| 842 | # increment byte count by file size | ||
| 843 | $ctl->db_get(bytes => my $bytes); | ||
| 844 | $nok += $ctl->db_put(bytes => ($bytes || 0) + $p{size}); | ||
| 845 | # increment object count by 1 | ||
| 846 | $ctl->db_get(objects => my $objects); | ||
| 847 | $nok += $ctl->db_put(objects => ($objects || 0) + 1); | ||
| 848 | # set the modification time | ||
| 849 | $nok += $ctl->db_put(mtime => $now); | ||
| 850 | |||
| 851 | if ($nok) { | ||
| 852 | $txn->txn_abort; | ||
| 853 | die 'INTERNAL ERROR: Could not modify control database: ' . | ||
| 854 | $BerkeleyDB::Error; | ||
| 855 | } | ||
| 856 | |||
| 857 | # create the new object | ||
| 858 | $obj = Store::Digest::Object->new(%p); | ||
| 859 | |||
| 860 | # overwrite the record | ||
| 861 | $rec = $self->_deflate($obj); | ||
| 862 | |||
| 863 | # XXX ???? WTF | ||
| 864 | utf8::downgrade($rec); | ||
| 865 | |||
| 866 | unless ($db->db_put($bin, $rec) == 0) { | ||
| 867 | die 'INTERNAL ERROR: Could not modify digest database: ' . | ||
| 868 | $BerkeleyDB::Error; | ||
| 869 | } | ||
| 870 | } | ||
| 871 | |||
| 872 | # Step 4: commit the changes | ||
| 873 | |||
| 874 | unless ($txn->txn_commit == 0) { | ||
| 875 | die 'INTERNAL ERROR: Could not commit transaction: ' . | ||
| 876 | $BerkeleyDB::Error; | ||
| 877 | } | ||
| 878 | |||
| 879 | # Step 5: return the object | ||
| 880 | |||
| 881 | return $obj; | ||
| 882 | } | ||
| 883 | |||
| 884 | sub _best_uri { | ||
| 885 | my ($self, $obj) = @_; | ||
| 886 | |||
| 887 | # get the algorithm list from the object | ||
| 888 | my %algo = map { $_ => 1 } $obj->digest; | ||
| 889 | |||
| 890 | # see if the primary is present | ||
| 891 | my $pri = $self->_primary; | ||
| 892 | return $obj->digest($pri) if $algo{$pri}; | ||
| 893 | |||
| 894 | # extract the first ni: URI that matches | ||
| 895 | my $d; | ||
| 896 | for my $a ($self->_algorithms) { | ||
| 897 | # this will attempt to match the primary algorithm a | ||
| 898 | # second time but fuggit | ||
| 899 | if ($algo{$a}) { | ||
| 900 | $d = $obj->digest($a); | ||
| 901 | last; | ||
| 902 | } | ||
| 903 | } | ||
| 904 | return $d; | ||
| 905 | } | ||
| 906 | |||
| 907 | sub _obj_to_digest { | ||
| 908 | my ($self, $obj) = @_; | ||
| 909 | # $digest has been sanitized to be either a ni: URI or a | ||
| 910 | # Store::Digest::Object. | ||
| 911 | return $obj if $obj->isa('URI::ni'); | ||
| 912 | return $self->_best_uri($obj) if $obj->isa('Store::Digest::Object'); | ||
| 913 | } | ||
| 914 | |||
| 915 | sub get { | ||
| 916 | my $self = shift; | ||
| 917 | my $digest = $self->_obj_to_digest(shift) or return; | ||
| 918 | |||
| 919 | # get raw data whatnot | ||
| 920 | my $algo = $digest->algorithm; | ||
| 921 | my $bin = $digest->digest; | ||
| 922 | utf8::downgrade($bin); # JIC | ||
| 923 | |||
| 924 | my @out = $self->_get($bin, $algo) or return; | ||
| 925 | |||
| 926 | wantarray ? @out : $out[0]; | ||
| 927 | } | ||
| 928 | |||
| 929 | sub _get { | ||
| 930 | my ($self, $bin, $algo, $txn) = @_; | ||
| 931 | |||
| 932 | my $txn_in = !!$txn; | ||
| 933 | |||
| 934 | # deal with database mumbo jumbo | ||
| 935 | my $pri = $self->_primary; | ||
| 936 | my $index = $self->_entries->{$algo}; | ||
| 937 | my $primary = $algo eq $pri ? $index : $self->_entries->{$pri}; | ||
| 938 | |||
| 939 | # open a transaction to prevent stuff from changing mid-select | ||
| 940 | $txn = $txn ? $self->_env->txn_begin($txn) : $self->_env->txn_begin | ||
| 941 | or Carp::croak("Failed to create transaction: $BerkeleyDB::Error " | ||
| 942 | . $self->_env->status); | ||
| 943 | $txn->Txn($index, $primary); | ||
| 944 | |||
| 945 | my $cursor = $index->db_cursor | ||
| 946 | or Carp::croak("Failed to get cursor: $BerkeleyDB::Error " | ||
| 947 | . $index->status); | ||
| 948 | |||
| 949 | # the 'last' key is padded with 0xff up to the algo size | ||
| 950 | my $last = $bin . (pack('C', 255) x ($DIGESTS{$algo} - length $bin)); | ||
| 951 | |||
| 952 | # pad the key too | ||
| 953 | $bin .= ("\0" x ($DIGESTS{$algo} - length $bin)); | ||
| 954 | |||
| 955 | # warn $algo; | ||
| 956 | # warn unpack('H*', $bin); | ||
| 957 | # warn unpack('H*', $last); | ||
| 958 | |||
| 959 | my @obj; | ||
| 960 | my $rec; | ||
| 961 | my $flag = DB_SET_RANGE | DB_NEXT | DB_GET_BOTH; | ||
| 962 | my $d = $bin; | ||
| 963 | |||
| 964 | if ($algo eq $pri) { | ||
| 965 | #warn "$algo eq $pri"; | ||
| 966 | while ($cursor->c_get($d, $rec, $flag) == 0) { | ||
| 967 | last if $d gt $last; | ||
| 968 | |||
| 969 | push @obj, $self->_inflate($d, $rec); | ||
| 970 | |||
| 971 | $d = _bin_inc($d, $algo); | ||
| 972 | } | ||
| 973 | } | ||
| 974 | else { | ||
| 975 | #warn "$algo ne $pri"; | ||
| 976 | my $pk; | ||
| 977 | while ($cursor->c_pget($d, $pk, $rec, $flag) == 0) { | ||
| 978 | |||
| 979 | #warn unpack 'H*', $d; | ||
| 980 | #warn unpack 'H*', $pk; | ||
| 981 | |||
| 982 | my $obj = $self->_inflate($pk, $rec); | ||
| 983 | |||
| 984 | # XXX WTF SLEEPYCAT why do i have to do this? | ||
| 985 | # DB_SET_RANGE does not set the key in c_pget so we have | ||
| 986 | # to set the key manually, and this is the easiest way to | ||
| 987 | # do it. | ||
| 988 | $d = $obj->digest($algo)->digest; | ||
| 989 | last if $d gt $last; | ||
| 990 | |||
| 991 | push @obj, $obj; | ||
| 992 | |||
| 993 | $d = _bin_inc($d, $algo); | ||
| 994 | } | ||
| 995 | } | ||
| 996 | |||
| 997 | $cursor->c_close; | ||
| 998 | undef $cursor; | ||
| 999 | |||
| 1000 | $txn->txn_commit; | ||
| 1001 | |||
| 1002 | # i dunno what a better solution to this is | ||
| 1003 | return unless @obj; | ||
| 1004 | wantarray ? @obj : $obj[0]; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | # this creates a binary key which is the "ceiling" of a partial | ||
| 1008 | # lookup | ||
| 1009 | sub _bin_inc { | ||
| 1010 | my ($bin, $algo) = @_; | ||
| 1011 | my $hex = unpack 'H*', $bin; | ||
| 1012 | my $int = Math::BigInt->new("0x$hex") + 1; | ||
| 1013 | |||
| 1014 | # warn "hex in: $hex"; | ||
| 1015 | |||
| 1016 | # give us a hex string sans '0x' | ||
| 1017 | $hex = substr $int->as_hex, 2; | ||
| 1018 | # now zero-pad | ||
| 1019 | $hex = ('0' x ($DIGESTS{$algo} * 2 - length $hex)) . $hex; | ||
| 1020 | |||
| 1021 | # warn "hex out: $hex"; | ||
| 1022 | |||
| 1023 | # and that's it | ||
| 1024 | return pack 'H*', $hex; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | # I want to be able to do partial matches for gets but not for purges. | ||
| 1028 | # There is no reasonable use case for mass deletions based on a | ||
| 1029 | # partial match on a cryptographic digest. | ||
| 1030 | |||
| 1031 | # removes payload | ||
| 1032 | sub remove { | ||
| 1033 | my $self = shift; | ||
| 1034 | my $digest = $self->_obj_to_digest(shift) or return; | ||
| 1035 | # get raw data whatnot | ||
| 1036 | my $algo = $digest->algorithm; | ||
| 1037 | my $bin = $digest->digest; | ||
| 1038 | utf8::downgrade($bin); | ||
| 1039 | |||
| 1040 | my $txn = $self->_env->txn_begin | ||
| 1041 | or Carp::croak("Failed to create transaction: $BerkeleyDB::Error " | ||
| 1042 | . $self->_env->status); | ||
| 1043 | |||
| 1044 | my @obj = $self->_get($bin, $algo, $txn); | ||
| 1045 | |||
| 1046 | $txn->Txn($self->_control, values %{$self->_entries}); | ||
| 1047 | |||
| 1048 | my $pri = $self->_primary; | ||
| 1049 | my $db = $self->_entries->{$pri}; | ||
| 1050 | my $dtime = DateTime->now; | ||
| 1051 | |||
| 1052 | my %ctl = (deleted => 0, bytes => 0); | ||
| 1053 | |||
| 1054 | my (@out, @del); | ||
| 1055 | while (my $obj = shift @obj) { | ||
| 1056 | next if $obj->dtime; | ||
| 1057 | |||
| 1058 | # update deletion time | ||
| 1059 | $obj->dtime($dtime->clone); | ||
| 1060 | |||
| 1061 | my $ni = $obj->digest($pri); | ||
| 1062 | my $bin = $ni->digest; | ||
| 1063 | my $rec = $self->_deflate($obj); | ||
| 1064 | # now for some cargo culting | ||
| 1065 | utf8::downgrade($bin); | ||
| 1066 | utf8::downgrade($rec); | ||
| 1067 | if ($db->db_put($bin, $rec) != 0) { | ||
| 1068 | $txn->txn_abort; | ||
| 1069 | Carp::croak("Could not update $ni: $BerkeleyDB::Error"); | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | push @del, $self->_file_for($bin); | ||
| 1073 | |||
| 1074 | push @out, $obj; | ||
| 1075 | |||
| 1076 | $ctl{deleted}++; | ||
| 1077 | $ctl{bytes} += $obj->size; | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | if (@out) { | ||
| 1081 | my $ctl = $self->_control; | ||
| 1082 | |||
| 1083 | if ($ctl{deleted}) { | ||
| 1084 | $ctl->db_get(deleted => my $del); | ||
| 1085 | $ctl->db_put(deleted => $del + $ctl{deleted}); | ||
| 1086 | } | ||
| 1087 | if ($ctl{bytes}) { | ||
| 1088 | $ctl->db_get(bytes => my $bytes); | ||
| 1089 | $ctl->db_put(bytes => $bytes - $ctl{bytes}); | ||
| 1090 | } | ||
| 1091 | $ctl->db_put(mtime => $dtime->epoch); | ||
| 1092 | } | ||
| 1093 | |||
| 1094 | # now delete the files | ||
| 1095 | map { $_->remove } @del; | ||
| 1096 | |||
| 1097 | $txn->txn_commit; | ||
| 1098 | |||
| 1099 | return unless @obj; | ||
| 1100 | wantarray ? @obj : $obj[0]; | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | # also purges | ||
| 1104 | sub forget { | ||
| 1105 | my $self = shift; | ||
| 1106 | my $digest = $self->_obj_to_digest(shift) or return; | ||
| 1107 | # get raw data whatnot | ||
| 1108 | my $algo = $digest->algorithm; | ||
| 1109 | my $bin = $digest->digest; | ||
| 1110 | utf8::downgrade($bin); | ||
| 1111 | |||
| 1112 | my $txn = $self->_env->txn_begin | ||
| 1113 | or Carp::croak("Failed to create transaction: $BerkeleyDB::Error " | ||
| 1114 | . $self->_env->status); | ||
| 1115 | |||
| 1116 | my @obj = $self->_get($bin, $algo, $txn); | ||
| 1117 | |||
| 1118 | $txn->Txn($self->_control, values %{$self->_entries}); | ||
| 1119 | |||
| 1120 | my $pri = $self->_primary; | ||
| 1121 | my $db = $self->_entries->{$pri}; | ||
| 1122 | my $dtime = DateTime->now; | ||
| 1123 | |||
| 1124 | my %ctl = (deleted => 0, bytes => 0); | ||
| 1125 | |||
| 1126 | my @del; | ||
| 1127 | for my $obj (@obj) { | ||
| 1128 | |||
| 1129 | # add to deleted | ||
| 1130 | if ($obj->dtime) { | ||
| 1131 | $ctl{deleted}++; | ||
| 1132 | } | ||
| 1133 | else { | ||
| 1134 | $ctl{bytes} += $obj->size; | ||
| 1135 | } | ||
| 1136 | |||
| 1137 | $obj->dtime($dtime->clone); | ||
| 1138 | $obj->_content(undef); | ||
| 1139 | |||
| 1140 | my $ni = $obj->digest($pri); | ||
| 1141 | my $bin = $ni->digest; | ||
| 1142 | utf8::downgrade($bin); | ||
| 1143 | |||
| 1144 | # now do the actual removing (secondary entries will also be removed) | ||
| 1145 | $db->db_del($bin); | ||
| 1146 | |||
| 1147 | # queue file for deletion | ||
| 1148 | push @del, $self->_file_for($bin); | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | if (@obj) { | ||
| 1152 | my $ctl = $self->_control; | ||
| 1153 | |||
| 1154 | # decrement object count | ||
| 1155 | $ctl->db_get(objects => my $count); | ||
| 1156 | $ctl->db_put(objects => $count - @obj); | ||
| 1157 | |||
| 1158 | # *decrement* deleted count of any objects previously deleted | ||
| 1159 | if ($ctl{deleted}) { | ||
| 1160 | $ctl->db_get(deleted => my $del); | ||
| 1161 | $ctl->db_put(deleted => $del - $ctl{deleted}); | ||
| 1162 | } | ||
| 1163 | |||
| 1164 | # decrement byte count of any objects stored | ||
| 1165 | if ($ctl{bytes}) { | ||
| 1166 | $ctl->db_get(bytes => my $bytes); | ||
| 1167 | $ctl->db_put(bytes => $bytes - $ctl{bytes}); | ||
| 1168 | } | ||
| 1169 | $ctl->db_put(mtime => $dtime->epoch); | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | # now delete the files | ||
| 1173 | map { $_->remove } @del; | ||
| 1174 | |||
| 1175 | $txn->txn_commit; | ||
| 1176 | |||
| 1177 | return unless @obj; | ||
| 1178 | wantarray ? @obj : $obj[0]; | ||
| 1179 | } | ||
| 1180 | |||
| 1181 | =head2 list | ||
| 1182 | |||
| 1183 | =cut | ||
| 1184 | |||
| 1185 | # not sure i remember what this was supposed to do | ||
| 1186 | sub list { | ||
| 1187 | my $self = shift; | ||
| 1188 | } | ||
| 1189 | |||
| 1190 | =head2 stats | ||
| 1191 | |||
| 1192 | =cut | ||
| 1193 | |||
| 1194 | # usage stats | ||
| 1195 | sub stats { | ||
| 1196 | my $self = shift; | ||
| 1197 | |||
| 1198 | # lol jeez get some self control | ||
| 1199 | my $ctl = $self->_control; | ||
| 1200 | |||
| 1201 | my %x; | ||
| 1202 | for my $k (qw(objects deleted bytes ctime mtime)) { | ||
| 1203 | die "INTERNAL ERROR: Control database has failed to store $k" | ||
| 1204 | unless $ctl->db_get($k, my $val) == 0; | ||
| 1205 | $x{$k} = $val; | ||
| 1206 | } | ||
| 1207 | |||
| 1208 | # fix these into datetime objects | ||
| 1209 | $x{created} = DateTime->from_epoch(epoch => delete $x{ctime}); | ||
| 1210 | $x{modified} = DateTime->from_epoch(epoch => delete $x{mtime}); | ||
| 1211 | |||
| 1212 | Store::Digest::Stats->new(%x); | ||
| 1213 | } | ||
| 1214 | |||
| 1215 | =head1 AUTHOR | ||
| 1216 | |||
| 1217 | Dorian Taylor, C<E<lt>dorian at cpan.orgE<gt>> | ||
| 1218 | |||
| 1219 | =head1 LICENSE AND COPYRIGHT | ||
| 1220 | |||
| 1221 | Copyright 2012-2018 Dorian Taylor. | ||
| 1222 | |||
| 1223 | Licensed under the Apache License, Version 2.0 (the "License"); you | ||
| 1224 | may not use this file except in compliance with the License. You may | ||
| 1225 | obtain a copy of the License at | ||
| 1226 | L<http://www.apache.org/licenses/LICENSE-2.0>. | ||
| 1227 | |||
| 1228 | Unless required by applicable law or agreed to in writing, software | ||
| 1229 | distributed under the License is distributed on an "AS IS" BASIS, | ||
| 1230 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
| 1231 | implied. See the License for the specific language governing | ||
| 1232 | permissions and limitations under the License. | ||
| 1233 | |||
| 1234 | |||
| 1235 | =cut | ||
| 1236 | |||
| 1237 | __PACKAGE__->meta->make_immutable; | ||
| 1238 | |||
| 1239 | 1; # End of Store::Digest::Driver::FileSystem | ||