store last table.name.username json for 15 min
[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 sub pg_insert {
28         my ( $self, $table, $h ) = @_;
29
30         my $redis = Redis->new;
31
32         my @c;
33
34         if ( my $cols = $redis->get("pg.$table") ) {
35                 @c = split(/\s+/,$cols);
36         } else {
37                 my $sth = $self->dbh->prepare( "select * from $table limit 1" );
38                 $sth->execute;
39                 @c = @{ $sth->{NAME_lc} };
40                 $redis->set( "pg.$table" => join(' ',@c) );
41                 $redis->expire( "pg.$table" => 5 * 60 ); # refresh every 5 min
42         }
43
44         my $sql = "INSERT INTO $table (" . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
45         warn $sql;
46         my $sth = $self->dbh->prepare($sql);
47
48         my $h_lc;
49         $h_lc->{ lc $_ } = $h->{$_} foreach keys %$h;
50
51         if ( my $username = $h->{username} ) {
52                 my $key = join('.', 'table', $table, $username);
53                 $redis->set( $key => $self->e_json($h) );
54                 $redis->expire( $key => 15 * 60 ); # 15 min timeout
55         }
56
57         $sth->execute( map { $h_lc->{$_} } @c );
58 }
59
60 sub insert : Job : Decode(d_json) : MinProcesses(0) {
61         my ( $self, $job, $workload ) = @_;
62         my $table = delete $workload->{_table} || die "no _table";
63         $self->pg_insert($table => $workload);
64 }
65
66 sub sql : Job : Encode(e_json) : MinProcesses(1) {
67         my ( $self, $job, $workload ) = @_;
68
69         my $sth = $self->dbh->prepare($workload);
70         my $rows = eval { $sth->execute };
71         return { error => $@ } if $@;
72
73         warn "# $rows rows get_username_table $workload\n";
74
75         $rows = $sth->fetchall_arrayref;
76         my @columns = @{ $sth->{NAME} };
77
78         # decode hash column
79         my $hash_col;
80         foreach ( 0 .. $#columns ) {
81                 $hash_col = $_ if $columns[$_] eq 'h';
82         }
83         if ( defined $hash_col ) {
84                 map {
85                         my $hash = $_->[$hash_col];
86                         $hash =~ s/\@/\\\@/g;
87                         $_->[$hash_col] = eval "{ $hash }";
88                         $_->[$hash_col] = "ERROR: $@ for $hash" if $@;
89                 } @$rows
90         }
91
92         return {
93                 columns => \@columns,
94                 rows => $rows,
95                 hash_col => $hash_col,
96         };
97 }
98
99 1;