3 use base qw(Gearman::Driver::Worker);
6 use Data::Dump qw(dump);
10 with 'APKPM::Gearman';
12 sub prefix { 'Store_' }
15 my ( $self, $orig, $job_name ) = @_;
16 warn "# process_name $orig $job_name\n";
17 return "$orig ($job_name)";
21 DBI->connect_cached('DBI:Pg:dbname=apkpm','','', {
28 sub redis { $redis ||= Redis->new };
31 my ( $self, $table, $timestamp ) = @_;
33 my $sth = $self->dbh->prepare( qq{
36 t.relname as table_name,
37 i.relname as index_name,
38 a.attname as column_name
46 and i.oid = ix.indexrelid
47 and a.attrelid = t.oid
48 and a.attnum = ANY(ix.indkey)
56 $sth->execute($table);
58 while( my $row = $sth->fetchrow_hashref ) {
59 #warn "# _create_index $table ",dump($row);
60 $sql .= qq|create index $row->{index_name}_$timestamp on ${table}_$timestamp($row->{column_name});\n|;
67 my ( $self, $base_table, $h ) = @_;
69 my $table = $base_table;
74 exists $h->{timestamp} ? $h->{timestamp} :
75 exists $h->{start} ? $h->{start} :
76 warn "no timestamp partitioning on $table";
80 $timestamp =~ s/^(\d{4})-(\d{2})-(\d{2}).*$/$1_$2_$3/;
81 my $part = $table . '_' . $timestamp;
83 create table $part () inherits ($table) ;
85 $create_table .= $self->_create_index( $table, $timestamp );
87 warn "# using partition $table";
90 if ( my $cols = $self->redis->get("pg.$table") ) {
91 @c = split(/\s+/,$cols);
93 my $sth = $self->dbh->prepare( "select * from $table limit 1" );
94 eval { $sth->execute; };
96 warn "ERROR $@\n# try $create_table\n";
97 $self->dbh->do( $create_table );
101 @c = @{ $sth->{NAME_lc} };
102 $self->redis->set( "pg.$table" => join(' ',@c) );
103 $self->redis->expire( "pg.$table" => 15 * 60 ); # refresh every 15 min
106 my $sql = "INSERT INTO $table (" . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
108 my $sth = $self->dbh->prepare($sql);
111 $h_lc->{ lc $_ } = $h->{$_} foreach keys %$h;
113 if ( my $username = $h->{username} ) {
114 my $key = join('.', 'table', $base_table, $username);
115 $self->redis->set( $key => $self->e_json($h) );
116 $self->redis->expire( $key => 15 * 60 ); # 15 min timeout
119 $h_lc->{h} = $self->to_hstore( $h_lc->{h} ) if exists $h_lc->{h};
121 $sth->execute( map { $h_lc->{$_} } @c );
124 sub insert : Job : Decode(d_json) : MinProcesses(0) {
125 my ( $self, $job, $workload ) = @_;
126 my $table = delete $workload->{_table} || die "no _table";
127 $self->pg_insert($table => $workload);
130 sub sql : Job : Encode(e_json) : MinProcesses(1) {
131 my ( $self, $job, $workload ) = @_;
133 my $sth = $self->dbh->prepare($workload);
134 my $rows = eval { $sth->execute };
135 return { error => $@ } if $@;
137 warn "# $rows rows get_username_table $workload\n";
139 $rows = $sth->fetchall_arrayref;
140 my @columns = @{ $sth->{NAME} };
144 foreach ( 0 .. $#columns ) {
145 $hash_col = $_ if $columns[$_] eq 'h';
147 if ( defined $hash_col ) {
149 my $hash = $_->[$hash_col];
150 $hash =~ s/\@/\\\@/g;
151 $_->[$hash_col] = eval "{ $hash }";
152 $_->[$hash_col] = "ERROR: $@ for $hash" if $@;
157 columns => \@columns,
159 hash_col => $hash_col,