storage server stuff
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 16 Mar 2005 01:58:29 +0000 (01:58 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 16 Mar 2005 01:58:29 +0000 (01:58 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@357 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/Driver/Pg.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/FTS.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/actor.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/config.pm
Open-ILS/src/perlmods/OpenILS/Application/Storage/Publisher/metabib.pm
Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm

index 5da85c9..9781f1e 100644 (file)
@@ -24,6 +24,13 @@ sub child_init {
                  WHERE %s = ?
        SQL
 
+       __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<"  SQL", 'Main');
+               SELECT  %s
+                 FROM  %s
+                 WHERE %s ~ ?
+                 ORDER BY %s
+       SQL
+
        __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<"      SQL", 'Main');
                SELECT  %s
                  FROM  %s
@@ -43,14 +50,15 @@ sub fast_flesh_sth {
        my $field = shift;
        my $value = shift;
        my $order = shift;
+       my $like = shift;
+
 
        if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
-               if (defined($value) and ref($order) and ref($order) eq 'HASH') {
+               if (defined($value) and ref($value) and ref($value) eq 'HASH') {
                        $order = $value;
-                       $value = $field;
-                       $field = $class->primary_column;
+                       $value = undef;
                } else {
-                       $order = { order_by => 'id' }
+                       $order = { order_by => $class->columns('Primary') }
                }
        }
 
@@ -59,10 +67,23 @@ sub fast_flesh_sth {
                $field = $class->primary_column;
        }
 
+       unless (defined $field) {
+               $field = $class->primary_column;
+       }
+
+       unless ($order->{order_by}) {
+               $order = { order_by => $class->columns('Primary') }
+       }
+
        my $fm_class = 'Fieldmapper::'.$class;
        my $field_list = join ',', $class->columns('All');
        
-       my $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
+       my $sth;
+       if (!$like) {
+               $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
+       } else {
+               $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
+       }
        $sth->execute($value);
        return $sth;
 }
@@ -74,13 +95,16 @@ sub fast_flesh {
 
 sub fast_fieldmapper {
        my $self = shift;
+       my $id = shift;
+       my $col = shift;
+       my $like = shift;
        my $class = ref($self) || $self;
        my $fm_class = 'Fieldmapper::'.$class;
        my @fms;
        $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
-       for my $hash ($self->fast_flesh_sth(map {"$_"} @_)->fetchall_hash) {
+       for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
                my $fm = $fm_class->new;
-               for my $field ( keys %$hash ) {
+               for my $field ( $fm_class->real_fields ) {
                        $fm->$field( $$hash{$field} );
                }
                push @fms, $fm;
index 95b88f6..3e29c67 100644 (file)
                $self->{fts_query} = ["to_tsquery('default',$newterm)"];
                $self->{fts_query_nots} = [];
                $self->{fts_op} = '@@';
+               $self->{text_col} = shift;
+               $self->{fts_col} = shift;
 
                return $self;
        }
 
        sub sql_where_clause {
                my $self = shift;
-               my $column = shift;
+               my $column = $self->fts_col;
                my @output;
        
                my @ranks;
                for my $fts ( $self->fts_query ) {
-                       push @output, join(' ', $column, $self->{fts_op}, $fts);
+                       push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
                        push @ranks, "rank($column, $fts)";
                }
                $self->{fts_rank} = \@ranks;
        
-               return join(' AND ', @output);
+               my $phrase_match = $self->sql_exact_phrase_match();
+               return join(' AND ', @output) . $phrase_match;
        }
 
 }
 
        sub current_xact_is_auto {
                my $self = shift;
-               return $_xact_session->session_data(autocommit => shift());
+               my $auto = shift;
+               if (defined($_xact_session) and ref($_xact_session)) {
+                       if (defined $auto) {
+                               $_xact_session->session_data(autocommit => $auto);
+                       }
+                       return $_xact_session->session_data('autocommit'); 
+               }
        }
 
        sub current_xact_id {
                        my $dc_cb = $client->session->register_callback(
                                disconnect => sub {
                                        my $ses = shift;
-                                       $ses->unregister_callback($death_cb);
+                                       $ses->unregister_callback(death => $death_cb);
                                        __PACKAGE__->pg_commit_xaction;
                                }
                        );
                        return 0;
                };
                
-               $pg->current_xact_session->unregister_callback(
+               $pg->current_xact_session->unregister_callback( death => 
                        $pg->current_xact_session->session_data( 'death_cb' )
-               );
+               ) if ($pg->current_xact_session);
 
                if ($pg->current_xact_is_auto) {
-                       $pg->current_xact_session->unregister_callback(
+                       $pg->current_xact_session->unregister_callback( disconnect => 
                                $pg->current_xact_session->session_data( 'disconnect_cb' )
                        );
                }
                        return 0;
                };
        
-               $pg->current_xact_session->unregister_callback(
+               $pg->current_xact_session->unregister_callback( death =>
                        $pg->current_xact_session->session_data( 'death_cb' )
-               );
+               ) if ($pg->current_xact_session);
 
                if ($pg->current_xact_is_auto) {
-                       $pg->current_xact_session->unregister_callback(
+                       $pg->current_xact_session->unregister_callback( disconnect =>
                                $pg->current_xact_session->session_data( 'disconnect_cb' )
                        );
                }
index d118a33..5d597d0 100644 (file)
@@ -5,8 +5,27 @@ my $log = 'OpenSRF::Utils::Logger';
 package OpenILS::Application::Storage::FTS;
 use OpenSRF::Utils::Logger qw/:level/;
 
-sub compile {
-       die "You must override me somewhere!";
+sub default_compile {
+
+       $log->debug("You must override me somewhere, or I will make searching really slow!!!!",ERROR);;
+
+       my $self = shift;
+       my $term = shift;
+
+       $self = ref($self) || $self;
+       $self = bless {} => $self;
+
+       $self->decompose($term);
+
+       for my $part ( $self->words, $self->phrases ) {
+               $part = OpenILS::Application::Storage::CDBI->quote($part);
+               push @{ $self->{ fts_query } },   "'\%$part\%'";
+       }
+
+       for my $part ( $self->nots ) {
+               $part = OpenILS::Application::Storage::CDBI->quote($part);
+               push @{ $self->{ fts_query_not } },   "'\%$part\%'";
+       }
 }
 
 sub decompose {
@@ -30,7 +49,7 @@ sub decompose {
        $log->debug("Stripped nots are[".join(', ',@nots)."]",DEBUG);
 
        my @parts;
-       while ($term =~ s/ ("+) (.*?) ((?<!\\)"){1} //x) {
+       while ($term =~ s/ ((?<!\\)"{1}) (.*?) ((?<!\\)"){1} //x) {
                my $part = $2;
                $part =~ s/^\s*//o;
                $part =~ s/\s*$//o;
@@ -39,17 +58,7 @@ sub decompose {
        }
 
        $self->{ fts_op } = 'ILIKE';
-
-       for my $part ( @words, @parts ) {
-               $part = OpenILS::Application::Storage::CDBI->quote($part);
-               push @{ $self->{ fts_query } },   "'\%$part\%'";
-       }
-
-       for my $part ( @nots ) {
-               $part = OpenILS::Application::Storage::CDBI->quote($part);
-               push @{ $self->{ fts_query_not } },   "'\%$part\%'";
-       }
-
+       $self->{ fts_col } = $self->{ text_col } = 'value';
        $self->{ raw } = $term;
        $self->{ words } = \@words;
        $self->{ nots } = \@nots;
@@ -78,6 +87,16 @@ sub raw {
        return $self->{raw};
 }
 
+sub fts_col {
+       my $self = shift;
+       return $self->{fts_col};
+}
+
+sub text_col {
+       my $self = shift;
+       return $self->{text_col};
+}
+
 sub phrases {
        my $self = shift;
        return wantarray ? @{ $self->{phrases} } : $self->{phrases};
@@ -95,7 +114,7 @@ sub nots {
 
 sub sql_exact_phrase_match {
        my $self = shift;
-       my $column = shift;
+       my $column = $self->text_col;
        my $output = '';
        for my $phrase ( $self->phrases ) {
                $phrase =~ s/%/\\%/go;
@@ -110,8 +129,9 @@ sub sql_exact_phrase_match {
 
 sub sql_exact_word_bump {
        my $self = shift;
-       my $column = shift;
        my $bump = shift || '0.1';
+
+       my $column = $self->text_col;
        my $output = '';
        for my $word ( $self->words ) {
                $word =~ s/%/\\%/go;
@@ -126,18 +146,18 @@ sub sql_exact_word_bump {
 
 sub sql_where_clause {
        my $self = shift;
-       my $column = shift;
        my @output;
 
        for my $fts ( $self->fts_query ) {
-               push @output, join(' ', $column, $self->{fts_op}, $fts);
+               push @output, join(' ', $self->fts_col, $self->{fts_op}, $fts);
        }
 
-       for my $fts ( $self->fts_query_nots ) {
-               push @output, 'NOT (' . join(' ', $column, $self->{fts_op}, $fts) . ')';
+       for my $fts ( $self->fts_query_not ) {
+               push @output, 'NOT (' . join(' ', $self->fts_col, $self->{fts_op}, $fts) . ')';
        }
 
-       return join(' AND ', @output);
+       my $phrase_match = $self->sql_exact_phrase_match();
+       return join(' AND ', @output); 
 }
 
 #-------------------------------------------------------------------------------
index 16f2d53..1129713 100644 (file)
@@ -34,11 +34,27 @@ sub retrieve_node {
                my ($rec) = $cdbi->fast_fieldmapper($id);
                $client->respond( $rec ) if ($rec);
 
-               last if ($self->api_name !~ /list/o);
+               last if ($self->api_name !~ /batch/o);
        }
        return undef;
 }
 
+sub search {
+       my $self = shift;
+       my $client = shift;
+       my $term = shift;
+
+       (my $search_type = $self->api_name) =~ s/.*\.(search[^.]*).*/$1/o;
+       (my $col = $self->api_name) =~ s/.*\.$search_type\.([^.]+).*/$1/;
+       my $cdbi = $self->{cdbi};
+       $log->debug("Searching $cdbi for $col using type $search_type, value '$term'",DEBUG);
+
+       my $like = 0;
+       $like = 1 if ($search_type =~ /like$/o);
+
+       return [ $cdbi->fast_fieldmapper($term,$col,$like) ];
+}
+
 
 sub create_node {
        my $self = shift;
@@ -129,12 +145,33 @@ for my $fmclass ( Fieldmapper->classes ) {
        unless ( __PACKAGE__->is_registered( $api_prefix.'.batch.retrieve' ) ) {
                __PACKAGE__->register_method(
                        api_name        => $api_prefix.'.batch.retrieve',
-                       method          => 'batch_call',
+                       method          => 'retrieve_node',
                        api_level       => 1,
+                       stream          => 1,
                        cdbi            => $cdbi,
                );
        }
 
+       for my $field ($fmclass->real_fields) {
+               unless ( __PACKAGE__->is_registered( $api_prefix.'.search.'.$field ) ) {
+                       __PACKAGE__->register_method(
+                               api_name        => $api_prefix.'.search.'.$field,
+                               method          => 'search',
+                               api_level       => 1,
+                               cdbi            => $cdbi,
+                       );
+               }
+               unless ( __PACKAGE__->is_registered( $api_prefix.'.search_like.'.$field ) ) {
+                       __PACKAGE__->register_method(
+                               api_name        => $api_prefix.'.search_like.'.$field,
+                               method          => 'search',
+                               api_level       => 1,
+                               cdbi            => $cdbi,
+                       );
+               }
+       }
+
+
        # Create the create method
        unless ( __PACKAGE__->is_registered( $api_prefix.'.create' ) ) {
                __PACKAGE__->register_method(
index cddf96a..c5fa3ef 100644 (file)
@@ -27,44 +27,44 @@ sub get_user_record {
        }
        return undef;
 }
-__PACKAGE__->register_method(
-       method          => 'get_user_record',
-       api_name        => 'open-ils.storage.actor.user.retrieve',
-       api_level       => 1,
-       argc            => 1,
-);
-__PACKAGE__->register_method(
-       method          => 'get_user_record',
-       api_name        => 'open-ils.storage.actor.user.search.username',
-       api_level       => 1,
-       argc            => 1,
-);
-__PACKAGE__->register_method(
-       method          => 'get_user_record',
-       api_name        => 'open-ils.storage.actor.user.search.userid',
-       api_level       => 1,
-       argc            => 1,
-);
-__PACKAGE__->register_method(
-       method          => 'get_user_record',
-       api_name        => 'open-ils.storage.actor.user.retrieve.list',
-       api_level       => 1,
-       argc            => 1,
-);
-__PACKAGE__->register_method(
-       method          => 'get_user_record',
-       api_name        => 'open-ils.storage.actor.user.search.username.list',
-       api_level       => 1,
-       stream          => 1,
-       argc            => 1,
-);
-__PACKAGE__->register_method(
-       method          => 'get_user_record',
-       api_name        => 'open-ils.storage.actor.user.search.userid.list',
-       api_level       => 1,
-       stream          => 1,
-       argc            => 1,
-);
+#__PACKAGE__->register_method(
+       #method         => 'get_user_record',
+       #api_name       => 'open-ils.storage.actor.user.retrieve',
+       #api_level      => 1,
+       #argc           => 1,
+#);
+#__PACKAGE__->register_method(
+       #method         => 'get_user_record',
+       #api_name       => 'open-ils.storage.actor.user.search.username',
+       #api_level      => 1,
+       #argc           => 1,
+#);
+#__PACKAGE__->register_method(
+       #method         => 'get_user_record',
+       #api_name       => 'open-ils.storage.actor.user.search.userid',
+       #api_level      => 1,
+       #argc           => 1,
+#);
+#__PACKAGE__->register_method(
+       #method         => 'get_user_record',
+       #api_name       => 'open-ils.storage.actor.user.retrieve.list',
+       #api_level      => 1,
+       #argc           => 1,
+#);
+#__PACKAGE__->register_method(
+       #method         => 'get_user_record',
+       #api_name       => 'open-ils.storage.actor.user.search.username.list',
+       #api_level      => 1,
+       #stream         => 1,
+       #argc           => 1,
+#);
+#__PACKAGE__->register_method(
+       #method         => 'get_user_record',
+       #api_name       => 'open-ils.storage.actor.user.search.userid.list',
+       #api_level      => 1,
+       #stream         => 1,
+       #argc           => 1,
+#);
 
 sub update_user_record {
         my $self = shift;
@@ -75,12 +75,12 @@ sub update_user_record {
         return 0 unless ($rec);
         return 1;
 }
-__PACKAGE__->register_method(
-        method          => 'update_user_record',
-        api_name        => 'open-ils.storage.actor.user.update',
-        api_level       => 1,
-        argc            => 1,
-);
+#__PACKAGE__->register_method(
+        #method          => 'update_user_record',
+        #api_name        => 'open-ils.storage.actor.user.update',
+        #api_level       => 1,
+        #argc            => 1,
+#);
 
 sub delete_record_entry {
         my $self = shift;
@@ -91,11 +91,11 @@ sub delete_record_entry {
        return 0 unless ($rec);
         return 1;
 }
-__PACKAGE__->register_method(
-        method          => 'delete_user_record',
-        api_name        => 'open-ils.storage.actor.user.delete',
-        api_level       => 1,
-        argc            => 1,
-);
+#__PACKAGE__->register_method(
+        #method          => 'delete_user_record',
+        #api_name        => 'open-ils.storage.actor.user.delete',
+        #api_level       => 1,
+        #argc            => 1,
+#);
 
 1;
index e30f5ea..1177ed6 100644 (file)
@@ -26,21 +26,21 @@ sub getBiblioFieldMaps {
                }
        }
 }      
-__PACKAGE__->register_method(
-       method          => 'getBiblioFieldMaps',
-       api_name        => 'open-ils.storage.config.metarecord_field',
-       argc            => 1,
-);
-__PACKAGE__->register_method(
-       method          => 'getBiblioFieldMaps',
-       api_name        => 'open-ils.storage.config.metarecord_field.all',
-       argc            => 0,
-);
-__PACKAGE__->register_method(
-       method          => 'getBiblioFieldMaps',
-       api_name        => 'open-ils.storage.config.metarecord_field.list.by_class',
-       argc            => 1,
-);
+#__PACKAGE__->register_method(
+#      method          => 'getBiblioFieldMaps',
+#      api_name        => 'open-ils.storage.config.metarecord_field',
+#      argc            => 1,
+#);
+#__PACKAGE__->register_method(
+#      method          => 'getBiblioFieldMaps',
+#      api_name        => 'open-ils.storage.config.metarecord_field.all',
+#      argc            => 0,
+#);
+#__PACKAGE__->register_method(
+#      method          => 'getBiblioFieldMaps',
+#      api_name        => 'open-ils.storage.config.metarecord_field.list.by_class',
+#      argc            => 1,
+#);
 
 
 sub getBiblioFieldMapClasses {
@@ -61,22 +61,22 @@ sub getBiblioFieldMapClasses {
                return undef;
        }
 }      
-__PACKAGE__->register_method(
-       method          => 'getBiblioFieldMapClasses',
-       api_name        => 'open-ils.storage.config.metarecord_field_class',
-       argc            => 1,
-);
-
-__PACKAGE__->register_method(
-       method          => 'getBiblioFieldMapClasses',
-       api_name        => 'open-ils.storage.config.metarecord_field_class.list',
-       argc            => 1,
-       stream          => 1,
-);
-__PACKAGE__->register_method(
-       method          => 'getBiblioFieldMapClasses',
-       api_name        => 'open-ils.storage.config.metarecord_field_class.all',
-       argc            => 0,
-);
+#__PACKAGE__->register_method(
+#      method          => 'getBiblioFieldMapClasses',
+#      api_name        => 'open-ils.storage.config.metarecord_field_class',
+#      argc            => 1,
+#);
+#
+#__PACKAGE__->register_method(
+#      method          => 'getBiblioFieldMapClasses',
+#      api_name        => 'open-ils.storage.config.metarecord_field_class.list',
+#      argc            => 1,
+#      stream          => 1,
+#);
+#__PACKAGE__->register_method(
+#      method          => 'getBiblioFieldMapClasses',
+#      api_name        => 'open-ils.storage.config.metarecord_field_class.all',
+#      argc            => 0,
+#);
 
 1;
index 7bd4117..021844b 100644 (file)
@@ -21,9 +21,9 @@ sub search_full_rec {
        my ($fts_col) = metabib::full_rec->columns('FTS');
        my $table = metabib::full_rec->table;
 
-       my $fts = OpenILS::Application::Storage::FTS->compile($term);
+       my $fts = OpenILS::Application::Storage::FTS->compile($term, 'value','index_vector');
 
-       my $fts_where = $fts->sql_where_clause($fts_col);
+       my $fts_where = $fts->sql_where_clause();
        my @fts_ranks = $fts->fts_rank;
 
        my $rank = join(' + ', @fts_ranks);
@@ -47,7 +47,13 @@ sub search_full_rec {
 
 }
 __PACKAGE__->register_method(
-       api_name        => 'open-ils.storage.metabib.full_rec.search.fts',
+       api_name        => 'open-ils.storage.metabib.full_rec.search_fts.value',
+       method          => 'search_full_rec',
+       api_level       => 1,
+       stream          => 1,
+);
+__PACKAGE__->register_method(
+       api_name        => 'open-ils.storage.metabib.full_rec.search_fts.index_vector',
        method          => 'search_full_rec',
        api_level       => 1,
        stream          => 1,
index 3b98508..542930d 100644 (file)
@@ -1,6 +1,11 @@
 package Fieldmapper;
 use JSON;
 use Data::Dumper;
+use base 'OpenSRF::Application';
+
+use OpenSRF::Utils::Logger;
+my $log = 'OpenSRF::Utils::Logger';
+
 use OpenILS::Application::Storage::CDBI;
 use OpenILS::Application::Storage::CDBI::actor;
 use OpenILS::Application::Storage::CDBI::biblio;
@@ -11,6 +16,19 @@ use vars qw/$fieldmap $VERSION/;
 
 _init();
 
+sub publish_fieldmapper {
+       my ($self,$client,$class) = @_;
+
+       return $fieldmap unless (defined $class);
+       return undef unless (exists($$fieldmap{$class}));
+       return {$class => $$fieldmap{$class}};
+}
+__PACKAGE__->register_method(
+       api_name        => 'opensrf.open-ils.system.fieldmapper',
+       api_level       => 1,
+       method          => 'publish_fieldmapper',
+);
+
 #
 # To dump the Javascript version of the fieldmapper struct use the command:
 #
@@ -61,7 +79,7 @@ sub _init {
                        use base 'Fieldmapper';
                PERL
 
-               $$fieldmapp{$pkg}{cdbi} = $cdbi;
+               $$fieldmap{$pkg}{cdbi} = $cdbi;
 
                my $pos = 0;
                for my $vfield ( qw/isnew ischanged isdeleted/ ) {