SPOPS/GDBM.pm untested

filter: Statementstmt 0.0% 0 / 167 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 84 MC/DCmcdc 0.0% 0 / 54 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 12 totaltotal 0.0% 0 / 392 CC 28 SCAR 37.1
CC
28
Cov
0%
global_datasource_handle
132.0
initialize
30.0
fetch
30.0
CRAP 40.7
1 package SPOPS::GDBM;
2  
3 # $Id: GDBM.pm,v 3.4 2004/06/02 00:48:21 lachoy Exp $
4  
5 use strict;
6 use base qw( SPOPS );
7 use Log::Log4perl qw( get_logger );
8 use Data::Dumper qw( Dumper );
9 use GDBM_File;
10 use SPOPS;
11 use SPOPS::Exception qw( spops_error );
12  
13 my $log = get_logger();
14  
15 $SPOPS::GDBM::VERSION = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/);
16  
17 # Make this the default for everyone -- they can override it
18 # themselves...
19  
20 sub class_initialize {
21 my ( $class, $CONFIG ) = @_;
22 $class->_class_initialize( $class->CONFIG ); # allow subclasses to do their own thing
23  
24 # Turn off warnings in this block so we don't get the 'subroutine
25 # id redefined' message (yes, we know what we're doing)
26  
27 {
28 no strict 'refs';
29 local $^W = 0;
30 *{ $class . '::id' } = \&id;
31 }
32 return 1;
33 }
34  
35 # Dummy for subclasses to override
36  
37 sub _class_initialize { return 1 }
38  
39 # Override the default SPOPS initialize call so we can use mixed-case
40 # fields
41  
42 sub initialize {
43 my ( $self, $p ) = @_;
44 return unless ( ref $p and scalar keys %{ $p } );
45  
46 # Set the GDBM filename if it was passed
47  
48 if ( $p->{GDBM_FILENAME} ) {
49 $self->{tmp_gdbm_filename} = $p->{GDBM_FILENAME};
50 delete $p->{GDBM_FILENAME};
51 }
52  
53 # We allow the user to substitute id => value instead for the
54 # specific fieldname.
55  
56 if ( my $id = $p->{id} ) {
57 $p->{ $self->id_field } ||= $id;
58 delete $p->{id};
59 }
60  
61 # Go through the field list and set any that are passed in
62  
63 foreach my $field ( @{ $self->field_list } ) {
64 next unless ( $p->{ $field } );
65 $self->{ $field } = $p->{ $field };
66 $log->is_debug &&
67 $log->debug( "Initialized [$field] to [$self->{ $field }]" );
68 }
69 return $self;
70 }
71  
72 # Override this to get the db handle from somewhere else, if necessary
73  
74 sub global_gdbm_tie {
75 my $item = shift;
76 return $item->global_datasource_handle( @_ );
77 }
78  
79 sub global_datasource_handle {
80 my ( $item, $p ) = @_;
81 return $p->{db} if ( ref $p->{db} );
82  
83 my $gdbm_filename = $p->{filename};
84 unless ( $gdbm_filename ) {
85 if ( ref $item ) {
86 $gdbm_filename = $item->{tmp_gdbm_filename};
87 }
88 if ( $item->CONFIG->{gdbm_info}{file_fragment} and $p->{directory} ) {
89 $log->is_info &&
90 $log->info( "Found file fragent and directory" );
91 $gdbm_filename ||= join( '/', $p->{directory},
92 $item->CONFIG->{gdbm_info}{file_fragment} );
93 }
94 $gdbm_filename ||= $item->CONFIG->{gdbm_info}{filename};
95 $gdbm_filename ||= $item->global_config->{gdbm_info}{filename};
96 }
97 $log->is_info &&
98 $log->info( "Trying file ($gdbm_filename) to connect" );
99 unless ( $gdbm_filename ) {
100 spops_error "Insufficient/incorrect information to tie to ",
101 "GDBM file [$gdbm_filename]";
102 }
103  
104 $log->is_debug &&
105 $log->debug( "Beginning perm: ", defined( $p->{perm} ) ? $p->{perm} : '' );
106 $p->{perm} = 'create' unless ( -e $gdbm_filename );
107 $p->{perm} ||= 'read';
108 $log->is_debug &&
109 $log->debug( "Final perm: $p->{perm}" );
110  
111 my $perm = GDBM_File::GDBM_READER;
112 $perm = GDBM_File::GDBM_WRITER if ( $p->{perm} eq 'write' );
113 $perm = GDBM_File::GDBM_WRCREAT if ( $p->{perm} eq 'create' );
114 $log->is_info &&
115 $log->info( "Trying to use perm ($perm) to connect" );
116 my %db = ();
117 tie( %db, 'GDBM_File', $gdbm_filename, $perm, 0666 );
118 if ( $p->{perm} eq 'create' && ! -w $gdbm_filename ) {
119 spops_error "Failed to create GDBM file! [$gdbm_filename]";
120 }
121  
122 return \%db;
123 }
124  
125 # Override the SPOPS method for finding ID values
126  
127 sub id {
128 my ( $self ) = @_;
129 if ( my $id_field = $self->id_field ) {
130 return $self->{ $id_field };
131 }
132 return $self->CONFIG->{create_id}->( $self );
133 }
134  
135  
136 sub object_key {
137 my ( $self, $id ) = @_;
138 $id ||= $self->id if ( ref $self );
139 unless ( $id ) {
140 spops_error "Cannot create object key without object or id!";
141 }
142 my $class = ref $self || $self;
143 return join( '--', $class, $id );
144 }
145  
146  
147 # Given a key, return the data structure from the db file
148  
149 sub _return_structure_for_key {
150 my ( $class, $key, $p ) = @_;
151 my $db = $class->global_datasource_handle( $p );
152 my $item_info = $db->{ $key };
153 return undef unless ( $item_info );
154 my $data = undef;
155 {
156 no strict 'vars';
157 $data = eval $item_info;
158 }
159 if ( $@ ) {
160 spops_error "Cannot rebuild object! Error: $@";
161 }
162 return $data;
163 }
164  
165  
166 # Retreive an object
167  
168 sub fetch {
169 my ( $class, $id, $p ) = @_;
170 $log->is_debug &&
171 $log->debug( "Trying to fetch ID ($id)" );
172 my $data = $p->{data} || {};
173 unless ( scalar keys %{ $data } ) {
174 return undef unless ( $id and $id !~ /^tmp/ );
175 return undef unless ( $class->pre_fetch_action( { id => $id } ) );
176 $data = $class->_return_structure_for_key( $class->object_key( $id ),
177 { filename => $p->{filename},
178 directory => $p->{directory} } );
179 $log->is_debug &&
180 $log->debug( "Returned data from GDBM: ", Dumper( $data ) );
181 }
182 my $obj = $class->new({ %{ $data }, skip_default_values => 1 });
183 $obj->clear_change;
184 return undef unless ( $class->post_fetch_action );
185 return $obj;
186 }
187  
188 # Return all objects in a particular class
189  
190 sub fetch_group {
191 my ( $item, $p ) = @_;
192 my $db = $item->global_datasource_handle( $p );
193 my $class = ref $item || $item;
194 $log->is_info &&
195 $log->info( "Trying to find keys beginning with ($class)" );
196 my @object_keys = grep /^$class/, keys %{ $db };
197 $log->is_debug &&
198 $log->debug( "Keys found in DB: ", join( ", ", @object_keys ) );
199 my @objects = ();
200 foreach my $key ( @object_keys ) {
201 my $data = eval { $class->_return_structure_for_key( $key, { db => $db } ) };
202 next unless ( $data );
203 push @objects, $class->fetch( undef, { data => $data } );
204 }
205 return \@objects;
206 }
207  
208 # Save (either insert or update) an item in the db
209  
210 sub save {
211 my ( $self, $p ) = @_;
212 $p->{perm} ||= 'write';
213 $log->is_info &&
214 $log->info( "Trying to save a <<", ref $self, ">>" );
215 my $id = $self->id;
216 my $is_add = ( $p->{is_add} or ! $id or $id =~ /^tmp/ );
217 unless ( $is_add or $self->changed ) {
218 $log->is_info &&
219 $log->info( "This object exists and has not changed. Exiting." );
220 return $id;
221 }
222 return undef unless ( $self->pre_save_action( { is_add => $is_add } ) );
223  
224 # Build the data and dump to string
225  
226 my %data = %{ $self };
227 local $Data::Dumper::Indent = 0;
228 my $obj_string = Data::Dumper->Dump( [ \%data ], [ 'data' ] );
229  
230 # Save to DB
231  
232 my $obj_index = $self->object_key;
233 my $db = $self->global_datasource_handle( $p );
234 $db->{ $obj_index } = $obj_string;
235  
236 return undef unless ( $self->post_save_action( { is_add => $is_add } ) );
237 $self->clear_change;
238 return $self;
239 }
240  
241 # Remove an item from the db
242  
243 sub remove {
244 my ( $self, $p ) = @_;
245 my $obj_index = $self->object_key;
246 my $db = $self->global_datasource_handle({ perm => 'write', %{ $p } });
247 $self->clear_change;
248 $self->clear_save;
249 return delete $db->{ $obj_index };
250 }
251  
252 1;
253  
254 __END__