1 /* $Id: zserver.c,v 1.100 2002-10-22 12:51:08 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
40 static int bend_sort (void *handle, bend_sort_rr *rr);
41 static int bend_delete (void *handle, bend_delete_rr *rr);
42 static int bend_esrequest (void *handle, bend_esrequest_rr *rr);
43 static int bend_segment (void *handle, bend_segment_rr *rr);
44 static int bend_search (void *handle, bend_search_rr *r);
45 static int bend_fetch (void *handle, bend_fetch_rr *r);
46 static int bend_scan (void *handle, bend_scan_rr *r);
48 bend_initresult *bend_init (bend_initrequest *q)
50 bend_initresult *r = (bend_initresult *)
51 odr_malloc (q->stream, sizeof(*r));
53 struct statserv_options_block *sob;
59 q->bend_sort = bend_sort;
60 q->bend_delete = bend_delete;
61 q->bend_esrequest = bend_esrequest;
62 q->bend_segment = bend_segment;
63 q->bend_search = bend_search;
64 q->bend_fetch = bend_fetch;
65 q->bend_scan = bend_scan;
67 q->implementation_name = "Zebra Information Server";
68 q->implementation_version = "Zebra " ZEBRAVER;
70 logf (LOG_DEBUG, "bend_init");
72 sob = statserv_getcontrol ();
73 if (!(zh = zebra_open (sob->handle)))
75 yaz_log (LOG_WARN, "Failed to read config `%s'", sob->configname);
81 if (q->auth->which == Z_IdAuthentication_open)
83 char *openpass = xstrdup (q->auth->u.open);
84 char *cp = strchr (openpass, '/');
88 user = nmem_strdup (odr_getmem (q->stream), openpass);
89 passwd = nmem_strdup (odr_getmem (q->stream), cp+1);
94 if (zebra_auth (zh, user, passwd))
102 if (q->charneg_request) /* characater set and langauge negotiation? */
111 NMEM nmem = nmem_create ();
112 yaz_log (LOG_LOG, "character set and language negotiation");
114 yaz_get_proposal_charneg (nmem, q->charneg_request,
115 &charsets, &num_charsets,
116 &langs, &num_langs, &selected);
117 for (i = 0; i < num_charsets; i++)
119 yaz_log (LOG_LOG, "charset %d %s", i, charsets[i]);
121 if (odr_set_charset (q->decode, "UTF-8", charsets[i]) == 0)
123 odr_set_charset (q->stream, charsets[i], "UTF-8");
125 zebra_record_encoding (zh, charsets[i]);
126 q->charneg_response =
127 yaz_set_response_charneg (q->stream, charsets[i],
137 static void search_terms (ZebraHandle zh, bend_search_rr *r)
143 struct Z_External *ext;
144 Z_SearchInfoReport *sr;
146 /* get no of terms for result set */
147 no_terms = zebra_resultSetTerms (zh, r->setname, 0, 0, 0, 0, 0);
151 r->search_info = odr_malloc (r->stream, sizeof(*r->search_info));
153 r->search_info->num_elements = 1;
154 r->search_info->list =
155 odr_malloc (r->stream, sizeof(*r->search_info->list));
156 r->search_info->list[0] =
157 odr_malloc (r->stream, sizeof(**r->search_info->list));
158 r->search_info->list[0]->category = 0;
159 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
160 ext = odr_malloc (r->stream, sizeof(*ext));
161 r->search_info->list[0]->information.externallyDefinedInfo = ext;
162 ext->direct_reference =
163 yaz_oidval_to_z3950oid (r->stream, CLASS_USERINFO, VAL_SEARCHRES1);
164 ext->indirect_reference = 0;
166 ext->which = Z_External_searchResult1;
167 sr = odr_malloc (r->stream, sizeof(Z_SearchInfoReport));
168 ext->u.searchResult1 = sr;
170 sr->elements = odr_malloc (r->stream, sr->num *
171 sizeof(*sr->elements));
172 for (i = 0; i<no_terms; i++)
176 size_t len = sizeof(outbuf);
177 zebra_resultSetTerms (zh, r->setname, i,
178 &count, &type, outbuf, &len);
180 sr->elements[i] = odr_malloc (r->stream, sizeof(**sr->elements));
181 sr->elements[i]->subqueryId = 0;
182 sr->elements[i]->fullQuery = odr_malloc (r->stream,
184 *sr->elements[i]->fullQuery = 0;
185 sr->elements[i]->subqueryExpression =
186 odr_malloc (r->stream, sizeof(Z_QueryExpression));
187 sr->elements[i]->subqueryExpression->which =
188 Z_QueryExpression_term;
189 sr->elements[i]->subqueryExpression->u.term =
190 odr_malloc (r->stream, sizeof(Z_QueryExpressionTerm));
191 term = odr_malloc (r->stream, sizeof(Z_Term));
192 sr->elements[i]->subqueryExpression->u.term->queryTerm = term;
195 case Z_Term_characterString:
196 yaz_log (LOG_DEBUG, "term as characterString");
197 term->which = Z_Term_characterString;
198 term->u.characterString = odr_strdup (r->stream, outbuf);
201 yaz_log (LOG_DEBUG, "term as general");
202 term->which = Z_Term_general;
203 term->u.general = odr_malloc (r->stream, sizeof(*term->u.general));
204 term->u.general->size = term->u.general->len = len;
205 term->u.general->buf = odr_malloc (r->stream, len);
206 memcpy (term->u.general->buf, outbuf, len);
209 term->which = Z_Term_general;
210 term->u.null = odr_nullval();
212 sr->elements[i]->subqueryExpression->u.term->termComment = 0;
213 sr->elements[i]->subqueryInterpretation = 0;
214 sr->elements[i]->subqueryRecommendation = 0;
215 sr->elements[i]->subqueryCount = odr_intdup (r->stream, count);
216 sr->elements[i]->subqueryWeight = 0;
217 sr->elements[i]->resultsByDB = 0;
221 int bend_search (void *handle, bend_search_rr *r)
223 ZebraHandle zh = (ZebraHandle) handle;
229 if (zebra_select_databases (zh, r->num_bases,
230 (const char **) r->basenames))
232 zebra_result (zh, &r->errcode, &r->errstring);
235 logf (LOG_LOG, "ResultSet '%s'", r->setname);
236 switch (r->query->which)
238 case Z_Query_type_1: case Z_Query_type_101:
239 zebra_search_rpn (zh, r->decode, r->stream, r->query->u.type_1,
240 r->setname, &r->hits);
241 zebra_result (zh, &r->errcode, &r->errstring);
243 search_terms (zh, r);
247 r->errstring = "type-2";
256 int bend_fetch (void *handle, bend_fetch_rr *r)
258 ZebraHandle zh = (ZebraHandle) handle;
259 ZebraRetrievalRecord retrievalRecord;
261 retrievalRecord.position = r->number;
264 zebra_records_retrieve (zh, r->stream, r->setname, r->comp,
265 r->request_format, 1, &retrievalRecord);
266 zebra_result (zh, &r->errcode, &r->errstring);
267 /* non Surrogate Diagnostic OR Surrogate Diagnostic */
268 if (r->errcode == 0 && retrievalRecord.errCode)
270 r->surrogate_flag = 1;
271 r->errcode = retrievalRecord.errCode;
272 r->errstring = retrievalRecord.errString;
273 r->basename = retrievalRecord.base;
275 else if (r->errcode == 0) /* Database Record */
278 r->basename = retrievalRecord.base;
279 r->record = retrievalRecord.buf;
280 r->len = retrievalRecord.len;
281 r->output_format = retrievalRecord.format;
286 static int bend_scan (void *handle, bend_scan_rr *r)
288 ZebraScanEntry *entries;
289 ZebraHandle zh = (ZebraHandle) handle;
292 if (zebra_select_databases (zh, r->num_bases,
293 (const char **) r->basenames))
295 zebra_result (zh, &r->errcode, &r->errstring);
298 r->entries = (struct scan_entry *)
299 odr_malloc (r->stream, sizeof(*r->entries) * r->num_entries);
300 zebra_scan (zh, r->stream, r->term,
303 &r->num_entries, &entries, &is_partial);
305 r->status = BEND_SCAN_PARTIAL;
307 r->status = BEND_SCAN_SUCCESS;
308 for (i = 0; i < r->num_entries; i++)
310 r->entries[i].term = entries[i].term;
311 r->entries[i].occurrences = entries[i].occurrences;
313 zebra_result (zh, &r->errcode, &r->errstring);
317 void bend_close (void *handle)
319 zebra_close ((ZebraHandle) handle);
320 xmalloc_trav("bend_close");
324 int bend_sort (void *handle, bend_sort_rr *rr)
326 ZebraHandle zh = (ZebraHandle) handle;
328 zebra_sort (zh, rr->stream,
329 rr->num_input_setnames, (const char **) rr->input_setnames,
330 rr->output_setname, rr->sort_sequence, &rr->sort_status);
331 zebra_result (zh, &rr->errcode, &rr->errstring);
335 int bend_delete (void *handle, bend_delete_rr *rr)
337 ZebraHandle zh = (ZebraHandle) handle;
339 rr->delete_status = zebra_deleleResultSet(zh, rr->function,
340 rr->num_setnames, rr->setnames,
345 static int es_admin_request (ZebraHandle zh, Z_AdminEsRequest *r)
347 switch (r->toKeep->which)
349 case Z_ESAdminOriginPartToKeep_reIndex:
350 yaz_log(LOG_LOG, "adm-reindex");
352 case Z_ESAdminOriginPartToKeep_truncate:
353 yaz_log(LOG_LOG, "adm-truncate");
355 case Z_ESAdminOriginPartToKeep_drop:
356 yaz_log(LOG_LOG, "adm-drop");
358 case Z_ESAdminOriginPartToKeep_create:
359 yaz_log(LOG_LOG, "adm-create %s", r->toKeep->databaseName);
360 zebra_admin_create (zh, r->toKeep->databaseName);
362 case Z_ESAdminOriginPartToKeep_import:
363 yaz_log(LOG_LOG, "adm-import");
364 zebra_admin_import_begin (zh, r->toKeep->databaseName,
365 r->toKeep->u.import->recordType);
367 case Z_ESAdminOriginPartToKeep_refresh:
368 yaz_log(LOG_LOG, "adm-refresh");
370 case Z_ESAdminOriginPartToKeep_commit:
371 yaz_log(LOG_LOG, "adm-commit");
373 case Z_ESAdminOriginPartToKeep_shutdown:
374 yaz_log(LOG_LOG, "shutdown");
375 zebra_admin_shutdown(zh);
377 case Z_ESAdminOriginPartToKeep_start:
378 yaz_log(LOG_LOG, "start");
379 zebra_admin_start(zh);
382 yaz_log(LOG_LOG, "unknown admin");
384 if (r->toKeep->databaseName)
386 yaz_log(LOG_LOG, "database %s", r->toKeep->databaseName);
391 static int es_admin (ZebraHandle zh, Z_Admin *r)
395 case Z_Admin_esRequest:
396 es_admin_request (zh, r->u.esRequest);
398 case Z_Admin_taskPackage:
399 yaz_log (LOG_LOG, "adm taskpackage (unhandled)");
408 int bend_segment (void *handle, bend_segment_rr *rr)
410 ZebraHandle zh = (ZebraHandle) handle;
411 Z_Segment *segment = rr->segment;
413 if (segment->num_segmentRecords)
414 zebra_admin_import_segment (zh, rr->segment);
416 zebra_admin_import_end (zh);
420 int bend_esrequest (void *handle, bend_esrequest_rr *rr)
422 ZebraHandle zh = (ZebraHandle) handle;
424 yaz_log(LOG_LOG, "function: %d", *rr->esr->function);
425 if (rr->esr->packageName)
426 yaz_log(LOG_LOG, "packagename: %s", rr->esr->packageName);
427 yaz_log(LOG_LOG, "Waitaction: %d", *rr->esr->waitAction);
429 if (!rr->esr->taskSpecificParameters)
431 yaz_log (LOG_WARN, "No task specific parameters");
433 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
435 es_admin (zh, rr->esr->taskSpecificParameters->u.adminService);
437 zebra_result (zh, &rr->errcode, &rr->errstring);
439 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
441 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
442 yaz_log (LOG_LOG, "Received DB Update");
443 if (up->which == Z_IUUpdate_esRequest)
445 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
446 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
447 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
449 yaz_log (LOG_LOG, "action");
452 switch (*toKeep->action)
454 case Z_IUOriginPartToKeep_recordInsert:
455 yaz_log (LOG_LOG, "recordInsert");
457 case Z_IUOriginPartToKeep_recordReplace:
458 yaz_log (LOG_LOG, "recordUpdate");
460 case Z_IUOriginPartToKeep_recordDelete:
461 yaz_log (LOG_LOG, "recordDelete");
463 case Z_IUOriginPartToKeep_elementUpdate:
464 yaz_log (LOG_LOG, "elementUpdate");
466 case Z_IUOriginPartToKeep_specialUpdate:
467 yaz_log (LOG_LOG, "specialUpdate");
469 case Z_ESAdminOriginPartToKeep_shutdown:
470 yaz_log (LOG_LOG, "shutDown");
472 case Z_ESAdminOriginPartToKeep_start:
473 yaz_log (LOG_LOG, "start");
476 yaz_log (LOG_LOG, " unknown (%d)", *toKeep->action);
479 if (toKeep->databaseName)
481 yaz_log (LOG_LOG, "database: %s", toKeep->databaseName);
483 if (zebra_select_database(zh, toKeep->databaseName))
488 yaz_log (LOG_WARN, "no database supplied for ES Update");
490 rr->errstring = "database";
496 zebra_begin_trans (zh);
497 for (i = 0; i < notToKeep->num; i++)
499 Z_External *rec = notToKeep->elements[i]->record;
500 struct oident *oident = 0;
501 Odr_oct *recid = notToKeep->elements[i]->u.opaque;
506 rr->errstring = "record Id not supplied";
509 if (notToKeep->elements[i]->which !=
510 Z_IUSuppliedRecords_elem_opaque)
513 rr->errstring = "only opaque record ID supported";
517 if (rec->direct_reference)
519 oident = oid_getentbyoid(rec->direct_reference);
521 yaz_log (LOG_LOG, "record %d type %s", i,
526 case Z_External_sutrs:
527 if (rec->u.octet_aligned->len > 170)
528 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
532 yaz_log (LOG_LOG, "%d bytes:\n%s",
536 case Z_External_octet:
537 if (rec->u.octet_aligned->len > 170)
538 yaz_log (LOG_LOG, "%d bytes:\n%.168s ...",
539 rec->u.octet_aligned->len,
540 rec->u.octet_aligned->buf);
542 yaz_log (LOG_LOG, "%d bytes\n%s",
543 rec->u.octet_aligned->len,
544 rec->u.octet_aligned->buf);
546 if (oident && oident->value != VAL_TEXT_XML)
549 rr->errstring = "only XML update supported";
552 if (rec->which == Z_External_octet)
556 if (*toKeep->action ==
557 Z_IUOriginPartToKeep_recordInsert)
559 if (*toKeep->action ==
560 Z_IUOriginPartToKeep_recordReplace)
562 if (*toKeep->action ==
563 Z_IUOriginPartToKeep_recordDelete)
565 if (*toKeep->action ==
566 Z_IUOriginPartToKeep_specialUpdate)
572 rr->errstring = "unsupported ES Update action";
577 int r = zebra_admin_exchange_record (
578 zh, toKeep->databaseName,
579 rec->u.octet_aligned->buf,
580 rec->u.octet_aligned->len,
581 recid->buf, recid->len,
583 if (r && *toKeep->action ==
584 Z_IUOriginPartToKeep_specialUpdate)
586 r = zebra_admin_exchange_record (
587 zh, toKeep->databaseName,
588 rec->u.octet_aligned->buf,
589 rec->u.octet_aligned->len,
590 recid->buf, recid->len,
596 rr->errstring = "record exchange failed";
602 zebra_end_trans (zh);
608 yaz_log (LOG_WARN, "Unknown Extended Service(%d)",
609 rr->esr->taskSpecificParameters->which);
616 static void bend_start (struct statserv_options_block *sob)
623 char *pidfile = "zebrasrv.pid";
624 int fd = creat (pidfile, 0666);
626 logf (LOG_WARN|LOG_ERRNO, "creat %s", pidfile);
631 sprintf (pidstr, "%ld", (long) getpid ());
632 write (fd, pidstr, strlen(pidstr));
638 zebra_stop((ZebraService) sob->handle);
639 sob->handle = zebra_start(sob->configname);
642 yaz_log (LOG_FATAL, "Failed to read config `%s'", sob->configname);
647 static void bend_stop(struct statserv_options_block *sob)
653 unlink ("zebrasrv.pid");
657 ZebraService service = sob->handle;
662 int main (int argc, char **argv)
664 struct statserv_options_block *sob;
666 sob = statserv_getcontrol ();
667 strcpy (sob->configname, "zebra.cfg");
668 sob->bend_start = bend_start;
669 sob->bend_stop = bend_stop;
671 strcpy (sob->service_display_name, "Zebra Server");
673 statserv_setcontrol (sob);
675 return statserv_main (argc, argv, bend_init, bend_close);