lp1102300 auto-register .md5 variants of methods collab/phasefx/auto_md5_method
authorJason Etheridge <jason@esilibrary.com>
Mon, 21 Jan 2013 08:12:18 +0000 (03:12 -0500)
committerJason Etheridge <jason@esilibrary.com>
Mon, 21 Jan 2013 08:20:02 +0000 (03:20 -0500)
This only handles the Perl side at the moment, and probably very naively (for
example, opensrf.system.echo.atomic.md5 and opensrf.system.echo.md5 do not
return the same MD5's for the same input--though that's not important for my
purposes).  If someone wants to clean it up and implement the C side of it as
well, that would be awesome.

One downside is that at times, we will be doubling the amount of work done on
the server (whenever we call an .md5 method and then decide to call the real
thing), however, in the context of Evergreen, most servers that I'm aware of are
hardly ever under load, and instead it's network, data-size, and client issues
that cause poor responsiveness, so I think it's more important to send less data
over the wire when we can, and this will help with that.

Example:

srfsh# request open-ils.actor opensrf.system.echo.md5 "test"

Received Data: "303b5c8988601647873b4ffd247d83cb"

Signed-off-by: Jason Etheridge <jason@esilibrary.com>
src/perl/lib/OpenSRF/Application.pm

index 0a5c188..b6aa2c1 100644 (file)
@@ -12,6 +12,7 @@ use Time::HiRes qw/time/;
 use OpenSRF::EX qw/:try/;
 use Carp;
 use OpenSRF::Utils::JSON;
+use Digest::MD5 qw(md5 md5_hex md5_base64);
 
 sub DESTROY{};
 
@@ -447,6 +448,16 @@ sub register_method {
                method => 'make_stream_atomic',
                notes => "This is a system generated method.  Please see the definition for $args{api_name}",
        ) if ($args{stream});
+
+       __PACKAGE__->register_method(
+               stream => 0,
+               argc => $args{argc},
+               api_name => $args{api_name}.'.md5',
+               method => 'get_md5',
+               notes => "This is a system generated method.  Please see the definition for $args{api_name}",
+        md5 => 1
+       ) if (!$args{'md5'});
+
 }
 
 sub retrieve_remote_apis {
@@ -765,4 +776,19 @@ sub make_stream_atomic {
        return \@results;
 }
 
+sub get_md5 {
+    my $self = shift;
+    my $req = shift;
+    my @args = @_;
+
+    (my $m_name = $self->api_name) =~ s/\.md5$//o;
+    my $m = $self->method_lookup($m_name);
+
+    $m->session( $req->session );
+       my @results = $m->run(@args);
+       $m->session('');
+
+    return md5_hex(OpenSRF::Utils::JSON->perl2JSON(@results));
+}
+
 1;