encode hstore for PostgreSQL in Store_insert
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 12 Jul 2011 09:55:50 +0000 (11:55 +0200)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 12 Jul 2011 09:57:40 +0000 (11:57 +0200)
lib/APKPM/CPE.pm
lib/APKPM/DSLAM.pm
lib/APKPM/Store.pm
persistant_worker.pl
t/Store.t

index 99b479f..9b61a7e 100644 (file)
@@ -130,7 +130,7 @@ sub info : Job : Decode(d_array) : Encode(e_json) {
                                ip => $ip,
                                username => $username,
                                timestamp => $self->datetime_now,
-                               h => $self->to_hstore( $ret ),
+                               h => $ret,
                        });
                        $redis->sadd( "CPE.$vendor.ok" => $ip );
                } else {
index 3a99340..e68ba6d 100644 (file)
@@ -59,7 +59,7 @@ sub search : Job : Encode(e_json_encode) {
                ip => $crm->{IP_MANAGEMENT}, # FIXME IP_UREDAJA ?
                username => $crm->{USERNAME},
                timestamp => $time,
-               h => $self->to_hstore($hash),
+               h => $hash,
        });
 
        return $hash;
index def1d83..e771543 100644 (file)
@@ -24,21 +24,22 @@ sub dbh {
        });
 }
 
+our $redis;
+sub redis { $redis ||= Redis->new };
+
 sub pg_insert {
        my ( $self, $table, $h ) = @_;
 
-       my $redis = Redis->new;
-
        my @c;
 
-       if ( my $cols = $redis->get("pg.$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;
                @c = @{ $sth->{NAME_lc} };
-               $redis->set( "pg.$table" => join(' ',@c) );
-               $redis->expire( "pg.$table" => 5 * 60 ); # refresh every 5 min
+               $self->redis->set( "pg.$table" => join(' ',@c) );
+               $self->redis->expire( "pg.$table" => 15 * 60 ); # refresh every 15 min
        }
 
        my $sql = "INSERT INTO $table (" . join(',',@c) . ') values (' . join(',', map { '?' } 0 .. $#c) . ')';
@@ -50,10 +51,12 @@ sub pg_insert {
 
        if ( my $username = $h->{username} ) {
                my $key = join('.', 'table', $table, $username);
-               $redis->set( $key => $self->e_json($h) );
-               $redis->expire( $key => 15 * 60 ); # 15 min timeout
+               $self->redis->set( $key => $self->e_json($h) );
+               $self->redis->expire( $key => 15 * 60 ); # 15 min timeout
        }
 
+       $h_lc->{h} = $self->to_hstore( $h_lc->{h} ) if exists $h_lc->{h};
+
        $sth->execute( map { $h_lc->{$_} } @c );
 }
 
index 647b2bd..60b60b1 100755 (executable)
@@ -114,7 +114,7 @@ foreach my $ip ( keys %$poll ) {
                        username => $username,
                        timestamp => $self->datetime_now,
                        variant => $variant,
-                       h => $self->to_hstore($hash),
+                       h => $hash,
                });
 
                return "ok $username $ip $port"; # body end
index 54d53ae..0a309f6 100755 (executable)
--- a/t/Store.t
+++ b/t/Store.t
@@ -2,7 +2,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 12;
+use Test::More tests => 16;
 use Data::Dump qw(dump);
 
 use lib 'lib';
@@ -19,6 +19,21 @@ rtt => 0.042,
 }), 'ping';
 diag dump($r);
 
+ok my $r = $o->insert('job',{
+_table => 'cpe_Davolink',
+ip => '127.0.0.1',
+username => 'nobody',
+h => {
+       foo => 1,
+       bar => 2,
+}
+}), 'ping';
+
+ok( my $r = $o->redis->get( 'table.cpe_Davolink.nobody' ), 'redis' );
+ok( my $j = $o->d_json($r), 'd_json' );
+isa_ok( $j->{h}, 'HASH', 'hstore' );
+diag dump($j);
+
 ok my $r = $o->sql('job', "select username,rtt from ping limit 2"), 'sql';
 diag dump($r);