Skip to content

Commit

Permalink
Use setup() method internally. Add pod.
Browse files Browse the repository at this point in the history
  • Loading branch information
zigorou committed Jun 7, 2011
1 parent 202fe77 commit 9f5bf70
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile.PL
Expand Up @@ -8,7 +8,7 @@ requires 'DBI';
requires 'POSIX';
requires 'Time::HiRes';

tests 't/*.t';
tests_recursive 't';
author_tests 'xt';

test_requires 'DBD::Mock';
Expand Down
84 changes: 81 additions & 3 deletions lib/MySQL/SustainableQuery.pm
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion lib/MySQL/SustainableQuery/Log.pm
Expand Up @@ -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), $_);
};
};

Expand Down Expand Up @@ -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>
Expand Down
6 changes: 5 additions & 1 deletion lib/MySQL/SustainableQuery/Strategy.pm
Expand Up @@ -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
Expand All @@ -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>
Expand Down
18 changes: 18 additions & 0 deletions lib/MySQL/SustainableQuery/Strategy/BalancedReplication.pm
Expand Up @@ -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>
Expand Down
14 changes: 14 additions & 0 deletions lib/MySQL/SustainableQuery/Strategy/ByLoad.pm
Expand Up @@ -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>
Expand Down
1 change: 0 additions & 1 deletion t/run.t
Expand Up @@ -9,7 +9,6 @@ use MySQL::SustainableQuery;

sub create_query {
my $query = MySQL::SustainableQuery->new(@_);
$query->setup;
$query;
}

Expand Down
1 change: 0 additions & 1 deletion t/setup_log.t
Expand Up @@ -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' );
};
};
Expand Down
2 changes: 0 additions & 2 deletions t/setup_strategy.t
Expand Up @@ -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';
Expand All @@ -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';
Expand Down
1 change: 0 additions & 1 deletion t/strategy/balanced_replication/wait_correction.t
Expand Up @@ -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 );
Expand Down
1 change: 0 additions & 1 deletion t/strategy/by_load/wait_correction.t
Expand Up @@ -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 );
Expand Down

0 comments on commit 9f5bf70

Please sign in to comment.