| 1 | package LCC::Backend::Storable; | ||
| 2 | |||
| 3 | # Make sure we do everything by the book | ||
| 4 | # Set modules to inherit from | ||
| 5 | # Set version information | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | @LCC::Backend::Storable::ISA = qw(LCC::Backend); | ||
| 9 | $LCC::Backend::Storable::VERSION = '0.02'; | ||
| 10 | |||
| 11 | # Use the modules that we always need | ||
| 12 | |||
| 13 | use Storable (); | ||
| 14 | |||
| 15 | # Default filename | ||
| 16 | |||
| 17 | my $default_file = 'LCC.storable'; | ||
| 18 | |||
| 19 | # Return true value for use | ||
| 20 | |||
| 21 | 1; | ||
| 22 | |||
| 23 | #------------------------------------------------------------------------ | ||
| 24 | |||
| 25 | # The following methods are class methods | ||
| 26 | |||
| 27 | #------------------------------------------------------------------------ | ||
| 28 | |||
| 29 | # IN: 1 instantiated object | ||
| 30 | # 2 (optional) flag, specifying whether to force partial update | ||
| 31 | |||
| 32 | sub partial { | ||
| 33 | |||
| 34 | # Obtain the object | ||
| 35 | # Obtain the source filename | ||
| 36 | # Return now if the file does not exist | ||
| 37 | |||
| 38 | my $self = shift; | ||
| 39 | my $source = $self->{'source'} || $default_file; | ||
| 40 | return $self unless -e $source; | ||
| 41 | |||
| 42 | # Initialize the newly obtained hash reference | ||
| 43 | # If we're storing in a gzipped file | ||
| 44 | # Attempt to open the file through a gzip pipe | ||
| 45 | # Add error if failed | ||
| 46 | # Obtain the hash ref to the hash in the file | ||
| 47 | # Else (just an ordinary file) | ||
| 48 | # Retrieve the hash from the file just like that | ||
| 49 | |||
| 50 | my $hash; | ||
| 51 | if ($source =~ m#\.gz$#) { | ||
| 52 | my $handle = IO::File->new( "gzip --stdout $source |" ); | ||
| 53 | $self->_add_error( "Could not open file '$source' for reading: $!" ) | ||
| 54 | unless $handle; | ||
| 55 | $hash = Storable::fd_retrieve( $handle ); | ||
| 56 | } else { | ||
| 57 | $hash = Storable::retrieve( $source ); | ||
| 58 | } | ||
| 59 | |||
| 60 | # For all of the fields in the hash | ||
| 61 | # Copy from the temp hash to the object | ||
| 62 | # Check whether UNS required full action | ||
| 63 | |||
| 64 | foreach (('old',$self->_additional_fields)) { | ||
| 65 | $self->{$_} = $hash->{$_}; | ||
| 66 | } | ||
| 67 | $self->_check_uns_complete unless shift || ''; | ||
| 68 | } #partial | ||
| 69 | |||
| 70 | #------------------------------------------------------------------------ | ||
| 71 | |||
| 72 | # The following methods change the object | ||
| 73 | |||
| 74 | #------------------------------------------------------------------------ | ||
| 75 | |||
| 76 | # IN: 1 instantiated object | ||
| 77 | |||
| 78 | sub update { | ||
| 79 | |||
| 80 | # Obtain the object | ||
| 81 | # Add error if unclear what kind of update was done | ||
| 82 | # Return now if there is nothing to do | ||
| 83 | |||
| 84 | my $self = shift; | ||
| 85 | $self->_add_error( "Unclear whether 'complete' or 'partial' update" ) | ||
| 86 | unless exists( $self->{'old'} ); | ||
| 87 | return unless exists $self->{'new'}; | ||
| 88 | |||
| 89 | # Create local copy of reference to old hash | ||
| 90 | # Create local copy of reference to new hash | ||
| 91 | # For all of the key => value pairs in the new hash | ||
| 92 | # Save/Overwrite the new value in the old hash | ||
| 93 | |||
| 94 | my $old = $self->{'old'}; | ||
| 95 | my $new = $self->{'new'}; | ||
| 96 | while (my ($key,$value) = each( %{$new} )) { | ||
| 97 | $old->{$key} = $value; | ||
| 98 | } | ||
| 99 | |||
| 100 | # Initialize reference to a new hash | ||
| 101 | # For all of the keys that need to be saved | ||
| 102 | # Copy the value to the temporary hash if there is one | ||
| 103 | |||
| 104 | my $hash = {}; | ||
| 105 | foreach (('old',$self->_additional_fields)) { | ||
| 106 | $hash->{$_} = $self->{$_} if exists $self->{$_}; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Obtain the name of the source file | ||
| 110 | # If we're to store to a gzipped file | ||
| 111 | # Open a pipe to write to a gzipped file | ||
| 112 | # Add error if failed | ||
| 113 | # Store the hash in the gzipped file | ||
| 114 | # Else (a normal file) | ||
| 115 | # Store the file directly | ||
| 116 | |||
| 117 | my $source = $self->{'source'} || $default_file; | ||
| 118 | if ($source =~ m#\.gz$#) { | ||
| 119 | my $handle = IO::File->new( "| gzip --best - >$source.new" ); | ||
| 120 | $self->_add_error( "Could not open file '$source.new' for writing: $!" ) | ||
| 121 | unless $handle; | ||
| 122 | Storable::nstore_fd( $hash,$handle ); | ||
| 123 | } else { | ||
| 124 | Storable::nstore( $hash,"$source.new" ); | ||
| 125 | } | ||
| 126 | |||
| 127 | # Forget about any changes made | ||
| 128 | # Move the current file to "old" file | ||
| 129 | # Move the "new" file to current file | ||
| 130 | |||
| 131 | delete( $self->{'new'} ); | ||
| 132 | rename( $source,"$source.old" ); | ||
| 133 | rename( "$source.new",$source ); | ||
| 134 | } #update | ||
| 135 | |||
| 136 | #------------------------------------------------------------------------ | ||
| 137 | |||
| 138 | # The following subroutines deal with standard Perl features | ||
| 139 | |||
| 140 | #------------------------------------------------------------------------ | ||
| 141 | |||
| 142 | __END__ |