partition tables using date
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2011 19:00:57 +0000 (20:00 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 5 Dec 2011 19:00:57 +0000 (20:00 +0100)
lib/APKPM/Store.pm
t/Store.t

index e771543..e04fda9 100644 (file)
@@ -32,11 +32,33 @@ sub pg_insert {
 
        my @c;
 
+       my $timestamp =
+               exists $h->{timestamp} ? $h->{timestamp} :
+               exists $h->{start}     ? $h->{start}     :
+               warn "no timestamp partitioning on $table";
+
+       my $create_table;
+       if ( $timestamp ) {
+               $timestamp =~ s/^(\d{4})-(\d{2})-(\d{2}).*$/$1_$2_$3/;
+               my $part = $table . '_' . $timestamp;
+               $create_table = qq{
+                       create table $part () inherits ($table) ;
+               };
+               $table = $part;
+               warn "# using partition $table";
+       }
+
        if ( my $cols = $self->redis->get("pg.$table") ) {
                @c = split(/\s+/,$cols);
        } else {
                my $sth = $self->dbh->prepare( "select * from $table limit 1" );
-               $sth->execute;
+               eval { $sth->execute; };
+               if ( $@ ) {
+                       warn "ERROR $@\n# try $create_table\n";
+                       $self->dbh->do( $create_table );
+                       $sth->execute;
+               }
+
                @c = @{ $sth->{NAME_lc} };
                $self->redis->set( "pg.$table" => join(' ',@c) );
                $self->redis->expire( "pg.$table" => 15 * 60 ); # refresh every 15 min
index 0a309f6..2e1004a 100755 (executable)
--- a/t/Store.t
+++ b/t/Store.t
@@ -16,6 +16,7 @@ _table => 'ping',
 ip => '127.0.0.1',
 username => 'nobody',
 rtt => 0.042,
+timestamp => '2011-12-05 01:01:01',
 }), 'ping';
 diag dump($r);
 
@@ -26,7 +27,8 @@ username => 'nobody',
 h => {
        foo => 1,
        bar => 2,
-}
+},
+start => '2011-12-05 01:01:01',
 }), 'ping';
 
 ok( my $r = $o->redis->get( 'table.cpe_Davolink.nobody' ), 'redis' );