IDZEBRA 2.2.8
zebrasrv.c
Go to the documentation of this file.
1/* This file is part of the Zebra server.
2 Copyright (C) Index Data
3
4Zebra is free software; you can redistribute it and/or modify it under
5the terms of the GNU General Public License as published by the Free
6Software Foundation; either version 2, or (at your option) any later
7version.
8
9Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or
11FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17
18*/
19
20#if HAVE_CONFIG_H
21#include <config.h>
22#endif
23#include <assert.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <fcntl.h>
27#ifdef WIN32
28#include <io.h>
29#include <process.h>
30#include <sys/locking.h>
31#endif
32#if HAVE_UNISTD_H
33#include <unistd.h>
34#endif
35
36#include <errno.h>
37#include <yaz/log.h>
38#include <yaz/ill.h>
39#include <yaz/yaz-util.h>
40#include <yaz/yaz-version.h>
41#include <yaz/diagbib1.h>
42#include <yaz/querytowrbuf.h>
43#include <yaz/snprintf.h>
44#include <yaz/pquery.h>
45#include <sys/types.h>
46
47#include <yaz/backend.h>
48#include <yaz/charneg.h>
49#include <idzebra/api.h>
50
51static int bend_sort(void *handle, bend_sort_rr *rr);
52static int bend_delete(void *handle, bend_delete_rr *rr);
53static int bend_esrequest(void *handle, bend_esrequest_rr *rr);
54static int bend_segment(void *handle, bend_segment_rr *rr);
55static int bend_search(void *handle, bend_search_rr *r);
56static int bend_fetch(void *handle, bend_fetch_rr *r);
57static int bend_scan(void *handle, bend_scan_rr *r);
58
59bend_initresult *bend_init(bend_initrequest *q)
60{
61 bend_initresult *r = (bend_initresult *)
62 odr_malloc(q->stream, sizeof(*r));
63 ZebraHandle zh;
64 struct statserv_options_block *sob;
65 char *user = NULL;
66 char *passwd = NULL;
67 char version_str[16];
68
69 r->errcode = 0;
70 r->errstring = 0;
71 q->bend_sort = bend_sort;
72 q->bend_delete = bend_delete;
73 q->bend_esrequest = bend_esrequest;
74 q->bend_segment = bend_segment;
75 q->bend_search = bend_search;
76 q->bend_fetch = bend_fetch;
77 q->bend_scan = bend_scan;
78
79 zebra_get_version(version_str, 0);
80
81 q->implementation_name = "Zebra Information Server";
82 q->implementation_version = odr_strdup(q->stream, version_str);
83
84 yaz_log(YLOG_DEBUG, "bend_init");
85
86 sob = statserv_getcontrol();
87 if (!(zh = zebra_open(sob->handle, 0)))
88 {
89 yaz_log(YLOG_WARN, "Failed to read config `%s'", sob->configname);
90 r->errcode = YAZ_BIB1_PERMANENT_SYSTEM_ERROR;
91 return r;
92 }
93 r->handle = zh;
94
95 q->query_charset = odr_strdup(q->stream, zebra_get_encoding(zh));
96 if (q->auth)
97 {
98 if (q->auth->which == Z_IdAuthentication_open)
99 {
100 char *openpass = xstrdup(q->auth->u.open);
101 char *cp = strchr(openpass, '/');
102 if (cp)
103 {
104 *cp = '\0';
105 user = nmem_strdup(odr_getmem(q->stream), openpass);
106 passwd = nmem_strdup(odr_getmem(q->stream), cp+1);
107 }
108 xfree(openpass);
109 }
110 else if (q->auth->which == Z_IdAuthentication_idPass)
111 {
112 Z_IdPass *idPass = q->auth->u.idPass;
113
114 user = idPass->userId;
115 passwd = idPass->password;
116 }
117 }
118 if (zebra_auth(zh, user, passwd) != ZEBRA_OK)
119 {
120 r->errcode = YAZ_BIB1_INIT_AC_BAD_USERID_AND_OR_PASSWORD;
121 r->errstring = user;
122 return r;
123 }
124 if (q->charneg_request) /* characater set and language negotiation? */
125 {
126 char **charsets = 0;
127 int num_charsets;
128 char **langs = 0;
129 int num_langs = 0;
130 int selected = 0;
131 int i;
132 NMEM nmem = nmem_create();
133
134 yaz_get_proposal_charneg(nmem, q->charneg_request,
135 &charsets, &num_charsets,
136 &langs, &num_langs, &selected);
137
138 for (i = 0; i < num_charsets; i++)
139 {
140 const char *right_name = "";
141 /*
142 * FIXME! It is like rudiment :-))
143 * We have to support this short names of character sets,
144 * because a lot servers in Russia to use own in during
145 * character set and language negotiation still.
146 */
147
148 if (!yaz_matchstr(charsets[i], "win")) {
149 right_name = "WINDOWS-1251";
150 } else if (!yaz_matchstr(charsets[i], "koi")) {
151 right_name = "KOI8-R";
152 } else if (!yaz_matchstr(charsets[i], "iso")) {
153 right_name = "ISO-8859-5";
154 } else if (!yaz_matchstr(charsets[i], "dos")) {
155 right_name = "CP866";
156 } else if (!yaz_matchstr(charsets[i], "uni")) {
157 right_name = "UTF-8";
158 } else {
159 right_name = charsets[i];
160 }
161 if (odr_set_charset(q->decode, "UTF-8", right_name) == 0)
162 {
163 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): OK", i,
164 charsets[i], right_name);
165 odr_set_charset(q->stream, right_name, "UTF-8");
166 if (selected)
167 zebra_record_encoding(zh, right_name);
168 zebra_octet_term_encoding(zh, right_name);
169 q->charneg_response =
170 yaz_set_response_charneg(q->stream, charsets[i],
171 0, selected);
172 break;
173 } else {
174 yaz_log(YLOG_LOG, "charset %d %s (proper name %s): unsupported", i,
175 charsets[i], right_name);
176 }
177 }
178 nmem_destroy(nmem);
179 }
180 return r;
181}
182
183static void search_terms(ZebraHandle zh, bend_search_rr *r)
184{
185 int no_terms;
186 int i;
187 int type = Z_Term_general;
188 struct Z_External *ext;
189 Z_SearchInfoReport *sr;
190
191 zebra_result_set_term_no(zh, r->setname, &no_terms);
192 if (!no_terms)
193 return;
194
195 r->search_info = odr_malloc(r->stream, sizeof(*r->search_info));
196
197 r->search_info->num_elements = 1;
198 r->search_info->list =
199 odr_malloc(r->stream, sizeof(*r->search_info->list));
200 r->search_info->list[0] =
201 odr_malloc(r->stream, sizeof(**r->search_info->list));
202 r->search_info->list[0]->category = 0;
203 r->search_info->list[0]->which = Z_OtherInfo_externallyDefinedInfo;
204 ext = odr_malloc(r->stream, sizeof(*ext));
205 r->search_info->list[0]->information.externallyDefinedInfo = ext;
206 ext->direct_reference = odr_oiddup(r->stream,
207 yaz_oid_userinfo_searchresult_1);
208 ext->indirect_reference = 0;
209 ext->descriptor = 0;
210 ext->which = Z_External_searchResult1;
211 sr = odr_malloc(r->stream, sizeof(Z_SearchInfoReport));
212 ext->u.searchResult1 = sr;
213 sr->num = no_terms;
214 sr->elements = odr_malloc(r->stream, sr->num *
215 sizeof(*sr->elements));
216 for (i = 0; i < no_terms; i++)
217 {
218 Z_SearchInfoReport_s *se;
219 Z_Term *term;
220 zint count;
221 int approx;
222 char outbuf[1024];
223 size_t len = sizeof(outbuf);
224 const char *term_ref_id = 0;
225
226 zebra_result_set_term_info(zh, r->setname, i,
227 &count, &approx, outbuf, &len,
228 &term_ref_id);
229 se = sr->elements[i] = odr_malloc(r->stream, sizeof(**sr->elements));
230 se->subqueryId = term_ref_id ?
231 odr_strdup(r->stream, term_ref_id) : 0;
232
233 se->fullQuery = odr_booldup(r->stream, 0);
234 se->subqueryExpression =
235 odr_malloc(r->stream, sizeof(Z_QueryExpression));
236 se->subqueryExpression->which =
237 Z_QueryExpression_term;
238 se->subqueryExpression->u.term =
239 odr_malloc(r->stream, sizeof(Z_QueryExpressionTerm));
240 term = odr_malloc(r->stream, sizeof(Z_Term));
241 se->subqueryExpression->u.term->queryTerm = term;
242 switch (type)
243 {
244 case Z_Term_characterString:
245 term->which = Z_Term_characterString;
246 term->u.characterString = odr_strdup(r->stream, outbuf);
247 break;
248 case Z_Term_general:
249 term->which = Z_Term_general;
250 term->u.general = odr_create_Odr_oct(r->stream,
251#if YAZ_VERSIONL < 0x50000
252 (unsigned char *)
253#endif
254 outbuf, len);
255 break;
256 default:
257 term->which = Z_Term_general;
258 term->u.null = odr_nullval();
259 }
260 se->subqueryExpression->u.term->termComment = 0;
261 se->subqueryInterpretation = 0;
262 se->subqueryRecommendation = 0;
263 se->subqueryCount = odr_intdup(r->stream, count);
264 se->subqueryWeight = 0;
265 se->resultsByDB = 0;
266 }
267}
268
269static int break_handler(void *client_data)
270{
271 bend_association assoc =(bend_association) client_data;
272 if (!bend_assoc_is_alive(assoc))
273 return 1;
274 return 0;
275}
276
277static Z_RPNQuery *query_add_sortkeys(ODR o, Z_RPNQuery *query,
278 const char *sortKeys)
279{
280#if YAZ_VERSIONL >= 0x40200
281 /* sortkey layour: path,schema,ascending,caseSensitive,missingValue */
282 /* see cql_sortby_to_sortkeys of YAZ. */
283 char **sortspec;
284 int num_sortspec = 0;
285 int i;
286
287 if (sortKeys)
288 nmem_strsplit_blank(odr_getmem(o), sortKeys, &sortspec, &num_sortspec);
289 if (num_sortspec > 0)
290 {
291 Z_RPNQuery *nquery;
292 WRBUF w = wrbuf_alloc();
293
294 /* left operand 'd' will be replaced by original query */
295 wrbuf_puts(w, "@or d");
296 for (i = 0; i < num_sortspec; i++)
297 {
298 char **arg;
299 int num_arg;
300 int ascending = 1;
301 nmem_strsplitx(odr_getmem(o), ",", sortspec[i], &arg, &num_arg, 0);
302
303 if (num_arg > 5 || num_arg < 1)
304 {
305 yaz_log(YLOG_WARN, "Invalid sort spec '%s' num_arg=%d",
306 sortspec[i], num_arg);
307 break;
308 }
309 if (num_arg > 2 && arg[2][0])
310 ascending = atoi(arg[2]);
311
312 if (i < num_sortspec-1)
313 wrbuf_puts(w, " @or");
314 wrbuf_puts(w, " @attr 1=");
315 yaz_encode_pqf_term(w, arg[0], strlen(arg[0]));
316 wrbuf_printf(w, "@attr 7=%d %d", ascending ? 1 : 2, i);
317 }
318 nquery = p_query_rpn(o, wrbuf_cstr(w));
319 if (!nquery)
320 {
321 yaz_log(YLOG_WARN, "query_add_sortkeys: bad RPN: '%s'",
322 wrbuf_cstr(w));
323 }
324 else
325 {
326 Z_RPNStructure *s = nquery->RPNStructure;
327
328 if (s->which != Z_RPNStructure_complex)
329 {
330 yaz_log(YLOG_WARN, "query_add_sortkeys: not complex operand");
331 }
332 else
333 {
334 /* left operand 'd' is replaced by origial query */
335 s->u.complex->s1 = query->RPNStructure;
336 /* and original query points to our new or+sort things */
337 query->RPNStructure = s;
338 }
339 }
340 wrbuf_destroy(w);
341 }
342#else
343 if (sortKeys)
344 {
345 yaz_log(YLOG_WARN, "sortkeys ignored because YAZ version < 4.2.0");
346 }
347#endif
348 return query;
349}
350
351int bend_search(void *handle, bend_search_rr *r)
352{
353 ZebraHandle zh = (ZebraHandle) handle;
354 zint zhits = 0;
355 ZEBRA_RES res;
356 Z_RPNQuery *q2;
357
358 res = zebra_select_databases(zh, r->num_bases,
359 (const char **) r->basenames);
360 if (res != ZEBRA_OK)
361 {
362 zebra_result(zh, &r->errcode, &r->errstring);
363 return 0;
364 }
365 zebra_set_break_handler(zh, break_handler, r->association);
366 yaz_log(YLOG_DEBUG, "ResultSet '%s'", r->setname);
367 switch (r->query->which)
368 {
369 case Z_Query_type_1: case Z_Query_type_101:
370 q2 = query_add_sortkeys(r->stream, r->query->u.type_1, r->srw_sortKeys);
371 res = zebra_search_RPN_x(zh, r->stream, q2,
372 r->setname, &zhits,
373 &r->estimated_hit_count,
374 &r->partial_resultset);
375 if (res != ZEBRA_OK)
376 zebra_result(zh, &r->errcode, &r->errstring);
377 else
378 {
379 r->hits = zhits;
380 search_terms(zh, r);
381 }
382 break;
383 case Z_Query_type_2:
384 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
385 r->errstring = "type-2";
386 break;
387 default:
388 r->errcode = YAZ_BIB1_QUERY_TYPE_UNSUPP;
389 }
390 zebra_set_break_handler(zh, 0, 0);
391 return 0;
392}
393
394
395int bend_fetch(void *handle, bend_fetch_rr *r)
396{
397 ZebraHandle zh = (ZebraHandle) handle;
398 ZebraRetrievalRecord retrievalRecord;
399 ZEBRA_RES res;
400
401 retrievalRecord.position = r->number;
402
403 r->last_in_set = 0;
404 res = zebra_records_retrieve(zh, r->stream, r->setname, r->comp,
405 r->request_format, 1, &retrievalRecord);
406 if (res != ZEBRA_OK)
407 {
408 /* non-surrogate diagnostic */
409 zebra_result(zh, &r->errcode, &r->errstring);
410 }
411 else if (retrievalRecord.errCode)
412 {
413 /* surrogate diagnostic (diagnostic per record) */
414 r->surrogate_flag = 1;
415 r->errcode = retrievalRecord.errCode;
416 r->errstring = retrievalRecord.errString;
417 r->basename = retrievalRecord.base;
418 }
419 else
420 {
421 r->basename = retrievalRecord.base;
422 r->record = retrievalRecord.buf;
423 r->len = retrievalRecord.len;
424 r->output_format = odr_oiddup(r->stream, retrievalRecord.format);
425 }
426 return 0;
427}
428
429static int bend_scan(void *handle, bend_scan_rr *r)
430{
431 ZebraScanEntry *entries;
432 ZebraHandle zh = (ZebraHandle) handle;
433 int is_partial, i;
434 ZEBRA_RES res;
435
436 res = zebra_select_databases(zh, r->num_bases,
437 (const char **) r->basenames);
438 if (res != ZEBRA_OK)
439 {
440 zebra_result(zh, &r->errcode, &r->errstring);
441 return 0;
442 }
443 if (r->step_size != 0 && *r->step_size != 0) {
444 r->errcode = YAZ_BIB1_ONLY_ZERO_STEP_SIZE_SUPPORTED_FOR_SCAN;
445 r->errstring = 0;
446 return 0;
447 }
448 res = zebra_scan(zh, r->stream, r->term,
449 r->attributeset,
450 &r->term_position,
451 &r->num_entries, &entries, &is_partial,
452 0 /* setname */);
453 if (res == ZEBRA_OK)
454 {
455 if (is_partial)
456 r->status = BEND_SCAN_PARTIAL;
457 else
458 r->status = BEND_SCAN_SUCCESS;
459 for (i = 0; i < r->num_entries; i++)
460 {
461 r->entries[i].term = entries[i].term;
462 r->entries[i].display_term = entries[i].display_term;
463 r->entries[i].occurrences = entries[i].occurrences;
464 }
465 }
466 else
467 {
468 r->status = BEND_SCAN_PARTIAL;
469 zebra_result(zh, &r->errcode, &r->errstring);
470 }
471 return 0;
472}
473
474void bend_close(void *handle)
475{
476 zebra_close((ZebraHandle) handle);
477 xmalloc_trav("bend_close");
478}
479
480int bend_sort(void *handle, bend_sort_rr *rr)
481{
482 ZebraHandle zh = (ZebraHandle) handle;
483
484 if (zebra_sort(zh, rr->stream,
485 rr->num_input_setnames, (const char **) rr->input_setnames,
486 rr->output_setname, rr->sort_sequence, &rr->sort_status)
487 != ZEBRA_OK)
488 zebra_result(zh, &rr->errcode, &rr->errstring);
489 return 0;
490}
491
492int bend_delete(void *handle, bend_delete_rr *rr)
493{
494 ZebraHandle zh = (ZebraHandle) handle;
495
496 rr->delete_status = zebra_deleteResultSet(zh, rr->function,
497 rr->num_setnames, rr->setnames,
498 rr->statuses);
499 return 0;
500}
501
502static void es_admin_request(bend_esrequest_rr *rr, ZebraHandle zh, Z_AdminEsRequest *r)
503{
504 ZEBRA_RES res = ZEBRA_OK;
505 if (r->toKeep->databaseName)
506 {
507 yaz_log(YLOG_LOG, "adm request database %s", r->toKeep->databaseName);
508 }
509 switch (r->toKeep->which)
510 {
511 case Z_ESAdminOriginPartToKeep_reIndex:
512 yaz_log(YLOG_LOG, "adm-reindex");
513 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
514 rr->errstring = "adm-reindex not implemented yet";
515 break;
516 case Z_ESAdminOriginPartToKeep_truncate:
517 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
518 rr->errstring = "adm-reindex not implemented yet";
519 yaz_log(YLOG_LOG, "adm-truncate");
520 break;
521 case Z_ESAdminOriginPartToKeep_drop:
522 yaz_log(YLOG_LOG, "adm-drop");
523 res = zebra_drop_database(zh, r->toKeep->databaseName);
524 break;
525 case Z_ESAdminOriginPartToKeep_create:
526 yaz_log(YLOG_LOG, "adm-create %s", r->toKeep->databaseName);
527 res = zebra_create_database(zh, r->toKeep->databaseName);
528 break;
529 case Z_ESAdminOriginPartToKeep_import:
530 yaz_log(YLOG_LOG, "adm-import");
531 res = zebra_admin_import_begin(zh, r->toKeep->databaseName,
532 r->toKeep->u.import->recordType);
533 break;
534 case Z_ESAdminOriginPartToKeep_refresh:
535 yaz_log(YLOG_LOG, "adm-refresh");
536 break;
537 case Z_ESAdminOriginPartToKeep_commit:
538 yaz_log(YLOG_LOG, "adm-commit");
539 if (r->toKeep->databaseName)
540 {
541 if (zebra_select_database(zh, r->toKeep->databaseName) !=
542 ZEBRA_OK)
543 yaz_log(YLOG_WARN, "zebra_select_database failed in "
544 "adm-commit");
545 }
546 zebra_commit(zh);
547 break;
548 case Z_ESAdminOriginPartToKeep_shutdown:
549 yaz_log(YLOG_LOG, "shutdown");
550 res = zebra_admin_shutdown(zh);
551 break;
552 case Z_ESAdminOriginPartToKeep_start:
553 yaz_log(YLOG_LOG, "start");
555 break;
556 default:
557 yaz_log(YLOG_LOG, "unknown admin");
558 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
559 rr->errstring = "adm-reindex not implemented yet";
560 }
561 if (res != ZEBRA_OK)
562 zebra_result(zh, &rr->errcode, &rr->errstring);
563}
564
565static void es_admin(bend_esrequest_rr *rr, ZebraHandle zh, Z_Admin *r)
566{
567 switch (r->which)
568 {
569 case Z_Admin_esRequest:
570 es_admin_request(rr, zh, r->u.esRequest);
571 return;
572 default:
573 break;
574 }
575 yaz_log(YLOG_WARN, "adm taskpackage (unhandled)");
576 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
577 rr->errstring = "adm-task package (unhandled)";
578}
579
580int bend_segment(void *handle, bend_segment_rr *rr)
581{
582 ZebraHandle zh = (ZebraHandle) handle;
583 Z_Segment *segment = rr->segment;
584
585 if (segment->num_segmentRecords)
586 zebra_admin_import_segment(zh, rr->segment);
587 else
589 return 0;
590}
591
592int bend_esrequest(void *handle, bend_esrequest_rr *rr)
593{
594 ZebraHandle zh = (ZebraHandle) handle;
595
596 yaz_log(YLOG_LOG, "function: " ODR_INT_PRINTF, *rr->esr->function);
597 if (rr->esr->packageName)
598 yaz_log(YLOG_LOG, "packagename: %s", rr->esr->packageName);
599 yaz_log(YLOG_LOG, "Waitaction: " ODR_INT_PRINTF, *rr->esr->waitAction);
600
601 if (!rr->esr->taskSpecificParameters)
602 {
603 yaz_log(YLOG_WARN, "No task specific parameters");
604 }
605 else if (rr->esr->taskSpecificParameters->which == Z_External_ESAdmin)
606 {
607 es_admin(rr, zh, rr->esr->taskSpecificParameters->u.adminService);
608 }
609 else if (rr->esr->taskSpecificParameters->which == Z_External_update)
610 {
611 Z_IUUpdate *up = rr->esr->taskSpecificParameters->u.update;
612 yaz_log(YLOG_LOG, "Received DB Update");
613 if (up->which == Z_IUUpdate_esRequest)
614 {
615 Z_IUUpdateEsRequest *esRequest = up->u.esRequest;
616 Z_IUOriginPartToKeep *toKeep = esRequest->toKeep;
617 Z_IUSuppliedRecords *notToKeep = esRequest->notToKeep;
618
619 yaz_log(YLOG_LOG, "action");
620 if (toKeep->action)
621 {
622 switch (*toKeep->action)
623 {
624 case Z_IUOriginPartToKeep_recordInsert:
625 yaz_log(YLOG_LOG, "recordInsert");
626 break;
627 case Z_IUOriginPartToKeep_recordReplace:
628 yaz_log(YLOG_LOG, "recordUpdate");
629 break;
630 case Z_IUOriginPartToKeep_recordDelete:
631 yaz_log(YLOG_LOG, "recordDelete");
632 break;
633 case Z_IUOriginPartToKeep_elementUpdate:
634 yaz_log(YLOG_LOG, "elementUpdate");
635 break;
636 case Z_IUOriginPartToKeep_specialUpdate:
637 yaz_log(YLOG_LOG, "specialUpdate");
638 break;
639 case Z_ESAdminOriginPartToKeep_shutdown:
640 yaz_log(YLOG_LOG, "shutDown");
641 break;
642 case Z_ESAdminOriginPartToKeep_start:
643 yaz_log(YLOG_LOG, "start");
644 break;
645 default:
646 yaz_log(YLOG_LOG, " unknown (" ODR_INT_PRINTF ")",
647 *toKeep->action);
648 }
649 }
650 if (toKeep->databaseName)
651 {
652 yaz_log(YLOG_LOG, "database: %s", toKeep->databaseName);
653
654 if (zebra_select_database(zh, toKeep->databaseName))
655 return 0;
656 }
657 else
658 {
659 yaz_log(YLOG_WARN, "no database supplied for ES Update");
660 rr->errcode =
661 YAZ_BIB1_ES_MISSING_MANDATORY_PARAMETER_FOR_SPECIFIED_FUNCTION_;
662 rr->errstring = "database";
663 return 0;
664 }
665 if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
666 {
667 zebra_result(zh, &rr->errcode, &rr->errstring);
668 }
669 else
670 {
671 int i;
672 for (i = 0; notToKeep && i < notToKeep->num; i++)
673 {
674 Z_External *rec = notToKeep->elements[i]->record;
675 Odr_oct *opaque_recid = 0;
676 zint *sysno = 0;
677 zint sysno_tmp;
678
679 if (notToKeep->elements[i]->u.opaque)
680 {
681 switch(notToKeep->elements[i]->which)
682 {
683 case Z_IUSuppliedRecords_elem_opaque:
684 opaque_recid = notToKeep->elements[i]->u.opaque;
685 break; /* OK, recid already set */
686 case Z_IUSuppliedRecords_elem_number:
687 sysno_tmp = *notToKeep->elements[i]->u.number;
688 sysno = &sysno_tmp;
689 break;
690 }
691 }
692 if (rec->direct_reference)
693 {
694 char oid_name_str[OID_STR_MAX];
695 const char *oid_name =
696 yaz_oid_to_string_buf(
697 rec->direct_reference,
698 0, oid_name_str);
699 if (oid_name)
700 yaz_log(YLOG_LOG, "record %d type %s", i,
701 oid_name);
702 }
703 switch (rec->which)
704 {
705 case Z_External_sutrs:
706 if (rec->u.octet_aligned->len > 170)
707 yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
708 rec->u.sutrs->len,
709 rec->u.sutrs->buf);
710 else
711 yaz_log(YLOG_LOG, "%d bytes:\n%s",
712 rec->u.sutrs->len,
713 rec->u.sutrs->buf);
714 break;
715 case Z_External_octet:
716 if (rec->u.octet_aligned->len > 170)
717 yaz_log(YLOG_LOG, "%d bytes:\n%.168s ...",
718 rec->u.octet_aligned->len,
719 rec->u.octet_aligned->buf);
720 else
721 yaz_log(YLOG_LOG, "%d bytes\n%s",
722 rec->u.octet_aligned->len,
723 rec->u.octet_aligned->buf);
724 }
725 if (rec->which == Z_External_octet)
726 {
728 char recid_str[256];
729 const char *match_criteria = 0;
730 ZEBRA_RES res;
731
732 if (*toKeep->action ==
733 Z_IUOriginPartToKeep_recordInsert)
734 action = action_insert;
735 else if (*toKeep->action ==
736 Z_IUOriginPartToKeep_recordReplace)
737 action = action_replace;
738 else if (*toKeep->action ==
739 Z_IUOriginPartToKeep_recordDelete)
740 action = action_delete;
741 else if (*toKeep->action ==
742 Z_IUOriginPartToKeep_specialUpdate)
743 action = action_update;
744 else
745 {
746 rr->errcode =
747 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
748 rr->errstring = "unsupported ES Update action";
749 break;
750 }
751
752 if (opaque_recid)
753 {
754 size_t l = opaque_recid->len;
755 if (l >= sizeof(recid_str))
756 {
757 rr->errcode = YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
758 rr->errstring = "opaque record ID too large";
759 break;
760 }
761 memcpy(recid_str, opaque_recid->buf, l);
762 recid_str[l] = '\0';
763 match_criteria = recid_str;
764 }
766 zh, action,
767 0, /* recordType */
768 sysno, match_criteria, 0, /* fname */
769 (const char *) rec->u.octet_aligned->buf,
770 rec->u.octet_aligned->len);
771 if (res == ZEBRA_FAIL)
772 {
773 rr->errcode =
774 YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED;
775 rr->errstring = "update_record failed";
776 }
777 }
778 }
779 if (zebra_end_trans(zh) != ZEBRA_OK)
780 {
781 yaz_log(YLOG_WARN, "zebra_end_trans failed for"
782 " extended service operation");
783 }
784 }
785 }
786 }
787 else
788 {
789 yaz_log(YLOG_WARN, "Unknown Extended Service(%d)",
790 rr->esr->taskSpecificParameters->which);
791 rr->errcode = YAZ_BIB1_ES_EXTENDED_SERVICE_TYPE_UNSUPP;
792
793 }
794 return 0;
795}
796
797static void bend_start(struct statserv_options_block *sob)
798{
799 Res default_res = res_open(0, 0);
800
801 if (sob->handle)
802 zebra_stop((ZebraService) sob->handle);
803 res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
804 res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
805 sob->handle = zebra_start_res(sob->configname, default_res, 0);
806 res_close(default_res);
807 if (!sob->handle)
808 {
809 yaz_log(YLOG_FATAL, "Failed to read config `%s'", sob->configname);
810 exit(1);
811 }
812#ifdef WIN32
813
814#else
815 if (!sob->inetd && !sob->background)
816 {
817 char pidfname[4096];
818 struct flock area;
819 int fd;
820
821 zebra_pidfname(sob->handle, pidfname);
822
823 fd = open(pidfname, O_EXCL|O_WRONLY|O_CREAT, 0666);
824 if (fd == -1)
825 {
826 if (errno != EEXIST)
827 {
828 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
829 exit(1);
830 }
831 fd = open(pidfname, O_RDWR, 0666);
832 if (fd == -1)
833 {
834 yaz_log(YLOG_FATAL|YLOG_ERRNO, "lock file %s", pidfname);
835 exit(1);
836 }
837 }
838 area.l_type = F_WRLCK;
839 area.l_whence = SEEK_SET;
840 area.l_len = area.l_start = 0L;
841 if (fcntl(fd, F_SETLK, &area) == -1)
842 {
843 yaz_log(YLOG_ERRNO|YLOG_FATAL, "Zebra server already running");
844 exit(1);
845 }
846 else
847 {
848 char pidstr[30];
849
850 yaz_snprintf(pidstr, sizeof(pidstr), "%ld", (long) getpid());
851 if (write(fd, pidstr, strlen(pidstr)) != strlen(pidstr))
852 {
853 yaz_log(YLOG_ERRNO|YLOG_FATAL, "write fail %s", pidfname);
854 exit(1);
855 }
856 }
857 }
858#endif
859}
860
861static void bend_stop(struct statserv_options_block *sob)
862{
863#ifdef WIN32
864
865#else
866 if (!sob->inetd && !sob->background && sob->handle)
867 {
868 char pidfname[4096];
869 zebra_pidfname(sob->handle, pidfname);
870 unlink(pidfname);
871 }
872#endif
873 if (sob->handle)
874 {
875 ZebraService service = sob->handle;
876 zebra_stop(service);
877 }
878}
879
880int main(int argc, char **argv)
881{
882 int exit_code;
883 struct statserv_options_block *sob;
884
885 sob = statserv_getcontrol();
886 strcpy(sob->configname, "zebra.cfg");
887 sob->bend_start = bend_start;
888 sob->bend_stop = bend_stop;
889#ifdef WIN32
890 strcpy(sob->service_name, "zebrasrv");
891 strcpy(sob->service_display_name, "Zebra Server");
892#endif
893 statserv_setcontrol(sob);
894 exit_code = statserv_main(argc, argv, bend_init, bend_close);
895 zebra_stop((ZebraService) sob->handle);
896 return exit_code;
897}
898/*
899 * Local variables:
900 * c-basic-offset: 4
901 * c-file-style: "Stroustrup"
902 * indent-tabs-mode: nil
903 * End:
904 * vim: shiftwidth=4 tabstop=8 expandtab
905 */
906
Zebra API.
ZEBRA_RES zebra_end_trans(ZebraHandle zh) ZEBRA_GCC_ATTR((warn_unused_result))
Definition zebraapi.c:1910
ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
Definition zebraapi.c:570
ZEBRA_RES zebra_close(ZebraHandle zh)
Destroys Zebra session handle.
Definition zebraapi.c:657
struct zebra_session * ZebraHandle
a Zebra Handle - (session)
Definition api.h:71
ZEBRA_RES zebra_admin_start(ZebraHandle zh)
Definition zebraapi.c:581
ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra service with resources.
Definition zebraapi.c:197
ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
Definition zebraapi.c:1421
ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
Deletes a database (drop)
Definition zebraapi.c:1518
ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
Definition zebraapi.c:2545
ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream, const char *setname, Z_RecordComposition *comp, const Odr_oid *input_format, int num_recs, ZebraRetrievalRecord *recs)
Retrieve records from result set (after search)
Definition zebraapi.c:1118
const char * zebra_get_encoding(ZebraHandle zh)
Returns character set encoding for session.
Definition zebraapi.c:107
ZEBRA_RES zebra_update_record(ZebraHandle zh, enum zebra_recctrl_action_t action, const char *recordType, zint *sysno, const char *match, const char *fname, const char *buf, int buf_size)
Updates record.
Definition zebraapi.c:2611
ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases, const char **basenames) ZEBRA_GCC_ATTR((warn_unused_result))
Definition zebraapi.c:948
ZEBRA_RES zebra_commit(ZebraHandle zh)
Definition zebraapi.c:2146
ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
Creates a database.
Definition zebraapi.c:1562
ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
Definition zebraapi.c:2522
ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw) ZEBRA_GCC_ATTR((warn_unused_result))
Definition zebraapi.c:1710
ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream, int num_input_setnames, const char **input_setnames, const char *output_setname, Z_SortKeySpecList *sort_sequence, int *sort_status) ZEBRA_GCC_ATTR((warn_unused_result))
Definition zebraapi.c:1291
void zebra_result(ZebraHandle zh, int *code, char **addinfo)
Returns error code and additional info for last error.
Definition zebraapi.c:2500
int zebra_deleteResultSet(ZebraHandle zh, int function, int num_setnames, char **setnames, int *statuses)
Deletes one or more resultsets.
Definition zebraapi.c:1314
ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename) ZEBRA_GCC_ATTR((warn_unused_result))
Definition zebraapi.c:939
ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
authenticate user. Returns 0 if OK, != 0 on failure
Definition zebraapi.c:1372
ZEBRA_RES zebra_set_break_handler(ZebraHandle zh, int(*f)(void *client_data), void *client_data)
Definition zebraapi.c:1070
ZEBRA_RES zebra_stop(ZebraService zs)
stops a Zebra service.
Definition zebraapi.c:627
ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
Definition zebraapi.c:1414
ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database, const char *record_type)
Definition zebraapi.c:1404
ZEBRA_RES zebra_result_set_term_info(ZebraHandle zh, const char *setname, int no, zint *count, int *approx, char *termbuf, size_t *termlen, const char **term_ref_id)
returns information about a term assocated with a result set
Definition zsets.c:1275
ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt, const Odr_oid *attributeset, int *position, int *num_entries, ZebraScanEntry **entries, int *is_partial, const char *setname)
performs Scan (Z39.50 style)
Definition zebraapi.c:1258
ZEBRA_RES zebra_result_set_term_no(ZebraHandle zh, const char *setname, int *num_terms)
returns number of term info terms assocaited with result set
Definition zsets.c:1262
ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query, const char *setname, zint *hits, int *estimated_hit_count, int *partial_resultset)
Search using RPN Query structure (from ASN.1)
Definition zebraapi.c:1079
ZebraHandle zebra_open(ZebraService zs, Res res) ZEBRA_GCC_ATTR((warn_unused_result))
Creates a Zebra session handle within service.
Definition zebraapi.c:113
void zebra_pidfname(ZebraService zs, char *path)
Definition zebraapi.c:314
zebra_recctrl_action_t
Definition recctrl.h:87
@ action_delete
Definition recctrl.h:93
@ action_insert
Definition recctrl.h:89
@ action_update
Definition recctrl.h:95
@ action_replace
Definition recctrl.h:91
void res_set(Res r, const char *name, const char *value)
Definition res.c:338
void res_close(Res r)
Definition res.c:261
Res res_open(Res res_def, Res over_res)
Definition res.c:234
const Odr_oid * format
Definition api.h:55
char * errString
Definition api.h:51
zint occurrences
Definition api.h:63
char * term
Definition api.h:64
char * display_term
Definition api.h:65
char ** basenames
Definition index.h:178
int fd
long zint
Zebra integer.
Definition util.h:66
#define ZEBRA_FAIL
Definition util.h:81
#define ZEBRA_OK
Definition util.h:82
short ZEBRA_RES
Common return type for Zebra API.
Definition util.h:80
void zebra_get_version(char *version_str, char *sha1_str)
Returns Zebra version and SHA1 ID as generated by Git.
Definition version.c:33
static void bend_start(struct statserv_options_block *sob)
Definition zebrasrv.c:797
static int bend_sort(void *handle, bend_sort_rr *rr)
Definition zebrasrv.c:480
void bend_close(void *handle)
Definition zebrasrv.c:474
bend_initresult * bend_init(bend_initrequest *q)
Definition zebrasrv.c:59
int main(int argc, char **argv)
Definition zebrasrv.c:880
static int break_handler(void *client_data)
Definition zebrasrv.c:269
static int bend_scan(void *handle, bend_scan_rr *r)
Definition zebrasrv.c:429
static Z_RPNQuery * query_add_sortkeys(ODR o, Z_RPNQuery *query, const char *sortKeys)
Definition zebrasrv.c:277
static void es_admin_request(bend_esrequest_rr *rr, ZebraHandle zh, Z_AdminEsRequest *r)
Definition zebrasrv.c:502
static void es_admin(bend_esrequest_rr *rr, ZebraHandle zh, Z_Admin *r)
Definition zebrasrv.c:565
static int bend_fetch(void *handle, bend_fetch_rr *r)
Definition zebrasrv.c:395
static int bend_segment(void *handle, bend_segment_rr *rr)
Definition zebrasrv.c:580
static void search_terms(ZebraHandle zh, bend_search_rr *r)
Definition zebrasrv.c:183
static void bend_stop(struct statserv_options_block *sob)
Definition zebrasrv.c:861
static int bend_esrequest(void *handle, bend_esrequest_rr *rr)
Definition zebrasrv.c:592
static int bend_search(void *handle, bend_search_rr *r)
Definition zebrasrv.c:351
static int bend_delete(void *handle, bend_delete_rr *rr)
Definition zebrasrv.c:492