changed the balancer to a role, created a new class to define the default balancer...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / Replicated.pm
index 6f81945..a70a8b5 100644 (file)
@@ -19,7 +19,7 @@ storage type, add some replicated (readonly) databases, and perform reporting
 tasks.
 
     ## Change storage_type in your schema class
-    $schema->storage_type( '::DBI::Replicated' );
+    $schema->storage_type( ['::DBI::Replicated', {balancer=>'::Random'}] );
     
     ## Add some slaves.  Basically this is an array of arrayrefs, where each
     ## arrayref is database connect information
@@ -67,15 +67,12 @@ to: L<DBIx::Class::Storage::DBI::Replicated::Pool>.
 has 'pool_type' => (
     is=>'ro',
     isa=>'ClassName',
-    required=>1,
-    lazy=>1,
-    default=>'DBIx::Class::Storage::DBI::Replicated::Pool',
+    lazy_build=>1,
     handles=>{
        'create_pool' => 'new',
     },
 );
 
-
 =head2 balancer_type
 
 The replication pool requires a balance class to provider the methods for
@@ -86,14 +83,23 @@ choose how to spread the query load across each replicant in the pool.
 has 'balancer_type' => (
     is=>'ro',
     isa=>'ClassName',
-    required=>1,
-    lazy=>1,
-    default=>'DBIx::Class::Storage::DBI::Replicated::Balancer',
+    lazy_build=>1,
     handles=>{
        'create_balancer' => 'new',
     },
 );
 
+=head2 balancer_args
+
+Contains a hashref of initialized information to pass to the Balancer object.
+See L<DBIx::Class::Storage::Replicated::Pool> for available arguments.
+
+=cut
+
+has 'balancer_args' => (
+    is=>'ro',
+    isa=>'HashRef',
+);
 
 =head2 pool
 
@@ -110,12 +116,9 @@ has 'pool' => (
         connect_replicants    
         replicants
         has_replicants
-        num_replicants
-        delete_replicant
     /],
 );
 
-
 =head2 balancer
 
 Is a <DBIx::Class::Storage::DBI::Replicated::Balancer> or derived class.  This 
@@ -127,9 +130,9 @@ has 'balancer' => (
     is=>'ro',
     isa=>'DBIx::Class::Storage::DBI::Replicated::Balancer',
     lazy_build=>1,
+    handles=>[qw/auto_validate_every/],
 );
 
-
 =head2 master
 
 The master defines the canonical state for a pool of connected databases.  All
@@ -146,7 +149,6 @@ has 'master' => (
     lazy_build=>1,
 );
 
-
 =head1 ATTRIBUTES IMPLEMENTING THE DBIx::Storage::DBI INTERFACE
 
 The following methods are delegated all the methods required for the 
@@ -169,7 +171,6 @@ has 'read_handler' => (
     /],    
 );
 
-
 =head2 write_handler
 
 Defines an object that implements the write side of L<BIx::Class::Storage::DBI>.
@@ -207,7 +208,6 @@ has 'write_handler' => (
     /],
 );
 
-
 =head1 METHODS
 
 This class defines the following methods.
@@ -227,7 +227,14 @@ sub new {
     my $schema = shift @_;
     my $storage_type_args = shift @_;
     my $obj = $class->SUPER::new($schema, $storage_type_args, @_);
-  
+    
+    ## 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}";
+    }
+    
     return $class->meta->new_object(
         __INSTANCE__ => $obj,
         %$storage_type_args,
@@ -245,6 +252,16 @@ sub _build_master {
        DBIx::Class::Storage::DBI->new;
 }
 
+=head2 _build_pool_type
+
+Lazy builder for the L</pool_type> attribute.
+
+=cut
+
+sub _build_pool_type {
+    return 'DBIx::Class::Storage::DBI::Replicated::Pool';
+}
+
 =head2 _build_pool
 
 Lazy builder for the L</pool> attribute.
@@ -252,7 +269,18 @@ Lazy builder for the L</pool> attribute.
 =cut
 
 sub _build_pool {
-    shift->create_pool;
+       my $self = shift @_;
+    $self->create_pool;
+}
+
+=head2 _build_balancer_type
+
+Lazy builder for the L</balancer_type> attribute.
+
+=cut
+
+sub _build_balancer_type {
+    return 'DBIx::Class::Storage::DBI::Replicated::Balancer::First';
 }
 
 =head2 _build_balancer
@@ -264,7 +292,10 @@ the balancer knows which pool it's balancing.
 
 sub _build_balancer {
     my $self = shift @_;
-    $self->create_balancer(pool=>$self->pool);
+    $self->create_balancer(
+        pool=>$self->pool, 
+        master=>$self->master,
+        %{$self->balancer_args},);
 }
 
 =head2 _build_write_handler