Proper use of setting:value in zebra conf doc
[idzebra-moved-to-github.git] / index / zebraapi.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, 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 <limits.h>
26 #ifdef WIN32
27 #include <io.h>
28 #include <process.h>
29 #include <direct.h>
30 #endif
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif
34
35 #include <yaz/diagbib1.h>
36 #include <yaz/pquery.h>
37 #include <yaz/sortspec.h>
38 #include "index.h"
39 #include "rank.h"
40 #include "orddict.h"
41 #include <charmap.h>
42 #include <idzebra/api.h>
43 #include <yaz/oid_db.h>
44
45 #define DEFAULT_APPROX_LIMIT 2000000000
46
47 /* simple asserts to validate the most essential input args */
48 #define ASSERTZH assert(zh && zh->service)
49 #define ASSERTZHRES assert(zh && zh->service && zh->res)
50 #define ASSERTZS assert(zs)
51
52 static int log_level = 0;
53 static int log_level_initialized = 0;
54
55 static void zebra_open_res(ZebraHandle zh);
56 static void zebra_close_res(ZebraHandle zh);
57
58 static ZEBRA_RES zebra_check_handle(ZebraHandle zh)
59 {
60     if (zh)
61         return ZEBRA_OK;
62     return ZEBRA_FAIL;
63 }
64
65 #define ZEBRA_CHECK_HANDLE(zh) if (zebra_check_handle(zh) != ZEBRA_OK) return ZEBRA_FAIL
66
67 static int zebra_chdir(ZebraService zs)
68 {
69     const char *dir ;
70     int r;
71     ASSERTZS;
72     yaz_log(log_level, "zebra_chdir");
73     dir = res_get(zs->global_res, "chdir");
74     if (!dir)
75         return 0;
76     yaz_log(YLOG_DEBUG, "chdir %s", dir);
77 #ifdef WIN32
78     r = _chdir(dir);
79 #else
80     r = chdir(dir);
81 #endif
82     if (r)
83         yaz_log(YLOG_FATAL|YLOG_ERRNO, "chdir %s", dir);
84     return r;
85 }
86
87 static ZEBRA_RES zebra_flush_reg(ZebraHandle zh)
88 {
89     ZEBRA_CHECK_HANDLE(zh);
90     yaz_log(log_level, "zebra_flush_reg");
91     zebraExplain_flush(zh->reg->zei, zh);
92
93     key_block_flush(zh->reg->key_block, 1);
94
95     zebra_index_merge(zh);
96     return ZEBRA_OK;
97 }
98
99 static struct zebra_register *zebra_register_open(ZebraService zs, 
100                                                   const char *name,
101                                                   int rw, int useshadow,
102                                                   Res res,
103                                                   const char *reg_path);
104 static void zebra_register_close(ZebraService zs, struct zebra_register *reg);
105
106 const char *zebra_get_encoding(ZebraHandle zh)
107 {
108     assert(zh && zh->session_res);
109     return res_get_def(zh->session_res, "encoding", "ISO-8859-1");
110 }
111
112 ZebraHandle zebra_open(ZebraService zs, Res res)
113 {
114     ZebraHandle zh;
115     const char *default_encoding;
116     if (!log_level_initialized)
117     {
118         log_level = yaz_log_module_level("zebraapi");
119         log_level_initialized = 1;
120     }
121
122     yaz_log(log_level, "zebra_open");
123
124     if (!zs)
125         return 0;
126
127     zh = (ZebraHandle) xmalloc(sizeof(*zh));
128     yaz_log(YLOG_DEBUG, "zebra_open zs=%p returns %p", zs, zh);
129
130     zh->service = zs;
131     zh->reg = 0;          /* no register attached yet */
132     zh->sets = 0;
133     zh->destroyed = 0;
134     zh->errCode = 0;
135     zh->errString = 0;
136     zh->res = 0; 
137     zh->session_res = res_open(zs->global_res, res);
138     zh->user_perm = 0;
139     zh->dbaccesslist = 0;
140
141     zh->reg_name = xstrdup("");
142     zh->path_reg = 0;
143     zh->num_basenames = 0;
144     zh->basenames = 0;
145
146     zh->approx_limit = DEFAULT_APPROX_LIMIT;
147     zh->trans_no = 0;
148     zh->trans_w_no = 0;
149
150     zh->lock_normal = 0;
151     zh->lock_shadow = 0;
152
153     zh->shadow_enable = 1;
154     zh->m_staticrank = 0;
155     zh->m_segment_indexing = 0;
156
157     zh->break_handler_func = 0;
158     zh->break_handler_data = 0;
159
160     default_encoding = zebra_get_encoding(zh);
161
162     zh->iconv_to_utf8 =
163         yaz_iconv_open("UTF-8", default_encoding);
164     if (zh->iconv_to_utf8 == 0)
165         yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported",
166                 default_encoding);
167     zh->iconv_from_utf8 =
168         yaz_iconv_open(default_encoding, "UTF-8");
169     if (zh->iconv_to_utf8 == 0)
170         yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported",
171                 default_encoding);
172
173     zh->record_encoding = 0;
174
175     zebra_mutex_cond_lock(&zs->session_lock);
176
177     zh->next = zs->sessions;
178     zs->sessions = zh;
179
180     zebra_mutex_cond_unlock(&zs->session_lock);
181
182     zh->store_data_buf = 0;
183
184     zh->m_limit = zebra_limit_create(1, 0);
185
186     zh->nmem_error = nmem_create();
187
188     return zh;
189 }
190
191 ZebraService zebra_start(const char *configName)
192 {
193     return zebra_start_res(configName, 0, 0);
194 }
195
196 ZebraService zebra_start_res(const char *configName, Res def_res, Res over_res)
197 {
198     Res res;
199     char version_str[16];
200     char system_str[80];
201
202     zebra_flock_init();
203
204     if (!log_level_initialized)
205     {
206         log_level = yaz_log_module_level("zebraapi");
207         log_level_initialized = 1;
208     }
209
210     *system_str = '\0';
211     *version_str = '\0';
212     zebra_get_version(version_str, system_str);
213
214     yaz_log(YLOG_LOG, "zebra_start %s %s", version_str, system_str);
215     if (configName)
216         yaz_log(YLOG_LOG, "config %s", configName);
217
218     if ((res = res_open(def_res, over_res)))
219     {
220         const char *passwd_plain = 0;
221         const char *passwd_encrypt = 0;
222         const char *dbaccess = 0;
223         ZebraService zh = 0;
224
225         if (configName)
226         {
227             ZEBRA_RES ret = res_read_file(res, configName);
228             if (ret != ZEBRA_OK)
229             {
230                 res_close(res);
231                 return 0;
232             }
233             if (zebra_check_res(res))
234             {
235                 yaz_log(YLOG_FATAL, "Configuration error(s) for %s",
236                         configName);
237                 return 0;
238             }
239         }
240         else
241         {
242             zebra_check_res(res);
243         }
244
245         zh = xmalloc(sizeof(*zh));
246         zh->global_res = res;
247         zh->sessions = 0;
248         
249         if (zebra_chdir(zh))
250         {
251             xfree(zh);
252             return 0;
253         }
254         
255         zebra_mutex_cond_init(&zh->session_lock);
256         passwd_plain = res_get(zh->global_res, "passwd");
257         passwd_encrypt = res_get(zh->global_res, "passwd.c");
258         dbaccess = res_get(zh->global_res, "dbaccess");
259
260         if (!passwd_plain && !passwd_encrypt)
261             zh->passwd_db = NULL;
262         else 
263         {
264             zh->passwd_db = passwd_db_open();
265             if (!zh->passwd_db)
266                 yaz_log(YLOG_WARN|YLOG_ERRNO, "passwd_db_open failed");
267             else
268             {
269                 if (passwd_plain)
270                     passwd_db_file_plain(zh->passwd_db, passwd_plain);
271                 if (passwd_encrypt)
272                     passwd_db_file_crypt(zh->passwd_db, passwd_encrypt);
273             }
274         }
275
276         if (!dbaccess)
277             zh->dbaccess = NULL;
278         else {
279             zh->dbaccess = res_open(NULL, NULL);
280             if (res_read_file(zh->dbaccess, dbaccess) != ZEBRA_OK) {
281                 yaz_log(YLOG_FATAL, "Failed to read %s", dbaccess);
282                 return NULL;
283             }
284         }
285
286         zh->timing = yaz_timing_create();
287         zh->path_root = res_get(zh->global_res, "root");
288         zh->nmem = nmem_create();
289         zh->record_classes = recTypeClass_create(zh->global_res, zh->nmem);
290
291         if (1)
292         {
293             const char *module_path = res_get(res, "modulePath");
294             if (module_path)
295                 recTypeClass_load_modules(&zh->record_classes, zh->nmem,
296                                           module_path);
297         }
298         return zh;
299     }
300     return 0;
301 }
302
303 void zebra_filter_info(ZebraService zs, void *cd,
304                        void(*cb)(void *cd, const char *name))
305 {
306     ASSERTZS;
307     assert(cb);
308     recTypeClass_info(zs->record_classes, cd, cb);
309 }
310
311 void zebra_pidfname(ZebraService zs, char *path)
312 {
313     ASSERTZS;
314     zebra_lock_prefix(zs->global_res, path);
315     strcat(path, "zebrasrv.pid");
316 }
317
318 Dict dict_open_res(BFiles bfs, const char *name, int cache, int rw,
319                    int compact_flag, Res res)
320 {
321     int page_size = 4096;
322     char resource_str[200];
323     sprintf(resource_str, "dict.%.100s.pagesize", name);
324     assert(bfs);
325     assert(name);
326
327     if (res_get_int(res, resource_str, &page_size) == ZEBRA_OK)
328         yaz_log(YLOG_LOG, "Using custom dictionary page size %d for %s",
329                 page_size, name);
330     return dict_open(bfs, name, cache, rw, compact_flag, page_size);
331 }
332
333 static
334 struct zebra_register *zebra_register_open(ZebraService zs, const char *name,
335                                            int rw, int useshadow, Res res,
336                                            const char *reg_path)
337 {
338     struct zebra_register *reg;
339     int record_compression = REC_COMPRESS_NONE;
340     const char *compression_str = 0;
341     const char *profilePath;
342     int sort_type = ZEBRA_SORT_TYPE_FLAT;
343     ZEBRA_RES ret = ZEBRA_OK;
344
345     ASSERTZS;
346     
347     reg = xmalloc(sizeof(*reg));
348
349     assert(name);
350     reg->name = xstrdup(name);
351
352     reg->seqno = 0;
353     reg->last_val = 0;
354
355     assert(res);
356
357     yaz_log(YLOG_DEBUG, "zebra_register_open rw=%d useshadow=%d p=%p n=%s rp=%s",
358             rw, useshadow, reg, name, reg_path ? reg_path : "(none)");
359     
360     reg->dh = data1_create();
361     if (!reg->dh)
362     {
363         xfree(reg->name);
364         xfree(reg);
365         return 0;
366     }
367     reg->bfs = bfs_create(res_get(res, "register"), reg_path);
368     if (!reg->bfs)
369     {
370         data1_destroy(reg->dh);
371         xfree(reg->name);
372         xfree(reg);
373         return 0;
374     }
375     if (useshadow)
376     {
377         if (bf_cache(reg->bfs, res_get(res, "shadow")) == ZEBRA_FAIL)
378         {
379             bfs_destroy(reg->bfs);
380             data1_destroy(reg->dh);
381             xfree(reg->name);
382             xfree(reg);
383             return 0;
384         }
385     }
386
387     profilePath = res_get_def(res, "profilePath", 0);
388
389     data1_set_tabpath(reg->dh, profilePath);
390     data1_set_tabroot(reg->dh, reg_path);
391     reg->recTypes = recTypes_init(zs->record_classes, reg->dh);
392
393     reg->zebra_maps =
394         zebra_maps_open(res, reg_path, profilePath);
395     if (!reg->zebra_maps)
396     {
397         recTypes_destroy(reg->recTypes);
398         bfs_destroy(reg->bfs);
399         data1_destroy(reg->dh);
400         xfree(reg->name);
401         xfree(reg);
402         return 0;
403     }
404     reg->rank_classes = NULL;
405
406     reg->key_block = 0;
407     reg->keys = zebra_rec_keys_open();
408
409     reg->sortKeys = zebra_rec_keys_open();
410
411     reg->records = 0;
412     reg->dict = 0;
413     reg->sort_index = 0;
414     reg->isams = 0;
415     reg->matchDict = 0;
416     reg->isamc = 0;
417     reg->isamb = 0;
418     reg->zei = 0;
419     
420     /* installing rank classes */
421     zebraRankInstall(reg, rank_1_class);
422     zebraRankInstall(reg, rank_2_class);
423     zebraRankInstall(reg, rank_similarity_class);
424     zebraRankInstall(reg, rank_static_class);
425
426     compression_str = res_get_def(res, "recordCompression", "none");
427     if (!strcmp(compression_str, "none"))
428         record_compression = REC_COMPRESS_NONE;
429     else if (!strcmp(compression_str, "bzip2"))
430         record_compression = REC_COMPRESS_BZIP2;
431     else if (!strcmp(compression_str, "zlib"))
432         record_compression = REC_COMPRESS_ZLIB;
433     else
434     {
435         yaz_log(YLOG_FATAL, "invalid recordCompression: %s", compression_str);
436         ret = ZEBRA_FAIL;
437     }
438
439     if (!rec_check_compression_method(record_compression))
440     {
441         yaz_log(YLOG_FATAL, "unsupported recordCompression: %s",
442                 compression_str);
443         ret = ZEBRA_FAIL;
444     }
445
446     {
447         const char *index_fname = res_get_def(res, "index", "default.idx");
448         if (index_fname && *index_fname && strcmp(index_fname, "none"))
449         {
450             if (zebra_maps_read_file(reg->zebra_maps, index_fname) != ZEBRA_OK)
451                 ret = ZEBRA_FAIL;
452         }
453         else
454         {
455             zebra_maps_define_default_sort(reg->zebra_maps);
456         }
457     }
458
459     if (!(reg->records = rec_open(reg->bfs, rw, record_compression)))
460     {
461         yaz_log(YLOG_WARN, "rec_open failed");
462         ret = ZEBRA_FAIL;
463     }
464     if (rw)
465     {
466         reg->matchDict = dict_open_res(reg->bfs, GMATCH_DICT, 20, 1, 0, res);
467     }
468     if (!(reg->dict = dict_open_res(reg->bfs, FNAME_DICT, 40, rw, 0, res)))
469     {
470         yaz_log(YLOG_WARN, "dict_open failed");
471         ret = ZEBRA_FAIL;
472     }
473
474     
475     if (res_get_match(res, "sortindex", "f", "f"))
476         sort_type = ZEBRA_SORT_TYPE_FLAT;
477     else if (res_get_match(res, "sortindex", "i", "f"))
478         sort_type = ZEBRA_SORT_TYPE_ISAMB;
479     else if (res_get_match(res, "sortindex", "m", "f"))
480         sort_type = ZEBRA_SORT_TYPE_MULTI;
481     else
482     {
483         yaz_log(YLOG_WARN, "bad_value for 'sortindex'");
484         ret = ZEBRA_FAIL;
485     }
486
487
488     if (!(reg->sort_index = zebra_sort_open(reg->bfs, rw, sort_type)))
489     {
490         yaz_log(YLOG_WARN, "zebra_sort_open failed");
491         ret = ZEBRA_FAIL;
492     }
493     if (res_get_match(res, "isam", "s", ISAM_DEFAULT))
494     {
495         struct ISAMS_M_s isams_m;
496         if (!(reg->isams = isams_open(reg->bfs, FNAME_ISAMS, rw,
497                                       key_isams_m(res, &isams_m))))
498         {
499             yaz_log(YLOG_WARN, "isams_open failed");
500             ret = ZEBRA_FAIL;
501         }
502     }
503     if (res_get_match(res, "isam", "c", ISAM_DEFAULT))
504     {
505         struct ISAMC_M_s isamc_m;
506         if (!(reg->isamc = isamc_open(reg->bfs, FNAME_ISAMC,
507                                       rw, key_isamc_m(res, &isamc_m))))
508         {
509             yaz_log(YLOG_WARN, "isamc_open failed");
510             ret = ZEBRA_FAIL;
511         }
512     }
513     if (res_get_match(res, "isam", "b", ISAM_DEFAULT))
514     {
515         struct ISAMC_M_s isamc_m;
516         
517         if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
518                                       rw, key_isamc_m(res, &isamc_m), 0)))
519         {
520             yaz_log(YLOG_WARN, "isamb_open failed");
521             ret = ZEBRA_FAIL;
522         }
523     }
524     if (res_get_match(res, "isam", "bc", ISAM_DEFAULT))
525     {
526         struct ISAMC_M_s isamc_m;
527         
528         if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
529                                       rw, key_isamc_m(res, &isamc_m), 1)))
530         {
531             yaz_log(YLOG_WARN, "isamb_open failed");
532             ret = ZEBRA_FAIL;
533         }
534     }
535     if (res_get_match(res, "isam", "null", ISAM_DEFAULT))
536     {
537         struct ISAMC_M_s isamc_m;
538         
539         if (!(reg->isamb = isamb_open(reg->bfs, "isamb",
540                                       rw, key_isamc_m(res, &isamc_m), -1)))
541         {
542             yaz_log(YLOG_WARN, "isamb_open failed");
543             ret = ZEBRA_FAIL;
544         }
545     }
546     if (ret == ZEBRA_OK)
547     {
548         reg->zei = zebraExplain_open(reg->records, reg->dh,
549                                      res, rw, reg,
550                                      zebra_extract_explain);
551         if (!reg->zei)
552         {
553             yaz_log(YLOG_WARN, "Cannot obtain EXPLAIN information");
554             ret = ZEBRA_FAIL;
555         }
556     }
557     
558     if (ret != ZEBRA_OK)
559     {
560         zebra_register_close(zs, reg);
561         return 0;
562     }
563     yaz_log(YLOG_DEBUG, "zebra_register_open ok p=%p", reg);
564     return reg;
565 }
566
567 ZEBRA_RES zebra_admin_shutdown(ZebraHandle zh)
568 {
569     ZEBRA_CHECK_HANDLE(zh);
570     yaz_log(log_level, "zebra_admin_shutdown");
571
572     zebra_mutex_cond_lock(&zh->service->session_lock);
573     zh->service->stop_flag = 1;
574     zebra_mutex_cond_unlock(&zh->service->session_lock);
575     return ZEBRA_OK;
576 }
577
578 ZEBRA_RES zebra_admin_start(ZebraHandle zh)
579 {
580     ZebraService zs;
581     ZEBRA_CHECK_HANDLE(zh);
582     yaz_log(log_level, "zebra_admin_start");
583     zs = zh->service;
584     zebra_mutex_cond_lock(&zs->session_lock);
585     zebra_mutex_cond_unlock(&zs->session_lock);
586     return ZEBRA_OK;
587 }
588
589 static void zebra_register_close(ZebraService zs, struct zebra_register *reg)
590 {
591     ASSERTZS;
592     assert(reg);
593     yaz_log(YLOG_DEBUG, "zebra_register_close p=%p", reg);
594     reg->stop_flag = 0;
595     zebra_chdir(zs);
596     
597     zebraExplain_close(reg->zei);
598     dict_close(reg->dict);
599     if (reg->matchDict)
600         dict_close(reg->matchDict);
601     zebra_sort_close(reg->sort_index);
602     if (reg->isams)
603         isams_close(reg->isams);
604     if (reg->isamc)
605         isamc_close(reg->isamc);
606     if (reg->isamb)
607         isamb_close(reg->isamb);
608     rec_close(&reg->records);
609
610     recTypes_destroy(reg->recTypes);
611     zebra_maps_close(reg->zebra_maps);
612     zebraRankDestroy(reg);
613     bfs_destroy(reg->bfs);
614     data1_destroy(reg->dh);
615
616     zebra_rec_keys_close(reg->keys);
617     zebra_rec_keys_close(reg->sortKeys);
618
619     key_block_destroy(&reg->key_block);
620     xfree(reg->name);
621     xfree(reg);
622 }
623
624 ZEBRA_RES zebra_stop(ZebraService zs)
625 {
626     if (!zs)
627         return ZEBRA_OK;
628     while (zs->sessions)
629     {
630         zebra_close(zs->sessions);
631     }
632         
633     zebra_mutex_cond_destroy(&zs->session_lock);
634
635     if (zs->passwd_db)
636         passwd_db_close(zs->passwd_db);
637
638     recTypeClass_destroy(zs->record_classes);
639     nmem_destroy(zs->nmem);
640     res_close(zs->global_res);
641
642     yaz_timing_stop(zs->timing);
643     yaz_log(YLOG_LOG, "zebra_stop: %4.2f %4.2f %4.2f",
644             yaz_timing_get_real(zs->timing),
645             yaz_timing_get_user(zs->timing),
646             yaz_timing_get_sys(zs->timing));
647     
648
649     yaz_timing_destroy(&zs->timing);
650     xfree(zs);
651     return ZEBRA_OK;
652 }
653
654 ZEBRA_RES zebra_close(ZebraHandle zh)
655 {
656     ZebraService zs;
657     struct zebra_session **sp;
658     int i;
659
660     yaz_log(log_level, "zebra_close");
661     ZEBRA_CHECK_HANDLE(zh);
662
663     zh->errCode = 0;
664     
665     zs = zh->service;
666     yaz_log(YLOG_DEBUG, "zebra_close zh=%p", zh);
667     resultSetDestroy(zh, -1, 0, 0);
668
669     if (zh->reg)
670         zebra_register_close(zh->service, zh->reg);
671     zebra_close_res(zh);
672     res_close(zh->session_res);
673
674     xfree(zh->record_encoding);
675
676     xfree(zh->dbaccesslist);
677
678     for (i = 0; i < zh->num_basenames; i++)
679         xfree(zh->basenames[i]);
680     xfree(zh->basenames);
681
682     if (zh->iconv_to_utf8 != 0)
683         yaz_iconv_close(zh->iconv_to_utf8);
684     if (zh->iconv_from_utf8 != 0)
685         yaz_iconv_close(zh->iconv_from_utf8);
686
687     zebra_mutex_cond_lock(&zs->session_lock);
688     zebra_lock_destroy(zh->lock_normal);
689     zebra_lock_destroy(zh->lock_shadow);
690     sp = &zs->sessions;
691     while (1)
692     {
693         assert(*sp);
694         if (*sp == zh)
695         {
696             *sp = (*sp)->next;
697             break;
698         }
699         sp = &(*sp)->next;
700     }
701     zebra_mutex_cond_unlock(&zs->session_lock);
702     xfree(zh->reg_name);
703     xfree(zh->user_perm);
704     zh->service = 0; /* more likely to trigger an assert */
705
706     zebra_limit_destroy(zh->m_limit);
707
708     nmem_destroy(zh->nmem_error);
709
710     xfree(zh->path_reg);
711     xfree(zh);
712     return ZEBRA_OK;
713 }
714
715 struct map_baseinfo {
716     ZebraHandle zh;
717     NMEM mem;
718     int num_bases;
719     char **basenames;
720     int new_num_bases;
721     char **new_basenames;
722     int new_num_max;
723 };
724
725 static void zebra_open_res(ZebraHandle zh)
726 {
727     char fname[512];
728     ASSERTZH;
729     zh->errCode = 0;
730
731     if (zh->path_reg)
732     {
733         sprintf(fname, "%.200s/zebra.cfg", zh->path_reg);
734         zh->res = res_open(zh->session_res, 0);
735         res_read_file(zh->res, fname);
736     }
737     else if (*zh->reg_name == 0)
738     {
739         zh->res = res_open(zh->session_res, 0);
740     }
741     else
742     {
743         yaz_log(YLOG_WARN, "no register root specified");
744         zh->res = 0;  /* no path for register - fail! */
745     }
746 }
747
748 static void zebra_close_res(ZebraHandle zh)
749 {
750     ASSERTZH;
751     zh->errCode = 0;
752     res_close(zh->res);
753     zh->res = 0;
754 }
755
756 static void zebra_select_register(ZebraHandle zh, const char *new_reg)
757 {
758     ASSERTZH;
759     zh->errCode = 0;
760     if (zh->res && strcmp(zh->reg_name, new_reg) == 0)
761         return;
762     if (!zh->res)
763     {
764         assert(zh->reg == 0);
765         assert(*zh->reg_name == 0);
766     }
767     else
768     {
769         if (zh->reg)
770         {
771             resultSetInvalidate(zh);
772             zebra_register_close(zh->service, zh->reg);
773             zh->reg = 0;
774         }
775         zebra_close_res(zh);
776     }
777     xfree(zh->reg_name);
778     zh->reg_name = xstrdup(new_reg);
779
780     xfree(zh->path_reg);
781     zh->path_reg = 0;
782     if (zh->service->path_root)
783     {
784         zh->path_reg = xmalloc(strlen(zh->service->path_root) + 
785                                strlen(zh->reg_name) + 3);
786         strcpy(zh->path_reg, zh->service->path_root);
787         if (*zh->reg_name)
788         {
789             strcat(zh->path_reg, "/");
790             strcat(zh->path_reg, zh->reg_name);
791         }
792     }
793     zebra_open_res(zh);
794     
795     if (zh->lock_normal)
796         zebra_lock_destroy(zh->lock_normal);
797     zh->lock_normal = 0;
798
799     if (zh->lock_shadow)
800         zebra_lock_destroy(zh->lock_shadow);
801     zh->lock_shadow = 0;
802
803     if (zh->res)
804     {
805         char fname[512];
806         const char *lock_area = res_get(zh->res, "lockDir");
807         
808         if (!lock_area && zh->path_reg)
809             res_set(zh->res, "lockDir", zh->path_reg);
810         sprintf(fname, "norm.%s.LCK", zh->reg_name);
811         zh->lock_normal =
812             zebra_lock_create(res_get(zh->res, "lockDir"), fname);
813         
814         sprintf(fname, "shadow.%s.LCK", zh->reg_name);
815         zh->lock_shadow =
816             zebra_lock_create(res_get(zh->res, "lockDir"), fname);
817
818         if (!zh->lock_normal || !zh->lock_shadow)
819         {
820             if (zh->lock_normal)
821             {
822                 zebra_lock_destroy(zh->lock_normal);
823                 zh->lock_normal = 0;
824             }
825             if (zh->lock_shadow)
826             {
827                 zebra_lock_destroy(zh->lock_shadow);
828                 zh->lock_shadow = 0;
829             }
830             zebra_close_res(zh);
831         }
832     }
833     if (zh->res)
834     {
835         int approx = 0;
836         if (res_get_int(zh->res, "estimatehits", &approx) == ZEBRA_OK)
837             zebra_set_approx_limit(zh, approx);
838     }
839     if (zh->res)
840     {
841         if (res_get_int(zh->res, "staticrank", &zh->m_staticrank) == ZEBRA_OK)
842             yaz_log(YLOG_LOG, "static rank set and is %d", zh->m_staticrank);
843     }
844     if (zh->res)
845     {
846         if (res_get_int(zh->res, "segment", &zh->m_segment_indexing) == 
847             ZEBRA_OK)
848         {
849             yaz_log(YLOG_DEBUG, "segment indexing set and is %d",
850                     zh->m_segment_indexing);
851         }
852     }
853 }
854
855 void map_basenames_func(void *vp, const char *name, const char *value)
856 {
857     struct map_baseinfo *p = (struct map_baseinfo *) vp;
858     int i, no;
859     char fromdb[128], todb[8][128];
860
861     assert(value);
862     assert(name);
863     assert(vp);
864     
865     no =
866         sscanf(value, "%127s %127s %127s %127s %127s %127s %127s %127s %127s",
867                fromdb,  todb[0], todb[1], todb[2], todb[3], todb[4],
868                todb[5], todb[6], todb[7]);
869     if (no < 2)
870         return ;
871     no--;
872     for (i = 0; i<p->num_bases; i++)
873         if (p->basenames[i] && !STRCASECMP(p->basenames[i], fromdb))
874         {
875             p->basenames[i] = 0;
876             for (i = 0; i < no; i++)
877             {
878                 if (p->new_num_bases == p->new_num_max)
879                     return;
880                 p->new_basenames[(p->new_num_bases)++] = 
881                     nmem_strdup(p->mem, todb[i]);
882             }
883             return;
884         }
885 }
886
887 int zebra_select_default_database(ZebraHandle zh)
888 {
889     if (!zh->res)
890     {
891         /* no database has been selected - so we select based on
892            resource setting (including group)
893         */
894         const char *group = res_get(zh->session_res, "group");
895         const char *v = res_get_prefix(zh->session_res,
896                                        "database", group, "Default");
897         return zebra_select_database(zh, v);
898     }
899     return 0;
900 }
901
902 void map_basenames(ZebraHandle zh, ODR stream,
903                    int *num_bases, char ***basenames)
904 {
905     struct map_baseinfo info;
906     struct map_baseinfo *p = &info;
907     int i;
908     ASSERTZH;
909     yaz_log(log_level, "map_basenames ");
910     assert(stream);
911
912     info.zh = zh;
913
914     info.num_bases = *num_bases;
915     info.basenames = *basenames;
916     info.new_num_max = 128;
917     info.new_num_bases = 0;
918     info.new_basenames = (char **)
919         odr_malloc(stream, sizeof(*info.new_basenames) * info.new_num_max);
920     info.mem = stream->mem;
921
922     res_trav(zh->session_res, "mapdb", &info, map_basenames_func);
923     
924     for (i = 0; i<p->num_bases; i++)
925         if (p->basenames[i] && p->new_num_bases < p->new_num_max)
926         {
927             p->new_basenames[(p->new_num_bases)++] = 
928                 nmem_strdup(p->mem, p->basenames[i]);
929         }
930     *num_bases = info.new_num_bases;
931     *basenames = info.new_basenames;
932     for (i = 0; i<*num_bases; i++)
933         yaz_log(YLOG_DEBUG, "base %s", (*basenames)[i]);
934 }
935
936 ZEBRA_RES zebra_select_database(ZebraHandle zh, const char *basename)
937 {
938     ZEBRA_CHECK_HANDLE(zh);
939
940     yaz_log(log_level, "zebra_select_database %s",basename);
941     assert(basename);
942     return zebra_select_databases(zh, 1, &basename);
943 }
944
945 ZEBRA_RES zebra_select_databases(ZebraHandle zh, int num_bases,
946                                  const char **basenames)
947 {
948     int i;
949     const char *cp;
950     int len = 0;
951     char *new_reg = 0;
952
953     ZEBRA_CHECK_HANDLE(zh);
954     assert(basenames);
955
956     yaz_log(log_level, "zebra_select_databases n=%d [0]=%s",
957             num_bases,basenames[0]);
958     zh->errCode = 0;
959     
960     if (num_bases < 1)
961     {
962         zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
963         return ZEBRA_FAIL;
964     }
965
966     /* Check if the user has access to all databases (Seb) */
967     /* You could argue that this should happen later, after we have
968      * determined that the database(s) exist. */
969     if (zh->dbaccesslist) {
970         for (i = 0; i < num_bases; i++) {
971             const char *db = basenames[i];
972             char *p, *pp;
973             for (p = zh->dbaccesslist; p && *p; p = pp) {
974                 int len;
975                 if ((pp = strchr(p, '+'))) {
976                     len = pp - p;
977                     pp++;
978                 }
979                 else
980                     len = strlen(p);
981                 if (len == strlen(db) && !strncmp(db, p, len))
982                     break;
983             }
984             if (!p) {
985                 zh->errCode = YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED;
986                 return ZEBRA_FAIL;
987             }
988         }
989     }
990
991     for (i = 0; i < zh->num_basenames; i++)
992         xfree(zh->basenames[i]);
993     xfree(zh->basenames);
994     
995     zh->num_basenames = num_bases;
996     zh->basenames = xmalloc(zh->num_basenames * sizeof(*zh->basenames));
997     for (i = 0; i < zh->num_basenames; i++)
998         zh->basenames[i] = xstrdup(basenames[i]);
999
1000     cp = strrchr(basenames[0], '/');
1001     if (cp)
1002     {
1003         len = cp - basenames[0];
1004         new_reg = xmalloc(len + 1);
1005         memcpy(new_reg, basenames[0], len);
1006         new_reg[len] = '\0';
1007     }
1008     else
1009         new_reg = xstrdup("");
1010     for (i = 1; i<num_bases; i++)
1011     {
1012         const char *cp1;
1013
1014         cp1 = strrchr(basenames[i], '/');
1015         if (cp)
1016         {
1017             if (!cp1)
1018             {
1019                 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1020                 return -1;
1021             }
1022             if (len != cp1 - basenames[i] ||
1023                 memcmp(basenames[i], new_reg, len))
1024             {
1025                 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1026                 return -1;
1027             }
1028         }
1029         else
1030         {
1031             if (cp1)
1032             {
1033                 zh->errCode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
1034                 return ZEBRA_FAIL;
1035             }
1036         }
1037     }
1038     zebra_select_register(zh, new_reg);
1039     xfree(new_reg);
1040     if (!zh->res)
1041     {
1042         zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1043         return ZEBRA_FAIL;
1044     }
1045     if (!zh->lock_normal || !zh->lock_shadow)
1046     {
1047         zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1048         return ZEBRA_FAIL;
1049     }
1050     return ZEBRA_OK;
1051 }
1052
1053 ZEBRA_RES zebra_set_approx_limit(ZebraHandle zh, zint approx_limit)
1054 {
1055     if (approx_limit == 0)
1056         approx_limit = DEFAULT_APPROX_LIMIT;
1057     zh->approx_limit = approx_limit;
1058     return ZEBRA_OK;
1059 }
1060
1061 void zebra_set_partial_result(ZebraHandle zh)
1062 {
1063     zh->partial_result = 1;
1064 }
1065
1066
1067 ZEBRA_RES zebra_set_break_handler(ZebraHandle zh,
1068                                   int (*f)(void *client_data),
1069                                   void *client_data)
1070 {
1071     zh->break_handler_func = f;
1072     zh->break_handler_data = client_data;
1073     return ZEBRA_OK;
1074 }
1075
1076 ZEBRA_RES zebra_search_RPN_x(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1077                              const char *setname, zint *hits,
1078                              int *estimated_hit_count,
1079                              int *partial_resultset)
1080 {
1081     ZEBRA_RES r;
1082     
1083     ZEBRA_CHECK_HANDLE(zh);
1084
1085     assert(o);
1086     assert(query);
1087     assert(hits);
1088     assert(setname);
1089     yaz_log(log_level, "zebra_search_rpn");
1090
1091     zh->partial_result = 0;
1092
1093     if (zebra_begin_read(zh) == ZEBRA_FAIL)
1094         return ZEBRA_FAIL;
1095
1096     r = resultSetAddRPN(zh, odr_extract_mem(o), query, 
1097                         zh->num_basenames, zh->basenames, setname,
1098                         hits, estimated_hit_count);
1099
1100     *partial_resultset = zh->partial_result;
1101     zebra_end_read(zh);
1102     return r;
1103 }
1104
1105 ZEBRA_RES zebra_search_RPN(ZebraHandle zh, ODR o, Z_RPNQuery *query,
1106                            const char *setname, zint *hits)
1107 {
1108     int estimated_hit_count;
1109     int partial_resultset;
1110     return zebra_search_RPN_x(zh, o, query, setname, hits,
1111                               &estimated_hit_count,
1112                               &partial_resultset);
1113 }
1114
1115 ZEBRA_RES zebra_records_retrieve(ZebraHandle zh, ODR stream,
1116                                  const char *setname,
1117                                  Z_RecordComposition *comp,
1118                                  const Odr_oid *input_format, int num_recs,
1119                                  ZebraRetrievalRecord *recs)
1120 {
1121     ZebraMetaRecord *poset;
1122     int i;
1123     ZEBRA_RES ret = ZEBRA_OK;
1124     zint *pos_array;
1125
1126     ZEBRA_CHECK_HANDLE(zh);
1127     assert(stream);
1128     assert(setname);
1129     assert(recs);
1130     assert(num_recs>0);
1131
1132     yaz_log(log_level, "zebra_records_retrieve n=%d", num_recs);
1133
1134     if (!zh->res)
1135     {
1136         zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1137                        setname);
1138         return ZEBRA_FAIL;
1139     }
1140     
1141     if (zebra_begin_read(zh) == ZEBRA_FAIL)
1142         return ZEBRA_FAIL;
1143
1144     pos_array = (zint *) xmalloc(num_recs * sizeof(*pos_array));
1145     for (i = 0; i<num_recs; i++)
1146         pos_array[i] = recs[i].position;
1147     poset = zebra_meta_records_create(zh, setname, num_recs, pos_array);
1148     if (!poset)
1149     {
1150         yaz_log(YLOG_DEBUG, "zebraPosSetCreate error");
1151         zebra_setError(zh, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1152                        setname);
1153         ret = ZEBRA_FAIL;
1154     }
1155     else
1156     {
1157         WRBUF addinfo_w = wrbuf_alloc();
1158         for (i = 0; i < num_recs; i++)
1159         {
1160             recs[i].errCode = 0;
1161             recs[i].errString = 0;
1162             recs[i].format = 0;
1163             recs[i].len = 0;
1164             recs[i].buf = 0;
1165             recs[i].base = 0;
1166             recs[i].sysno = poset[i].sysno;
1167             if (poset[i].term)
1168             {
1169                 recs[i].format = yaz_oid_recsyn_sutrs;
1170                 recs[i].len = strlen(poset[i].term);
1171                 recs[i].buf = poset[i].term;
1172                 recs[i].base = poset[i].db;
1173             }
1174             else if (poset[i].sysno)
1175             {
1176                 char *buf;
1177                 int len = 0;
1178                 zebra_snippets *hit_snippet = zebra_snippets_create();
1179
1180                 /* we disable hit snippets for now. It does not work well
1181                    and it slows retrieval down a lot */
1182 #if 0
1183                 zebra_snippets_hit_vector(zh, setname, poset[i].sysno, 
1184                                           hit_snippet);
1185 #endif
1186                 wrbuf_rewind(addinfo_w);
1187                 recs[i].errCode =
1188                     zebra_record_fetch(zh, setname,
1189                                        poset[i].sysno, poset[i].score,
1190                                        stream, input_format, comp,
1191                                        &recs[i].format, &buf, &len,
1192                                        &recs[i].base, addinfo_w);
1193                 
1194                 if (wrbuf_len(addinfo_w))
1195                     recs[i].errString =
1196                         odr_strdup(stream, wrbuf_cstr(addinfo_w));
1197                 recs[i].len = len;
1198                 if (len > 0)
1199                 {
1200                     recs[i].buf = (char*) odr_malloc(stream, len);
1201                     memcpy(recs[i].buf, buf, len);
1202                 }
1203                 else
1204                     recs[i].buf = buf;
1205                 recs[i].score = poset[i].score;
1206                 zebra_snippets_destroy(hit_snippet);
1207             }
1208             else
1209             {
1210                 /* only need to set it once */
1211                 if (pos_array[i] < zh->approx_limit && ret == ZEBRA_OK)
1212                 {
1213                     zebra_setError_zint(zh,
1214                                         YAZ_BIB1_PRESENT_REQUEST_OUT_OF_RANGE,
1215                                         pos_array[i]);
1216                     ret = ZEBRA_FAIL;
1217                     break;
1218                 }
1219             }
1220         }
1221         zebra_meta_records_destroy(zh, poset, num_recs);
1222         wrbuf_destroy(addinfo_w);
1223     }
1224     zebra_end_read(zh);
1225     xfree(pos_array);
1226     return ret;
1227 }
1228
1229 ZEBRA_RES zebra_scan_PQF(ZebraHandle zh, ODR stream, const char *query,
1230                          int *position,
1231                          int *num_entries, ZebraScanEntry **entries,
1232                          int *is_partial,
1233                          const char *setname)
1234 {
1235     YAZ_PQF_Parser pqf_parser = yaz_pqf_create();
1236     Z_AttributesPlusTerm *zapt;
1237     Odr_oid *attributeSet;
1238     ZEBRA_RES res;
1239     
1240     if (!(zapt = yaz_pqf_scan(pqf_parser, stream, &attributeSet, query)))
1241     {
1242         res = ZEBRA_FAIL;
1243         zh->errCode = YAZ_BIB1_SCAN_MALFORMED_SCAN;
1244     }
1245     else
1246     {
1247         res = zebra_scan(zh, stream, zapt, yaz_oid_attset_bib_1,
1248                          position, num_entries, entries, is_partial,
1249                          setname);
1250     }
1251     yaz_pqf_destroy(pqf_parser);
1252     return res;
1253 }
1254
1255 ZEBRA_RES zebra_scan(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
1256                      const Odr_oid *attributeset,
1257                      int *position,
1258                      int *num_entries, ZebraScanEntry **entries,
1259                      int *is_partial,
1260                      const char *setname)
1261 {
1262     ZEBRA_RES res;
1263
1264     ZEBRA_CHECK_HANDLE(zh);
1265
1266     assert(stream);
1267     assert(zapt);
1268     assert(position);
1269     assert(num_entries);
1270     assert(is_partial);
1271     assert(entries);
1272     yaz_log(log_level, "zebra_scan");
1273
1274     if (zebra_begin_read(zh) == ZEBRA_FAIL)
1275     {
1276         *entries = 0;
1277         *num_entries = 0;
1278         return ZEBRA_FAIL;
1279     }
1280
1281     res = rpn_scan(zh, stream, zapt, attributeset,
1282                    zh->num_basenames, zh->basenames, position,
1283                    num_entries, entries, is_partial, setname);
1284     zebra_end_read(zh);
1285     return res;
1286 }
1287
1288 ZEBRA_RES zebra_sort(ZebraHandle zh, ODR stream,
1289                      int num_input_setnames, const char **input_setnames,
1290                      const char *output_setname,
1291                      Z_SortKeySpecList *sort_sequence,
1292                      int *sort_status)
1293 {
1294     ZEBRA_RES res;
1295     ZEBRA_CHECK_HANDLE(zh);
1296     assert(stream);
1297     assert(num_input_setnames>0);
1298     assert(input_setnames);
1299     assert(sort_sequence);
1300     assert(sort_status);
1301     yaz_log(log_level, "zebra_sort");
1302
1303     if (zebra_begin_read(zh) == ZEBRA_FAIL)
1304         return ZEBRA_FAIL;
1305     res = resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
1306                         output_setname, sort_sequence, sort_status);
1307     zebra_end_read(zh);
1308     return res;
1309 }
1310
1311 int zebra_deleteResultSet(ZebraHandle zh, int function,
1312                           int num_setnames, char **setnames,
1313                           int *statuses)
1314 {
1315     int i, status;
1316     ASSERTZH;
1317     yaz_log(log_level, "zebra_deleteResultSet n=%d", num_setnames);
1318
1319     if (zebra_begin_read(zh))
1320         return Z_DeleteStatus_systemProblemAtTarget;
1321     switch (function)
1322     {
1323     case Z_DeleteResultSetRequest_list:
1324         assert(num_setnames>0);
1325         assert(setnames);
1326         resultSetDestroy(zh, num_setnames, setnames, statuses);
1327         break;
1328     case Z_DeleteResultSetRequest_all:
1329         resultSetDestroy(zh, -1, 0, statuses);
1330         break;
1331     }
1332     zebra_end_read(zh);
1333     status = Z_DeleteStatus_success;
1334     for (i = 0; i<num_setnames; i++)
1335         if (statuses[i] == Z_DeleteStatus_resultSetDidNotExist)
1336             status = statuses[i];
1337     return status;
1338 }
1339
1340 int zebra_errCode(ZebraHandle zh)
1341 {
1342     if (zh)
1343     {
1344         yaz_log(log_level, "zebra_errCode: %d",zh->errCode);
1345         return zh->errCode;
1346     }
1347     yaz_log(log_level, "zebra_errCode: o");
1348     return 0; 
1349 }
1350
1351 const char *zebra_errString(ZebraHandle zh)
1352 {
1353     const char *e = 0;
1354     if (zh)
1355         e= diagbib1_str(zh->errCode);
1356     yaz_log(log_level, "zebra_errString: %s",e);
1357     return e;
1358 }
1359
1360 char *zebra_errAdd(ZebraHandle zh)
1361 {
1362     char *a = 0;
1363     if (zh)
1364         a= zh->errString;
1365     yaz_log(log_level, "zebra_errAdd: %s",a);
1366     return a;
1367 }
1368
1369 ZEBRA_RES zebra_auth(ZebraHandle zh, const char *user, const char *pass)
1370 {
1371     const char *p;
1372     const char *astring;
1373     char u[40];
1374     ZebraService zs;
1375
1376     ZEBRA_CHECK_HANDLE(zh);
1377
1378     zs = zh->service;
1379     
1380     sprintf(u, "perm.%.30s", user ? user : "anonymous");
1381     p = res_get(zs->global_res, u);
1382     xfree(zh->user_perm);
1383     zh->user_perm = xstrdup(p ? p : "r");
1384
1385     /* Determine database access list */
1386     astring = res_get(zs->dbaccess, user ? user : "anonymous");
1387     if (astring)
1388         zh->dbaccesslist = xstrdup(astring);
1389     else
1390         zh->dbaccesslist = 0;
1391
1392     /* users that don't require a password .. */
1393     if (zh->user_perm && strchr(zh->user_perm, 'a'))
1394         return ZEBRA_OK;
1395     
1396     if (!zs->passwd_db || !passwd_db_auth(zs->passwd_db, user, pass))
1397         return ZEBRA_OK;
1398     return ZEBRA_FAIL;
1399 }
1400
1401 ZEBRA_RES zebra_admin_import_begin(ZebraHandle zh, const char *database,
1402                                    const char *record_type)
1403 {
1404     yaz_log(log_level, "zebra_admin_import_begin db=%s rt=%s", 
1405             database, record_type);
1406     if (zebra_select_database(zh, database) == ZEBRA_FAIL)
1407         return ZEBRA_FAIL;
1408     return zebra_begin_trans(zh, 1);
1409 }
1410
1411 ZEBRA_RES zebra_admin_import_end(ZebraHandle zh)
1412 {
1413     ZEBRA_CHECK_HANDLE(zh);
1414     yaz_log(log_level, "zebra_admin_import_end");
1415     return zebra_end_trans(zh);
1416 }
1417
1418 ZEBRA_RES zebra_admin_import_segment(ZebraHandle zh, Z_Segment *segment)
1419 {
1420     ZEBRA_RES res = ZEBRA_OK;
1421     zint sysno;
1422     int i;
1423     ZEBRA_CHECK_HANDLE(zh);
1424     yaz_log(log_level, "zebra_admin_import_segment");
1425
1426     for (i = 0; i<segment->num_segmentRecords; i++)
1427     {
1428         Z_NamePlusRecord *npr = segment->segmentRecords[i];
1429
1430         if (npr->which == Z_NamePlusRecord_intermediateFragment)
1431         {
1432             Z_FragmentSyntax *fragment = npr->u.intermediateFragment;
1433             if (fragment->which == Z_FragmentSyntax_notExternallyTagged)
1434             {
1435                 Odr_oct *oct = fragment->u.notExternallyTagged;
1436                 sysno = 0;
1437                 
1438                 if(zebra_update_record(
1439                        zh, 
1440                        action_update,
1441                        0, /* record Type */
1442                        &sysno,
1443                        0, /* match */
1444                        0, /* fname */
1445                        (const char *) oct->buf, oct->len) == ZEBRA_FAIL)
1446                     res = ZEBRA_FAIL;
1447             }
1448         }
1449     }
1450     return res;
1451 }
1452
1453 int delete_w_handle(const char *info, void *handle)
1454 {
1455     ZebraHandle zh = (ZebraHandle) handle;
1456     ISAM_P pos;
1457
1458     if (*info == sizeof(pos))
1459     {
1460         memcpy(&pos, info+1, sizeof(pos));
1461         isamb_unlink(zh->reg->isamb, pos);
1462     }
1463     return 0;
1464 }
1465
1466 int delete_w_all_handle(const char *info, void *handle)
1467 {
1468     ZebraHandle zh = (ZebraHandle) handle;
1469     ISAM_P pos;
1470
1471     if (*info == sizeof(pos))
1472     {
1473         ISAMB_PP pt;
1474         memcpy(&pos, info+1, sizeof(pos));
1475         pt = isamb_pp_open(zh->reg->isamb, pos, 2);
1476         if (pt)
1477         {
1478             struct it_key key;
1479             key.mem[0] = 0;
1480             while (isamb_pp_read(pt, &key))
1481             {
1482                 Record rec;
1483                 rec = rec_get(zh->reg->records, key.mem[0]);
1484                 rec_del(zh->reg->records, &rec);
1485             }
1486             isamb_pp_close(pt);
1487         }
1488     }
1489     return delete_w_handle(info, handle);
1490 }
1491
1492 static int delete_SU_handle(void *handle, int ord,
1493                             const char *index_type, const char *string_index,
1494                             zinfo_index_category_t cat)
1495 {
1496     ZebraHandle zh = (ZebraHandle) handle;
1497     char ord_buf[20];
1498     int ord_len;
1499 #if 0
1500     yaz_log(YLOG_LOG, "ord=%d index_type=%s index=%s cat=%d", ord,
1501             index_type, string_index, (int) cat);
1502 #endif
1503     ord_len = key_SU_encode(ord, ord_buf);
1504     ord_buf[ord_len] = '\0';
1505
1506     assert(zh->reg->isamb);
1507     assert(zh->reg->records);
1508     dict_delete_subtree(zh->reg->dict, ord_buf,
1509                         zh, 
1510                         !strcmp(string_index, "_ALLRECORDS") ?
1511                         delete_w_all_handle : delete_w_handle);
1512     return 0;
1513 }
1514
1515 ZEBRA_RES zebra_drop_database(ZebraHandle zh, const char *db)
1516 {
1517     ZEBRA_RES ret = ZEBRA_OK;
1518
1519     yaz_log(log_level, "zebra_drop_database %s", db);
1520     ZEBRA_CHECK_HANDLE(zh);
1521
1522     if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1523         return ZEBRA_FAIL;
1524     if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
1525         return ZEBRA_FAIL;
1526     if (zh->reg->isamb)
1527     {
1528         int db_ord;
1529         if (zebraExplain_curDatabase(zh->reg->zei, db))
1530         {
1531             zebra_setError(zh, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db);
1532             ret = ZEBRA_FAIL;
1533         }
1534         else
1535         {
1536             db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1537             dict_delete_subtree_ord(zh->reg->matchDict, db_ord,
1538                                     0 /* handle */, 0 /* func */);
1539             zebraExplain_trav_ord(zh->reg->zei, zh, delete_SU_handle);
1540             zebraExplain_removeDatabase(zh->reg->zei, zh);
1541             zebra_remove_file_match(zh);
1542         }
1543     }
1544     else
1545     {
1546         yaz_log(YLOG_WARN, "drop database only supported for isam:b");
1547         zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED,
1548                        "drop database only supported for isam:b");
1549         ret = ZEBRA_FAIL;
1550     }
1551     if (zebra_end_trans(zh) != ZEBRA_OK)
1552     {
1553         yaz_log(YLOG_WARN, "zebra_end_trans failed");
1554         ret = ZEBRA_FAIL;
1555     }
1556     return ret;
1557 }
1558
1559 ZEBRA_RES zebra_create_database(ZebraHandle zh, const char *db)
1560 {
1561     yaz_log(log_level, "zebra_create_database %s", db);
1562     ZEBRA_CHECK_HANDLE(zh);
1563     assert(db);
1564
1565     if (zebra_select_database(zh, db) == ZEBRA_FAIL)
1566         return ZEBRA_FAIL;
1567     if (zebra_begin_trans(zh, 1))
1568         return ZEBRA_FAIL;
1569
1570     /* announce database */
1571     if (zebraExplain_newDatabase(zh->reg->zei, db, 0 
1572                                  /* explainDatabase */))
1573     {
1574         if (zebra_end_trans(zh) != ZEBRA_OK)
1575         {
1576             yaz_log(YLOG_WARN, "zebra_end_trans failed");
1577         }
1578         zebra_setError(zh, YAZ_BIB1_ES_IMMEDIATE_EXECUTION_FAILED, db);
1579         return ZEBRA_FAIL;
1580     }
1581     return zebra_end_trans(zh);
1582 }
1583
1584 int zebra_string_norm(ZebraHandle zh, const char *index_type,
1585                       const char *input_str, int input_len,
1586                       char *output_str, int output_len)
1587 {
1588     WRBUF wrbuf;
1589     zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, index_type);
1590     ASSERTZH;
1591     assert(input_str);
1592     assert(output_str);
1593     yaz_log(log_level, "zebra_string_norm ");
1594
1595     if (!zh->reg->zebra_maps)
1596         return -1;
1597     wrbuf = zebra_replace(zm, "", input_str, input_len);
1598     if (!wrbuf)
1599         return -2;
1600     if (wrbuf_len(wrbuf) >= output_len)
1601         return -3;
1602     if (wrbuf_len(wrbuf))
1603         memcpy(output_str, wrbuf_buf(wrbuf), wrbuf_len(wrbuf));
1604     output_str[wrbuf_len(wrbuf)] = '\0';
1605     return wrbuf_len(wrbuf);
1606 }
1607
1608 /** \brief set register state (state*.LCK)
1609     \param zh Zebra handle
1610     \param val state
1611     \param seqno sequence number
1612     
1613     val is one of:
1614     d=writing to shadow(shadow enabled); writing to register (shadow disabled)
1615     o=reading only
1616     c=commit (writing to register, reading from shadow, shadow mode only)
1617 */
1618 static void zebra_set_state(ZebraHandle zh, int val, int seqno)
1619 {
1620     char state_fname[256];
1621     char *fname;
1622     long p = getpid();
1623     FILE *f;
1624     ASSERTZH;
1625     yaz_log(log_level, "zebra_set_state v=%c seq=%d", val, seqno);
1626
1627     sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1628     fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1629     f = fopen(fname, "w");
1630
1631     yaz_log(YLOG_DEBUG, "zebra_set_state: %c %d %ld", val, seqno, p);
1632     fprintf(f, "%c %d %ld\n", val, seqno, p);
1633     fclose(f);
1634     xfree(fname);
1635 }
1636
1637 static void zebra_get_state(ZebraHandle zh, char *val, int *seqno)
1638 {
1639     char state_fname[256];
1640     char *fname;
1641     FILE *f;
1642
1643     ASSERTZH;
1644     yaz_log(log_level, "zebra_get_state ");
1645
1646     sprintf(state_fname, "state.%s.LCK", zh->reg_name);
1647     fname = zebra_mk_fname(res_get(zh->res, "lockDir"), state_fname);
1648     f = fopen(fname, "r");
1649     *val = 'o';
1650     *seqno = 0;
1651
1652     if (f)
1653     {
1654         if (fscanf(f, "%c %d", val, seqno) != 2)
1655         {
1656             yaz_log(YLOG_ERRNO|YLOG_WARN, "fscan fail %s",
1657                     state_fname);
1658         }
1659         fclose(f);
1660     }
1661     xfree(fname);
1662 }
1663
1664 ZEBRA_RES zebra_begin_read(ZebraHandle zh)
1665 {
1666     return zebra_begin_trans(zh, 0);
1667 }
1668
1669 ZEBRA_RES zebra_end_read(ZebraHandle zh)
1670 {
1671     return zebra_end_trans(zh);
1672 }
1673
1674 static void read_res_for_transaction(ZebraHandle zh)
1675 {
1676     const char *group = res_get(zh->res, "group");
1677     const char *v;
1678     /* FIXME - do we still use groups ?? */
1679     
1680     zh->m_group = group;
1681     v = res_get_prefix(zh->res, "followLinks", group, "1");
1682     zh->m_follow_links = atoi(v);
1683
1684     zh->m_record_id = res_get_prefix(zh->res, "recordId", group, 0);
1685     zh->m_record_type = res_get_prefix(zh->res, "recordType", group, 0);
1686
1687     v = res_get_prefix(zh->res, "storeKeys", group, "1");
1688     zh->m_store_keys = atoi(v);
1689
1690     v = res_get_prefix(zh->res, "storeData", group, "1");
1691     zh->m_store_data = atoi(v);
1692
1693     v = res_get_prefix(zh->res, "explainDatabase", group, "0");
1694     zh->m_explain_database = atoi(v);
1695
1696     v = res_get_prefix(zh->res, "openRW", group, "1");
1697     zh->m_flag_rw = atoi(v);
1698
1699     v = res_get_prefix(zh->res, "fileVerboseLimit", group, "1000");
1700     zh->m_file_verbose_limit = atoi(v);
1701 }
1702
1703 ZEBRA_RES zebra_begin_trans(ZebraHandle zh, int rw)
1704 {
1705     ZEBRA_CHECK_HANDLE(zh);
1706     zebra_select_default_database(zh);
1707     if (!zh->res)
1708     {
1709         zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1710                        "zebra_begin_trans: no database selected");
1711         return ZEBRA_FAIL;
1712     }
1713     ASSERTZHRES;
1714     yaz_log(log_level, "zebra_begin_trans rw=%d",rw);
1715
1716     if (zh->user_perm)
1717     {
1718         if (rw && !strchr(zh->user_perm, 'w'))
1719         {
1720             zebra_setError(
1721                 zh,
1722                 YAZ_BIB1_ES_PERMISSION_DENIED_ON_ES_CANNOT_MODIFY_OR_DELETE,
1723                 0);
1724             return ZEBRA_FAIL;
1725         }
1726     }
1727
1728     assert(zh->res);
1729     if (rw)
1730     {
1731         int seqno = 0;
1732         char val = '?';
1733         const char *rval = 0;
1734         
1735         (zh->trans_no++);
1736         if (zh->trans_w_no)
1737         {
1738             read_res_for_transaction(zh);
1739             return 0;
1740         }
1741         if (zh->trans_no != 1)
1742         {
1743             zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1744                            "zebra_begin_trans: no write trans within read");
1745             return ZEBRA_FAIL;
1746         }
1747         if (zh->reg)
1748         {
1749             resultSetInvalidate(zh);
1750             zebra_register_close(zh->service, zh->reg);
1751         }
1752         zh->trans_w_no = zh->trans_no;
1753
1754         zh->records_inserted = 0;
1755         zh->records_updated = 0;
1756         zh->records_deleted = 0;
1757         zh->records_processed = 0;
1758         zh->records_skipped = 0;
1759         
1760 #if HAVE_SYS_TIMES_H
1761         times(&zh->tms1);
1762 #endif
1763         /* lock */
1764         if (zh->shadow_enable)
1765             rval = res_get(zh->res, "shadow");
1766         
1767         if (rval)
1768         {
1769             zebra_lock_r(zh->lock_normal);
1770             zebra_lock_w(zh->lock_shadow);
1771         }
1772         else
1773         {
1774             zebra_lock_w(zh->lock_normal);
1775             zebra_lock_w(zh->lock_shadow);
1776         }
1777         zebra_get_state(zh, &val, &seqno);
1778         if (val != 'o')
1779         {
1780             /* either we didn't finish commit or shadow is dirty */
1781             if (!rval)
1782             {
1783                 yaz_log(YLOG_WARN, "previous transaction did not finish "
1784                         "(shadow disabled)");
1785             }
1786             zebra_unlock(zh->lock_shadow);
1787             zebra_unlock(zh->lock_normal);
1788             if (zebra_commit(zh))
1789             {
1790                 zh->trans_no--;
1791                 zh->trans_w_no = 0;
1792                 return ZEBRA_FAIL;
1793             }
1794             if (rval)
1795             {
1796                 zebra_lock_r(zh->lock_normal);
1797                 zebra_lock_w(zh->lock_shadow);
1798             }
1799             else
1800             {
1801                 zebra_lock_w(zh->lock_normal);
1802                 zebra_lock_w(zh->lock_shadow);
1803             }
1804         }
1805
1806         zebra_set_state(zh, 'd', seqno);
1807         
1808         zh->reg = zebra_register_open(zh->service, zh->reg_name,
1809                                       1, rval ? 1 : 0, zh->res,
1810                                       zh->path_reg);
1811         if (zh->reg)
1812             zh->reg->seqno = seqno;
1813         else
1814         {
1815             zebra_set_state(zh, 'o', seqno);
1816             
1817             zebra_unlock(zh->lock_shadow);
1818             zebra_unlock(zh->lock_normal);
1819
1820             zh->trans_no--;
1821             zh->trans_w_no = 0;
1822
1823             zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1824                            "zebra_begin_trans: cannot open register");
1825             yaz_log(YLOG_FATAL, "%s", zh->errString);
1826             return ZEBRA_FAIL;
1827         }
1828         zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]);
1829     }
1830     else
1831     {
1832         int dirty = 0;
1833         char val;
1834         int seqno;
1835         
1836         (zh->trans_no)++;
1837         
1838         if (zh->trans_no != 1)
1839         {
1840             return zebra_flush_reg(zh);
1841         }
1842 #if HAVE_SYS_TIMES_H
1843         times(&zh->tms1);
1844 #endif
1845         if (!zh->res)
1846         {
1847             (zh->trans_no)--;
1848             zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1849             return ZEBRA_FAIL;
1850         }
1851         if (!zh->lock_normal || !zh->lock_shadow)
1852         {
1853             (zh->trans_no)--;
1854             zh->errCode = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
1855             return ZEBRA_FAIL;
1856         }
1857         zebra_get_state(zh, &val, &seqno);
1858         if (val == 'd')
1859             val = 'o';
1860         
1861         if (!zh->reg)
1862             dirty = 1;
1863         else if (seqno != zh->reg->seqno)
1864         {
1865             yaz_log(YLOG_DEBUG, "reopen seqno cur/old %d/%d",
1866                     seqno, zh->reg->seqno);
1867             dirty = 1;
1868         }
1869         else if (zh->reg->last_val != val)
1870         {
1871             yaz_log(YLOG_DEBUG, "reopen last cur/old %d/%d",
1872                     val, zh->reg->last_val);
1873             dirty = 1;
1874         }
1875         if (!dirty)
1876             return ZEBRA_OK;
1877         
1878         if (val == 'c')
1879             zebra_lock_r(zh->lock_shadow);
1880         else
1881             zebra_lock_r(zh->lock_normal);
1882         
1883         if (zh->reg)
1884         {
1885             resultSetInvalidate(zh);
1886             zebra_register_close(zh->service, zh->reg);
1887         }
1888         zh->reg = zebra_register_open(zh->service, zh->reg_name,
1889                                       0, val == 'c' ? 1 : 0,
1890                                       zh->res, zh->path_reg);
1891         if (!zh->reg)
1892         {
1893             zebra_unlock(zh->lock_normal);
1894             zebra_unlock(zh->lock_shadow);
1895             zh->trans_no--;
1896             zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
1897             return ZEBRA_FAIL;
1898         }
1899         zh->reg->last_val = val;
1900         zh->reg->seqno = seqno;
1901     }
1902     read_res_for_transaction(zh);
1903     return ZEBRA_OK;
1904 }
1905
1906 ZEBRA_RES zebra_end_trans(ZebraHandle zh)
1907 {
1908     ZebraTransactionStatus dummy;
1909
1910     yaz_log(log_level, "zebra_end_trans");
1911     ZEBRA_CHECK_HANDLE(zh);
1912     return zebra_end_transaction(zh, &dummy);
1913 }
1914
1915 ZEBRA_RES zebra_end_transaction(ZebraHandle zh, ZebraTransactionStatus *status)
1916 {
1917     char val;
1918     int seqno;
1919     const char *rval;
1920
1921     ZEBRA_CHECK_HANDLE(zh);
1922
1923     assert(status);
1924     yaz_log(log_level, "zebra_end_transaction");
1925
1926     status->processed = 0;
1927     status->inserted  = 0;
1928     status->updated   = 0;
1929     status->deleted   = 0;
1930     status->utime     = 0;
1931     status->stime     = 0;
1932
1933     if (!zh->res || !zh->reg)
1934     {
1935         zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
1936                        "zebra_end_trans: no open transaction");
1937         return ZEBRA_FAIL;
1938     }
1939     if (zh->trans_no != zh->trans_w_no)
1940     {
1941         zh->trans_no--;
1942         if (zh->trans_no != 0)
1943             return ZEBRA_OK;
1944
1945         /* release read lock */
1946
1947         zebra_unlock(zh->lock_normal);
1948         zebra_unlock(zh->lock_shadow);
1949     }
1950     else
1951     {   /* release write lock */
1952         zh->trans_no--;
1953         zh->trans_w_no = 0;
1954         
1955         yaz_log(YLOG_DEBUG, "zebra_end_trans");
1956         rval = res_get(zh->res, "shadow");
1957         
1958         zebraExplain_runNumberIncrement(zh->reg->zei, 1);
1959         
1960         zebra_flush_reg(zh);
1961         
1962         resultSetInvalidate(zh);
1963
1964         zebra_register_close(zh->service, zh->reg);
1965         zh->reg = 0;
1966         
1967         yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
1968                 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT, 
1969                 zh->records_processed, zh->records_inserted,
1970                 zh->records_updated, zh->records_deleted);
1971         
1972         status->processed = zh->records_processed;
1973         status->inserted = zh->records_inserted;
1974         status->updated = zh->records_updated;
1975         status->deleted = zh->records_deleted;
1976         
1977         zebra_get_state(zh, &val, &seqno);
1978         if (val != 'd')
1979         {
1980             BFiles bfs = bfs_create(rval, zh->path_reg);
1981             bf_commitClean(bfs, rval);
1982             bfs_destroy(bfs);
1983         }
1984         if (!rval)
1985             seqno++;
1986         zebra_set_state(zh, 'o', seqno);
1987         zebra_unlock(zh->lock_shadow);
1988         zebra_unlock(zh->lock_normal);
1989         
1990     }
1991 #if HAVE_SYS_TIMES_H
1992     times(&zh->tms2);
1993     yaz_log(log_level, "user/system: %ld/%ld",
1994             (long) (zh->tms2.tms_utime - zh->tms1.tms_utime),
1995             (long) (zh->tms2.tms_stime - zh->tms1.tms_stime));
1996     
1997     status->utime = (long) (zh->tms2.tms_utime - zh->tms1.tms_utime);
1998     status->stime = (long) (zh->tms2.tms_stime - zh->tms1.tms_stime);
1999 #endif
2000     return ZEBRA_OK;
2001 }
2002
2003 ZEBRA_RES zebra_repository_update(ZebraHandle zh, const char *path)
2004 {
2005     return zebra_repository_index(zh, path, action_update);
2006 }
2007
2008 ZEBRA_RES zebra_repository_delete(ZebraHandle zh, const char *path)
2009 {
2010     return zebra_repository_index(zh, path, action_delete);
2011 }
2012
2013 ZEBRA_RES zebra_repository_index(ZebraHandle zh, const char *path,
2014                                  enum zebra_recctrl_action_t action)
2015 {
2016     ASSERTZH;
2017     assert(path);
2018
2019     if (action == action_update)
2020         yaz_log(log_level, "updating %s", path);
2021     else if (action == action_delete)
2022         yaz_log(log_level, "deleting %s", path);
2023     else if (action == action_a_delete)
2024         yaz_log(log_level, "attempt deleting %s", path);
2025     else
2026         yaz_log(log_level, "update action=%d", (int) action);
2027
2028     if (zh->m_record_id && !strcmp(zh->m_record_id, "file"))
2029         return zebra_update_file_match(zh, path);
2030     else
2031         return zebra_update_from_path(zh, path, action);
2032 }
2033
2034 ZEBRA_RES zebra_repository_show(ZebraHandle zh, const char *path)
2035 {
2036     ASSERTZH;
2037     assert(path);
2038     yaz_log(log_level, "zebra_repository_show");
2039     repositoryShow(zh, path);
2040     return ZEBRA_OK;
2041 }
2042
2043 static ZEBRA_RES zebra_commit_ex(ZebraHandle zh, int clean_only)
2044 {
2045     int seqno;
2046     char val;
2047     const char *rval;
2048     BFiles bfs;
2049     ZEBRA_RES res = ZEBRA_OK;
2050
2051     ASSERTZH;
2052
2053     yaz_log(log_level, "zebra_commit_ex clean_only=%d", clean_only);
2054     zebra_select_default_database(zh);
2055     if (!zh->res)
2056     {
2057         zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2058         return ZEBRA_FAIL;
2059     }
2060     rval = res_get(zh->res, "shadow");    
2061     if (!rval)
2062     {
2063         yaz_log(YLOG_WARN, "Cannot perform commit - No shadow area defined");
2064         return ZEBRA_OK;
2065     }
2066
2067     zebra_lock_w(zh->lock_normal);
2068     zebra_lock_r(zh->lock_shadow);
2069
2070     bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2071     if (!bfs)
2072     {
2073         zebra_unlock(zh->lock_shadow);
2074         zebra_unlock(zh->lock_normal);
2075         return ZEBRA_FAIL;
2076     }
2077     zebra_get_state(zh, &val, &seqno);
2078
2079     if (val == 'd')
2080     {
2081         /* shadow area is dirty and so we must throw it away */
2082         yaz_log(YLOG_WARN, "previous transaction didn't reach commit");
2083         clean_only = 1;
2084     }
2085     else if (val == 'c')
2086     {
2087         /* commit has started. We can not remove it anymore */
2088         clean_only = 0;
2089     }
2090
2091     if (rval && *rval)
2092         bf_cache(bfs, rval);
2093     if (bf_commitExists(bfs))
2094     {
2095         if (clean_only)
2096             zebra_set_state(zh, 'd', seqno);
2097         else
2098         {
2099             zebra_set_state(zh, 'c', seqno);
2100             
2101             yaz_log(log_level, "commit start");
2102             if (bf_commitExec(bfs))
2103                 res = ZEBRA_FAIL;
2104         }
2105         if (res == ZEBRA_OK)
2106         {
2107             seqno++;
2108             zebra_set_state(zh, 'o', seqno);
2109             
2110             zebra_unlock(zh->lock_shadow);
2111             zebra_unlock(zh->lock_normal);
2112             
2113             zebra_lock_w(zh->lock_shadow);
2114             bf_commitClean(bfs, rval);
2115             zebra_unlock(zh->lock_shadow);
2116         }
2117         else
2118         {
2119             zebra_unlock(zh->lock_shadow);
2120             zebra_unlock(zh->lock_normal);
2121             yaz_log(YLOG_WARN, "zebra_commit: failed");
2122         }
2123     }
2124     else
2125     {
2126         zebra_unlock(zh->lock_shadow);
2127         zebra_unlock(zh->lock_normal);
2128         yaz_log(log_level, "nothing to commit");
2129     }
2130     bfs_destroy(bfs);
2131
2132     return res;
2133 }
2134
2135 ZEBRA_RES zebra_clean(ZebraHandle zh)
2136 {
2137     yaz_log(log_level, "zebra_clean");
2138     ZEBRA_CHECK_HANDLE(zh);
2139     return zebra_commit_ex(zh, 1);
2140 }
2141
2142 ZEBRA_RES zebra_commit(ZebraHandle zh)
2143 {
2144     yaz_log(log_level, "zebra_commit");
2145     ZEBRA_CHECK_HANDLE(zh);
2146     return zebra_commit_ex(zh, 0);
2147 }
2148
2149
2150 ZEBRA_RES zebra_init(ZebraHandle zh)
2151 {
2152     const char *rval;
2153     BFiles bfs = 0;
2154
2155     yaz_log(log_level, "zebra_init");
2156
2157     ZEBRA_CHECK_HANDLE(zh);
2158
2159     zebra_select_default_database(zh);
2160     if (!zh->res)
2161     {
2162         zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
2163                        "cannot select default database");
2164         return ZEBRA_FAIL;
2165     }
2166     rval = res_get(zh->res, "shadow");
2167
2168     bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2169     if (!bfs)
2170     {
2171         zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "bfs_create");
2172         return ZEBRA_FAIL;
2173     }
2174     if (rval && *rval)
2175         bf_cache(bfs, rval);
2176     
2177     bf_reset(bfs);
2178     bfs_destroy(bfs);
2179     zebra_set_state(zh, 'o', 0);
2180     return ZEBRA_OK;
2181 }
2182
2183 ZEBRA_RES zebra_compact(ZebraHandle zh)
2184 {
2185     BFiles bfs;
2186
2187     yaz_log(log_level, "zebra_compact");
2188     ZEBRA_CHECK_HANDLE(zh);
2189     if (!zh->res)
2190     {
2191         zh->errCode = YAZ_BIB1_DATABASE_UNAVAILABLE;
2192         return ZEBRA_FAIL;
2193     }
2194     bfs = bfs_create(res_get(zh->res, "register"), zh->path_reg);
2195     inv_compact(bfs);
2196     bfs_destroy(bfs);
2197     return ZEBRA_OK;
2198 }
2199
2200 #define ZEBRA_CHECK_DICT 1
2201 #define ZEBRA_CHECK_ISAM 2
2202
2203 static ZEBRA_RES zebra_record_check(ZebraHandle zh, Record rec,
2204                                     zint *no_keys, int message_limit,
2205                                     unsigned flags,
2206                                     zint *no_long_dict_entries,
2207                                     zint *no_failed_dict_lookups,
2208                                     zint *no_invalid_keys,
2209                                     zint *no_invalid_dict_infos,
2210                                     zint *no_invalid_isam_entries)
2211 {
2212     ZEBRA_RES res = ZEBRA_OK;
2213     zebra_rec_keys_t keys = zebra_rec_keys_open();
2214     zebra_rec_keys_set_buf(keys, rec->info[recInfo_delKeys],
2215                            rec->size[recInfo_delKeys], 0);
2216     
2217     *no_keys = 0;
2218     if (!zebra_rec_keys_rewind(keys))
2219     {
2220         ;
2221     }
2222     else
2223     {
2224         size_t slen;
2225         const char *str;
2226         struct it_key key_in;
2227         NMEM nmem = nmem_create();
2228
2229         while (zebra_rec_keys_read(keys, &str, &slen, &key_in))
2230         {
2231             int do_fail = 0;
2232             int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
2233             char ord_buf[IT_MAX_WORD+20];
2234             int ord_len = key_SU_encode(ord, ord_buf);
2235             char *info = 0;
2236
2237             (*no_keys)++;
2238
2239             if (key_in.len < 2 || key_in.len > IT_KEY_LEVEL_MAX)
2240             {
2241                 res = ZEBRA_FAIL;
2242                 (*no_invalid_keys)++;
2243                 if (*no_invalid_keys <= message_limit)
2244                 {
2245                     do_fail = 1;
2246                     yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2247                             ": unexpected key length %d",
2248                             rec->sysno, key_in.len);
2249                 }
2250             }
2251             if (ord_len + slen >= sizeof(ord_buf)-1)
2252             {
2253                 res = ZEBRA_FAIL;
2254                 (*no_long_dict_entries)++;
2255                 if (*no_long_dict_entries <= message_limit)
2256                 {
2257                     do_fail = 1;
2258                     /* so bad it can not fit into our ord_buf */
2259                     yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2260                             ": long dictionary entry %d + %d",
2261                             rec->sysno, ord_len, (int) slen);
2262                 }
2263                 continue;
2264             }
2265             memcpy(ord_buf + ord_len, str, slen);
2266             ord_buf[ord_len + slen] = '\0'; 
2267             if (ord_len + slen >= IT_MAX_WORD)
2268             {
2269                 res = ZEBRA_FAIL;
2270                 (*no_long_dict_entries)++;
2271                 if (*no_long_dict_entries <= message_limit)
2272                 {
2273                     do_fail = 1;
2274                     yaz_log(YLOG_WARN, "Record " ZINT_FORMAT 
2275                             ": long dictionary entry %d + %d",
2276                             rec->sysno, (int) ord_len, (int) slen);
2277                 }
2278             }
2279             if ((flags & ZEBRA_CHECK_DICT) == 0)
2280                 continue;
2281             info = dict_lookup(zh->reg->dict, ord_buf);
2282             if (!info)
2283             {
2284                 res = ZEBRA_FAIL;
2285                 (*no_failed_dict_lookups)++;
2286                 if (*no_failed_dict_lookups <= message_limit)
2287                 {
2288                     do_fail = 1;
2289                     yaz_log(YLOG_WARN, "Record " ZINT_FORMAT
2290                             ": term do not exist in dictionary", rec->sysno);
2291                 }
2292             }
2293             else if (flags & ZEBRA_CHECK_ISAM)
2294             {
2295                 ISAM_P pos;
2296
2297                 if (*info != sizeof(pos))
2298                 {
2299                     res = ZEBRA_FAIL;
2300                     (*no_invalid_dict_infos)++;
2301                     if (*no_invalid_dict_infos <= message_limit)
2302                     {
2303                         do_fail = 1;
2304                         yaz_log(YLOG_WARN, "Record " ZINT_FORMAT 
2305                                 ": long dictionary entry %d + %d",
2306                                 rec->sysno, (int) ord_len, (int) slen);
2307                     }
2308                 }
2309                 else
2310                 {
2311                     int scope = 1;
2312                     memcpy(&pos, info+1, sizeof(pos));
2313                     if (zh->reg->isamb)
2314                     {
2315                         ISAMB_PP ispt = isamb_pp_open(zh->reg->isamb, pos,
2316                                                       scope);
2317                         if (!ispt)
2318                         {
2319                             res = ZEBRA_FAIL;
2320                             (*no_invalid_isam_entries)++;
2321                             if (*no_invalid_isam_entries <= message_limit)
2322                             {
2323                                 do_fail = 1;
2324                                 yaz_log(YLOG_WARN, "Record " ZINT_FORMAT 
2325                                         ": isamb_pp_open entry " ZINT_FORMAT
2326                                         " not found",
2327                                         rec->sysno, pos);
2328                             }
2329                         }
2330                         else if (zh->m_staticrank)
2331                         {
2332                             isamb_pp_close(ispt);
2333                         }
2334                         else
2335                         {
2336                             struct it_key until_key;
2337                             struct it_key isam_key;
2338                             int r;
2339                             int i = 0;
2340                             
2341                             until_key.len = key_in.len - 1;
2342                             for (i = 0; i < until_key.len; i++)
2343                                 until_key.mem[i] = key_in.mem[i+1];
2344                             
2345                             if (until_key.mem[0] == 0)
2346                                 until_key.mem[0] = rec->sysno;
2347                             r = isamb_pp_forward(ispt, &isam_key, &until_key);
2348                             if (r != 1)
2349                             {
2350                                 res = ZEBRA_FAIL;
2351                                 (*no_invalid_isam_entries)++;
2352                                 if (*no_invalid_isam_entries <= message_limit)
2353                                 {
2354                                     do_fail = 1;
2355                                     yaz_log(YLOG_WARN, "Record " ZINT_FORMAT 
2356                                             ": isamb_pp_forward " ZINT_FORMAT
2357                                             " returned no entry",
2358                                             rec->sysno, pos);
2359                                 }
2360                             }
2361                             else
2362                             {
2363                                 int cmp = key_compare(&until_key, &isam_key);
2364                                 if (cmp != 0)
2365                                 {
2366                                     res = ZEBRA_FAIL;
2367                                     (*no_invalid_isam_entries)++;
2368                                     if (*no_invalid_isam_entries
2369                                         <= message_limit)
2370                                     {
2371                                         do_fail = 1;
2372                                         yaz_log(YLOG_WARN, "Record "
2373                                                 ZINT_FORMAT 
2374                                                 ": isamb_pp_forward "
2375                                                 ZINT_FORMAT
2376                                                 " returned different entry",
2377                                                 rec->sysno, pos);
2378
2379                                         key_logdump_txt(YLOG_LOG,
2380                                                         &until_key,
2381                                                         "until");
2382
2383                                         key_logdump_txt(YLOG_LOG,
2384                                                         &isam_key,
2385                                                         "isam");
2386
2387                                     }
2388                                 }
2389                             }
2390                             isamb_pp_close(ispt);
2391                         }
2392
2393                     }
2394                 }
2395             }
2396             if (do_fail)
2397             {
2398                 zebra_it_key_str_dump(zh, &key_in, str,
2399                                       slen, nmem, YLOG_LOG);
2400                 nmem_reset(nmem);
2401             }
2402         }
2403         nmem_destroy(nmem);
2404     }
2405     zebra_rec_keys_close(keys);
2406     return res;
2407 }
2408
2409 ZEBRA_RES zebra_register_check(ZebraHandle zh, const char *spec)
2410 {
2411     ZEBRA_RES res = ZEBRA_FAIL;
2412     unsigned flags = 0;
2413     int message_limit = 10;
2414     
2415     if (!spec || *spec == '\0'
2416         || !strcmp(spec, "dict") || !strcmp(spec, "default"))
2417         flags = ZEBRA_CHECK_DICT;
2418     else if (!strcmp(spec, "isam") || !strcmp(spec, "full"))
2419         flags = ZEBRA_CHECK_DICT|ZEBRA_CHECK_ISAM;
2420     else if (!strcmp(spec, "quick"))
2421         flags = 0;
2422     else
2423         return ZEBRA_FAIL;
2424
2425     yaz_log(YLOG_LOG, "zebra_register_check begin flags=%u message_limit=%d",
2426             flags, message_limit);
2427     if (zebra_begin_read(zh) == ZEBRA_OK)
2428     {
2429         zint no_records_total = 0;
2430         zint no_records_fail = 0;
2431         zint total_keys = 0;
2432
2433         if (zh->reg)
2434         {
2435             Record rec = rec_get_root(zh->reg->records);
2436             
2437             zint no_long_dict_entries = 0;
2438             zint no_failed_dict_lookups = 0;
2439             zint no_invalid_keys = 0;
2440             zint no_invalid_dict_infos = 0;
2441             zint no_invalid_isam_entries = 0;
2442
2443             res = ZEBRA_OK;
2444             while (rec)
2445             {
2446                 Record r1;
2447                 zint no_keys;
2448
2449                 if (zebra_record_check(zh, rec, &no_keys, message_limit,
2450                                        flags,
2451                                        &no_long_dict_entries,
2452                                        &no_failed_dict_lookups,
2453                                        &no_invalid_keys,
2454                                        &no_invalid_dict_infos,
2455                                        &no_invalid_isam_entries
2456                         )
2457                     != ZEBRA_OK)
2458                 {
2459                     res = ZEBRA_FAIL;
2460                     no_records_fail++;
2461                 }
2462
2463                 r1 = rec_get_next(zh->reg->records, rec);
2464                 rec_free(&rec);
2465                 rec = r1;
2466                 no_records_total++;
2467                 total_keys += no_keys;
2468             }
2469             yaz_log(YLOG_LOG, "records total:        " ZINT_FORMAT,
2470                     no_records_total);
2471             yaz_log(YLOG_LOG, "records fail:         " ZINT_FORMAT,
2472                     no_records_fail);
2473             yaz_log(YLOG_LOG, "total keys:           " ZINT_FORMAT,
2474                     total_keys);
2475             yaz_log(YLOG_LOG, "long dict entries:    " ZINT_FORMAT,
2476                     no_long_dict_entries);
2477             if (flags & ZEBRA_CHECK_DICT)
2478             {
2479                 yaz_log(YLOG_LOG, "failed dict lookups:  " ZINT_FORMAT,
2480                         no_failed_dict_lookups);
2481                 yaz_log(YLOG_LOG, "invalid dict infos:   " ZINT_FORMAT,
2482                         no_invalid_dict_infos);
2483             }
2484             if (flags & ZEBRA_CHECK_ISAM)
2485                 yaz_log(YLOG_LOG, "invalid isam entries: " ZINT_FORMAT,
2486                         no_invalid_isam_entries);
2487         }
2488         zebra_end_read(zh);
2489     }
2490     yaz_log(YLOG_LOG, "zebra_register_check end ret=%d", res);
2491     return res;
2492 }
2493
2494 void zebra_result(ZebraHandle zh, int *code, char **addinfo)
2495 {
2496     yaz_log(log_level, "zebra_result");
2497     if (zh)
2498     {
2499         *code = zh->errCode;
2500         *addinfo = zh->errString;
2501     }
2502     else
2503     {
2504         *code = YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
2505         *addinfo ="ZebraHandle is NULL";
2506     }
2507 }
2508
2509 void zebra_shadow_enable(ZebraHandle zh, int value)
2510 {
2511     ASSERTZH;
2512     yaz_log(log_level, "zebra_shadow_enable");
2513     zh->shadow_enable = value;
2514 }
2515
2516 ZEBRA_RES zebra_octet_term_encoding(ZebraHandle zh, const char *encoding)
2517 {
2518     yaz_log(log_level, "zebra_octet_term_encoding %s", encoding);
2519     ZEBRA_CHECK_HANDLE(zh);
2520     assert(encoding);
2521
2522     if (zh->iconv_to_utf8 != 0)
2523         yaz_iconv_close(zh->iconv_to_utf8);
2524     if (zh->iconv_from_utf8 != 0)
2525         yaz_iconv_close(zh->iconv_from_utf8);
2526     
2527     zh->iconv_to_utf8 =
2528         yaz_iconv_open("UTF-8", encoding);
2529     if (zh->iconv_to_utf8 == 0)
2530         yaz_log(YLOG_WARN, "iconv: %s to UTF-8 unsupported", encoding);
2531     zh->iconv_from_utf8 =
2532         yaz_iconv_open(encoding, "UTF-8");
2533     if (zh->iconv_to_utf8 == 0)
2534         yaz_log(YLOG_WARN, "iconv: UTF-8 to %s unsupported", encoding);
2535
2536     return ZEBRA_OK;
2537 }
2538
2539 ZEBRA_RES zebra_record_encoding(ZebraHandle zh, const char *encoding)
2540 {
2541     yaz_log(log_level, "zebra_record_encoding");
2542     ZEBRA_CHECK_HANDLE(zh);
2543     xfree(zh->record_encoding);
2544     zh->record_encoding = 0;
2545     if (encoding)
2546         zh->record_encoding = xstrdup(encoding);
2547     return ZEBRA_OK;
2548 }
2549
2550 void zebra_set_resource(ZebraHandle zh, const char *name, const char *value)
2551 {
2552     assert(name);
2553     assert(value);
2554     yaz_log(log_level, "zebra_set_resource %s:%s", name, value);
2555     ASSERTZH;
2556     res_set(zh->res, name, value);
2557 }
2558
2559 const char *zebra_get_resource(ZebraHandle zh,
2560                                const char *name, const char *defaultvalue)
2561 {
2562     const char *v;
2563     ASSERTZH;
2564     assert(name);
2565     v = res_get_def(zh->res, name,(char *)defaultvalue);
2566     yaz_log(log_level, "zebra_get_resource %s:%s", name, v);
2567     return v;
2568 }
2569
2570 /* moved from zebra_api_ext.c by pop */
2571 /* FIXME: Should this really be public??? -Heikki */
2572
2573 int zebra_trans_no(ZebraHandle zh)
2574 {
2575     yaz_log(log_level, "zebra_trans_no");
2576     ASSERTZH;
2577     return zh->trans_no;
2578 }
2579
2580 int zebra_get_shadow_enable(ZebraHandle zh)
2581 {
2582     yaz_log(log_level, "zebra_get_shadow_enable");
2583     ASSERTZH;
2584     return zh->shadow_enable;
2585 }
2586
2587 void zebra_set_shadow_enable(ZebraHandle zh, int value)
2588 {
2589     yaz_log(log_level, "zebra_set_shadow_enable %d",value);
2590     ASSERTZH;
2591     zh->shadow_enable = value;
2592 }
2593
2594 ZEBRA_RES zebra_add_record(ZebraHandle zh,
2595                            const char *buf, int buf_size)
2596 {
2597     return zebra_update_record(zh, action_update, 
2598                                0 /* record type */,
2599                                0 /* sysno */ ,
2600                                0 /* match */, 
2601                                0 /* fname */,
2602                                buf, buf_size);
2603 }
2604
2605 ZEBRA_RES zebra_update_record(ZebraHandle zh, 
2606                               enum zebra_recctrl_action_t action,
2607                               const char *recordType,
2608                               zint *sysno, const char *match,
2609                               const char *fname,
2610                               const char *buf, int buf_size)
2611 {
2612     ZEBRA_RES res;
2613
2614     ZEBRA_CHECK_HANDLE(zh);
2615
2616     assert(buf);
2617
2618     yaz_log(log_level, "zebra_update_record");
2619     if (sysno)
2620         yaz_log(log_level, " sysno=" ZINT_FORMAT, *sysno);
2621
2622     if (buf_size < 1)
2623         buf_size = strlen(buf);
2624
2625     if (zebra_begin_trans(zh, 1) == ZEBRA_FAIL)
2626         return ZEBRA_FAIL;
2627     res = zebra_buffer_extract_record(zh, buf, buf_size, 
2628                                       action,
2629                                       recordType,
2630                                       sysno,   
2631                                       match, 
2632                                       fname);
2633     if (zebra_end_trans(zh) != ZEBRA_OK)
2634     {
2635         yaz_log(YLOG_WARN, "zebra_end_trans failed");
2636         res = ZEBRA_FAIL;
2637     }
2638     return res; 
2639 }
2640
2641 /* ---------------------------------------------------------------------------
2642    Searching 
2643 */
2644
2645 ZEBRA_RES zebra_search_PQF(ZebraHandle zh, const char *pqf_query,
2646                            const char *setname, zint *hits)
2647 {
2648     zint lhits = 0;
2649     ZEBRA_RES res = ZEBRA_OK;
2650     Z_RPNQuery *query;
2651     ODR odr;
2652
2653
2654     ZEBRA_CHECK_HANDLE(zh);
2655
2656     odr = odr_createmem(ODR_ENCODE);
2657
2658     assert(pqf_query);
2659     assert(setname);
2660
2661     yaz_log(log_level, "zebra_search_PQF s=%s q=%s", setname, pqf_query);
2662     
2663     query = p_query_rpn(odr, pqf_query);
2664     
2665     if (!query)
2666     {
2667         yaz_log(YLOG_WARN, "bad query %s\n", pqf_query);
2668         zh->errCode = YAZ_BIB1_MALFORMED_QUERY;
2669         res = ZEBRA_FAIL;
2670     }
2671     else
2672         res = zebra_search_RPN(zh, odr, query, setname, &lhits);
2673     
2674     odr_destroy(odr);
2675
2676     yaz_log(log_level, "Hits: " ZINT_FORMAT, lhits);
2677
2678     if (hits)
2679         *hits = lhits;
2680
2681     return res;
2682 }
2683
2684 /* ---------------------------------------------------------------------------
2685    Sort - a simplified interface, with optional read locks.
2686 */
2687 int zebra_sort_by_specstr(ZebraHandle zh, ODR stream,
2688                           const char *sort_spec,
2689                           const char *output_setname,
2690                           const char **input_setnames) 
2691 {
2692     int num_input_setnames = 0;
2693     int sort_status = 0;
2694     Z_SortKeySpecList *sort_sequence;
2695
2696     ZEBRA_CHECK_HANDLE(zh);
2697     assert(stream);
2698     assert(sort_spec);
2699     assert(output_setname);
2700     assert(input_setnames);
2701     sort_sequence = yaz_sort_spec(stream, sort_spec);
2702     yaz_log(log_level, "sort (FIXME) ");
2703     if (!sort_sequence)
2704     {
2705         yaz_log(YLOG_WARN, "invalid sort specs '%s'", sort_spec);
2706         zh->errCode = YAZ_BIB1_CANNOT_SORT_ACCORDING_TO_SEQUENCE;
2707         return -1;
2708     }
2709     
2710     /* we can do this, since the perl typemap code for char** will 
2711        put a NULL at the end of list */
2712     while (input_setnames[num_input_setnames]) num_input_setnames++;
2713
2714     if (zebra_begin_read(zh))
2715         return -1;
2716     
2717     resultSetSort(zh, stream->mem, num_input_setnames, input_setnames,
2718                   output_setname, sort_sequence, &sort_status);
2719     
2720     zebra_end_read(zh);
2721     return sort_status;
2722 }
2723
2724 /* ---------------------------------------------------------------------------
2725    Get BFS for Zebra system (to make alternative storage methods)
2726 */
2727 struct BFiles_struct *zebra_get_bfs(ZebraHandle zh)
2728 {
2729     if (zh && zh->reg)
2730         return zh->reg->bfs;
2731     return 0;
2732 }
2733
2734
2735 /* ---------------------------------------------------------------------------
2736    Set limit for search/scan
2737 */
2738 ZEBRA_RES zebra_set_limit(ZebraHandle zh, int complement_flag, zint *ids)
2739 {
2740     ZEBRA_CHECK_HANDLE(zh);
2741     zebra_limit_destroy(zh->m_limit);
2742     zh->m_limit = zebra_limit_create(complement_flag, ids);
2743     return ZEBRA_OK;
2744 }
2745
2746 /*
2747   Set Error code + addinfo
2748 */
2749 void zebra_setError(ZebraHandle zh, int code, const char *addinfo)
2750 {
2751     if (!zh)
2752         return;
2753     zh->errCode = code;
2754     nmem_reset(zh->nmem_error);
2755     zh->errString = addinfo ? nmem_strdup(zh->nmem_error, addinfo) : 0;
2756 }
2757
2758 void zebra_setError_zint(ZebraHandle zh, int code, zint i)
2759 {
2760     char vstr[60];
2761     sprintf(vstr, ZINT_FORMAT, i);
2762
2763     zh->errCode = code;
2764     nmem_reset(zh->nmem_error);
2765     zh->errString = nmem_strdup(zh->nmem_error, vstr);
2766 }
2767
2768 void zebra_lock_prefix(Res res, char *path)
2769 {
2770     const char *lock_dir = res_get_def(res, "lockDir", "");
2771     
2772     strcpy(path, lock_dir);
2773     if (*path && path[strlen(path)-1] != '/')
2774         strcat(path, "/");
2775 }
2776
2777 /*
2778  * Local variables:
2779  * c-basic-offset: 4
2780  * c-file-style: "Stroustrup"
2781  * indent-tabs-mode: nil
2782  * End:
2783  * vim: shiftwidth=4 tabstop=8 expandtab
2784  */
2785