Author: nwellnhof
Date: Fri Feb 10 10:46:05 2012
New Revision: 1242733

URL: http://svn.apache.org/viewvc?rev=1242733&view=rev
Log:
Make Lucy::Simple work with legacy schemas

Modified:
    incubator/lucy/trunk/perl/lib/Lucy/Simple.pm

Modified: incubator/lucy/trunk/perl/lib/Lucy/Simple.pm
URL: 
http://svn.apache.org/viewvc/incubator/lucy/trunk/perl/lib/Lucy/Simple.pm?rev=1242733&r1=1242732&r2=1242733&view=diff
==============================================================================
--- incubator/lucy/trunk/perl/lib/Lucy/Simple.pm (original)
+++ incubator/lucy/trunk/perl/lib/Lucy/Simple.pm Fri Feb 10 10:46:05 2012
@@ -47,11 +47,6 @@ sub new {
         },
         ref($either) || $either;
 
-    # Get type and schema.
-    my $analyzer = Lucy::Analysis::EasyAnalyzer->new( language => $language );
-    $self->{type} = Lucy::Plan::FullTextType->new( analyzer => $analyzer, );
-    my $schema = $self->{schema} = Lucy::Plan::Schema->new;
-
     # Cache the object for later clean-up.
     weaken( $obj_cache{ refaddr $self } = $self );
 
@@ -61,8 +56,26 @@ sub new {
 sub _lazily_create_indexer {
     my $self = shift;
     if ( !defined $self->{indexer} ) {
+        # Get type and schema
+        my $schema;
+        my $reader = Lucy::Index::PolyReader->open( index => $self->{path} );
+        if ( !@{ $reader->seg_readers } ) {
+            # index is empty, create new schema and type
+            $schema = Lucy::Plan::Schema->new;
+            my $analyzer = Lucy::Analysis::EasyAnalyzer->new(
+                language => $self->{language}, );
+            $self->{type}
+                = Lucy::Plan::FullTextType->new( analyzer => $analyzer, );
+        }
+        else {
+            # get schema from reader
+            $schema = $reader->get_schema;
+            my $field = $schema->all_fields->[0];
+            $self->{type} = $schema->fetch_type($field);
+        }
+        $self->{schema}  = $schema;
         $self->{indexer} = Lucy::Index::Indexer->new(
-            schema => $self->{schema},
+            schema => $schema,
             index  => $self->{path},
         );
     }
@@ -70,11 +83,11 @@ sub _lazily_create_indexer {
 
 sub add_doc {
     my ( $self, $hashref ) = @_;
-    my $schema = $self->{schema};
-    my $type   = $self->{type};
     croak("add_doc requires exactly one argument: a hashref")
         unless ( @_ == 2 and reftype($hashref) eq 'HASH' );
     $self->_lazily_create_indexer;
+    my $schema = $self->{schema};
+    my $type   = $self->{type};
     $schema->spec_field( name => $_, type => $type ) for keys %$hashref;
     $self->{indexer}->add_doc($hashref);
 }


Reply via email to