line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Cluster::Init::DB; |
2
|
1
|
|
|
1
|
|
10824
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
41
|
|
3
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
39
|
|
4
|
|
|
|
|
|
|
# |
5
|
|
|
|
|
|
|
# an in-memory object database |
6
|
|
|
|
|
|
|
# |
7
|
|
|
|
|
|
|
# objects must be hashes |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
# @itemrefs = $db->ins($hashobj); |
10
|
|
|
|
|
|
|
# @itemrefs = $db->upd($class,$filterhash,$valuehash); |
11
|
|
|
|
|
|
|
# @itemrefs = $db->get($class,$filterhash); |
12
|
|
|
|
|
|
|
# @itemrefs = $db->del($class,$filterhash); |
13
|
|
|
|
|
|
|
# |
14
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
1126
|
use Data::Dump qw(dump); |
|
1
|
|
|
|
|
14811
|
|
|
1
|
|
|
|
|
96
|
|
16
|
1
|
|
|
1
|
|
1496
|
use Carp::Assert; |
|
1
|
|
|
|
|
1890
|
|
|
1
|
|
|
|
|
7
|
|
17
|
1
|
|
|
1
|
|
2295
|
use Storable qw(dclone); |
|
1
|
|
|
|
|
5363
|
|
|
1
|
|
|
|
|
1502
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
sub new |
20
|
|
|
|
|
|
|
{ |
21
|
0
|
|
|
0
|
0
|
|
my $class=shift; |
22
|
0
|
|
0
|
|
|
|
$class = (ref $class || $class); |
23
|
0
|
|
|
|
|
|
my %parms=@_; |
24
|
0
|
|
|
|
|
|
my $self=\%parms; |
25
|
0
|
|
|
|
|
|
$self->{nextid}=1; |
26
|
0
|
|
|
|
|
|
$self->{_mtime}=0; |
27
|
0
|
|
|
|
|
|
bless $self, $class; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
sub ins |
31
|
|
|
|
|
|
|
{ |
32
|
0
|
|
|
0
|
0
|
|
my ($self,$obj)=@_; |
33
|
|
|
|
|
|
|
# warn dump($obj); |
34
|
|
|
|
|
|
|
# warn dump(ref($obj)); |
35
|
0
|
0
|
|
|
|
|
die 'usage: $db->ins($hashobj)' |
36
|
|
|
|
|
|
|
unless ref($obj); |
37
|
0
|
|
|
|
|
|
my $class = ref($obj); |
38
|
0
|
|
|
|
|
|
my @out; |
39
|
0
|
|
|
|
|
|
my $id = $self->{nextid}++; |
40
|
0
|
|
|
|
|
|
$self->{db}{$class}{$id} = $obj; |
41
|
0
|
|
|
|
|
|
$self->{db}{$class}{$id}{_mtime}=time; |
42
|
0
|
|
|
|
|
|
$self->{_mtime}=time; |
43
|
0
|
|
|
|
|
|
return $self->{db}{$class}{$id}; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub upd |
47
|
|
|
|
|
|
|
{ |
48
|
0
|
|
|
0
|
0
|
|
my $self=shift; |
49
|
0
|
|
|
|
|
|
my $class=shift; |
50
|
0
|
|
|
|
|
|
my $filter; |
51
|
0
|
0
|
|
|
|
|
if (ref($class)) |
52
|
|
|
|
|
|
|
{ |
53
|
0
|
|
|
|
|
|
$filter=$class; |
54
|
0
|
|
|
|
|
|
$class=ref($class); |
55
|
|
|
|
|
|
|
} |
56
|
|
|
|
|
|
|
else |
57
|
|
|
|
|
|
|
{ |
58
|
0
|
|
|
|
|
|
$filter=shift; |
59
|
|
|
|
|
|
|
} |
60
|
0
|
|
|
|
|
|
my $value=shift; |
61
|
0
|
0
|
0
|
|
|
|
die 'usage: $db->upd( { $class,$filterhash | $obj } , $valuehash )' |
62
|
|
|
|
|
|
|
unless ref($filter) && ref($value); |
63
|
|
|
|
|
|
|
# warn dump $filter; |
64
|
0
|
|
|
|
|
|
my @out; |
65
|
0
|
|
|
|
|
|
for my $item ($self->get($class,$filter)) |
66
|
|
|
|
|
|
|
{ |
67
|
0
|
|
|
|
|
|
for my $var (keys %$value) |
68
|
|
|
|
|
|
|
{ |
69
|
0
|
|
|
|
|
|
$item->{$var}=$value->{$var}; |
70
|
|
|
|
|
|
|
} |
71
|
0
|
|
|
|
|
|
$item->{_mtime}=time; |
72
|
0
|
|
|
|
|
|
push @out, $item; |
73
|
|
|
|
|
|
|
} |
74
|
0
|
|
|
|
|
|
$self->{_mtime}=time; |
75
|
0
|
|
|
|
|
|
return @out; |
76
|
|
|
|
|
|
|
} |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub get |
79
|
|
|
|
|
|
|
{ |
80
|
0
|
|
|
0
|
0
|
|
my ($self,$class,$filter)=@_; |
81
|
0
|
0
|
|
|
|
|
die 'usage: $db->get($class,$filterhash)' |
82
|
|
|
|
|
|
|
unless ref($filter); |
83
|
0
|
|
|
|
|
|
return $self->getordel($class,$filter); |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
sub del |
87
|
|
|
|
|
|
|
{ |
88
|
0
|
|
|
0
|
0
|
|
my $self=shift; |
89
|
0
|
|
|
|
|
|
my $class=shift; |
90
|
0
|
|
|
|
|
|
my $filter; |
91
|
0
|
0
|
|
|
|
|
if (ref($class)) |
92
|
|
|
|
|
|
|
{ |
93
|
0
|
|
|
|
|
|
$filter=$class; |
94
|
0
|
|
|
|
|
|
$class=ref($class); |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
else |
97
|
|
|
|
|
|
|
{ |
98
|
0
|
|
|
|
|
|
$filter=shift; |
99
|
|
|
|
|
|
|
} |
100
|
0
|
0
|
|
|
|
|
die 'usage: $db->del({$class,$filterhash}|{$obj})' |
101
|
|
|
|
|
|
|
unless ref($filter); |
102
|
0
|
|
|
|
|
|
return $self->getordel($class,$filter,'delete'); |
103
|
|
|
|
|
|
|
} |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
sub getordel |
106
|
|
|
|
|
|
|
{ |
107
|
0
|
|
|
0
|
0
|
|
my ($self,$class,$filter,$delete)=@_; |
108
|
|
|
|
|
|
|
# warn "$class ". dump($filter); |
109
|
0
|
|
|
|
|
|
my @out; |
110
|
0
|
|
|
|
|
|
for my $id (keys %{$self->{db}{$class}}) |
|
0
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
{ |
112
|
0
|
|
|
|
|
|
my $match=1; |
113
|
0
|
|
|
|
|
|
my $item=$self->{db}{$class}{$id}; |
114
|
0
|
0
|
|
|
|
|
next unless $item; |
115
|
|
|
|
|
|
|
# see if this item reference is the same as the filter reference |
116
|
0
|
0
|
|
|
|
|
unless ($item eq $filter) |
117
|
|
|
|
|
|
|
{ |
118
|
|
|
|
|
|
|
# look for a value match instead |
119
|
0
|
|
|
|
|
|
for my $var (keys %$filter) |
120
|
|
|
|
|
|
|
{ |
121
|
|
|
|
|
|
|
# item doesn't contain this var -- no match |
122
|
0
|
0
|
|
|
|
|
do { $match=0;last } unless exists($item->{$var}); |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
# accept if both are undef |
124
|
0
|
0
|
0
|
|
|
|
next unless (defined($filter->{$var}) || defined($item->{$var})); |
125
|
|
|
|
|
|
|
# bail if only one is undef |
126
|
0
|
0
|
0
|
|
|
|
unless (defined($filter->{$var}) && defined($item->{$var})) |
127
|
|
|
|
|
|
|
{ |
128
|
0
|
|
|
|
|
|
$match=0; |
129
|
0
|
|
|
|
|
|
last; |
130
|
|
|
|
|
|
|
} |
131
|
0
|
0
|
|
|
|
|
unless ($filter->{$var} eq $item->{$var}) |
132
|
|
|
|
|
|
|
{ |
133
|
|
|
|
|
|
|
# item contains var but the value doesn't match |
134
|
|
|
|
|
|
|
# ...so check for a regex match |
135
|
0
|
0
|
|
|
|
|
if (ref($filter->{$var}) eq 'Regexp') |
136
|
|
|
|
|
|
|
{ |
137
|
0
|
0
|
|
|
|
|
next if $item->{$var} =~ $filter->{$var}; |
138
|
|
|
|
|
|
|
} |
139
|
|
|
|
|
|
|
# okay, give up |
140
|
0
|
|
|
|
|
|
$match=0; |
141
|
0
|
|
|
|
|
|
last; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
0
|
0
|
|
|
|
|
next unless $match; |
146
|
0
|
0
|
|
|
|
|
if ($delete) |
147
|
|
|
|
|
|
|
{ |
148
|
|
|
|
|
|
|
# hang onto the item reference so we can return it |
149
|
0
|
|
|
|
|
|
push @out, $item; |
150
|
|
|
|
|
|
|
# ...then delete the item |
151
|
0
|
|
|
|
|
|
delete $self->{db}{$class}{$id}; |
152
|
0
|
|
|
|
|
|
$self->{_mtime}=time; |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
else |
155
|
|
|
|
|
|
|
{ |
156
|
0
|
0
|
|
|
|
|
push @out, $item if $item; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
} |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
# warn dump (@out); |
161
|
|
|
|
|
|
|
# warn "returning ".scalar(@out)." items"; |
162
|
0
|
|
|
|
|
|
return @out; |
163
|
|
|
|
|
|
|
} |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
sub allclass |
166
|
|
|
|
|
|
|
{ |
167
|
0
|
|
|
0
|
0
|
|
my ($self,$class)=@_; |
168
|
0
|
0
|
|
|
|
|
die 'usage: $db->all($class)' unless $class; |
169
|
0
|
|
|
|
|
|
my @out; |
170
|
0
|
|
|
|
|
|
for my $id (keys %{$self->{db}{$class}}) |
|
0
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
{ |
172
|
0
|
|
|
|
|
|
push @out, $self->{db}{$class}{$id}; |
173
|
|
|
|
|
|
|
} |
174
|
0
|
|
|
|
|
|
return @out; |
175
|
|
|
|
|
|
|
} |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
1; |