1 package DBIx::Class::Storage::DBI::Replicated::Pool;
4 use MooseX::AttributeHelpers;
5 use DBIx::Class::Storage::DBI::Replicated::Replicant;
6 use List::Util qw(sum);
10 DBIx::Class::Storage::DBI::Replicated::Pool; Manage a pool of replicants
14 This class is used internally by L<DBIx::Class::Storage::DBI::Replicated>. You
15 shouldn't need to create instances of this class.
19 In a replicated storage type, there is at least one replicant to handle the
20 read only traffic. The Pool class manages this replicant, or list of
21 replicants, and gives some methods for querying information about their status.
25 This class defines the following attributes.
29 Base class used to instantiate replicants that are in the pool. Unless you
30 need to subclass L<DBIx::Class::Storage::DBI::Replicated::Replicant> you should
31 just leave this alone.
35 has 'replicant_type' => (
39 default=>'DBIx::Class::Storage::DBI::Replicated::Replicant',
41 'create_replicant' => 'new',
48 A hashref of replicant, with the key being the dsn and the value returning the
49 actual replicant storage. For example if the $dsn element is something like:
51 "dbi:SQLite:dbname=dbfile"
53 You could access the specific replicant via:
55 $schema->storage->replicants->{'dbname=dbfile'}
57 This attributes also supports the following helper methods
61 =item set_replicant($key=>$storage)
63 Pushes a replicant onto the HashRef under $key
65 =item get_replicant($key)
67 Retrieves the named replicant
71 Returns true if the Pool defines replicants.
75 The number of replicants in the pool
77 =item delete_replicant ($key)
79 removes the replicant under $key from the pool
87 metaclass => 'Collection::Hash',
88 isa=>'HashRef[DBIx::Class::Storage::DBI::Replicated::Replicant]',
91 'set' => 'set_replicant',
92 'get' => 'get_replicant',
93 'empty' => 'has_replicants',
94 'count' => 'num_replicants',
95 'delete' => 'delete_replicant',
102 This class defines the following methods.
104 =head2 create_replicants (Array[$connect_info])
106 Given an array of $dsn suitable for connected to a database, create an
107 L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
108 L</replicants> attribute.
112 sub create_replicants {
115 my @newly_created = ();
116 foreach my $connect_info (@_) {
117 my $replicant = $self->create_replicant;
118 $replicant->connect_info($connect_info);
119 $replicant->ensure_connected;
120 my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
121 $self->set_replicant( $key => $replicant);
122 push @newly_created, $replicant;
125 return @newly_created;
129 =head2 connected_replicants
131 Returns true if there are connected replicants. Actually is overloaded to
132 return the number of replicants. So you can do stuff like:
134 if( my $num_connected = $storage->has_connected_replicants ) {
135 print "I have $num_connected connected replicants";
137 print "Sorry, no replicants.";
140 This method will actually test that each replicant in the L</replicants> hashref
141 is actually connected, try not to hit this 10 times a second.
145 sub connected_replicants {
149 } $self->all_replicants );
152 =head2 all_replicants
154 Just a simple array of all the replicant storages. No particular order to the
155 array is given, nor should any meaning be derived.
161 return values %{$self->replicants};
167 John Napiorkowski <john.napiorkowski@takkle.com>
171 You may distribute this code under the same terms as Perl itself.