Changeset 1921

Show
Ignore:
Timestamp:
03/14/07 10:19:44 (1 year ago)
Author:
karpet
Message:

reorg the perl namespaces and rename/rework some of the tokenizing to optimize for ascii -- TODO refactor the config.c stuff to match the docs

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libswish3/trunk/bindings/perl/3.xs

    r1920 r1921  
    239239    OUTPUT: 
    240240        RETVAL     
    241          
     241       
     242       
    242243# ********************************************************************************* 
    243  
    244 MODULE = SWISH::3               PACKAGE = SWISH::3::Parser       
     244MODULE = SWISH::3       PACKAGE = SWISH::3::Constants 
    245245 
    246246PROTOTYPES: enable 
    247                      
    248 void 
    249 _make_subclasses (self) 
    250     SV * self 
    251      
    252         PREINIT: 
    253         char* class; 
    254          
    255         CODE: 
    256         class = sv_reftype(SvRV(self), 1); 
    257         //printf("parent class is %s\n", class); 
    258             _make_subclasses(class); 
    259  
    260  
    261 void 
    262 _cleanup(self) 
    263     SV* self; 
    264     
    265     CODE: 
    266         /* TODO */ 
    267  
    268  
    269  
    270 SV* 
    271 slurp_file(self, filename) 
    272     SV* self; 
    273     char* filename; 
    274      
    275     CODE: 
    276         RETVAL = newSVpv( (const char*)swish_slurp_file((xmlChar*)filename), 0 ); 
    277          
    278     OUTPUT: 
    279         RETVAL 
    280          
    281  
    282 void 
    283 _init_parser(self) 
    284     SV* self; 
    285      
    286     CODE: 
    287         swish_init_parser(); 
    288         _remember_handler(_get_object_key(self,callback_method)); 
    289          
    290          
    291 void 
    292 _free(self) 
    293     SV* self; 
    294      
    295     CODE: 
    296         swish_free_parser(); 
    297          
    298 
    299 #   TODO: pass our own _word_tokenizer() callback so we can use Perl regexp 
    300 #   TODO: passing void*self is NOT right. reference counting gets messed up. 
    301 #   maybe clone? or pass SvIV and then re-make SV in data->parser? 
    302  
    303 int 
    304 parse_file (self, filename) 
    305     SV* self; 
    306         SV*     filename; 
    307      
    308     PREINIT: 
    309         char * file; 
    310         SV *   config; 
    311          
    312     CODE: 
    313         file = SvPV(filename, PL_na); 
    314         config = _get_object_key(self,"config"); 
    315  
    316 # need to swap return values to make it Perlish 
    317         RETVAL = swish_parse_file((xmlChar*)file,  
    318                                    (swish_Config*)SvIV((SV*)SvRV( config )), 
    319                                     &swish_perl_handler, 
    320                                     NULL, 
    321                                     (void*)self 
    322                                     )  
    323                 ? 0  
    324                 : 1; 
    325                          
    326     OUTPUT: 
    327         RETVAL 
    328          
    329       
    330 int 
    331 parse_buf (self, buffer) 
    332     SV* self; 
    333     SV* buffer; 
    334      
    335     PREINIT: 
    336         SV* config; 
    337         char* buf; 
    338          
    339     CODE: 
    340         config =  _get_object_key(self,"config"); 
    341         buf    = SvPV(buffer, PL_na); 
    342                  
    343         RETVAL = swish_parse_buffer((xmlChar*)buf, 
    344                                      (swish_Config*)SvIV((SV*)SvRV( config )), 
    345                                      &swish_perl_handler, 
    346                                      NULL, 
    347                                     (void*)self 
    348                                     ) 
    349                 ? 0 
    350                 : 1; 
    351                  
    352                  
    353     OUTPUT: 
    354         RETVAL 
    355          
    356   
    357          
    358  
    359 # ******************************************************************************* 
    360      
    361 MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Word 
    362  
    363 PROTOTYPES: disable 
    364  
    365 SV * 
    366 word (self) 
    367         swish_Word *    self;    
    368     CODE: 
    369         RETVAL = newSVpvn( (char*)self->word, strlen((char*)self->word) ); 
    370          
    371     OUTPUT: 
    372         RETVAL 
    373          
    374  
    375 SV * 
    376 metaname (self) 
    377         swish_Word *    self; 
    378     CODE: 
    379         RETVAL = newSVpvn( (char*)self->metaname, strlen((char*)self->metaname) ); 
    380          
    381     OUTPUT: 
    382         RETVAL 
    383  
    384 SV * 
    385 position (self) 
    386         swish_Word *    self; 
    387     CODE: 
    388         RETVAL = newSViv( self->position ); 
    389          
    390     OUTPUT: 
    391         RETVAL 
    392  
    393 SV * 
    394 start_offset(self) 
    395         swish_Word *    self; 
    396     CODE: 
    397         RETVAL = newSViv( self->start_offset ); 
    398          
    399     OUTPUT: 
    400         RETVAL 
    401  
    402 SV * 
    403 end_offset(self) 
    404         swish_Word *    self; 
    405     CODE: 
    406         RETVAL = newSViv( self->end_offset ); 
    407          
    408     OUTPUT: 
    409         RETVAL 
    410  
    411  
    412  
    413 # ******************************************************************************* 
    414      
    415 MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Doc 
    416  
    417 PROTOTYPES: disable 
    418  
    419 SV* 
    420 mtime(self) 
    421     swish_DocInfo * self; 
    422      
    423     CODE: 
    424         RETVAL = newSViv( self->mtime ); 
    425          
    426     OUTPUT: 
    427         RETVAL 
    428          
    429 SV* 
    430 size(self) 
    431     swish_DocInfo * self; 
    432      
    433     CODE: 
    434         RETVAL = newSViv( self->size ); 
    435          
    436     OUTPUT: 
    437         RETVAL 
    438          
    439 SV* 
    440 nwords(self) 
    441     swish_DocInfo * self; 
    442      
    443     CODE: 
    444         RETVAL = newSViv( self->nwords ); 
    445          
    446     OUTPUT: 
    447         RETVAL 
    448  
    449  
    450 SV * 
    451 encoding(self) 
    452         swish_DocInfo * self; 
    453     CODE: 
    454         RETVAL = newSVpvn( (char*)self->encoding, strlen((char*)self->encoding) ); 
    455          
    456     OUTPUT: 
    457         RETVAL 
    458  
    459 SV * 
    460 uri(self) 
    461         swish_DocInfo * self; 
    462     CODE: 
    463         RETVAL = newSVpvn( (char*)self->uri, strlen((char*)self->uri) ); 
    464          
    465     OUTPUT: 
    466         RETVAL 
    467  
    468 SV * 
    469 ext(self) 
    470         swish_DocInfo * self; 
    471     CODE: 
    472         RETVAL = newSVpvn( (char*)self->ext, strlen((char*)self->ext) ); 
    473          
    474     OUTPUT: 
    475         RETVAL 
    476          
    477 SV * 
    478 mime(self) 
    479         swish_DocInfo * self; 
    480     CODE: 
    481         RETVAL = newSVpvn( (char*)self->mime, strlen((char*)self->mime) ); 
    482          
    483     OUTPUT: 
    484         RETVAL 
    485          
    486  
    487 SV * 
    488 parser(self) 
    489         swish_DocInfo * self; 
    490     CODE: 
    491         RETVAL = newSVpvn( (char*)self->parser, strlen((char*)self->parser) ); 
    492          
    493     OUTPUT: 
    494         RETVAL 
    495  
    496  
    497  
    498 # ******************************************************************************* 
    499      
    500 MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Property 
    501  
    502 PROTOTYPES: disable 
    503  
    504          
    505  
    506 # ******************************************************************************* 
    507      
    508 MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::WordList 
    509  
    510 PROTOTYPES: disable 
    511          
    512 swish_Word * 
    513 next(self) 
    514     swish_WordList * self 
    515      
    516     PREINIT: 
    517         char * CLASS; 
    518      
    519     CODE: 
    520         CLASS = _which_class("Word"); 
    521         if (self->current == NULL)  
    522         { 
    523             self->current = self->head; 
    524         } 
    525         else  
    526         { 
    527             self->current = self->current->next; 
    528         } 
    529         RETVAL = self->current; 
    530          
    531     OUTPUT: 
    532         RETVAL 
    533  
    534  
    535  
    536  
    537 # ******************************************************************************* 
    538      
    539 MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Data 
    540  
    541 PROTOTYPES: disable 
    542  
    543 SV* 
    544 parser(self) 
    545     swish_ParseData * self 
    546      
    547     PREINIT: 
    548         SV* parser; 
    549          
    550     CODE: 
    551         RETVAL = self->user_data; 
    552          
    553     OUTPUT: 
    554         RETVAL 
    555  
    556  
    557 swish_Config * 
    558 config(self) 
    559     swish_ParseData * self 
    560      
    561         PREINIT: 
    562         char* CLASS; 
    563  
    564     CODE: 
    565         CLASS = "SWISH::3::Config"; 
    566         RETVAL = self->config; 
    567         RETVAL->ref_cnt++; 
    568          
    569     OUTPUT: 
    570         RETVAL 
    571          
    572          
    573 SV* 
    574 property(self,p) 
    575     swish_ParseData * self; 
    576     xmlChar * p; 
    577      
    578         PREINIT: 
    579         xmlBufferPtr buf; 
    580          
    581     CODE: 
    582         buf = xmlHashLookup(self->propHash,p); 
    583         RETVAL = newSVpvn((char*)xmlBufferContent(buf), xmlBufferLength(buf)); 
    584          
    585     OUTPUT: 
    586         RETVAL 
    587          
    588  
    589 swish_DocInfo * 
    590 doc(self) 
    591     swish_ParseData * self 
    592      
    593     PREINIT: 
    594         char* CLASS; 
    595          
    596     CODE: 
    597         CLASS = _which_class("Doc"); 
    598         RETVAL = self->docinfo; 
    599          
    600     OUTPUT: 
    601         RETVAL 
    602          
    603 swish_WordList * 
    604 wordlist(self) 
    605     swish_ParseData * self 
    606      
    607     PREINIT: 
    608         char* CLASS; 
    609          
    610     CODE: 
    611         CLASS = _which_class("WordList"); 
    612         RETVAL = self->wordlist; 
    613          
    614     OUTPUT: 
    615         RETVAL 
    616          
    617  
    618  
    619 # *************************************************************************************/ 
    620  
    621 MODULE = SWISH::3               PACKAGE = SWISH::3::Config       
    622  
    623 PROTOTYPES: enable 
    624                      
     247 
     248# TODO more from libswish3.h                
    625249# constants            
    626250BOOT: 
     
    628252        HV *stash; 
    629253   
    630         stash = gv_stashpv("SWISH::3::Config",       TRUE); 
     254        stash = gv_stashpv("SWISH::3::Constants",       TRUE); 
    631255        newCONSTSUB(stash, "SWISH_PROP",           newSVpv(SWISH_PROP, 0)); 
    632256        newCONSTSUB(stash, "SWISH_PROP_MAX",       newSVpv(SWISH_PROP_MAX, 0)); 
     
    652276        newCONSTSUB(stash, "SWISH_PROP_INT",       newSViv(SWISH_PROP_INT)); 
    653277        } 
    654  
    655  
     278   
     279# ********************************************************************************* 
     280 
     281MODULE = SWISH::3               PACKAGE = SWISH::3::Parser       
     282 
     283PROTOTYPES: enable 
     284                     
     285void 
     286_make_subclasses (self) 
     287    SV * self 
     288     
     289        PREINIT: 
     290        char* class; 
     291         
     292        CODE: 
     293        class = sv_reftype(SvRV(self), 1); 
     294        //printf("parent class is %s\n", class); 
     295            _make_subclasses(class); 
     296 
     297 
     298void 
     299_cleanup(self) 
     300    SV* self; 
     301    
     302    CODE: 
     303        /* TODO */ 
     304 
     305 
     306 
     307SV* 
     308slurp_file(self, filename) 
     309    SV* self; 
     310    char* filename; 
     311     
     312    CODE: 
     313        RETVAL = newSVpv( (const char*)swish_slurp_file((xmlChar*)filename), 0 ); 
     314         
     315    OUTPUT: 
     316        RETVAL 
     317         
     318 
     319void 
     320_init_parser(self) 
     321    SV* self; 
     322     
     323    CODE: 
     324        swish_init_parser(); 
     325        _remember_handler(_get_object_key(self,callback_method)); 
     326         
     327         
     328void 
     329_free(self) 
     330    SV* self; 
     331     
     332    CODE: 
     333        swish_free_parser(); 
     334         
     335
     336#   TODO: pass our own _word_tokenizer() callback so we can use Perl regexp 
     337
     338 
     339 
     340int 
     341parse_file (self, filename) 
     342    SV* self; 
     343        SV*     filename; 
     344     
     345    PREINIT: 
     346        char * file; 
     347        SV *   config; 
     348         
     349    CODE: 
     350        file = SvPV(filename, PL_na); 
     351        config = _get_object_key(self,"config"); 
     352 
     353# need to swap return values to make it Perlish 
     354        RETVAL = swish_parse_file((xmlChar*)file,  
     355                                   (swish_Config*)SvIV((SV*)SvRV( config )), 
     356                                    &swish_perl_handler, 
     357                                    NULL, 
     358                                    (void*)SvREFCNT_inc( self ) 
     359                                    )  
     360                ? 0  
     361                : 1; 
     362                         
     363    OUTPUT: 
     364        RETVAL 
     365         
     366      
     367int 
     368parse_buf (self, buffer) 
     369    SV* self; 
     370    SV* buffer; 
     371     
     372    PREINIT: 
     373        SV* config; 
     374        char* buf; 
     375         
     376    CODE: 
     377        config =  _get_object_key(self,"config"); 
     378        buf    = SvPV(buffer, PL_na); 
     379                 
     380        RETVAL = swish_parse_buffer((xmlChar*)buf, 
     381                                     (swish_Config*)SvIV((SV*)SvRV( config )), 
     382                                     &swish_perl_handler, 
     383                                     NULL, 
     384                                    (void*)SvREFCNT_inc( self ) 
     385                                    ) 
     386                ? 0 
     387                : 1; 
     388                 
     389                 
     390    OUTPUT: 
     391        RETVAL 
     392         
     393  
     394         
     395 
     396# ******************************************************************************* 
     397     
     398MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Word 
     399 
     400PROTOTYPES: disable 
     401 
     402SV * 
     403word (self) 
     404        swish_Word *    self;    
     405    CODE: 
     406        RETVAL = newSVpvn( (char*)self->word, strlen((char*)self->word) ); 
     407         
     408    OUTPUT: 
     409        RETVAL 
     410         
     411 
     412SV * 
     413metaname (self) 
     414        swish_Word *    self; 
     415    CODE: 
     416        RETVAL = newSVpvn( (char*)self->metaname, strlen((char*)self->metaname) ); 
     417         
     418    OUTPUT: 
     419        RETVAL 
     420         
     421SV * 
     422context (self) 
     423        swish_Word *    self; 
     424    CODE: 
     425        RETVAL = newSVpvn( (char*)self->context, strlen((char*)self->context) ); 
     426         
     427    OUTPUT: 
     428        RETVAL 
     429         
     430 
     431SV * 
     432position (self) 
     433        swish_Word *    self; 
     434    CODE: 
     435        RETVAL = newSViv( self->position ); 
     436         
     437    OUTPUT: 
     438        RETVAL 
     439 
     440SV * 
     441start_offset(self) 
     442        swish_Word *    self; 
     443    CODE: 
     444        RETVAL = newSViv( self->start_offset ); 
     445         
     446    OUTPUT: 
     447        RETVAL 
     448 
     449SV * 
     450end_offset(self) 
     451        swish_Word *    self; 
     452    CODE: 
     453        RETVAL = newSViv( self->end_offset ); 
     454         
     455    OUTPUT: 
     456        RETVAL 
     457 
     458 
     459 
     460# ******************************************************************************* 
     461     
     462MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Doc 
     463 
     464PROTOTYPES: disable 
     465 
     466SV* 
     467mtime(self) 
     468    swish_DocInfo * self; 
     469     
     470    CODE: 
     471        RETVAL = newSViv( self->mtime ); 
     472         
     473    OUTPUT: 
     474        RETVAL 
     475         
     476SV* 
     477size(self) 
     478    swish_DocInfo * self; 
     479     
     480    CODE: 
     481        RETVAL = newSViv( self->size ); 
     482         
     483    OUTPUT: 
     484        RETVAL 
     485         
     486SV* 
     487nwords(self) 
     488    swish_DocInfo * self; 
     489     
     490    CODE: 
     491        RETVAL = newSViv( self->nwords ); 
     492         
     493    OUTPUT: 
     494        RETVAL 
     495 
     496 
     497SV * 
     498encoding(self) 
     499        swish_DocInfo * self; 
     500    CODE: 
     501        RETVAL = newSVpvn( (char*)self->encoding, strlen((char*)self->encoding) ); 
     502         
     503    OUTPUT: 
     504        RETVAL 
     505 
     506SV * 
     507uri(self) 
     508        swish_DocInfo * self; 
     509    CODE: 
     510        RETVAL = newSVpvn( (char*)self->uri, strlen((char*)self->uri) ); 
     511         
     512    OUTPUT: 
     513        RETVAL 
     514 
     515SV * 
     516ext(self) 
     517        swish_DocInfo * self; 
     518    CODE: 
     519        RETVAL = newSVpvn( (char*)self->ext, strlen((char*)self->ext) ); 
     520         
     521    OUTPUT: 
     522        RETVAL 
     523         
     524SV * 
     525mime(self) 
     526        swish_DocInfo * self; 
     527    CODE: 
     528        RETVAL = newSVpvn( (char*)self->mime, strlen((char*)self->mime) ); 
     529         
     530    OUTPUT: 
     531        RETVAL 
     532         
     533 
     534SV * 
     535parser(self) 
     536        swish_DocInfo * self; 
     537    CODE: 
     538        RETVAL = newSVpvn( (char*)self->parser, strlen((char*)self->parser) ); 
     539         
     540    OUTPUT: 
     541        RETVAL 
     542 
     543 
     544 
     545# ******************************************************************************* 
     546     
     547MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Property 
     548 
     549PROTOTYPES: disable 
     550 
     551         
     552 
     553# ******************************************************************************* 
     554     
     555MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::WordList 
     556 
     557PROTOTYPES: disable 
     558         
     559swish_Word * 
     560next(self) 
     561    swish_WordList * self 
     562     
     563    PREINIT: 
     564        char * CLASS; 
     565     
     566    CODE: 
     567        CLASS = _which_class("Word"); 
     568        if (self->current == NULL)  
     569        { 
     570            self->current = self->head; 
     571        } 
     572        else  
     573        { 
     574            self->current = self->current->next; 
     575        } 
     576        RETVAL = self->current; 
     577         
     578    OUTPUT: 
     579        RETVAL 
     580 
     581 
     582 
     583 
     584# ******************************************************************************* 
     585     
     586MODULE = SWISH::3               PACKAGE = SWISH::3::Parser::Data 
     587 
     588PROTOTYPES: disable 
     589 
     590SV* 
     591parser(self) 
     592    swish_ParseData * self 
     593     
     594    PREINIT: 
     595        SV* parser; 
     596         
     597    CODE: 
     598        RETVAL = self->user_data; 
     599         
     600    OUTPUT: 
     601        RETVAL 
     602 
     603 
     604swish_Config * 
     605config(self) 
     606    swish_ParseData * self 
     607     
     608        PREINIT: 
     609        char* CLASS; 
     610 
     611    CODE: 
     612        CLASS = "SWISH::3::Config"; 
     613        RETVAL = self->config; 
     614        RETVAL->ref_cnt++; 
     615         
     616    OUTPUT: 
     617        RETVAL 
     618         
     619         
     620SV* 
     621property(self,p) 
     622    swish_ParseData * self; 
     623    xmlChar * p; 
     624     
     625        PREINIT: 
     626        xmlBufferPtr buf; 
     627         
     628    CODE: 
     629        buf = xmlHashLookup(self->propHash,p); 
     630        RETVAL = newSVpvn((char*)xmlBufferContent(buf), xmlBufferLength(buf)); 
     631         
     632    OUTPUT: 
     633        RETVAL 
     634         
     635 
     636swish_DocInfo * 
     637doc(self) 
     638    swish_ParseData * self 
     639     
     640    PREINIT: 
     641        char* CLASS; 
     642         
     643    CODE: 
     644        CLASS = _which_class("Doc"); 
     645        RETVAL = self->docinfo; 
     646         
     647    OUTPUT: 
     648        RETVAL 
     649         
     650swish_WordList * 
     651wordlist(self) 
     652    swish_ParseData * self 
     653     
     654    PREINIT: 
     655        char* CLASS; 
     656         
     657    CODE: 
     658        CLASS = _which_class("WordList"); 
     659        RETVAL = self->wordlist; 
     660         
     661    OUTPUT: 
     662        RETVAL 
     663         
     664     
     665# must decrement refcount for stashed SWISH::3::Parser object 
     666# since we increment it in parse_buf() and parse_file() 
     667# TODO: this way of doing it doesn't work. 
     668# but isn't it a potential mem leak to just _inc in parse_*() without 
     669# _dec somewhere else? just means that the SWISH::3::Parser object 
     670# may never get garbage collected. 
     671#void 
     672#DESTROY(self) 
     673#    swish_ParseData * self; 
     674#     
     675#    CODE: 
     676#        SvREFCNT_dec( self->user_data ); 
     677         
     678 
     679 
     680# *************************************************************************************/ 
     681 
     682MODULE = SWISH::3               PACKAGE = SWISH::3::Config       
     683 
     684PROTOTYPES: disable 
    656685 
    657686AV* 
  • libswish3/trunk/bindings/perl/MANIFEST

    r1920 r1921  
    77t/SWISH-3.t 
    88lib/SWISH/3.pm 
     9lib/SWISH/3/Constants.pm 
    910lib/SWISH/3/Parser.pm 
    1011lib/SWISH/3/Parser/Doc.pm 
     
    1516lib/SWISH/3/Config.pm 
    1617lib/SWISH/3/Parser/Data.pm 
     18lib/SWISH/3/Indexer.pm 
     19lib/SWISH/3/Doc.pm 
     20lib/SWISH/3/Xapian/ 
     21lib/SWISH/3/Xapian/Indexer.pm 
     22lib/SWISH/3/Xapian/Doc.pm 
  • libswish3/trunk/bindings/perl/lib/SWISH/3.pm

    r1920 r1921  
    88 
    99use Carp; 
    10 use Data::Dump qw(pp); 
    11  
    1210require XSLoader; 
    1311XSLoader::load('SWISH::3', $VERSION); 
    1412 
    15 # defer till runtime so any constants can load 
    16 require SWISH::3::Config; 
    17 require SWISH::3::Parser; 
    18  
    19  
    20 # variables used by various classes 
    21 our @DocFields  = qw( mtime size encoding mime uri nwords ext parser ); 
    22 our @WordFields = qw( word position metaname start_offset end_offset ); 
     13$ENV{SWISH3} = 1;    # flag let's SWISH::Prog et al know we are version3 
     14                     # TODO doesn't libswish3 do this already? 
    2315 
    24161; 
  • libswish3/trunk/bindings/perl/lib/SWISH/3/Config.pm

    r1920 r1921  
    55use warnings; 
    66use Carp; 
     7use SWISH::3;    # in case this class gets 'use'd directly 
     8require SWISH::3::Constants; 
    79 
    810our $VERSION = '0.01'; 
     
    1012#use Data::Dump qw(pp); 
    1113#use Devel::Peek; 
    12  
    13  
    14 use base qw( Exporter ); 
    15 our @EXPORT = qw( 
    16   SWISH_INCLUDE_FILE 
    17   SWISH_PROP 
    18   SWISH_PROP_MAX 
    19   SWISH_PROP_SORT 
    20   SWISH_META 
    21   SWISH_MIME 
    22   SWISH_PARSERS 
    23   SWISH_INDEX 
    24   SWISH_ALIAS 
    25   SWISH_WORDS 
    26  
    27   ); 
    2814 
    2915sub new 
  • libswish3/trunk/bindings/perl/lib/SWISH/3/Doc.pm

    r1920 r1921  
    1 package SWISH::3::Indexer::Doc; 
     1package SWISH::3::Doc; 
    22 
    33use strict; 
     
    77 
    88our $VERSION = '0.01'; 
    9 use SWISH::3
     9use SWISH::3::Constants
    1010 
    11 __PACKAGE__->mk_accessors(@SWISH::3::Fields); 
    12  
     11__PACKAGE__->mk_accessors(SWISH_DOC_FIELDS); 
    1312 
    1413sub init 
    1514{ 
    1615    my $self = shift; 
    17      
    18     # only URI is required 
    19     unless(defined $self->uri) 
     16    unless ($self->uri) 
    2017    { 
    21         croak "URI required in new() SWISH::3::Indexer::Doc"; 
     18        croak "SWISH::3::Doc object requires at least a uri"; 
    2219    } 
    2320} 
     
    3330sub props           { croak "must override" } 
    3431 
     32sub ok_word 
     33{ 
     34    my $self = shift; 
     35    unless (@_) 
     36    { 
     37        croak "need word value"; 
     38    } 
     39    if (!@_ % 2) 
     40    { 
     41        return {@_}; 
     42    } 
     43    else 
     44    { 
     45        croak "bad word format"; 
     46    } 
     47} 
     48 
     49sub ok_property 
     50{ 
     51    my $self = shift; 
     52    unless (@_) 
     53    { 
     54        croak "need property value"; 
     55    } 
     56    if (@_ == 1) 
     57    { 
     58        return $_[0]; 
     59    } 
     60    elsif (!@_ % 2) 
     61    { 
     62        return {@_}; 
     63    } 
     64    else 
     65    { 
     66        croak "bad property format"; 
     67    } 
     68} 
     69 
    35701; 
    3671__END__ 
     
    3974=head1 NAME 
    4075 
    41 SWISH::Doc - Swish-e document class 
     76SWISH::3::Doc - Swish3 document class 
    4277 
    4378=head1 SYNOPSIS 
    4479 
    45   use SWISH::Doc; 
    46   use SWISH::Index; 
     80  use SWISH::3::Doc; 
     81  use SWISH::3::Indexer; 
     82  use SWISH::3::Constants; 
    4783   
    48   my $index = SWISH::Index->new(); 
     84  my $indexer = SWISH::3::Indexer->new(); 
    4985   
    5086  # create a doc object for indexing 
    5187   
    52   my $doc = SWISH::Doc->new( 
     88  my $doc = SWISH::3::Doc->new( 
    5389                uri         => 'http://swish-e.org/docs/readme.html', 
    5490                mtime       => 1234567890, 
     
    6399  for my $w ( your_function_to_parse_doc() ) 
    64100  { 
    65     $doc->add_word(  
    66         metaname        => $w->metaname 
    67         word            => $w->word, 
    68         start_offset    => $w->start_offset, 
    69         end_offset      => $w->end_offset, 
    70         position        => $w->position 
    71         ); 
     101    my @word; 
     102    for my $field (SWISH_WORD_FIELDS) 
     103    { 
     104      push(@word, $field => $w->$field); 
     105    } 
     106    $doc->add_word( @word ); 
    72107  } 
    73108     
    74109  for my $p ( your_function_to_get_properties() ) 
    75110  { 
    76     $doc->add_property( 
     111    # as hashref 
     112    $doc->add_property({ 
    77113        id      => $p->id, 
    78114        value   => $p->value 
    79         ); 
     115        }); 
     116    # or as array 
     117    $doc->add_property($p->id => $p->value); 
    80118  } 
    81119   
    82   $index->add_doc( $doc ); 
     120  $indexer->insert( $doc ); 
    83121   
    84122  # fetch a doc from index and see what it contains 
    85123   
    86   my $doc = $index->get_doc( $doc->uri ); 
     124  my $doc = $indexer->fetch( $doc->uri ); 
    87125   
    88126  my $wordlist = $doc->words; 
     
    94132  $doc->delete_property( $someprop ); 
    95133   
    96   $index->replace_doc( $doc->uri, $doc ); 
     134  $index->update( $doc->uri, $doc ); 
    97135 
    98136 
     
    104142=head1 SEE ALSO 
    105143 
    106 SWISH::Index, SWISH::Parser, SWISH::Config, SWISH::Search 
     144SWISH::3, SWISH::3::Indexer, SWISH::3::Searcher, SWISH::3::Parser 
    107145 
    108146 
     
    113151=head1 COPYRIGHT AND LICENSE 
    114152 
    115 Copyright (C) 2006 by Peter Karman 
     153Copyright (C) 2007 by Peter Karman 
    116154 
    117155This library is free software; you can redistribute it and/or modify 
  • libswish3/trunk/bindings/perl/lib/SWISH/3/Index.pm

    r1920 r1921  
    44use warnings; 
    55use base qw( SWISH::3::Object ); 
     6use Path::Class; 
     7use File::Path; 
     8use Carp; 
    69 
     10__PACKAGE__->mk_accessors(qw( path clobber )); 
     11 
     12sub init 
     13{ 
     14    my $self = shift; 
     15 
     16    unless ($self->path) 
     17    { 
     18        croak "path required to create SWISH::3::Index"; 
     19    } 
     20 
     21    if (-d $self->path) 
     22    { 
     23 
     24        if ($self->clobber) 
     25        { 
     26            rmtree([$self->path], 1, 1); 
     27        } 
     28 
     29    } 
     30    elsif (-e $self->path) 
     31    { 
     32        croak $self->path 
     33          . " is not a directory -- won't even attempt to clobber"; 
     34    } 
     35 
     36    mkpath([$self->path], 1);    # should ignore if already -d 
     37 
     38} 
    739 
    8401; 
  • libswish3/trunk/bindings/perl/lib/SWISH/3/Indexer.pm

    r1920 r1921  
    44use warnings; 
    55use base qw( SWISH::3::Object ); 
     6use Carp; 
     7use SWISH::3::Index; 
    68 
    79our $VERSION = '0.01'; 
     10__PACKAGE__->mk_accessors(qw( index path flush_at clobber config doc_count )); 
    811 
    912sub init 
    1013{ 
    1114    my $self = shift; 
    12     my %e    = @_; 
    13     $self->{$_} = $e{$_} for keys %e; 
    1415    $self->{_start} = time(); 
    15     $self->mk_accessors(qw( name flush_at overwrite config doc_count )); 
    1616    $self->{flush_at} ||= 10000; 
     17    $self->{index}    ||= 
     18      SWISH::3::Index->new(path    => $self->path, 
     19                           clobber => $self->clobber || 0); 
     20 
    1721} 
    1822 
    19 sub finish      { croak "must override" } 
    20 sub get_doc     { croak "must override" } 
    21 sub add_doc     { croak "must override" } 
    22 sub replace_doc { croak "must override" } 
    23 sub delete_doc  { croak "must override" } 
     23sub finish 
     24
     25    my $self = shift; 
     26    unless ($self->config) 
     27    { 
     28        carp "no config() set in Indexer - swish_header.xml cannot be written"; 
     29        return; 
     30    } 
     31 
     32
     33 
     34sub fetch  { croak "must override fetch" } 
     35sub insert { croak "must override insert" } 
     36sub update { croak "must override update" } 
     37sub delete { croak "must override delete" } 
    2438 
    25391; 
     
    3852  my $config = SWISH::3::Config->new(); 
    3953   
    40   my $index = SWISH::3::Indexer->new( 
    41                 name        => 'index.swish-e', 
     54  my $indexer = SWISH::3::Indexer->new( 
     55                path        => 'index.swish-e', 
    4256                flush_at    => 10000, 
    43                 overwrite   => 1, 
     57                clobber     => 1, 
    4458                config      => $config 
    4559                ); 
     
    4761  for my $doc (@bunch_of_doc_objects) 
    4862  {     
    49     if (my $old_doc = $index->get_doc($doc->uri)) 
     63    if (my $old_doc = $indexer->fetch($doc->uri)) 
    5064    { 
    51         $index->replace_doc($old_doc, $doc); 
     65        $indexer->update($old_doc, $doc); 
    5266    } 
    5367    else 
    5468    { 
    55         $index->add_doc($doc); 
     69        $indexer->insert($doc); 
    5670    } 
    5771     
    5872    # oops! change your mind? 
    59     $index->delete_doc( $doc ); 
     73    $indexer->delete( $doc ); 
    6074  } 
    6175   
    62   $index->close
     </