File Coverage

blib/lib/MogileFS/Factory.pm
Criterion Covered Total %
statement 19 45 42.2
branch 2 4 50.0
condition n/a
subroutine 6 13 46.1
pod 0 11 0.0
total 27 73 36.9


line stmt bran cond sub pod time code
1             package MogileFS::Factory;
2 21     21   133 use strict;
  21         33  
  21         459  
3 21     21   79 use warnings;
  21         33  
  21         8158  
4              
5             =head1
6              
7             MogileFS::MogFactory - singleton class for holding some common objects.
8              
9             =head1 ABOUT
10              
11             This module holds a singleton for caching objects which are common but
12             relatively low in number. Such as devices, compared to fids.
13              
14             This singleton is to be maintained by the parent process, and inherited to
15             children during fork. Post-fork, the cache is updated by natural commands, or
16             a monitor process pushing changes through the parent.
17              
18             The purpose is to provide a fresh cache, without forcing new children to
19             wait for a monitoring run before becoming useful. It also should greatly
20             reduce the number of simple DB queries, as those should only happen
21             periodically directly from the monitor job.
22              
23             =cut
24              
25             my %singleton;
26              
27             # Rename to new maybe?
28             sub get_factory {
29 258     258 0 347 my $class = shift;
30 258 100       438 if (!exists $singleton{$class}) {
31 60         149 $singleton{$class} = bless {
32             by_id => {},
33             by_name => {},
34             }, $class;
35             }
36 258         424 return $singleton{$class};
37             }
38              
39             # Allow unit tests to blow us up.
40             sub t_wipe {
41 60     60 0 241 my $class = shift;
42 60         95 delete $singleton{$class};
43             }
44              
45             # because 'add' means bail if already exists.
46             sub set {
47 198     198 0 245 my $self = shift;
48 198         221 my $obj = shift;
49 198         355 $self->{by_id}->{$obj->id} = $obj;
50 198         372 $self->{by_name}->{$obj->name} = $obj;
51 198         393 return $obj;
52             }
53              
54             sub remove {
55 0     0 0 0 my $self = shift;
56 0         0 my $obj = shift;
57              
58 0 0       0 if (exists $self->{by_id}->{$obj->id}) {
59 0         0 delete $self->{by_id}->{$obj->id};
60 0         0 delete $self->{by_name}->{$obj->name};
61             }
62             }
63              
64             sub get_by_id {
65 104     104 0 136 my ($self, $id) = @_;
66 104         190 return $self->{by_id}->{$id};
67             }
68              
69             sub get_by_name {
70 0     0 0   my ($self, $name) = @_;
71 0           return $self->{by_name}->{$name};
72             }
73              
74             sub get_ids {
75 0     0 0   my $self = shift;
76 0           return keys %{$self->{by_id}};
  0            
77             }
78              
79             sub get_names {
80 0     0 0   my $self = shift;
81 0           return keys %{$self->{by_name}};
  0            
82             }
83              
84             sub get_all {
85 0     0 0   my $self = shift;
86 0           return values %{$self->{by_id}};
  0            
87             }
88              
89             sub map_by_id {
90 0     0 0   my $self = shift;
91 0           my $set = $self->{by_id};
92 0           return { map { $_ => $set->{$_} } keys %{$set} };
  0            
  0            
93             }
94              
95             sub map_by_name {
96 0     0 0   my $self = shift;
97 0           my $set = $self->{by_name};
98 0           return { map { $_ => $set->{$_} } keys %{$set} };
  0            
  0            
99             }
100              
101             1;