PObject/Driver/DBM.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 11 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 141 CC 9 SCAR 25.6
CC
9
Cov
0%
load_ids
30.0
_lock
20.0
save
6.0
CRAP 13.0
1 package Class::PObject::Driver::DBM;
2  
3 # DBM.pm,v 1.8 2003/09/09 00:11:54 sherzodr Exp
4  
5 use strict;
6 #use diagnostics;
7 use Carp;
8 use Class::PObject::Driver;
9 use File::Spec;
10 use Fcntl (':DEFAULT', ':flock');
11 use vars ('$VERSION', '@ISA', '$lock');
12  
13 @ISA = ('Class::PObject::Driver');
14  
15 $VERSION = '2.00';
16  
17  
18 sub save {
19 my ($self, $object_name, $properties, $columns) = @_;
20
21 my (undef, $dbh, $unlock) = $self->dbh($object_name, $properties, 'w') or return undef;
22 unless ( $columns->{id} ) {
23 my $lastid = $dbh->{_lastid} || 0;
24 $columns->{id} = ++$dbh->{_lastid}
25 }
26 $dbh->{ "!ID:" . $columns->{id} } = $self->freeze($object_name, $properties, $columns);
27 $unlock->();
28 return $columns->{id}
29 }
30  
31  
32  
33 sub load_ids {
34 my ($self, $object_name, $properties, $terms, $args) = @_;
35  
36 my (undef, $dbh, $unlock) = $self->dbh($object_name, $properties) or return undef;
37 my @data_set = ();
38 my $n = 0;
39 while ( my ($k, $v) = each %$dbh ) {
40 if ( $args && $args->{limit} && !$args->{offset} && !$args->{sort} ) {
41 $n++ == $args->{limit} and last
42 }
43 $k =~ /!ID:/ or next;
44 my $data = $self->thaw( $object_name, $properties, $v );
45 if ( $self->_matches_terms($data, $terms) ) {
46 push @data_set, keys %$args ? $data : $data->{id}
47 }
48 }
49 $unlock->();
50 unless ( keys %$args ) {
51 return \@data_set
52 }
53 my $data = $self->_filter_by_args(\@data_set, $args);
54 return [ map { $_->{id} } @$data ]
55 }
56  
57  
58  
59  
60  
61  
62 sub load {
63 my ($self, $object_name, $props, $id) = @_;
64  
65 my (undef, $dbh, $unlock) = $self->dbh($object_name, $props) or return undef;
66 return $self->thaw($object_name, $props, $dbh->{ "!ID:" . $id })
67 }
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86 sub remove {
87 my ($self, $object_name, $properties, $id) = @_;
88  
89
90 my (undef, $dbh, $unlock) = $self->dbh($object_name, $properties, 'w') or return undef;
91 delete $dbh->{ "!ID:" . $id };
92 $unlock->();
93 return 1
94 }
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105 sub _lock {
106 my $self = shift;
107 my ($file, $type) = @_;
108
109 $file .= '.lck';
110 my $lock_flags = $type eq 'w' ? LOCK_EX : LOCK_SH;
111  
112 require Symbol;
113 my $lock_h = Symbol::gensym();
114 unless ( sysopen($lock_h, $file, O_RDWR|O_CREAT, 0666) ) {
115 $self->errstr("couldn't create/open '$file': $!");
116 return undef
117 }
118 unless (flock($lock_h, $lock_flags)) {
119 $self->errstr("couldn't lock '$file': $!");
120 close($lock_h);
121 return undef
122 }
123 return sub {
124 close($lock_h);
125 unlink $file
126 }
127 }
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141 1;
142 __END__