App-Repository
view release on metacpan or search on metacpan
lib/App/Repository.pm view on Meta::CPAN
#############################################################################
# METHODS
#############################################################################
=head1 Methods: Locking (Concurrency Management)
=cut
# this is a write lock for the table
sub _lock_table {
&App::sub_entry if ($App::trace);
my ($self, $table) = @_;
if (! $self->{locked}) { # I have locked it myself, so I don't need to again
my ($name, $dbname, $context, $rlock);
$name = $self->{name};
$dbname = $self->{dbname};
$context = $self->{context};
$rlock = $context->resource_locker($name); # get the one that corresponds to this repository
$rlock->lock("db.$dbname.$table");
$self->{locked} = 1;
}
&App::sub_exit() if ($App::trace);
}
# unlocks the write lock for the table
sub _unlock_table {
&App::sub_entry if ($App::trace);
my ($self, $table) = @_;
if ($self->{locked}) {
my ($name, $dbname, $context, $rlock);
$name = $self->{name};
$dbname = $self->{dbname};
$context = $self->{context};
$rlock = $context->resource_locker($name); # get the one that corresponds to this repository
$rlock->unlock("db.$dbname.$table");
delete $self->{locked};
}
&App::sub_exit() if ($App::trace);
}
#############################################################################
# METHODS
#############################################################################
=head1 Methods: Miscellaneous
=cut
#############################################################################
# summarize_rows()
#############################################################################
=head2 summarize_rows()
* Signature: $summarized_rows = $rep->summarize_rows($table, $rows, $columns, $summary_keys, $options);
* Param: $table string
* Param: $rows [][]
* Param: $columns []
* Param: $summary_keys []
* Param: $formulas {}
* Return: $summarized_rows []
* Throws: App::Exception::Repository
* Since: 0.01
Sample Usage:
@rows = (
[ 5, "Jim", "Green", 13.5, 320, ],
[ 3, "Bob", "Green", 4.2, 230, ],
[ 9, "Ken", "Green", 27.4, 170, ],
[ 2, "Kim", "Blue", 11.7, 440, ],
[ 7, "Jan", "Blue", 55.1, 90, ],
[ 1, "Ben", "Blue", 22.6, 195, ],
);
@columns = ( "id", "name", "team", "rating", "score" );
@summary_keys = ( "team" );
$summarized_rows = $rep->summarize_rows(\@rows, \@columns, \@summary_keys, \%formulas);
@rows = (
{ id=>5, name=>"Jim", team=>"Green", rating=>13.5, score=>320, },
{ id=>3, name=>"Bob", team=>"Green", rating=> 4.2, score=>230, },
{ id=>9, name=>"Ken", team=>"Green", rating=>27.4, score=>170, },
{ id=>2, name=>"Kim", team=>"Blue", rating=>11.7, score=>440, },
{ id=>7, name=>"Jan", team=>"Blue", rating=>55.1, score=> 90, },
{ id=>1, name=>"Ben", team=>"Blue", rating=>22.6, score=>195, },
);
@columns = ( "rating", "score" ); # summarize a subset of the columns
@summary_keys = ( "team" );
%options = (
ext_summaries => \%summaries, # extended summaries
ext_summary_columns => [ "rating", "score", "team", ], # optional
ext_summary_functions => { # optional
sum => 1,
count => 1,
sum_sq => 1,
distinct => 1,
min => 1,
max => 1,
average => 1, # requires sum, count
median => 1, # requires distinct
mode => 1, # requires min, max
stddev => 1, # requires sum, sum_sq, count
},
);
# returns the "natural" summaries
$summarized_rows = $rep->summarize_rows(\@rows, \@columns, \@summary_keys, \%options);
=cut
sub summarize_rows {
&App::sub_entry if ($App::trace);
my ($self, $table, $rows, $columns, $summary_keys, $options) = @_;
#print STDERR "summarize_rows($table, ..., ..., [@$summary_keys], ...)\n";
#foreach my $row (@$rows) {
# print STDERR "summarize_rows(IN) {", join("|", %$row), "}\n";
#}
$summary_keys = [] if (!$summary_keys);
my $table_def = $self->get_table_def($table);
my $column_defs = $table_def->{column};
my ($ext_summaries, $ext_column_summary, $ext_summary_columns, $ext_summary_functions);
$ext_summaries = $options->{ext_summaries};
if ($ext_summaries) {
$ext_summary_columns = $options->{ext_summary_columns};
$ext_summary_functions = $options->{ext_summary_functions} || { # do all of them
count => 1,
distinct => 1,
sum => 1,
sum_sq => 1,
min => 1,
max => 1,
average => 1, # requires sum, count
median => 1, # requires distinct
( run in 0.686 second using v1.01-cache-2.11-cpan-df04353d9ac )