--- lib/PXElator/CouchDB.pm 2009/08/12 19:49:36 205 +++ lib/PXElator/CouchDB.pm 2009/08/12 22:27:57 206 @@ -8,6 +8,10 @@ use LWP::UserAgent; use JSON; use Data::Dump qw/dump/; +use Time::HiRes qw/time/; +use Data::Structure::Util qw(unbless); +use Scalar::Util qw/blessed/; +use Storable qw/dclone/; sub new { my ($class, $host, $port, $options) = @_; @@ -65,18 +69,21 @@ sub get { my ($self, $url) = @_; - from_json $self->request(GET => $url); + JSON->new->utf8->decode( $self->request(GET => $url) ); } sub put { my ($self, $url, $json) = @_; warn "put $url ",dump($json); - $rev->{$url} ||= eval { $self->get( $url )->{_rev} }; + if ( ! defined $json->{_rev} ) { + my $old = eval { $self->get( $url )->{_rev} }; + $rev->{$url} = $json->{_rev} = $old if defined $old; + } - $json->{_rev} = $rev->{$url} if $rev->{$url}; + $json = unbless dclone $json if blessed $json; - $json = to_json $json if $json; + $json = JSON->new->utf8->encode( $json ) if $json; $self->request(PUT => $url, $json); } @@ -87,4 +94,27 @@ $self->request(POST => $url, $json); } +our $audit = __PACKAGE__->new; + +sub audit { + my $data = pop @_; + + my $url = join(' ', @_); + $url =~ s/-\S+//g; + $url =~ s/\W+/-/g; + + my ( $package, undef, $line, $sub ) = caller(1); + ( $package, undef, $line ) = caller(0) if $package eq 'main'; + + $data->{x_meta} = { + 'ident' => [ @_ ], + 'time' => time(), + 'package' => $package, + 'line' => $line, + 'sub' => $sub, + }; + + $audit->put( "pxelator/$package.$url", $data ); +} + 1;