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',
41 'create_replicant' => 'new',
47 A hashref of replicant, with the key being the dsn and the value returning the
48 actual replicant storage. For example if the $dsn element is something like:
50 "dbi:SQLite:dbname=dbfile"
52 You could access the specific replicant via:
54 $schema->storage->replicants->{'dbname=dbfile'}
56 This attributes also supports the following helper methods
60 =item set_replicant($key=>$storage)
62 Pushes a replicant onto the HashRef under $key
64 =item get_replicant($key)
66 Retrieves the named replicant
70 Returns true if the Pool defines replicants.
74 The number of replicants in the pool
76 =item delete_replicant ($key)
78 removes the replicant under $key from the pool
86 metaclass => 'Collection::Hash',
87 isa=>'HashRef[DBIx::Class::Storage::DBI]',
90 'set' => 'set_replicant',
91 'get' => 'get_replicant',
92 'empty' => 'has_replicants',
93 'count' => 'num_replicants',
94 'delete' => 'delete_replicant',
100 This class defines the following methods.
102 =head2 connect_replicants ($schema, Array[$connect_info])
104 Given an array of $dsn suitable for connected to a database, create an
105 L<DBIx::Class::Storage::DBI::Replicated::Replicant> object and store it in the
106 L</replicants> attribute.
110 use Data::Dump qw/dump/;
112 sub connect_replicants {
114 my $schema = shift @_;
116 my @newly_created = ();
117 foreach my $connect_info (@_) {
119 my $replicant = $self->create_replicant($schema);
120 $replicant->connect_info($connect_info);
121 $replicant->ensure_connected;
122 DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
124 my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
125 $self->set_replicant( $key => $replicant);
126 push @newly_created, $replicant;
129 return @newly_created;
132 =head2 connected_replicants
134 Returns true if there are connected replicants. Actually is overloaded to
135 return the number of replicants. So you can do stuff like:
137 if( my $num_connected = $storage->has_connected_replicants ) {
138 print "I have $num_connected connected replicants";
140 print "Sorry, no replicants.";
143 This method will actually test that each replicant in the L</replicants> hashref
144 is actually connected, try not to hit this 10 times a second.
148 sub connected_replicants {
152 } $self->all_replicants );
155 =head2 active_replicants
157 This is an array of replicants that are considered to be active in the pool.
158 This does not check to see if they are connected, but if they are not, DBIC
159 should automatically reconnect them for us when we hit them with a query.
163 sub active_replicants {
165 return ( grep {$_} map {
167 } $self->all_replicants );
170 =head2 all_replicants
172 Just a simple array of all the replicant storages. No particular order to the
173 array is given, nor should any meaning be derived.
179 return values %{$self->replicants};
184 John Napiorkowski <john.napiorkowski@takkle.com>
188 You may distribute this code under the same terms as Perl itself.