line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Class::ReluctantORM::Registry::Hash; |
2
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
36
|
|
3
|
1
|
|
|
1
|
|
6
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
25
|
|
4
|
1
|
|
|
1
|
|
5
|
use base 'Class::ReluctantORM::Registry'; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
66
|
|
5
|
1
|
|
|
1
|
|
6
|
use Scalar::Util qw(weaken); |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
497
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 NAME |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
Class::ReluctantORM::Registry::Hash - Use Hash to store Weak refs |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
=head1 DESCRIPTION |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
Uses a simple hash structure to store weak references to objects. |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 AUTHOR |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Clinton Wolfe clwolfe@cpan.org January 2010 |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=cut |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new { |
22
|
0
|
|
|
0
|
1
|
|
my $reg_class = shift; |
23
|
0
|
|
|
|
|
|
my $tgt_class = shift; |
24
|
0
|
|
|
|
|
|
my $self = $reg_class->SUPER::new($tgt_class); |
25
|
0
|
|
|
|
|
|
$self->{_hash_by_id} = {}; |
26
|
0
|
|
|
|
|
|
return $self; |
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
sub _hash_by_id { |
30
|
0
|
|
|
0
|
|
|
return shift->{_hash_by_id}; |
31
|
|
|
|
|
|
|
} |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub fetch { |
34
|
0
|
|
|
0
|
1
|
|
my $reg = shift; |
35
|
0
|
|
|
|
|
|
my $id = shift; |
36
|
0
|
0
|
|
|
|
|
my $obj = exists($reg->_hash_by_id->{$id}) ? $reg->_hash_by_id->{$id} : undef; |
37
|
0
|
|
|
|
|
|
return $obj; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub store { |
41
|
0
|
|
|
0
|
1
|
|
my $reg = shift; |
42
|
0
|
|
|
|
|
|
my $obj = shift; |
43
|
0
|
0
|
|
|
|
|
unless ($obj->has_all_primary_keys_defined()) { |
44
|
0
|
|
|
|
|
|
return; |
45
|
|
|
|
|
|
|
} |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $id = $obj->id(); |
48
|
0
|
|
|
|
|
|
my $hash_by_id = $reg->_hash_by_id(); |
49
|
0
|
|
|
|
|
|
$hash_by_id->{$id} = $obj; |
50
|
0
|
|
|
|
|
|
weaken($hash_by_id->{$id}); |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
return $obj; |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
sub purge { |
56
|
0
|
|
|
0
|
1
|
|
my $reg = shift; |
57
|
0
|
|
|
|
|
|
my $thing = shift; |
58
|
0
|
|
|
|
|
|
my $id; |
59
|
0
|
0
|
0
|
|
|
|
if (ref($thing) && $thing->isa($reg->target_class)) { |
60
|
0
|
|
|
|
|
|
$id = $thing->id(); |
61
|
|
|
|
|
|
|
} else { |
62
|
0
|
|
|
|
|
|
$id = $thing; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
0
|
0
|
|
|
|
|
return unless defined($id); |
66
|
0
|
|
|
|
|
|
delete($reg->_hash_by_id->{$id}); |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
sub purge_all { |
70
|
0
|
|
|
0
|
1
|
|
my $reg = shift; |
71
|
0
|
|
|
|
|
|
$reg->{_hash_by_id} = {}; |
72
|
0
|
|
|
|
|
|
return 1; |
73
|
|
|
|
|
|
|
} |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub count { |
76
|
0
|
|
|
0
|
1
|
|
my $reg = shift; |
77
|
0
|
|
|
|
|
|
my $total = 0; |
78
|
0
|
|
|
0
|
|
|
$reg->walk(sub { $total++; }); |
|
0
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
return $total; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
sub walk { |
83
|
0
|
|
|
0
|
1
|
|
my $reg = shift; |
84
|
0
|
|
|
|
|
|
my $code = shift; |
85
|
0
|
|
|
|
|
|
while (my ($id, $obj) = each %{$reg->_hash_by_id}) { |
|
0
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
$code->($obj) if defined($obj); |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
1; |