diff --git a/lib/Foswiki/Contrib/DBCacheContrib.pm b/lib/Foswiki/Contrib/DBCacheContrib.pm index c84b934..968fa86 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib.pm @@ -42,7 +42,8 @@ FormQueryPlugin for an example of this. our $VERSION = '$Rev$'; our $RELEASE = '10 Jan 2012'; -our $SHORTDESCRIPTION = 'Reusable code that treats forms as if they were table rows in a database'; +our $SHORTDESCRIPTION = + 'Reusable code that treats forms as if they were table rows in a database'; =begin TML @@ -60,7 +61,7 @@ Construct a new DBCache object. sub new { my ( $class, $web, $cacheName, $standardSchema, $cachePreferences ) = @_; - $cacheName ||= '_DBCache' . ($standardSchema ? '_standard' : ''); + $cacheName ||= '_DBCache' . ( $standardSchema ? '_standard' : '' ); # Backward compatibility unless ( $Foswiki::cfg{DBCacheContrib}{Archivist} ) { @@ -70,12 +71,12 @@ sub new { eval "use $Foswiki::cfg{DBCacheContrib}{Archivist}"; die $@ if ($@); - my $workDir = Foswiki::Func::getWorkArea('DBCacheContrib'); - my $this = bless( + my $workDir = Foswiki::Func::getWorkArea('DBCacheContrib'); + my $this = bless( { - _cache => undef, # pointer to the DB, load on demand - _web => $web, - _cachename => $cacheName, + _cache => undef, # pointer to the DB, load on demand + _web => $web, + _cachename => $cacheName, _standardSchema => $standardSchema, _cachePreferences => $cachePreferences || 0, }, @@ -86,7 +87,7 @@ sub new { # a new DB if required. $this->{archivist} = - $Foswiki::cfg{DBCacheContrib}{Archivist}->new($web.'.'.$cacheName); + $Foswiki::cfg{DBCacheContrib}{Archivist}->new( $web . '.' . $cacheName ); return $this; } @@ -167,24 +168,28 @@ sub toString { sub _loadTopic { my ( $this, $web, $topic ) = @_; - my ($tom, $text) = Foswiki::Func::readTopic( $web, $topic ); + my ( $tom, $text ) = Foswiki::Func::readTopic( $web, $topic ); my $standardSchema = $this->{_standardSchema}; my $meta = $this->{archivist}->newMap(); - $meta->set( 'name', $topic ); + $meta->set( 'name', $topic ); if ($standardSchema) { $meta->set( 'web', $web ); - } else { + } + else { $meta->set( 'topic', $topic ); } + # SMELL: core API my $time; - if ($Foswiki::Plugins::SESSION->can('getApproxRevTime')) { - $time = $Foswiki::Plugins::SESSION->getApproxRevTime($web, $topic); - } else { + if ( $Foswiki::Plugins::SESSION->can('getApproxRevTime') ) { + $time = $Foswiki::Plugins::SESSION->getApproxRevTime( $web, $topic ); + } + else { # This is here for TWiki - $time = $Foswiki::Plugins::SESSION->{store}-> - getTopicLatestRevTime($web, $topic); + $time = + $Foswiki::Plugins::SESSION->{store} + ->getTopicLatestRevTime( $web, $topic ); } $meta->set( '.cache_path', "$web.$topic" ); $meta->set( '.cache_time', $time ); @@ -192,11 +197,13 @@ sub _loadTopic { my $lookup; my $atts; if ($standardSchema) { + # Add a fast lookup table for fields. This must be present # for QueryAcceleratorPlugin $lookup = $this->{archivist}->newMap(); - $meta->set( '.fields', $lookup ); + $meta->set( '.fields', $lookup ); $meta->set( '.form_name', '' ); + # Create an empty array for the attachments. We have to have this # due to a deficiency in the 1.0.5 query algorithm $atts = $this->{archivist}->newArray(); @@ -206,16 +213,17 @@ sub _loadTopic { my $form; my $hash; - if ($hash = $tom->get('FORM')) { + if ( $hash = $tom->get('FORM') ) { my ( $formWeb, $formTopic ) = Foswiki::Func::normalizeWebTopicName( $web, $hash->{name} ); - $formWeb =~ s/\//./g; # normalize the normalization + $formWeb =~ s/\//./g; # normalize the normalization $form = $this->{archivist}->newMap(); if ($standardSchema) { - $form->set( 'name', "$formWeb.$formTopic" ); - $meta->set( 'META:FORM', $form ); + $form->set( 'name', "$formWeb.$formTopic" ); + $meta->set( 'META:FORM', $form ); $meta->set( '.form_name', "$formWeb.$formTopic" ); - } else { + } + else { $form->set( 'name', "$formWeb.$formTopic" ); $form->set( '_up', $meta ); $form->set( '_web', $this->{_cache} ); @@ -227,7 +235,8 @@ sub _loadTopic { if ($standardSchema) { my $parent = $this->{archivist}->newMap( initial => $hash ); $meta->set( 'META:TOPICPARENT', $parent ); - } else { + } + else { $meta->set( 'parent', $hash->{name} ); } } @@ -235,24 +244,26 @@ sub _loadTopic { my $att = $this->{archivist}->newMap( initial => $hash ); if ($standardSchema) { $meta->set( 'META:TOPICINFO', $att ); - } else { + } + else { $att->set( '_up', $meta ); $att->set( '_web', $this->{_cache} ); $meta->set( 'info', $att ); } } - if ( $hash = $tom->get('TOPICMOVED')) { + if ( $hash = $tom->get('TOPICMOVED') ) { my $att = $this->{archivist}->newMap( initial => $hash ); if ($standardSchema) { $meta->set( 'META:TOPICMOVED', $att ); - } else { + } + else { $att->set( '_up', $meta ); $att->set( '_web', $this->{_cache} ); $meta->set( 'moved', $att ); } } my @fields = $tom->find('FIELD'); - if ( scalar(@fields)) { + if ( scalar(@fields) ) { my $fields; if ($standardSchema) { $fields = $this->{archivist}->newArray(); @@ -261,21 +272,22 @@ sub _loadTopic { foreach my $field (@fields) { if ($standardSchema) { my $att = $this->{archivist}->newMap( initial => $field ); - $fields->add( $att ); + $fields->add($att); $lookup->set( $field->{name}, $att ); - } else { + } + else { unless ($form) { - $form = $this->{archivist}->newMap() ; + $form = $this->{archivist}->newMap(); $form->set( '_web', $this->{_cache} ); } $form->set( $field->{name}, $field->{value} ); } } } - my @attachments = $tom->find('FILEATTACHMENT'); + my @attachments = $tom->find('FILEATTACHMENT'); foreach my $attachment (@attachments) { my $att = $this->{archivist}->newMap( initial => $attachment ); - if (!$standardSchema) { + if ( !$standardSchema ) { $att->set( '_up', $meta ); $att->set( '_web', $this->{_cache} ); $atts = $meta->get('attachments'); @@ -289,17 +301,19 @@ sub _loadTopic { my $processedText = ''; if ( $this->can('readTopicLine') ) { - my @lines = split(/\r?\n/, $text); - while (scalar(@lines)) { + my @lines = split( /\r?\n/, $text ); + while ( scalar(@lines) ) { my $line = shift(@lines); $text .= $this->readTopicLine( $topic, $meta, $line, \@lines ); } - } else { + } + else { $processedText = $text; } my $prefsCache; - if ($this->{_cachePreferences}) { + if ( $this->{_cachePreferences} ) { + # Extract and cache all preference settings from the topic $prefsCache = $this->{archivist}->newMap(); $this->_parsePreferences( $processedText, $prefsCache ); @@ -314,7 +328,8 @@ sub _loadTopic { $prefs = $this->{archivist}->newArray(); $meta->set( 'META:PREFERENCE', $prefs ); } - } else { + } + else { $pref->set( '_up', $meta ); $pref->set( '_web', $this->{_cache} ); $prefs = $meta->get('preferences'); @@ -325,10 +340,10 @@ sub _loadTopic { } $prefs->add($pref); if ($prefsCache) { - $this->_addSetting( $prefsCache, - $preference->{type}, - $preference->{name}, - $preference->{value} ); + $this->_addSetting( + $prefsCache, $preference->{type}, + $preference->{name}, $preference->{value} + ); } } @@ -336,30 +351,30 @@ sub _loadTopic { $meta->set( '_sets', $prefsCache ); } $meta->set( 'text', $processedText ); - $meta->set( 'all', $tom->getEmbeddedStoreForm() ) unless $standardSchema; + $meta->set( 'all', $tom->getEmbeddedStoreForm() ) unless $standardSchema; return $meta; } sub _addSetting { - my ($this, $map, $type, $key, $value) = @_; + my ( $this, $map, $type, $key, $value ) = @_; my $submap = $map->fastget($type); unless ($submap) { $submap = $this->{archivist}->newMap(); - $map->set($type, $submap); + $map->set( $type, $submap ); } $submap->set( $key, $value ); } # Parse preference settings out of topic text sub _parsePreferences { - my ($this, $text, $map) = @_; - my ($key, $value, $type ) = ( '', '' ); + my ( $this, $text, $map ) = @_; + my ( $key, $value, $type ) = ( '', '' ); foreach ( split( "\n", $text ) ) { if (m/$Foswiki::regex{setVarRegex}/os) { if ( defined $type ) { - $this->_addSetting($map, $type, $key, $value); + $this->_addSetting( $map, $type, $key, $value ); } $type = $1; $key = $2; @@ -372,13 +387,13 @@ sub _parsePreferences { $value .= "\n" . $_; } else { - $this->_addSetting($map, $type, $key, $value); + $this->_addSetting( $map, $type, $key, $value ); undef $type; } } } - if (defined $type) { - $this->_addSetting($map, $type, $key, $value); + if ( defined $type ) { + $this->_addSetting( $map, $type, $key, $value ); } } @@ -413,9 +428,9 @@ sub onReload { sub _onReload { my $this = shift; - unless ($this->{_standardSchema}) { + unless ( $this->{_standardSchema} ) { foreach my $topic ( $this->{_cache}->getValues() ) { - next unless $topic; # SMELL: why does that sometimes happen? + next unless $topic; # SMELL: why does that sometimes happen? # Fill in parent relations unless ( $topic->FETCH('parent') ) { @@ -490,13 +505,15 @@ sub load { ASSERT( 0, $@ ) if DEBUG; print STDERR "Cache read failed $@...\n" if DEBUG; Foswiki::Func::writeWarning("DBCache: Cache read failed: $@"); - # $this->{_cache} = undef; # SMELL: don't nuke the cache although this object still exists - } elsif ( $readFromFile || $removed ) { + +# $this->{_cache} = undef; # SMELL: don't nuke the cache although this object still exists + } + elsif ( $readFromFile || $removed ) { $this->{archivist}->sync( $this->{_cache} ); } } - #print STDERR "DBCacheContrib: Loaded $readFromCache from cache, $readFromFile from file, $removed removed\n"; +#print STDERR "DBCacheContrib: Loaded $readFromCache from cache, $readFromFile from file, $removed removed\n"; return ( $readFromCache, $readFromFile, $removed ); } @@ -507,19 +524,19 @@ sub loadTopic { #print STDERR "loadTopic($web, $topic)\n"; my $found = 0; - eval { - $found = $this->_updateTopic($web, $topic); - }; + eval { $found = $this->_updateTopic( $web, $topic ); }; if ($@) { ASSERT( 0, $@ ) if DEBUG; print STDERR "Cache read failed $@...\n" if DEBUG; Foswiki::Func::writeWarning("DBCache: Cache read failed: $@"); - # $this->{_cache} = undef; # SMELL: don't nuke the cache although this object still exists + +# $this->{_cache} = undef; # SMELL: don't nuke the cache although this object still exists $found = 0; } - if ( $found ) { + if ($found) { + # refresh relations $this->_onReload( [$topic] ); $this->{archivist}->sync( $this->{_cache} ); @@ -551,14 +568,15 @@ sub _updateTopic { #print STDERR "$web.$topic is not in the cache\n"; # Not in cache - my $exists = Foswiki::Func::topicExists($web,$topic); - if ($exists) { - $topcache = $this->_loadTopic( $web, $topic ); - $this->{_cache}->set( $topic, $topcache ); - } else { + my $exists = Foswiki::Func::topicExists( $web, $topic ); + if ($exists) { + $topcache = $this->_loadTopic( $web, $topic ); + $this->{_cache}->set( $topic, $topcache ); + } + else { $$readInfo[2]++ if $readInfo; $found = 1; - } + } if ($topcache) { $$readInfo[1]++ if $readInfo; $found = 1; @@ -575,14 +593,14 @@ sub _updateCache { my ( $this, $web ) = @_; my @readInfo = ( - 0, # read from cache - 0, # read from file - 0, # removed + 0, # read from cache + 0, # read from file + 0, # removed ); $readInfo[0] = $this->{_cache}->size(); foreach my $cached ( $this->{_cache}->getValues() ) { - next unless $cached; # SMELL: why does that happen sometimes + next unless $cached; # SMELL: why does that happen sometimes $cached->set( '.fresh', 0 ); } @@ -594,21 +612,20 @@ sub _updateCache { # load topics that are missing from the cache foreach my $topic ( Foswiki::Func::getTopicList($web) ) { - if ($this->_updateTopic($web, $topic, \@readInfo)) { + if ( $this->_updateTopic( $web, $topic, \@readInfo ) ) { push( @readTopic, $topic ); } #don't disadvantage users just because the cache is off last if defined( $Foswiki::cfg{DBCacheContrib}{LoadFileLimit} ) - && ( $Foswiki::cfg{DBCacheContrib}{LoadFileLimit} > 0 ) - && ( $readInfo[1] > - $Foswiki::cfg{DBCacheContrib}{LoadFileLimit} ); + && ( $Foswiki::cfg{DBCacheContrib}{LoadFileLimit} > 0 ) + && ( $readInfo[1] > $Foswiki::cfg{DBCacheContrib}{LoadFileLimit} ); } # Find smelly topics in the cache foreach my $cached ( $this->{_cache}->getValues() ) { - next unless $cached; # SMELL: why does that happen sometimes + next unless $cached; # SMELL: why does that happen sometimes if ( $cached->FETCH('.fresh') ) { $cached->remove('.fresh'); } @@ -637,17 +654,21 @@ Check the file time against what is seen on disc. Return 1 if consistent, 0 if i sub uptodate { my ( $path, $time ) = @_; - my ($web, $topic) = split(/\./, $path, 2); - ASSERT($web, $path) if DEBUG; - ASSERT($topic, $path) if DEBUG; + my ( $web, $topic ) = split( /\./, $path, 2 ); + ASSERT( $web, $path ) if DEBUG; + ASSERT( $topic, $path ) if DEBUG; + # SMELL: core API my $fileTime; - if ($Foswiki::Plugins::SESSION->can('getApproxRevTime')) { - $fileTime = $Foswiki::Plugins::SESSION->getApproxRevTime($web, $topic); - } else { + if ( $Foswiki::Plugins::SESSION->can('getApproxRevTime') ) { + $fileTime = + $Foswiki::Plugins::SESSION->getApproxRevTime( $web, $topic ); + } + else { # This is here for TWiki - $fileTime = $Foswiki::Plugins::SESSION->{store}-> - getTopicLatestRevTime($web, $topic); + $fileTime = + $Foswiki::Plugins::SESSION->{store} + ->getTopicLatestRevTime( $web, $topic ); } return ( $fileTime == $time ) ? 1 : 0; } diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB.pm b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB.pm index 45d1820..c52c6c4 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB.pm @@ -25,33 +25,33 @@ package Foswiki::Contrib::DBCacheContrib::Archivist::BDB; use strict; use Foswiki::Contrib::DBCacheContrib::Archivist (); -our @ISA = ( 'Foswiki::Contrib::DBCacheContrib::Archivist' ); +our @ISA = ('Foswiki::Contrib::DBCacheContrib::Archivist'); use Assert; use BerkeleyDB (); -use Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Map (); +use Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Map (); use Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Array (); # Type constants -sub _SCALAR { 0 }; -sub _MAP { 1 }; -sub _ARRAY { 2 }; +sub _SCALAR { 0 } +sub _MAP { 1 } +sub _ARRAY { 2 } sub new { my ( $class, $cacheName ) = @_; - my $workDir = Foswiki::Func::getWorkArea('DBCacheContrib'); + my $workDir = Foswiki::Func::getWorkArea('DBCacheContrib'); $cacheName =~ s/\//\./go; - my $file = $workDir.'/'.$cacheName; + my $file = $workDir . '/' . $cacheName; my $this = bless( {}, $class ); $this->{db} = new BerkeleyDB::Hash( -Flags => BerkeleyDB::DB_CREATE(), -Filename => $file ); - ASSERT(defined $this->{db}, "$file: $! $BerkeleyDB::Error") if DEBUG; + ASSERT( defined $this->{db}, "$file: $! $BerkeleyDB::Error" ) if DEBUG; $this->{stubs} = {}; return $this; } @@ -61,46 +61,45 @@ sub new { # Package private sub db_get { - my ($this, $key) = @_; + my ( $this, $key ) = @_; my $value; - my $status = $this->{db}->db_get($key, $value); + my $status = $this->{db}->db_get( $key, $value ); return $status ? undef : $value; } # Package private sub db_set { - my ($this, $key, $value) = @_; - my $status = $this->{db}->db_put($key, $value); + my ( $this, $key, $value ) = @_; + my $status = $this->{db}->db_put( $key, $value ); return $status; } # Package private sub db_exists { - my ($this, $key) = @_; + my ( $this, $key ) = @_; my $value; - my $status = $this->{db}->db_get($key, $value); + my $status = $this->{db}->db_get( $key, $value ); return $status ? 0 : 1; } # Package private sub db_delete { - my ($this, $key) = @_; + my ( $this, $key ) = @_; my $status = $this->{db}->db_del($key); return $status; } - sub getRoot { my $this = shift; unless ( $this->{root} ) { my $root = $this->db_get('__ROOT__'); - if ( $root ) { - $this->{root} = $this->decode( $root ); + if ($root) { + $this->{root} = $this->decode($root); } else { # The root is always a map $this->{root} = $this->newMap(); - $this->{db}->db_put('__ROOT__', $this->encode($this->{root})) + $this->{db}->db_put( '__ROOT__', $this->encode( $this->{root} ) ); } } return $this->{root}; @@ -165,6 +164,7 @@ sub encode { $value = $value->{id}; } elsif ( ref($value) ) { + # SMELL: could use Data::Dumper or another freezer die 'Unstorable type ' . ref($value); } @@ -191,7 +191,7 @@ sub decode { $this->{stubs}->{$value} ||= $this->newArray( id => $value ); } else { - die 'Corrupt DB; type '.$type; + die 'Corrupt DB; type ' . $type; } $value = $this->{stubs}->{$value}; } @@ -206,12 +206,12 @@ sub allocateID { my $oid = $this->db_get('__ID__') || 0; if ( defined $id ) { if ( $id >= $oid ) { - $this->db_set('__ID__', $id + 1); + $this->db_set( '__ID__', $id + 1 ); } return $id; } else { - $this->db_set('__ID__', $oid + 1); + $this->db_set( '__ID__', $oid + 1 ); return $oid; } } diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Array.pm b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Array.pm index 7794104..b5b50f1 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Array.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Array.pm @@ -4,10 +4,13 @@ package Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Array; use strict; use Foswiki::Contrib::DBCacheContrib::Array (); + # Mixin collections code use Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Collection (); -our @ISA = ( 'Foswiki::Contrib::DBCacheContrib::Array', - 'Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Collection' ); +our @ISA = ( + 'Foswiki::Contrib::DBCacheContrib::Array', + 'Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Collection' +); use Assert; @@ -33,14 +36,14 @@ sub STORE { if ( $index >= $this->FETCHSIZE() ) { $this->STORESIZE( $index + 1 ); } - $this->{archivist}->db_set( - $this->getID($index), $this->{archivist}->encode($value)); + $this->{archivist} + ->db_set( $this->getID($index), $this->{archivist}->encode($value) ); } sub FETCHSIZE { my ($this) = @_; - unless (defined $this->{size}) { - $this->{size} = $this->{archivist}->db_get('S'.$this->{id}) || 0; + unless ( defined $this->{size} ) { + $this->{size} = $this->{archivist}->db_get( 'S' . $this->{id} ) || 0; } return $this->{size}; } @@ -50,7 +53,7 @@ sub STORESIZE { my $sz = $this->FETCHSIZE(); if ( $count > $sz ) { for ( my $i = $sz ; $i < $count ; $i++ ) { - $this->{archivist}->db_set( $this->getID($i), ''); + $this->{archivist}->db_set( $this->getID($i), '' ); } } elsif ( $count < $sz ) { @@ -59,7 +62,7 @@ sub STORESIZE { } } $this->{size} = $count; - $this->{archivist}->db_set( 'S'.$this->{id}, $count ); + $this->{archivist}->db_set( 'S' . $this->{id}, $count ); } sub EXISTS { diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Collection.pm b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Collection.pm index 135a4b8..52189e6 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Collection.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Collection.pm @@ -7,13 +7,13 @@ use Assert; sub getID { my ( $this, $k ) = @_; return $this->{id} unless defined $k; - return $this->{id}."\0$k"; + return $this->{id} . "\0$k"; } sub FETCH { my ( $this, $key ) = @_; - return $this->{archivist}->decode( - $this->{archivist}->db_get( $this->getID($key) )); + return $this->{archivist} + ->decode( $this->{archivist}->db_get( $this->getID($key) ) ); } sub DESTROY { diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Map.pm b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Map.pm index 0e6169b..b1f6a09 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Map.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/BDB/Map.pm @@ -11,10 +11,13 @@ package Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Map; use strict; use Foswiki::Contrib::DBCacheContrib::Map (); + # Mixin collections code use Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Collection (); -our @ISA = ( 'Foswiki::Contrib::DBCacheContrib::Map', - 'Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Collection' ); +our @ISA = ( + 'Foswiki::Contrib::DBCacheContrib::Map', + 'Foswiki::Contrib::DBCacheContrib::Archivist::BDB::Collection' +); use Assert; @@ -23,7 +26,7 @@ sub new { my $class = shift; my %args = @_; my $initial; - if ( $args{initial}) { + if ( $args{initial} ) { # Delay parsing until we are bound $initial = $args{initial}; @@ -41,11 +44,12 @@ sub new { $this->{id} = $this->{archivist}->allocateID(); } if ($initial) { - if (ref($initial)) { - while (my ($k, $v) = each %$initial) { - $this->STORE($k, $v); + if ( ref($initial) ) { + while ( my ( $k, $v ) = each %$initial ) { + $this->STORE( $k, $v ); } - } else { + } + else { $this->parse($initial); } } @@ -58,23 +62,23 @@ sub STORE { my %keys = map { $_ => 1 } $this->getKeys(); unless ( $keys{$key} ) { push( @{ $this->{keys} }, $key ); - $this->{archivist}->db_set( - 'K'.$this->{id}, join( "\0", @{ $this->{keys} } )); + $this->{archivist} + ->db_set( 'K' . $this->{id}, join( "\0", @{ $this->{keys} } ) ); } - $this->{archivist}->db_set($id, $this->{archivist}->encode($value)); + $this->{archivist}->db_set( $id, $this->{archivist}->encode($value) ); } sub FIRSTKEY { my $this = shift; $this->getKeys(); $this->{keyIt} = 0; - return $this->{keys}->[$this->{keyIt}++]; + return $this->{keys}->[ $this->{keyIt}++ ]; } sub NEXTKEY { my ( $this, $lastkey ) = @_; - return unless $this->{keyIt} < scalar(@{$this->{keys}}); - return $this->{keys}->[$this->{keyIt}++]; + return unless $this->{keyIt} < scalar( @{ $this->{keys} } ); + return $this->{keys}->[ $this->{keyIt}++ ]; } sub EXISTS { @@ -88,7 +92,7 @@ sub DELETE { $this->getKeys(); my %keys = map { $_ => 1 } $this->getKeys(); delete( $keys{$key} ); - $this->{archivist}->db_set('K'.$this->{id}, join( "\0", keys %keys )); + $this->{archivist}->db_set( 'K' . $this->{id}, join( "\0", keys %keys ) ); $this->{keys} = undef; } @@ -109,7 +113,7 @@ sub getKeys { unless ( defined $this->{keys} ) { @{ $this->{keys} } = - split( "\0", $this->{archivist}->db_get('K'.$this->{id}) || '' ); + split( "\0", $this->{archivist}->db_get( 'K' . $this->{id} ) || '' ); } return @{ $this->{keys} }; diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/Storable.pm b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/Storable.pm index ec0f224..f640e96 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Archivist/Storable.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Archivist/Storable.pm @@ -5,7 +5,7 @@ package Foswiki::Contrib::DBCacheContrib::Archivist::Storable; use strict; use Foswiki::Contrib::DBCacheContrib::MemArchivist (); -our @ISA = ( 'Foswiki::Contrib::DBCacheContrib::MemArchivist' ); +our @ISA = ('Foswiki::Contrib::DBCacheContrib::MemArchivist'); use Storable; @@ -23,8 +23,8 @@ sub DESTROY { sub sync { my $this = shift; -#my ($package, $filename, $line) = caller(2); -#print STDERR "called sync $this->{_file} from $package, $line\n"; + #my ($package, $filename, $line) = caller(2); + #print STDERR "called sync $this->{_file} from $package, $line\n"; # Clear the archivist to avoid having pointers in the Storable $this->{root}->setArchivist(undef) if $this->{root}; diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Array.pm b/lib/Foswiki/Contrib/DBCacheContrib/Array.pm index 116e0a7..e98a0c1 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Array.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Array.pm @@ -29,10 +29,11 @@ package Foswiki::Contrib::DBCacheContrib::Array; use strict; use Tie::Array (); + # Mixin archivability use Foswiki::Contrib::DBCacheContrib::Archivable (); -our @ISA = ('Tie::Array', 'Foswiki::Contrib::DBCacheContrib::Archivable'); +our @ISA = ( 'Tie::Array', 'Foswiki::Contrib::DBCacheContrib::Archivable' ); use Assert; @@ -271,8 +272,9 @@ sub sum { die "$field has no subfield $subfields" unless ( ref($fieldval) ); $sum += $fieldval->sum($subfields); - } elsif ( $fieldval =~ - m/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/ ) { + } + elsif ( $fieldval =~ m/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?/ ) + { $sum += $fieldval; } } diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Map.pm b/lib/Foswiki/Contrib/DBCacheContrib/Map.pm index c43e781..89055fe 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Map.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Map.pm @@ -44,6 +44,7 @@ package Foswiki::Contrib::DBCacheContrib::Map; use strict; use Tie::Hash (); + # Mixin archivability use Foswiki::Contrib::DBCacheContrib::Archivable; @@ -60,14 +61,15 @@ use Assert; sub new { my $class = shift; - my %args = @_; - my $this = bless( {}, $class ); + my %args = @_; + my $this = bless( {}, $class ); if ( $args{initial} ) { - if (ref($args{initial}) eq 'HASH') { - while (my ($k, $v) = each %{$args{initial}}) { - $this->STORE($k, $v); + if ( ref( $args{initial} ) eq 'HASH' ) { + while ( my ( $k, $v ) = each %{ $args{initial} } ) { + $this->STORE( $k, $v ); } - } else { + } + else { $this->parse( $args{initial} ); } } diff --git a/lib/Foswiki/Contrib/DBCacheContrib/MemArchivist.pm b/lib/Foswiki/Contrib/DBCacheContrib/MemArchivist.pm index 9cba685..a569a1f 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/MemArchivist.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/MemArchivist.pm @@ -8,19 +8,19 @@ use strict; use Foswiki::Contrib::DBCacheContrib::Archivist (); -our @ISA = ( 'Foswiki::Contrib::DBCacheContrib::Archivist' ); +our @ISA = ('Foswiki::Contrib::DBCacheContrib::Archivist'); use Assert; -use Foswiki::Contrib::DBCacheContrib::MemMap (); +use Foswiki::Contrib::DBCacheContrib::MemMap (); use Foswiki::Contrib::DBCacheContrib::MemArray (); sub new { my ( $class, $cacheName ) = @_; - my $workDir = Foswiki::Func::getWorkArea('DBCacheContrib'); + my $workDir = Foswiki::Func::getWorkArea('DBCacheContrib'); $cacheName =~ s/\//\./go; - my $this = bless( { _file => $workDir.'/'.$cacheName }, $class ); + my $this = bless( { _file => $workDir . '/' . $cacheName }, $class ); return $this; } @@ -28,14 +28,15 @@ sub new { sub newMap { my $this = shift; return new Foswiki::Contrib::DBCacheContrib::MemMap( - archivist => $this, @_ ); + archivist => $this, + @_ + ); } # Factory for new Array objects sub newArray { my ($this) = @_; - return new Foswiki::Contrib::DBCacheContrib::MemArray( - archivist => $this ); + return new Foswiki::Contrib::DBCacheContrib::MemArray( archivist => $this ); } # Subclasses must provide getRoot and clear. diff --git a/lib/Foswiki/Contrib/DBCacheContrib/MemArray.pm b/lib/Foswiki/Contrib/DBCacheContrib/MemArray.pm index 2f6a165..e681ce7 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/MemArray.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/MemArray.pm @@ -4,7 +4,7 @@ package Foswiki::Contrib::DBCacheContrib::MemArray; use strict; use Foswiki::Contrib::DBCacheContrib::Array (); -our @ISA = ( 'Foswiki::Contrib::DBCacheContrib::Array' ); +our @ISA = ('Foswiki::Contrib::DBCacheContrib::Array'); use Assert; diff --git a/lib/Foswiki/Contrib/DBCacheContrib/MemMap.pm b/lib/Foswiki/Contrib/DBCacheContrib/MemMap.pm index ebc77ba..6524bc5 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/MemMap.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/MemMap.pm @@ -2,7 +2,7 @@ package Foswiki::Contrib::DBCacheContrib::MemMap; use strict; use Foswiki::Contrib::DBCacheContrib::Map (); -our @ISA = ( 'Foswiki::Contrib::DBCacheContrib::Map' ); +our @ISA = ('Foswiki::Contrib::DBCacheContrib::Map'); use Assert; @@ -20,10 +20,10 @@ sub DESTROY { map { $_->DESTROY() if $_ - && $_ ne $this - && ref($_) - && !Scalar::Util::isweak($_) - && UNIVERSAL::can( $_, 'DESTROY' ); + && $_ ne $this + && ref($_) + && !Scalar::Util::isweak($_) + && UNIVERSAL::can( $_, 'DESTROY' ); } values %{ $this->{keys} }; $this->{keys} = undef; @@ -33,7 +33,7 @@ sub STORE { my ( $this, $key, $value ) = @_; $this->{keys}{$key} = $value; Scalar::Util::weaken( $this->{keys}{$key} ) - if ( ref($value) && $key =~ /^_/ ); + if ( ref($value) && $key =~ /^_/ ); } sub FETCH { diff --git a/lib/Foswiki/Contrib/DBCacheContrib/Search.pm b/lib/Foswiki/Contrib/DBCacheContrib/Search.pm index 526d15a..a06dd97 100644 --- a/lib/Foswiki/Contrib/DBCacheContrib/Search.pm +++ b/lib/Foswiki/Contrib/DBCacheContrib/Search.pm @@ -278,7 +278,7 @@ sub OP_lc { my $rval = $r->matches($map); return undef unless defined $rval; - return lc( $rval ); + return lc($rval); } sub OP_uc { @@ -289,7 +289,7 @@ sub OP_uc { my $rval = $r->matches($map); return undef unless defined $rval; - return uc( $rval ); + return uc($rval); } sub OP_i2d { @@ -356,7 +356,7 @@ sub OP_node { my $form = $map->get("form"); my $val; $form = $map->get($form) if $form; - $val = $form->get($r) if $form; + $val = $form->get($r) if $form; $val = $map->get($r) unless defined $val; return $val; @@ -432,7 +432,7 @@ sub OP_not_equal { sub OP_match { my ( $r, $l, $map ) = @_; - + return undef unless defined $l; my $lval = $l->matches($map); diff --git a/lib/Foswiki/Contrib/DBCacheContrib/build.pl b/lib/Foswiki/Contrib/DBCacheContrib/build.pl index 62fb7b1..7463a96 100755 --- a/lib/Foswiki/Contrib/DBCacheContrib/build.pl +++ b/lib/Foswiki/Contrib/DBCacheContrib/build.pl @@ -2,22 +2,22 @@ # Standard preamble BEGIN { - foreach my $pc (split(/:/, $ENV{FOSWIKI_LIBS})) { - unshift @INC, $pc; - } + foreach my $pc ( split( /:/, $ENV{FOSWIKI_LIBS} ) ) { + unshift @INC, $pc; + } } use Foswiki::Contrib::Build; package DBCacheBuild; -@DBCacheBuild::ISA = ( "Foswiki::Contrib::Build" ); +@DBCacheBuild::ISA = ("Foswiki::Contrib::Build"); sub new { - my $class = shift; - return bless( $class->SUPER::new( "DBCacheContrib" ), $class ); + my $class = shift; + return bless( $class->SUPER::new("DBCacheContrib"), $class ); } $build = new DBCacheBuild(); -$build->build($build->{target}); +$build->build( $build->{target} ); diff --git a/test/unit/DBCacheContrib/DBCacheContribSuite.pm b/test/unit/DBCacheContrib/DBCacheContribSuite.pm index 2ce389c..f1d7e47 100644 --- a/test/unit/DBCacheContrib/DBCacheContribSuite.pm +++ b/test/unit/DBCacheContrib/DBCacheContribSuite.pm @@ -5,8 +5,9 @@ our @ISA = qw( Unit::TestSuite ); sub name { 'DBCacheContrib' } sub include_tests { - #qw(ArrayTest MapTest SearchTest DBCacheTest); - qw(SearchTest); + + #qw(ArrayTest MapTest SearchTest DBCacheTest); + qw(SearchTest); } 1; diff --git a/test/unit/DBCacheContrib/DBCacheContribTestCase.pm b/test/unit/DBCacheContrib/DBCacheContribTestCase.pm index 91e3e32..a1db90a 100644 --- a/test/unit/DBCacheContrib/DBCacheContribTestCase.pm +++ b/test/unit/DBCacheContrib/DBCacheContribTestCase.pm @@ -25,14 +25,14 @@ sub tear_down { $this->SUPER::tear_down(); - unlink($this->{tempfn}) if $this->{tempfn}; + unlink( $this->{tempfn} ) if $this->{tempfn}; } sub setArchivist { my ( $this, $archivist ) = @_; $this->{archivist} = $archivist; - $this->{tempfn} = File::Temp::tmpnam(); - $this->{ar} = $archivist->new( $this->{tempfn} ); + $this->{tempfn} = File::Temp::tmpnam(); + $this->{ar} = $archivist->new( $this->{tempfn} ); } sub StorableArchivist { @@ -67,10 +67,13 @@ sub BDBArchivist { } sub fixture_groups { - return ( [ - 'StorableArchivist', - #'BDBArchivist' - ] ); + return ( + [ + 'StorableArchivist', + + #'BDBArchivist' + ] + ); } 1; diff --git a/test/unit/DBCacheContrib/DBCacheTest.pm b/test/unit/DBCacheContrib/DBCacheTest.pm index 1b97788..11cc46a 100644 --- a/test/unit/DBCacheContrib/DBCacheTest.pm +++ b/test/unit/DBCacheContrib/DBCacheTest.pm @@ -123,29 +123,29 @@ sub set_up { $meta->putKeyed( 'PREFERENCE', { - type => 'Set', - name => "SHIRT", - value => "purple", + type => 'Set', + name => "SHIRT", + value => "purple", } ); $meta->putKeyed( 'PREFERENCE', { - type => 'Local', - name => "TROUSERS", - value => "yellow", + type => 'Local', + name => "TROUSERS", + value => "yellow", } ); - Foswiki::Func::saveTopic( $this->{test_web}, "FormTest", $meta, - $baseText ); + Foswiki::Func::saveTopic( $this->{test_web}, "FormTest", $meta, $baseText ); $this->{test_meta} = $meta; } sub verify_loadSimple { my $this = shift; - my $db = new Foswiki::Contrib::DBCacheContrib( $this->{test_web}, undef, 0, 1 ); + my $db = + new Foswiki::Contrib::DBCacheContrib( $this->{test_web}, undef, 0, 1 ); my @res = $db->load(); $this->assert_str_equals( "0 3 0", join( ' ', @res ) ); my $topic = $db->cache->get("WebPreferences"); @@ -169,8 +169,8 @@ sub verify_loadSimple { $this->assert_str_equals( "1.1", $info->get("format") ); $this->assert_str_equals( "1.1", $info->get("version") ); $this->assert_str_equals( "WebHome", $topic->get("parent") ); - $this->assert_str_equals( $baseText, $topic->get("text") ); - $this->assert_str_equals( "ThisForm", $topic->get("form") ); + $this->assert_str_equals( $baseText, $topic->get("text") ); + $this->assert_str_equals( "ThisForm", $topic->get("form") ); my $form = $topic->get("ThisForm"); $this->assert_not_null($form); $this->assert( $topic->equals( $form->get("_up") ) ); @@ -226,13 +226,13 @@ sub verify_loadSimple { my $sets = $topic->get('_sets'); $this->assert_not_null($sets); - my ($set, $local) = ($sets->get('Set'), $sets->get('Local')); + my ( $set, $local ) = ( $sets->get('Set'), $sets->get('Local') ); $this->assert_not_null($set); $this->assert_not_null($local); - $this->assert_equals('beehive', $set->get('HAIR')); - $this->assert_equals('yellow', $local->get('TROUSERS')); - $this->assert_equals('purple', $set->get('SHIRT')); - $this->assert_equals('goatee', $local->get('BEARD')); + $this->assert_equals( 'beehive', $set->get('HAIR') ); + $this->assert_equals( 'yellow', $local->get('TROUSERS') ); + $this->assert_equals( 'purple', $set->get('SHIRT') ); + $this->assert_equals( 'goatee', $local->get('BEARD') ); $db = undef; $atts = undef; $moved = undef; diff --git a/test/unit/DBCacheContrib/SearchTest.pm b/test/unit/DBCacheContrib/SearchTest.pm index 1489f05..318610b 100644 --- a/test/unit/DBCacheContrib/SearchTest.pm +++ b/test/unit/DBCacheContrib/SearchTest.pm @@ -35,7 +35,6 @@ sub check { . $this->{map}->toString() ); } - sub verify_empty { my $this = shift; $this->usual(); @@ -44,7 +43,6 @@ sub verify_empty { $this->assert_equals( 1, $search->matches( $this->{map} ) ); } - sub verify_badparse1 { my $this = shift; $this->usual(); @@ -454,7 +452,6 @@ sub verify_and7 { $this->check( "fumber AND string", 0 ); } - sub verify_andor1 { my $this = shift; $this->usual(); @@ -527,7 +524,6 @@ sub verify_or8 { $this->check( "number OR fring", 1 ); } - sub conjoin { my ( $this, $last, $A, $B, $a, $b, $c, $r ) = @_; @@ -629,7 +625,6 @@ sub verify_caseops8 { $this->check( "uc(bing) = 'string'", 0 ); } - sub verify_contains { my $this = shift; $this->usual(); @@ -642,7 +637,8 @@ sub verify_d2n1 { my $this = shift; $this->usual(); my $now = time; - $this->check( "d2n '" . Foswiki::Time::formatTime( $now, '$iso' ) . "'", $now ); + $this->check( "d2n '" . Foswiki::Time::formatTime( $now, '$iso' ) . "'", + $now ); } sub verify_d2n2 { @@ -683,182 +679,181 @@ sub verify_length4 { sub verify_equal1 { my $this = shift; $this->usual(); - $this->check( "'string' = 'string'", 1); + $this->check( "'string' = 'string'", 1 ); } sub verify_equal2 { my $this = shift; $this->usual(); - $this->check( "99 = 99", 1); + $this->check( "99 = 99", 1 ); } sub verify_equal3 { my $this = shift; $this->usual(); - $this->check( "'string' = 'strong'", 0); + $this->check( "'string' = 'strong'", 0 ); } sub verify_equal4 { my $this = shift; $this->usual(); - $this->check( "99 = 98", 0); + $this->check( "99 = 98", 0 ); } sub verify_equal5 { my $this = shift; $this->usual(); - $this->check(" number = 98", 0); + $this->check( " number = 98", 0 ); } sub verify_equal6 { my $this = shift; $this->usual(); - $this->check(" bumber = 99", 0); + $this->check( " bumber = 99", 0 ); } sub verify_equal7 { my $this = shift; $this->usual(); - $this->check(" bing = bong", 1); + $this->check( " bing = bong", 1 ); } sub verify_notequal1 { my $this = shift; $this->usual(); - $this->check( "'string' != 'string'", 0); + $this->check( "'string' != 'string'", 0 ); } sub verify_notequal2 { my $this = shift; $this->usual(); - $this->check( "99 != 99", 0); + $this->check( "99 != 99", 0 ); } sub verify_notequal3 { my $this = shift; $this->usual(); - $this->check( "'string' != 'strong'", 1); + $this->check( "'string' != 'strong'", 1 ); } sub verify_notequal4 { my $this = shift; $this->usual(); - $this->check( "99 != 98", 1); + $this->check( "99 != 98", 1 ); } sub verify_notequal5 { my $this = shift; $this->usual(); - $this->check(" number != 98", 1); + $this->check( " number != 98", 1 ); } sub verify_notequal6 { my $this = shift; $this->usual(); - $this->check(" bumber != 99", 1); + $this->check( " bumber != 99", 1 ); } sub verify_notequal7 { my $this = shift; $this->usual(); - $this->check(" number != bumber", 1); + $this->check( " number != bumber", 1 ); } sub verify_notequal8 { my $this = shift; $this->usual(); - $this->check(" 99 != bumber", 1); + $this->check( " 99 != bumber", 1 ); } sub verify_notequal9 { my $this = shift; $this->usual(); - $this->check(" bing != bing", 0); + $this->check( " bing != bing", 0 ); } sub verify_match1 { my $this = shift; $this->usual(); - $this->check(" 'foo' =~ 'f'", 1); + $this->check( " 'foo' =~ 'f'", 1 ); } sub verify_match2 { my $this = shift; $this->usual(); - $this->check(" 'foo' =~ 'o'", 1); + $this->check( " 'foo' =~ 'o'", 1 ); } sub verify_match3 { my $this = shift; $this->usual(); - $this->check(" 'foo' =~ 'f.*'", 1); + $this->check( " 'foo' =~ 'f.*'", 1 ); } sub verify_match4 { my $this = shift; $this->usual(); - $this->check(" 'foo' =~ '.*o+'", 1); + $this->check( " 'foo' =~ '.*o+'", 1 ); } sub verify_match5 { my $this = shift; $this->usual(); - $this->check(" 'foo' =~ 'bar'", 0); + $this->check( " 'foo' =~ 'bar'", 0 ); } sub verify_match6 { my $this = shift; $this->usual(); - $this->check(" string =~ 'ring'", 1); + $this->check( " string =~ 'ring'", 1 ); } sub verify_match7 { my $this = shift; $this->usual(); - $this->check(" string =~ 'rang'", 0); + $this->check( " string =~ 'rang'", 0 ); } sub verify_match8 { my $this = shift; $this->usual(); - $this->check(" string =~ bang", 0); + $this->check( " string =~ bang", 0 ); } sub verify_match9 { my $this = shift; $this->usual(); - $this->check(" foo =~ string", 0); + $this->check( " foo =~ string", 0 ); } - sub verify_defined1 { my $this = shift; $this->usual(); - $this->check("defined string", 1); + $this->check( "defined string", 1 ); } sub verify_defined2 { my $this = shift; $this->usual(); - $this->check("defined 'string'", 1); + $this->check( "defined 'string'", 1 ); } sub verify_defined3 { my $this = shift; $this->usual(); - $this->check("defined 99", 1); + $this->check( "defined 99", 1 ); } sub verify_defined4 { my $this = shift; $this->usual(); - $this->check("defined bing", 0); + $this->check( "defined bing", 0 ); } sub verify_defined5 { my $this = shift; $this->usual(); - $this->check("!defined(bing)", 1); + $this->check( "!defined(bing)", 1 ); } 1;