}
sub _normalize_storage_type {
- my ($self, $storage_type) = @_;
- if(ref $storage_type eq 'ARRAY') {
- return @$storage_type;
- } elsif(ref $storage_type eq 'HASH') {
- return %$storage_type;
- } else {
- $self->throw_exception('Unsupported REFTYPE given: '. ref $storage_type);
- }
+ my ($self, $storage_type) = @_;
+ if(ref $storage_type eq 'ARRAY') {
+ return @$storage_type;
+ } elsif(ref $storage_type eq 'HASH') {
+ return %$storage_type;
+ } else {
+ $self->throw_exception('Unsupported REFTYPE given: '. ref $storage_type);
+ }
}
=head2 connect
isa=>'ClassName',
lazy_build=>1,
handles=>{
- 'create_pool' => 'new',
+ 'create_pool' => 'new',
},
);
isa=>'ClassName',
lazy_build=>1,
handles=>{
- 'create_balancer' => 'new',
+ 'create_balancer' => 'new',
},
);
## Hate to do it this way, but can't seem to get advice on the attribute working right
## maybe we can do a type and coercion for it.
if( $storage_type_args->{balancer_type} && $storage_type_args->{balancer_type}=~m/^::/) {
- $storage_type_args->{balancer_type} = 'DBIx::Class::Storage::DBI::Replicated::Balancer'.$storage_type_args->{balancer_type};
- eval "require $storage_type_args->{balancer_type}";
+ $storage_type_args->{balancer_type} = 'DBIx::Class::Storage::DBI::Replicated::Balancer'.$storage_type_args->{balancer_type};
+ eval "require $storage_type_args->{balancer_type}";
}
return $class->meta->new_object(
=cut
sub _build_master {
- DBIx::Class::Storage::DBI->new;
+ DBIx::Class::Storage::DBI->new;
}
=head2 _build_pool_type
=cut
sub _build_pool {
- my $self = shift @_;
+ my $self = shift @_;
$self->create_pool(%{$self->pool_args});
}
$self->create_balancer(
pool=>$self->pool,
master=>$self->master,
- %{$self->balancer_args},);
+ %{$self->balancer_args},
+ );
}
=head2 _build_write_handler
=cut
around 'connect_replicants' => sub {
- my ($method, $self, @args) = @_;
- $self->$method($self->schema, @args);
+ my ($method, $self, @args) = @_;
+ $self->$method($self->schema, @args);
};
=head2 all_storages
=cut
sub all_storages {
- my $self = shift @_;
-
- return grep {defined $_ && blessed $_} (
- $self->master,
- $self->replicants,
- );
+ my $self = shift @_;
+
+ return grep {defined $_ && blessed $_} (
+ $self->master,
+ $self->replicants,
+ );
}
=head2 execute_reliably ($coderef, ?@args)
sub execute_reliably {
my ($self, $coderef, @args) = @_;
-
+
unless( ref $coderef eq 'CODE') {
$self->throw_exception('Second argument must be a coderef');
}
my $want_array = wantarray;
eval {
- if($want_array) {
- @result = $coderef->(@args);
- }
- elsif(defined $want_array) {
- ($result[0]) = ($coderef->(@args));
- } else {
- $coderef->(@args);
- }
+ if($want_array) {
+ @result = $coderef->(@args);
+ }
+ elsif(defined $want_array) {
+ ($result[0]) = ($coderef->(@args));
+ } else {
+ $coderef->(@args);
+ }
};
##Reset to the original state
if($@) {
$self->throw_exception("coderef returned an error: $@");
} else {
- return $want_array ? @result : $result[0];
+ return $want_array ? @result : $result[0];
}
}
=cut
sub set_reliable_storage {
- my $self = shift @_;
- my $schema = $self->schema;
- my $write_handler = $self->schema->storage->write_handler;
-
- $schema->storage->read_handler($write_handler);
+ my $self = shift @_;
+ my $schema = $self->schema;
+ my $write_handler = $self->schema->storage->write_handler;
+
+ $schema->storage->read_handler($write_handler);
}
=head2 set_balanced_storage
around 'txn_do' => sub {
my($txn_do, $self, $coderef, @args) = @_;
- $self->execute_reliably(sub {$self->$txn_do($coderef, @args)});
+ $self->execute_reliably(sub {$self->$txn_do($coderef, @args)});
};
=head2 reload_row ($row)
=cut
around 'reload_row' => sub {
- my ($reload_row, $self, $row) = @_;
- return $self->execute_reliably(sub {
- return $self->$reload_row(shift);
- }, $row);
+ my ($reload_row, $self, $row) = @_;
+ return $self->execute_reliably(sub {
+ return $self->$reload_row(shift);
+ }, $row);
};
=head2 connected
=cut
sub connected {
- my $self = shift @_;
-
- return
- $self->master->connected &&
- $self->pool->connected_replicants;
+ my $self = shift @_;
+
+ return
+ $self->master->connected &&
+ $self->pool->connected_replicants;
}
=head2 ensure_connected
=cut
sub set_schema {
- my $self = shift @_;
- foreach my $source ($self->all_storages) {
- $source->set_schema(@_);
- }
+ my $self = shift @_;
+ foreach my $source ($self->all_storages) {
+ $source->set_schema(@_);
+ }
}
=head2 debug
=cut
around 'next_storage' => sub {
- my ($next_storage, $self, @args) = @_;
- my $now = time;
-
+ my ($next_storage, $self, @args) = @_;
+ my $now = time;
+
## Do we need to validate the replicants?
if(
$self->has_auto_validate_every &&
## Get a replicant, or the master if none
my $next = $self->$next_storage(@args);
- return $next ? $next:$self->master;
+ return $next ? $next:$self->master;
};
=head2 before: select
required=>1,
default=>'DBIx::Class::Storage::DBI',
handles=>{
- 'create_replicant' => 'new',
- },
+ 'create_replicant' => 'new',
+ },
);
=head2 replicants
isa=>'HashRef[DBIx::Class::Storage::DBI]',
default=>sub {{}},
provides => {
- 'set' => 'set_replicant',
- 'get' => 'get_replicant',
- 'empty' => 'has_replicants',
- 'count' => 'num_replicants',
- 'delete' => 'delete_replicant',
- },
+ 'set' => 'set_replicant',
+ 'get' => 'get_replicant',
+ 'empty' => 'has_replicants',
+ 'count' => 'num_replicants',
+ 'delete' => 'delete_replicant',
+ },
);
=head1 METHODS
=cut
sub connect_replicants {
- my $self = shift @_;
- my $schema = shift @_;
-
- my @newly_created = ();
- foreach my $connect_info (@_) {
-
- my $replicant = $self->create_replicant($schema);
- $replicant->connect_info($connect_info);
- $replicant->ensure_connected;
- DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
-
- my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
- $self->set_replicant( $key => $replicant);
- push @newly_created, $replicant;
- }
-
- return @newly_created;
+ my $self = shift @_;
+ my $schema = shift @_;
+
+ my @newly_created = ();
+ foreach my $connect_info (@_) {
+
+ my $replicant = $self->create_replicant($schema);
+ $replicant->connect_info($connect_info);
+ $replicant->ensure_connected;
+ DBIx::Class::Storage::DBI::Replicated::Replicant->meta->apply($replicant);
+
+ my ($key) = ($connect_info->[0]=~m/^dbi\:.+\:(.+)$/);
+ $self->set_replicant( $key => $replicant);
+ push @newly_created, $replicant;
+ }
+
+ return @newly_created;
}
=head2 connected_replicants
return the number of replicants. So you can do stuff like:
if( my $num_connected = $storage->has_connected_replicants ) {
- print "I have $num_connected connected replicants";
+ print "I have $num_connected connected replicants";
} else {
- print "Sorry, no replicants.";
+ print "Sorry, no replicants.";
}
This method will actually test that each replicant in the L</replicants> hashref
=cut
sub connected_replicants {
- my $self = shift @_;
- return sum( map {
- $_->connected ? 1:0
- } $self->all_replicants );
+ my $self = shift @_;
+ return sum( map {
+ $_->connected ? 1:0
+ } $self->all_replicants );
}
=head2 active_replicants
$replicant->lag_behind_master <= $self->maximum_lag &&
$replicant->ensure_connected
) {
- ## TODO:: Hook debug for this
+ ## TODO:: Hook debug for this
$replicant->active(1)
} else {
- ## TODO:: Hook debug for this
+ ## TODO:: Hook debug for this
$replicant->active(0);
}
}
=cut
around '_query_start' => sub {
- my ($method, $self, $sql, @bind) = @_;
- my $dsn = $self->connect_info->[0];
- $self->$method("DSN: $dsn SQL: $sql", @bind);
+ my ($method, $self, $sql, @bind) = @_;
+ my $dsn = $self->connect_info->[0];
+ $self->$method("DSN: $dsn SQL: $sql", @bind);
};
-
=head1 AUTHOR
John Napiorkowski <john.napiorkowski@takkle.com>