| 1 | package MYDan::Node::Cache; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | MYDan::Node::Cache - Covert root cluster dbs into cache db | ||
| 6 | |||
| 7 | =head1 SYNOPSIS | ||
| 8 | |||
| 9 | use MYDan::Node::Cache; | ||
| 10 | |||
| 11 | my $cache = MYDan::Node::Cache->new | ||
| 12 | ( | ||
| 13 | root => '/root/data/dir', | ||
| 14 | cache => '/cache/data/dir', | ||
| 15 | ); | ||
| 16 | |||
| 17 | $cache->make(); | ||
| 18 | |||
| 19 | =cut | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | use Carp; | ||
| 24 | use POSIX; | ||
| 25 | use YAML::XS; | ||
| 26 | use Digest::MD5; | ||
| 27 | use File::Spec; | ||
| 28 | use File::Copy; | ||
| 29 | use File::Basename; | ||
| 30 | |||
| 31 | use MYDan::Node::DBI::Root; | ||
| 32 | use MYDan::Node::DBI::Cache; | ||
| 33 | |||
| 34 | |||
| 35 | our %LINK = ( md5 => '.md5', cdb => '.cdb', link => 'current' ); | ||
| 36 | |||
| 37 | sub new | ||
| 38 | { | ||
| 39 | my ( $class, %path ) = splice @_; | ||
| 40 | |||
| 41 | for my $name ( qw( root cache ) ) | ||
| 42 | { | ||
| 43 | confess "undefined $name" unless my $path = $path{$name}; | ||
| 44 | $path = readlink $path if -l $path; | ||
| 45 | |||
| 46 | confess "invalid path $path: not a directory" unless -d $path; | ||
| 47 | $path{$name} = File::Spec->rel2abs( $path ); | ||
| 48 | } | ||
| 49 | |||
| 50 | map { $path{$_} = File::Spec->join( $path{cache}, $LINK{$_} ) } keys %LINK; | ||
| 51 | bless \%path, ref $class || $class; | ||
| 52 | } | ||
| 53 | |||
| 54 | sub make | ||
| 55 | { | ||
| 56 | my $self = shift; | ||
| 57 | my ( $md5, $cdb, $root ) = @$self{ qw( md5 cdb root ) }; | ||
| 58 | |||
| 59 | unlink $md5, $cdb unless -f $md5 && -f $cdb; ## start over if either gone | ||
| 60 | |||
| 61 | my $prev = YAML::XS::LoadFile( $md5 ) if -f $md5; ## load previous md5 | ||
| 62 | $prev = {} unless $prev && ref $prev eq 'HASH'; | ||
| 63 | |||
| 64 | my ( $handle, $diff, %curr ); ## connect to cache db | ||
| 65 | $cdb = MYDan::Node::DBI::Cache->new( $cdb, $MYDan::Node::DBI::Cache::TABLE ); | ||
| 66 | |||
| 67 | for my $cluster ( grep { -f $_ } glob File::Spec->join( $root, '*' ) ) | ||
| 68 | { | ||
| 69 | next unless open( $handle, $cluster ) && binmode $handle; | ||
| 70 | |||
| 71 | my $name = File::Basename::basename( $cluster ); | ||
| 72 | my $prev = $prev->{$name}; | ||
| 73 | |||
| 74 | $curr{$name} = Digest::MD5->new()->addfile( $handle )->hexdigest(); | ||
| 75 | next if $prev && $prev eq $curr{$name}; ## compare md5, skip if same | ||
| 76 | |||
| 77 | $diff = 1; | ||
| 78 | $cdb->delete( name => [ 1, $name ] ); ## delete old | ||
| 79 | $cluster = MYDan::Node::DBI::Root->new( $cluster ); | ||
| 80 | |||
| 81 | for my $attr ( $cluster->table() ) ## insert new | ||
| 82 | { | ||
| 83 | warn "$name: $attr\n"; | ||
| 84 | map { $cdb->insert( $name, $attr, @$_ ) } $cluster->dump( $attr ); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | my @name = keys %curr; | ||
| 89 | $cdb->delete( name => [ 0, @name ] ); ## delete removed clusters | ||
| 90 | |||
| 91 | if ( $diff || keys %$prev != @name ) ## switch symlink to new cache | ||
| 92 | { | ||
| 93 | YAML::XS::DumpFile( $self->{md5}, \%curr ); | ||
| 94 | my $curr = POSIX::strftime( "%Y.%m.%d_%H:%M:%S", localtime ); | ||
| 95 | |||
| 96 | unlink( $curr = File::Spec->join( $self->{cache}, $curr ) ); | ||
| 97 | File::Copy::copy( $self->{cdb}, $curr ) && unlink $self->{link}; | ||
| 98 | symlink File::Basename::basename( $curr ), $self->{link}; | ||
| 99 | } | ||
| 100 | else | ||
| 101 | { | ||
| 102 | warn "no change\n"; | ||
| 103 | } | ||
| 104 | |||
| 105 | return $self; | ||
| 106 | } | ||
| 107 | |||
| 108 | 1; |