Return-Path: X-Original-To: apmail-incubator-lucy-dev-archive@www.apache.org Delivered-To: apmail-incubator-lucy-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 7099991C4 for ; Thu, 9 Feb 2012 20:09:31 +0000 (UTC) Received: (qmail 92991 invoked by uid 500); 9 Feb 2012 20:09:31 -0000 Delivered-To: apmail-incubator-lucy-dev-archive@incubator.apache.org Received: (qmail 92918 invoked by uid 500); 9 Feb 2012 20:09:30 -0000 Mailing-List: contact lucy-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: lucy-dev@incubator.apache.org Delivered-To: mailing list lucy-dev@incubator.apache.org Received: (qmail 92910 invoked by uid 99); 9 Feb 2012 20:09:30 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Feb 2012 20:09:30 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=RCVD_IN_DNSWL_NONE,SPF_HELO_PASS,SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [212.227.17.8] (HELO moutng.kundenserver.de) (212.227.17.8) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 09 Feb 2012 20:09:22 +0000 Received: from [192.168.178.26] (mnch-5d86cff1.pool.mediaWays.net [93.134.207.241]) by mrelayeu.kundenserver.de (node=mrbap0) with ESMTP (Nemesis) id 0LxMeg-1Sc7uB0LJC-017GP8; Thu, 09 Feb 2012 21:09:01 +0100 Message-ID: <4F3427E4.4030100@aevum.de> Date: Thu, 09 Feb 2012 21:09:08 +0100 From: Nick Wellnhofer User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:9.0) Gecko/20111222 Thunderbird/9.0.1 MIME-Version: 1.0 To: lucy-dev@incubator.apache.org References: <20111220220532.A7C4023888CD@eris.apache.org> <20111222051556.GA15440@rectangular.com> <4EF3AFB9.5010602@aevum.de> <20111223031833.GA30962@rectangular.com> <4F329D28.7080107@aevum.de> <20120209014952.GA7383@rectangular.com> <4F33C12E.4020203@aevum.de> <20120209174951.GA22340@rectangular.com> In-Reply-To: <20120209174951.GA22340@rectangular.com> Content-Type: multipart/mixed; boundary="------------050707090000080505090101" X-Provags-ID: V02:K0:Gm/UC/GqEgXvjULRE6SPNeVZ/efowNK1DziXiBLlOQc xta9jCNegLULuygzoXxjPLUN51WMethof6NU53vaR26dt7m2mG yebKoGAcY05BfGKECQMAynsX2s5CYJbL0gi7J5Tz5aMfrdl/1D r0dbwywc67xYfUYTe2XPAMKsdKmQZGqBrYGZ3VJBaAFyxW3pik VAnfN7S4duX9z3D9DtEbT4v57tPQ2yUByL3txYwA3ZZsClr22x psx1JJG2WX2nhVOBwHrCmyq7ioZBCnNzbqHqRrQQ5YAAqlBIGy dMmF4gzimIh5Vn6QaxTrmKo1Cub6hww7Xi24KEcThCUsMtwzIS Bw6iR0BzLSjjxyJU4pzo= Subject: Re: [lucy-dev] Promoting new analysis components --------------050707090000080505090101 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 09/02/2012 18:49, Marvin Humphrey wrote: > Rehashing our short exchange on IRC for the benefit of the list... I suggest > using PolyReader#open for this, since it returns NULL rather than throwing an > exception on failure. If open() is successful, the resulting reader can be > used as an argument to IndexSearcher#new: Actually, PolyReader#open doesn't return NULL if the index is empty. But the list of seg_readers will be empty, so we can test for that. See the attached patch for my second attempt. Nick --------------050707090000080505090101 Content-Type: text/plain; name="lucy-simple-v2.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="lucy-simple-v2.patch" diff --git a/perl/lib/Lucy/Simple.pm b/perl/lib/Lucy/Simple.pm index aeb92c4..f09301b 100644 --- a/perl/lib/Lucy/Simple.pm +++ b/perl/lib/Lucy/Simple.pm @@ -50,7 +50,6 @@ sub new { # 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,6 +60,15 @@ sub new { sub _lazily_create_indexer { my $self = shift; if ( !defined $self->{indexer} ) { + my $reader = Lucy::Index::PolyReader->open( index => $self->{path} ); + if ( ! @{ $reader->seg_readers } ) { + # index is empty, create new schema + $self->{schema} = Lucy::Plan::Schema->new; + } + else { + # get schema from reader + $self->{schema} = $reader->get_schema; + } $self->{indexer} = Lucy::Index::Indexer->new( schema => $self->{schema}, index => $self->{path}, @@ -70,11 +78,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}; $schema->spec_field( name => $_, type => $type ) for keys %$hashref; $self->{indexer}->add_doc($hashref); } --------------050707090000080505090101--