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, $table, $h ) = @_;
72 exists $h->{timestamp} ? $h->{timestamp} :
73 exists $h->{start} ? $h->{start} :
74 warn "no timestamp partitioning on $table";
78 $timestamp =~ s/^(\d{4})-(\d{2})-(\d{2}).*$/$1_$2_$3/;
79 my $part = $table . '_' . $timestamp;
81 create table $part () inherits ($table) ;
83 $create_table .= $self->_create_index( $table, $timestamp );
85 warn "# using partition $table";
88 if ( my $cols = $self->redis->get("pg.$table") ) {
89 @c = split(/\s+/,$cols);
91 my $sth = $self->dbh->prepare( "select * from $table limit 1" );
92 eval { $sth->execute; };
94 warn "ERROR $@\n# try $create_table\n";
95 $self->dbh->do( $create_table );
99 @c = @{ $sth->{NAME_lc} };
100 $self->redis->set( "pg.$table" => join(' ',@c) );
101 $self->redis->expire( "pg.$table" => 15 * 60 ); # refresh every 15 min
104 my $sql = "INSERT INTO $table (" . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
106 my $sth = $self->dbh->prepare($sql);
109 $h_lc->{ lc $_ } = $h->{$_} foreach keys %$h;
111 if ( my $username = $h->{username} ) {
112 my $key = join('.', 'table', $table, $username);
113 $self->redis->set( $key => $self->e_json($h) );
114 $self->redis->expire( $key => 15 * 60 ); # 15 min timeout
117 $h_lc->{h} = $self->to_hstore( $h_lc->{h} ) if exists $h_lc->{h};
119 $sth->execute( map { $h_lc->{$_} } @c );
122 sub insert : Job : Decode(d_json) : MinProcesses(0) {
123 my ( $self, $job, $workload ) = @_;
124 my $table = delete $workload->{_table} || die "no _table";
125 $self->pg_insert($table => $workload);
128 sub sql : Job : Encode(e_json) : MinProcesses(1) {
129 my ( $self, $job, $workload ) = @_;
131 my $sth = $self->dbh->prepare($workload);
132 my $rows = eval { $sth->execute };
133 return { error => $@ } if $@;
135 warn "# $rows rows get_username_table $workload\n";
137 $rows = $sth->fetchall_arrayref;
138 my @columns = @{ $sth->{NAME} };
142 foreach ( 0 .. $#columns ) {
143 $hash_col = $_ if $columns[$_] eq 'h';
145 if ( defined $hash_col ) {
147 my $hash = $_->[$hash_col];
148 $hash =~ s/\@/\\\@/g;
149 $_->[$hash_col] = eval "{ $hash }";
150 $_->[$hash_col] = "ERROR: $@ for $hash" if $@;
155 columns => \@columns,
157 hash_col => $hash_col,