1 # $Id: Session.pm,v 1.20 2004-07-28 08:15:46 adam Exp $
3 # Zebra perl API header
4 # =============================================================================
5 package IDZebra::Session;
14 use IDZebra::Logger qw(:flags :calls);
15 use IDZebra::Resultset;
16 use IDZebra::ScanList;
17 use IDZebra::RetrievalRecord;
19 our $VERSION = do { my @r = (q$Revision: 1.20 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
20 our @ISA = qw(IDZebra::Logger Exporter);
21 our @EXPORT = qw (TRANS_RW TRANS_RO);
24 use constant TRANS_RW => 1;
25 use constant TRANS_RO => 0;
28 # -----------------------------------------------------------------------------
29 # Class constructors, destructor
30 # -----------------------------------------------------------------------------
32 my ($proto, %args) = @_;
33 my $class = ref($proto) || $proto;
35 $self->{args} = \%args;
37 bless ($self, $class);
38 $self->{cql_ct} = undef;
39 $self->{cql_mapfile} = "";
42 $self->{databases} = {};
46 my ($self, %args) = @_;
49 unless (defined($self->{zs})) {
50 if (defined($args{'configFile'})) {
51 $self->{zs} = IDZebra::start($args{'configFile'});
53 $self->{zs} = IDZebra::start("zebra.cfg");
60 if (defined($self->{zs})) {
61 IDZebra::stop($self->{zs}) if ($self->{zs});
68 my ($proto,%args) = @_;
71 if (ref($proto)) { $self = $proto; } else {
72 $self = $proto->new(%args);
76 %args = %{$self->{args}};
79 $self->start_service(%args);
81 unless (defined($self->{zs})) {
82 croak ("Falied to open zebra service");
85 unless (defined($self->{zh})) {
86 $self->{zh}=IDZebra::open($self->{zs});
89 # Reset result set counter
92 # This is needed in order to somehow initialize the service
93 $self->databases("Default");
95 # ADAM: group call deleted
96 # Load the default configuration
97 # $self->group(%args);
99 # ADAM: Set group resource instead
100 if (defined($args{groupName})) {
101 IDZebra::set_resource($self->{zh}, "group", $args{groupName});
105 my $shadow = defined($args{shadow}) ? $args{shadow} : 0;
106 $self->shadow($shadow);
108 $self->{odr_input} = IDZebra::odr_createmem($IDZebra::ODR_DECODE);
109 $self->{odr_output} = IDZebra::odr_createmem($IDZebra::ODR_ENCODE);
116 unless (defined($self->{zh})) {
117 croak ("Zebra session is not opened");
127 # Delete all resulsets
128 my $r = IDZebra::deleteResultSet($self->{zh},
129 1, #Z_DeleteRequest_all,
133 while (IDZebra::trans_no($self->{zh}) > 0) {
134 logf (LOG_WARN,"Explicitly closing transaction with session");
138 IDZebra::close($self->{zh});
142 if ($self->{odr_input}) {
143 IDZebra::odr_reset($self->{odr_input});
144 IDZebra::odr_destroy($self->{odr_input});
145 $self->{odr_input} = undef;
148 if ($self->{odr_output}) {
149 IDZebra::odr_reset($self->{odr_output});
150 IDZebra::odr_destroy($self->{odr_output});
151 $self->{odr_output} = undef;
159 logf (LOG_LOG,"DESTROY $self");
162 if (defined ($self->{cql_ct})) {
163 IDZebra::cql_transform_close($self->{cql_ct});
167 # -----------------------------------------------------------------------------
168 # Record group selection This is a bit nasty... but used at many places
169 # -----------------------------------------------------------------------------
171 # ADAM: All these group functions have been disabled.
173 my ($self,%args) = @_;
176 $self->{rg} = $self->_makeRecordGroup(%args);
177 $self->_selectRecordGroup($self->{rg});
182 sub selectRecordGroup_deleted {
183 my ($self, $groupName) = @_;
185 $self->{rg} = $self->_getRecordGroup($groupName);
186 $self->_selectRecordGroup($self->{rg});
189 sub _displayRecordGroup_deleted {
190 my ($self, $rg) = @_;
191 print STDERR "-----\n";
192 foreach my $key qw (groupName
203 print STDERR "$key:",$rg->{$key},"\n";
207 sub _cloneRecordGroup_deleted {
208 my ($self, $orig) = @_;
209 my $rg = IDZebra::recordGroup->new();
210 my $r = IDZebra::init_recordGroup($rg);
211 foreach my $key qw (groupName
223 $rg->{$key} = $orig->{$key} if ($orig->{$key});
228 sub _getRecordGroup_deleted {
229 my ($self, $groupName, $ext) = @_;
230 my $rg = IDZebra::recordGroup->new();
231 my $r = IDZebra::init_recordGroup($rg);
232 $rg->{groupName} = $groupName if ($groupName ne "");
233 $ext = "" unless ($ext);
234 $r = IDZebra::res_get_recordGroup($self->{zh}, $rg, $ext);
238 sub _makeRecordGroup_deleted {
239 my ($self, %args) = @_;
242 my @keys = keys(%args);
243 unless ($#keys >= 0) {
244 return ($self->{rg});
247 if ($args{groupName}) {
248 $rg = $self->_getRecordGroup($args{groupName});
250 $rg = $self->_cloneRecordGroup($self->{rg});
252 $self->_setRecordGroupOptions($rg, %args);
256 sub _setRecordGroupOptions_deleted {
257 my ($self, $rg, %args) = @_;
259 foreach my $key qw (databaseName
270 if (defined ($args{$key})) {
271 $rg->{$key} = $args{$key};
275 sub _selectRecordGroup_deleted {
276 my ($self, $rg) = @_;
278 my $r = IDZebra::set_group($self->{zh}, $rg);
280 unless ($dbName = $rg->{databaseName}) {
283 unless ($self->databases($dbName)) {
284 croak("Fatal error selecting database $dbName");
287 # -----------------------------------------------------------------------------
288 # Selecting databases for search (and also for updating - internally)
289 # -----------------------------------------------------------------------------
291 my ($self, @databases) = @_;
296 return (keys(%{$self->{databases}}));
301 foreach my $db (@databases) {
303 next if ($self->{databases}{$db});
307 foreach my $db (keys (%{$self->{databases}})) {
308 $changed++ unless ($tmp{$db});
313 delete ($self->{databases});
314 foreach my $db (@databases) {
315 $self->{databases}{$db}++;
318 if (IDZebra::select_databases($self->{zh},
322 "Could not select database(s) %s errCode=%d",
323 join(",",@databases),
327 logf(LOG_LOG,"Database(s) selected: %s",join(",",@databases));
330 return (keys(%{$self->{databases}}));
333 # -----------------------------------------------------------------------------
335 # -----------------------------------------------------------------------------
338 return(IDZebra::errCode($self->{zh}));
343 return(IDZebra::errString($self->{zh}));
348 return(IDZebra::errAdd($self->{zh}));
351 # -----------------------------------------------------------------------------
353 # -----------------------------------------------------------------------------
356 $m = TRANS_RW unless (defined ($m));
357 if (my $err = IDZebra::begin_trans($self->{zh},$m)) {
358 if ($self->errCode == 2) {
359 croak ("TRANS_RW not allowed within TRANS_RO");
361 croak("Error starting transaction; code:".
362 $self->errCode . " message: " . $self->errString);
370 my $stat = IDZebra::ZebraTransactionStatus->new();
371 IDZebra::end_trans($self->{zh}, $stat);
376 my ($self, $value) = @_;
379 $value = 0 unless (defined($value));
380 my $r =IDZebra::set_shadow_enable($self->{zh},$value);
382 return (IDZebra::get_shadow_enable($self->{zh}));
389 return(IDZebra::commit($self->{zh}));
393 # -----------------------------------------------------------------------------
394 # We don't really need that...
395 # -----------------------------------------------------------------------------
397 my ($self, $name) = @_;
398 if ($name !~/^(input|output)$/) {
399 croak("Undefined ODR '$name'");
401 IDZebra::odr_reset($self->{"odr_$name"});
404 # -----------------------------------------------------------------------------
406 # -----------------------------------------------------------------------------
410 return(IDZebra::init($self->{zh}));
416 return(IDZebra::compact($self->{zh}));
420 my ($self, %args) = @_;
422 # ADAM: Set group resource
423 if (defined($args{groupName})) {
424 IDZebra::set_resource($self->{zh}, "group", $args{groupName});
427 # my $rg = $self->_update_args(%args); deleted
428 # $self->_selectRecordGroup($rg); deleted
430 IDZebra::repository_update($self->{zh}, $args{path});
431 # $self->_selectRecordGroup($self->{rg}); deleted
436 my ($self, %args) = @_;
438 # ADAM: Set group resource
439 if (defined($args{groupName})) {
440 IDZebra::set_resource($self->{zh}, "group", $args{groupName});
443 # my $rg = $self->_update_args(%args); deleted
444 # $self->_selectRecordGroup($rg); deleted
446 IDZebra::repository_delete($self->{zh}, $args{path});
448 # $self->_selectRecordGroup($self->{rg});
453 my ($self, %args) = @_;
455 # ADAM: Set group resource
456 if (defined($args{groupName})) {
457 IDZebra::set_resource($self->{zh}, "group", $args{groupName});
460 # my $rg = $self->_update_args(%args);
461 # $self->_selectRecordGroup($rg);
464 IDZebra::repository_show($self->{zh});
465 $self->_selectRecordGroup($self->{rg});
469 sub _update_args_deleted {
470 my ($self, %args) = @_;
471 my $rg = $self->_makeRecordGroup(%args);
472 $self->_selectRecordGroup($rg);
476 # -----------------------------------------------------------------------------
478 # -----------------------------------------------------------------------------
480 my ($self, %args) = @_;
482 my @args = $self->_record_update_args(%args);
483 my $stat = IDZebra::insert_record($self->{zh}, @args);
484 # ADAM: rg no longer part of vector
485 my $sysno = $args[1]; $stat = -1 * $stat if ($stat > 0);
486 return $stat ? $stat : $$sysno;
490 my ($self, %args) = @_;
492 my @args = $self->_record_update_args(%args);
493 my $stat = IDZebra::update_record($self->{zh}, @args);
494 # ADAM: rg no longer part of vector
495 my $sysno = $args[1]; $stat = -1 * $stat if ($stat > 0);
496 return $stat ? $stat : $$sysno;
500 my ($self, %args) = @_;
502 my @args = $self->_record_update_args(%args);
503 my $stat = IDZebra::delete_record($self->{zh}, @args);
504 # ADAM: rg no longer part of vector
505 my $sysno = $args[1]; $stat = -1 * $stat if ($stat > 0);
508 sub _record_update_args {
509 my ($self, %args) = @_;
511 my $sysno = $args{sysno} ? $args{sysno} : 0;
512 my $match = $args{match} ? $args{match} : "";
513 my $rectype = $args{recordType} ? $args{recordType} : "";
514 my $fname = $args{file} ? $args{file} : "<no file>";
515 my $force = $args{force} ? $args{force} : 0;
522 elsif ($args{file}) {
523 CORE::open (F, $args{file}) || warn ("Cannot open $args{file}");
524 $buff = join('',(<F>));
527 my $len = length($buff);
529 delete ($args{sysno});
530 delete ($args{match});
531 delete ($args{recordType});
532 delete ($args{file});
533 delete ($args{data});
534 delete ($args{force});
536 # ADAM: recordGroup removed ...
537 # my $rg = $self->_makeRecordGroup(%args);
539 # If no record type is given, then try to find it out from the
540 # file extension; deleted
542 # if (my ($ext) = $fname =~ /\.(\w+)$/) {
543 # my $rg2 = $self->_getRecordGroup($rg->{groupName},$ext);
544 # $rectype = $rg2->{recordType};
548 # $rg->{databaseName} = "Default" unless ($rg->{databaseName});
553 # ADAM: set group resource
554 if (defined($args{groupName})) {
555 IDZebra::set_resource($self->{zh}, "group", $args{groupName});
558 # ADAM: rg no longer part of vector..
559 return ($rectype, \$sysno, $match, $fname, $buff, $len, $force);
562 # -----------------------------------------------------------------------------
565 my ($self,$mapfile) = @_;
567 if ($self->{cql_mapfile} ne $mapfile) {
568 unless (-f $mapfile) {
569 croak("Cannot find $mapfile");
571 if (defined ($self->{cql_ct})) {
572 IDZebra::cql_transform_close($self->{cql_ct});
574 $self->{cql_ct} = IDZebra::cql_transform_open_fname($mapfile);
575 $self->{cql_mapfile} = $mapfile;
578 return ($self->{cql_mapfile});
582 my ($self, $cqlquery) = @_;
583 unless (defined($self->{cql_ct})) {
584 croak("CQL map file is not specified yet.");
586 my $res = "\0" x 2048;
587 my $r = IDZebra::cql2pqf($self->{cql_ct}, $cqlquery, $res, 2048);
589 # carp ("Error transforming CQL query: '$cqlquery', status:$r");
596 # -----------------------------------------------------------------------------
598 # -----------------------------------------------------------------------------
600 my ($self, %args) = @_;
604 if ($args{cqlmap}) { $self->cqlmap($args{cqlmap}); }
612 ($query, $cqlstat) = $self->cql2pqf($args{cql});
614 croak ("Failed to transform query: '$args{cql}', ".
615 "status: ($cqlstat)");
619 croak ("No query given to search");
624 if ($args{databases}) {
625 @origdbs = $self->databases;
626 $self->databases(@{$args{databases}});
630 my $rsname = $args{rsname} ? $args{rsname} : $self->_new_setname;
632 my $rs = $self->_search_pqf($query, $rsname);
634 if ($args{databases}) {
635 $self->databases(@origdbs);
640 carp("Sort skipped due to search error: ".
643 $rs->sort($args{sort});
652 return ("set_".$self->{rscount}++);
656 my ($self, $query, $setname) = @_;
661 my $res = IDZebra::search_PQF($self->{zh},
666 my $rs = IDZebra::Resultset->new($self,
669 recordCount => $hits,
670 errCode => $self->errCode,
671 errString => $self->errString);
675 # -----------------------------------------------------------------------------
678 # Sorting of multiple result sets is not supported by zebra...
679 # -----------------------------------------------------------------------------
682 my ($self, $sortspec, $setname, @sets) = @_;
687 croak ("Sorting/merging of multiple resultsets is not supported now");
692 foreach my $rs (@sets) {
693 push (@setnames, $rs->{name});
694 $count += $rs->{recordCount}; # is this really sure ??? It doesn't
698 my $status = IDZebra::sort($self->{zh},
704 my $errCode = $self->errCode;
705 my $errString = $self->errString;
707 logf (LOG_LOG, "Sort status $setname: %d, errCode: %d, errString: %s",
708 $status, $errCode, $errString);
710 if ($status || $errCode) {$count = 0;}
712 my $rs = IDZebra::Resultset->new($self,
714 recordCount => $count,
716 errString => $errString);
720 # -----------------------------------------------------------------------------
722 # -----------------------------------------------------------------------------
724 my ($self, %args) = @_;
728 unless ($args{expression}) {
729 croak ("No scan expression given");
732 my $sl = IDZebra::ScanList->new($self,%args);
737 # ============================================================================
743 IDZebra::Session - A Zebra database server session for update and retrieval
747 $sess = IDZebra::Session->new(configFile => 'demo/zebra.cfg');
750 $sess = IDZebra::Session->open(configFile => 'demo/zebra.cfg',
751 groupName => 'demo1');
753 $sess->group(groupName => 'demo2');
759 $sess->update(path => 'lib');
761 my $s1=$sess->update_record(data => $rec1,
762 recordType => 'grs.perl.pod',
763 groupName => "demo1",
766 my $stat = $sess->end_trans;
768 $sess->databases('demo1','demo2');
770 my $rs1 = $sess->search(cqlmap => 'demo/cql.map',
771 cql => 'dc.title=IDZebra',
772 databases => [qw(demo1 demo2)]);
777 Zebra is a high-performance, general-purpose structured text indexing and retrieval engine. It reads structured records in a variety of input formats (eg. email, XML, MARC) and allows access to them through exact boolean search expressions and relevance-ranked free-text queries.
779 Zebra supports large databases (more than ten gigabytes of data, tens of millions of records). It supports incremental, safe database updates on live systems. You can access data stored in Zebra using a variety of Index Data tools (eg. YAZ and PHP/YAZ) as well as commercial and freeware Z39.50 clients and toolkits.
781 =head1 OPENING AND CLOSING A ZEBRA SESSIONS
783 For the time beeing only local database services are supported, the same way as calling zebraidx or zebrasrv from the command shell. In order to open a local Zebra database, with a specific configuration file, use
785 $sess = IDZebra::Session->new(configFile => 'demo/zebra.cfg');
790 $sess = IDZebra::Session->open(configFile => 'demo/zebra.cfg');
792 where $sess is going to be the object representing a Zebra Session. Whenever this variable gets out of scope, the session is closed, together with all active transactions, etc... Anyway, if you'd like to close the session, just say:
797 - close all transactions
798 - destroy all result sets and scan lists
801 Note, that if I<shadow registers> are enabled, the changes will not be committed automatically.
803 In the future different database access methods are going to be available,
806 $sess = IDZebra::Session->open(server => 'ostrich.technomat.hu:9999');
808 You can also use the B<record group> arguments described below directly when calling the constructor, or the open method:
810 $sess = IDZebra::Session->open(configFile => 'demo/zebra.cfg',
811 groupName => 'demo');
816 If you manage different sets of records that share common characteristics, you can organize the configuration settings for each type into "groups". See the Zebra manual on the configuration file (zebra.cfg).
818 For each open session a default record group is assigned. You can configure it in the constructor, or by the B<group> method:
820 $sess->group(groupName => ..., ...)
822 The following options are available:
828 This will select the named record group, and load the corresponding settings from the configuration file. All subsequent values will overwrite those...
830 =item B<databaseName>
832 The name of the (logical) database the updated records will belong to.
836 This path is used for directory updates (B<update>, B<delete> methods);
840 This option determines how to identify your records. See I<Zebra manual: Locating Records>
844 The record type used for indexing.
846 =item B<flagStoreData>
848 Specifies whether the records should be stored internally in the Zebra system files. If you want to maintain the raw records yourself, this option should be false (0). If you want Zebra to take care of the records for you, it should be true(1).
850 =item B<flagStoreKeys>
852 Specifies whether key information should be saved for a given group of records. If you plan to update/delete this type of records later this should be specified as 1; otherwise it should be 0 (default), to save register space.
858 =item B<fileVerboseLimit>
860 Skip log messages, when doing a directory update, and the specified number of files are processed...
862 =item B<databaseNamePath>
866 =item B<explainDatabase>
868 The name of the explain database to be used
872 Follow links when doing directory update.
876 You can use the same parameters calling all update methods.
878 =head1 TRANSACTIONS (READ / WRITE LOCKS)
880 A transaction is a block of record update (insert / modify / delete) or retrieval procedures. So, all call to such function will implicitly start a transaction, unless one is already started by
886 $sess->begin_trans(TRANS_RW)
888 (these two are equivalents). The effect of this call is a kind of lock: if you call is a write lock is put on the registers, so other processes trying to update the database will be blocked. If there is already an RW (Read-Write) transaction opened by another process, the I<begin_trans> call will be blocked.
892 $sess->begin_trans(TRANS_RO),
894 if you would like to put on a "read lock". This one is B<deprecated>, as while you have explicitly opened a transaction for read, you can't open another one for update. For example:
896 $sess->begin_trans(TRANS_RO);
897 $sess->begin_tran(TRANS_RW); # invalid, die here
903 $sess->begin_tran(TRANS_RW);
904 $sess->begin_trans(TRANS_RO);
908 is valid, but probably useless. Note again, that for each retrieval call, an RO transaction is opened. I<TRANS_RW> and I<TRANS_RO> are exported by default by IDZebra::Session.pm.
910 For multiple per-record I<updates> it's efficient to start transactions explicitly: otherwise registers (system files, vocabularies, etc..) are updated one by one. After finishing all requested updates, use
912 $stat = $sess->end_trans;
914 The return value is a ZebraTransactionStatus object, containing the following members as a hash reference:
916 $stat->{processed} # Number of records processed
917 $stat->{updated} # Number of records processed
918 $stat->{deleted} # Number of records processed
919 $stat->{inserted} # Number of records processed
920 $stat->{stime} # System time used
921 $stat->{utime} # User time used
923 Normally, if the perl code dies due to some runtime error, or the session is closed, then the API attempts to close all pending transactions.
925 =head1 THE SHADOW REGISTERS
927 The Zebra server supports updating of the index structures. That is, you can add, modify, or remove records from databases managed by Zebra without rebuilding the entire index. Since this process involves modifying structured files with various references between blocks of data in the files, the update process is inherently sensitive to system crashes, or to process interruptions: Anything but a successfully completed update process will leave the register files in an unknown state, and you will essentially have no recourse but to re-index everything, or to restore the register files from a backup medium. Further, while the update process is active, users cannot be allowed to access the system, as the contents of the register files may change unpredictably.
929 You can solve these problems by enabling the shadow register system in Zebra. During the updating procedure, zebraidx will temporarily write changes to the involved files in a set of "shadow files", without modifying the files that are accessed by the active server processes. If the update procedure is interrupted by a system crash or a signal, you simply repeat the procedure - the register files have not been changed or damaged, and the partially written shadow files are automatically deleted before the new updating procedure commences.
931 At the end of the updating procedure (or in a separate operation, if you so desire), the system enters a "commit mode". First, any active server processes are forced to access those blocks that have been changed from the shadow files rather than from the main register files; the unmodified blocks are still accessed at their normal location (the shadow files are not a complete copy of the register files - they only contain those parts that have actually been modified). If the commit process is interrupted at any point during the commit process, the server processes will continue to access the shadow files until you can repeat the commit procedure and complete the writing of data to the main register files. You can perform multiple update operations to the registers before you commit the changes to the system files, or you can execute the commit operation at the end of each update operation. When the commit phase has completed successfully, any running server processes are instructed to switch their operations to the new, operational register, and the temporary shadow files are deleted.
933 By default, (in the API !) the use of shadow registers is disabled. If zebra is configured that way (there is a "shadow" entry in zebra.cfg), then the shadow system can be enabled by calling:
941 If shadow system is enabled, then you have to commit changes you did, by calling:
945 Note, that you can also determine shadow usage in the session constructor:
947 $sess = IDZebra::Session->open(configFile => 'demo/zebra.cfg',
950 Changes to I<shadow> will not have effect, within a I<transaction> (ie.: a transaction is started either with shadow enabled or disabled). For more details, read Zebra documentation: I<Safe Updating - Using Shadow Registers>.
954 There are two ways to update data in a Zebra database using the perl API. You can update an entire directory structure just the way it's done by zebraidx:
956 $sess->update(path => 'lib');
958 This will update the database with the files in directory "lib", according to the current record group settings.
962 This will update the database with the files, specified by the default record group setting. I<path> has to be specified there...
964 $sess->update(groupName => 'demo1',
967 Update the database with files in "lib" according to the settings of group "demo1"
969 $sess->delete(groupName => 'demo1',
972 Delete the records derived from the files in directory "lib", according to the "demo1" group settings. Sounds complex? Read zebra documentation about identifying records.
974 You can also update records one by one, even directly from the memory:
976 $sysno = $sess->update_record(data => $rec1,
977 recordType => 'grs.perl.pod',
978 groupName => "demo1");
980 This will update the database with the given record buffer. Note, that in this case recordType is explicitly specified, as there is no filename given, and for the demo1 group, no default record type is specified. The return value is the system assigned id of the record.
982 You can also index a single file:
984 $sysno = $sess->update_record(file => "lib/IDZebra/Data1.pm");
986 Or, provide a buffer, and a filename (where filename will only be used to identify the record, if configured that way, and possibly to find out it's record type):
988 $sysno = $sess->update_record(data => $rec1,
989 file => "lib/IDZebra/Data1.pm");
991 And some crazy stuff:
993 $sysno = $sess->delete_record(sysno => $sysno);
995 where sysno in itself is sufficient to identify the record
997 $sysno = $sess->delete_record(data => $rec1,
998 recordType => 'grs.perl.pod',
999 groupName => "demo1");
1001 This case the record is extracted, and if already exists, located in the database, then deleted...
1003 $sysno = $sess->update_record(data => $rec1,
1005 recordType => 'grs.perl.pod',
1006 groupName => "demo1");
1008 Don't try this at home! This case, the record identifier string (which is normally generated according to the rules set in I<recordId> member of the record group, or in the I<recordId> parameter) is provided directly.... Looks much better this way:
1010 $sysno = $sess->update_record(data => $rec1,
1011 databaseName => 'books',
1012 recordId => '(bib1,ISBN)',
1013 recordType => 'grs.perl.pod',
1015 flagStoreKeys => 1);
1017 You can notice, that it's not necessary to define a record group in zebra.cfg: you can do it "on the fly" in your code.
1019 B<Important:> Note, that one record can be updated only once within a transaction - all subsequent updates are skipped. If you'd like to override this feature, use the I<force=E<gt>1> flag:
1021 $sysno = $sess->update_record(data => $rec1,
1022 recordType => 'grs.perl.pod',
1023 groupName => "demo1",
1026 If you don't like to update the record, if it alerady exists, use the I<insert_record> method:
1028 $sysno = $sess->insert_record(data => $rec1,
1029 recordType => 'grs.perl.pod',
1030 groupName => "demo1");
1032 In this case, sysno will be -1, if the record could not be added, because there was already one in the database, with the same record identifier (generated according to the I<recordId> setting).
1034 =head1 DATABASE SELECTION
1036 Within a zebra repository you can define logical databases. You can either do this by record groups, or by providing the databaseName argument for update methods. For each record the database name it belongs to is stored.
1038 For searching, you can select databases by calling:
1040 $sess->databases('db1','db2');
1042 This will not do anything if the given and only the given databases are already selected. You can get the list of the actually selected databases, by calling:
1044 @dblist = $sess->databases();
1048 It's nice to be able to store data in your repository... But it's useful to reach it as well. So this is how to do searching:
1050 $rs = $sess->search(databases => [qw(demo1,demo2)], # optional
1051 pqf => '@attr 1=4 computer');
1053 This is going to execute a search in databases demo1 and demo2, for title 'com,puter'. This is a PQF (Prefix Query Format) search, see YAZ documentation for details. The database selection is optional: if it's provided, the given list of databases is selected for this particular search, then the original selection is restored.
1055 =head2 CCL searching
1057 Not all users enjoy typing in prefix query structures and numerical attribute values, even in a minimalistic test client. In the library world, the more intuitive Common Command Language (or ISO 8777) has enjoyed some popularity - especially before the widespread availability of graphical interfaces. It is still useful in applications where you for some reason or other need to provide a symbolic language for expressing boolean query structures.
1059 The CCL searching is not currently supported by this API.
1061 =head2 CQL searching
1063 CQL - Common Query Language - was defined for the SRW protocol. In many ways CQL has a similar syntax to CCL. The objective of CQL is different. Where CCL aims to be an end-user language, CQL is the protocol query language for SRW.
1065 In order to map CQL queries to Zebra internal search structures, you have to define a mapping, the way it is described in YAZ documentation: I<Specification of CQL to RPN mapping>. The mapping is interpreted by the method:
1067 $sess->cqlmap($mapfile);
1069 Or, you can directly provide the I<mapfile> parameter for the search:
1071 $rs = $sess->search(cqlmap => 'demo/cql.map',
1072 cql => 'dc.title=IDZebra');
1074 As you see, CQL searching is so simple: just give the query in the I<cql> parameter.
1078 If you'd like the search results to be sorted, use the I<sort> parameter:
1080 $rs = $sess->search(cql => 'IDZebra',
1083 Note, that B<currently> this is (almost) equivalent to
1085 $rs = $sess->search(cql => 'IDZebra');
1086 $rs->sort('1=4 ia');
1088 but in the further versions of Zebra and this API a single phase search and sort will take place, optimizing performance. For more details on sorting, see I<IDZebra::ResultSet> manpage.
1092 As you have seen, the result of the search request is a I<Resultset> object.
1093 It contains number of hits, and search status, and can be used to sort and retrieve the resulting records.
1095 $count = $rs->count;
1097 printf ("RS Status is %d (%s)\n", $rs->errCode, $rs->errString);
1099 I<$rs-E<gt>errCode> is 0, if there were no errors during search. Read the I<IDZebra::Resultset> manpage for more details.
1103 Zebra supports scanning index values. The result of the
1105 $sl = $sess->scan(expression => "a");
1107 call is an I<IDZebra::ScanList> object, what you can use to list the values. The scan expression has to be provided in a PQF like format. Examples:
1109 B< a> (scan trough words of "default", "Any" indexes)
1112 B< @attr 1=1016 a> (same effect)
1115 B< @attr 1=4 @attr 6=2 a> (scan trough titles as phrases)
1117 An illegal scan expression will cause your code to die. If you'd like to select databases just for the scan call, you can optionally use the I<databases> parameter:
1119 $sl = $sess->scan(expression => "a",
1120 databases => [qw(demo1 demo2)]);
1122 You can use the I<IDZebra::ScanList> object returned by the i<scan> method, to reach the result. Check I<IDZebra::ScanList> manpage for more details.
1124 =head1 SESSION STATUS AND ERRORS
1126 Most of the API calls causes die, if an error occures. You avoid this, by using eval {} blocks. The following methods are available to get the status of Zebra service:
1132 The Zebra provided error code... (for the result of the last call);
1136 Error string corresponding to the message
1140 Additional information for the status
1144 This functionality may change, see TODO.
1146 =head1 LOGGING AND MISC. FUNCTIONS
1148 Zebra provides logging facility for the internal events, and also for application developers trough the API. See manpage I<IDZebra::Logger> for details.
1152 =item B<IDZebra::LogFile($filename)>
1154 Will set the output file for logging. By default it's STDERR;
1156 =item B<IDZebra::LogLevel(15)>
1158 Set log level. 0 for no logs. See IDZebra::Logger for usable flags.
1162 Some other functions
1166 =item B<$sess-E<gt>init>
1168 Initialize, and clean registers. This will remove all data!
1170 =item B<$sess-E<gt>compact>
1172 Compact the registers (? does this work)
1174 =item B<$sess-E<gt>show>
1176 Doesn't have too much meaning. Don't try :)
1184 =item B<Clean up error handling>
1186 By default all zebra errors should cause die. (such situations could be avoided by using eval {}), and then check for errCode, errString... An optional flag or package variable should be introduced to override this, and skip zebra errors, to let the user decide what to do.
1188 =item B<Make the package self-distributable>
1190 Build and link with installed header and library files
1194 Test shadow system, unicode...
1198 Cleanup, arrange, remove redundancy
1208 Peter Popovics, pop@technomat.hu
1212 Zebra documentation, Zebra::ResultSet, Zebra::ScanList, Zebra::Logger manpages