Permalink
Browse files

Use setup() method internally. Add pod.

  • Loading branch information...
1 parent 202fe77 commit 9f5bf703f91598672ac9864569ab5fa35e77b68a @zigorou committed Jun 7, 2011
View
@@ -8,7 +8,7 @@ requires 'DBI';
requires 'POSIX';
requires 'Time::HiRes';
-tests 't/*.t';
+tests_recursive 't';
author_tests 'xt';
test_requires 'DBD::Mock';
@@ -32,13 +32,15 @@ sub new {
%$args,
);
- bless $args => $class;
+ my $self = bless $args => $class;
+ $self->setup;
}
sub setup {
my $self = shift;
$self->setup_log;
$self->setup_strategy;
+ $self;
}
sub setup_log {
@@ -117,15 +119,91 @@ __END__
=head1 NAME
-MySQL::SustainableQuery -
+MySQL::SustainableQuery - Execute query sustainably by strategy
=head1 SYNOPSIS
+ use DBI;
use MySQL::SustainableQuery;
+ my $dbh = DBI->connect( ... );
+
+ my $query = MySQL::SustainableQuery->new(
+ exec_query => sub {
+ my ( $q, $i ) = @_;
+ return $dbh->do('DELETE FROM large_table ORDER BY id ASC LIMIT 100');
+ },
+ terminate_condition => sub {
+ my ( $q, $rv, $i, $ts ) = @_;
+ $rv < 100 ? 1 : 0;
+ }
+ );
+
+ my $rs = $query->run;
+ printf("execute count: %d; total times: %.02f sec\n", $rs->{executed}, $rs->{time_total});
+
=head1 DESCRIPTION
-MySQL::SustainableQuery is
+MySQL::SustainableQuery executes query to care load time or replication behind times or other factor.
+
+=head2 new( %args )
+
+The details of args is below.
+
+=over
+
+=item wait_interval
+
+Base interval time (seconds).
+
+=item check_strategy_interval
+
+The interval count calling strategy's wait_correction() method.
+
+=item strategy
+
+=over
+
+=item class
+
+Specify strategy class name. When the strategy class name is beggining of 'MySQL::SustainableQuery::Strategy::',
+you can omit it likes 'ByLoad' or 'BalancedReplication'.
+
+=item args
+
+Arguments passed to strategy modules's new() method.
+
+=back
+
+=item log
+
+Specify logger object or code reference.
+
+=item exec_query
+
+Specify code rederence to execute query.
+
+=item terminate_condition
+
+Specify code rederence to judge which it can terminate or not.
+
+=back
+
+=head2 run()
+
+Execute query.
+
+=head2 setup()
+
+Internal uses.
+
+=head2 setup_log()
+
+Internal uses.
+
+=head2 setup_strategy()
+
+Internal uses.
=head1 AUTHOR
@@ -42,7 +42,7 @@ sub new {
$logger ||= sub {
my ( $level, @messages ) = @_;
for ( @messages ) {
- printf("[%s %s] %s\n", $level, POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime), $_);
+ print STDERR sprintf("[%s %s] %s\n", $level, POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime), $_);
};
};
@@ -83,6 +83,22 @@ MySQL::SustainableQuery::Log - Logger for MySQL::SustainableQuery
=head1 METHODS
+=head2 new( $logger )
+
+=head2 debug( @msg )
+
+=head2 info( @msg )
+
+=head2 notice( @msg )
+
+=head2 warning( @msg )
+
+=head2 error( @msg )
+
+=head2 alert( @msg )
+
+=head2 emergency( @msg )
+
=head1 AUTHOR
Toru Yamaguchi E<lt>zigorou@dena.jp<gt>
@@ -19,7 +19,7 @@ __END__
=head1 NAME
-MySQL::SustainableQuery::Strategy - write short description for MySQL::SustainableQuery::Strategy
+MySQL::SustainableQuery::Strategy - Strategy base class
=head1 SYNOPSIS
@@ -29,6 +29,10 @@ MySQL::SustainableQuery::Strategy - write short description for MySQL::Sustainab
=head1 METHODS
+=head2 new( %args )
+
+=head2 wait_correction( $query, $time_sum, $executed_count )
+
=head1 AUTHOR
Toru Yamaguchi E<lt>zigorou@dena.jp<gt>
@@ -66,6 +66,24 @@ MySQL::SustainableQuery::Strategy::BalancedReplication - write short description
=head1 METHODS
+=head1 METHODS
+
+=head2 new( %args )
+
+=over
+
+=item dbh
+
+L<DBI::db> object
+
+=item capable_behind_seconds
+
+=item on_error_scale_factor
+
+=back
+
+=head2 wait_correction( $query, $time_sum, $executed_count )
+
=head1 AUTHOR
Toru Yamaguchi E<lt>zigorou@dena.jp<gt>
@@ -40,6 +40,20 @@ MySQL::SustainableQuery::Strategy::ByLoad - write short description for MySQL::S
=head1 METHODS
+=head1 METHODS
+
+=head2 new( %args )
+
+=over
+
+=item load
+
+The ratio of executed time.
+
+=back
+
+=head2 wait_correction( $query, $time_sum, $executed_count )
+
=head1 AUTHOR
Toru Yamaguchi E<lt>zigorou@dena.jp<gt>
View
@@ -9,7 +9,6 @@ use MySQL::SustainableQuery;
sub create_query {
my $query = MySQL::SustainableQuery->new(@_);
- $query->setup;
$query;
}
View
@@ -8,7 +8,6 @@ use MySQL::SustainableQuery;
subtest 'setup_log() default' => sub {
my $query = MySQL::SustainableQuery->new;
lives_and {
- $query->setup_log;
isa_ok( $query->log, 'MySQL::SustainableQuery::Log' );
};
};
View
@@ -14,7 +14,6 @@ subtest 'Setup MySQL::SustainableQuery::Strategy::Byload' => sub {
});
lives_and {
- $query->setup_strategy;
isa_ok( $query->strategy, 'MySQL::SustainableQuery::Strategy::ByLoad' );
can_ok( $query->strategy, 'wait_correction' );
} 'setup_strategy() lives ok';
@@ -29,7 +28,6 @@ subtest 'Setup MySQL::SustainableQuery::Strategy::BalancedReplication' => sub {
});
lives_and {
- $query->setup_strategy;
isa_ok( $query->strategy, 'MySQL::SustainableQuery::Strategy::BalancedReplication' );
can_ok( $query->strategy, 'wait_correction' );
} 'setup_strategy() lives ok';
@@ -9,7 +9,6 @@ use MySQL::SustainableQuery;
sub create_query_and_strategy {
my $query = MySQL::SustainableQuery->new(@_);
- $query->setup;
my $strategy = $query->strategy;
return ( $query, $strategy );
@@ -7,7 +7,6 @@ use MySQL::SustainableQuery;
sub create_query_and_strategy {
my $query = MySQL::SustainableQuery->new(@_);
- $query->setup;
my $strategy = $query->strategy;
return ( $query, $strategy );

0 comments on commit 9f5bf70

Please sign in to comment.