create indexes on inherited tables
[APKPM.git] / lib / APKPM / Store.pm
1 package APKPM::Store;
2
3 use base qw(Gearman::Driver::Worker);
4 use Moose;
5 use Time::HiRes;
6 use Data::Dump qw(dump);
7 use DBD::Pg;
8 use Redis;
9
10 with 'APKPM::Gearman';
11
12 sub prefix { 'Store_' }
13
14 sub process_name {
15         my ( $self, $orig, $job_name ) = @_;
16         warn "# process_name $orig $job_name\n";
17         return "$orig ($job_name)";
18 }
19
20 sub dbh {
21         DBI->connect_cached('DBI:Pg:dbname=apkpm','','', {
22                 RaiseError => 1,
23                 AutoCommit => 1,
24         });
25 }
26
27 our $redis;
28 sub redis { $redis ||= Redis->new };
29
30 sub _create_index {
31         my ( $self, $table, $timestamp ) = @_;
32
33         my $sth = $self->dbh->prepare( qq{
34
35 select
36     t.relname as table_name,
37     i.relname as index_name,
38     a.attname as column_name
39 from
40     pg_class t,
41     pg_class i,
42     pg_index ix,
43     pg_attribute a
44 where
45     t.oid = ix.indrelid
46     and i.oid = ix.indexrelid
47     and a.attrelid = t.oid
48     and a.attnum = ANY(ix.indkey)
49     and t.relkind = 'r'
50     and t.relname like ?
51 order by
52     t.relname,
53     i.relname;
54
55         } );
56         $sth->execute($table);
57         my $sql = '';
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|;
61         }
62
63         return $sql;
64 }
65
66 sub pg_insert {
67         my ( $self, $table, $h ) = @_;
68
69         my @c;
70
71         my $timestamp =
72                 exists $h->{timestamp} ? $h->{timestamp} :
73                 exists $h->{start}     ? $h->{start}     :
74                 warn "no timestamp partitioning on $table";
75
76         my $create_table;
77         if ( $timestamp ) {
78                 $timestamp =~ s/^(\d{4})-(\d{2})-(\d{2}).*$/$1_$2_$3/;
79                 my $part = $table . '_' . $timestamp;
80                 $create_table = qq{
81                         create table $part () inherits ($table) ;
82                 };
83                 $create_table .= $self->_create_index( $table, $timestamp );
84                 $table = $part;
85                 warn "# using partition $table";
86         }
87
88         if ( my $cols = $self->redis->get("pg.$table") ) {
89                 @c = split(/\s+/,$cols);
90         } else {
91                 my $sth = $self->dbh->prepare( "select * from $table limit 1" );
92                 eval { $sth->execute; };
93                 if ( $@ ) {
94                         warn "ERROR $@\n# try $create_table\n";
95                         $self->dbh->do( $create_table );
96                         $sth->execute;
97                 }
98
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
102         }
103
104         my $sql = "INSERT INTO $table (" . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
105         warn $sql;
106         my $sth = $self->dbh->prepare($sql);
107
108         my $h_lc;
109         $h_lc->{ lc $_ } = $h->{$_} foreach keys %$h;
110
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
115         }
116
117         $h_lc->{h} = $self->to_hstore( $h_lc->{h} ) if exists $h_lc->{h};
118
119         $sth->execute( map { $h_lc->{$_} } @c );
120 }
121
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);
126 }
127
128 sub sql : Job : Encode(e_json) : MinProcesses(1) {
129         my ( $self, $job, $workload ) = @_;
130
131         my $sth = $self->dbh->prepare($workload);
132         my $rows = eval { $sth->execute };
133         return { error => $@ } if $@;
134
135         warn "# $rows rows get_username_table $workload\n";
136
137         $rows = $sth->fetchall_arrayref;
138         my @columns = @{ $sth->{NAME} };
139
140         # decode hash column
141         my $hash_col;
142         foreach ( 0 .. $#columns ) {
143                 $hash_col = $_ if $columns[$_] eq 'h';
144         }
145         if ( defined $hash_col ) {
146                 map {
147                         my $hash = $_->[$hash_col];
148                         $hash =~ s/\@/\\\@/g;
149                         $_->[$hash_col] = eval "{ $hash }";
150                         $_->[$hash_col] = "ERROR: $@ for $hash" if $@;
151                 } @$rows
152         }
153
154         return {
155                 columns => \@columns,
156                 rows => $rows,
157                 hash_col => $hash_col,
158         };
159 }
160
161 1;