-void extract_flushWriteKeys (ZebraHandle zh, int final)
- /* optimizing: if final=1, and no files written yet */
- /* push the keys directly to merge, sidestepping the */
- /* temp file altogether. Speeds small updates */
-{
- FILE *outf;
- char out_fname[200];
- char *prevcp, *cp;
- struct encode_info encode_info;
- int ptr_i = zh->reg->ptr_i;
- int temp_policy;
-#if SORT_EXTRA
- int i;
-#endif
- if (!zh->reg->key_buf || ptr_i <= 0)
- {
- yaz_log(log_level_extract, " nothing to flush section=%d buf=%p i=%d",
- zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
- return;
- }
-
- (zh->reg->key_file_no)++;
- yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
- yaz_log(log_level_extract, " sort_buff at %p n=%d",
- zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
-#if !SORT_EXTRA
- qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
- sizeof(char*), key_qsort_compare);
-
- /* zebra.cfg: tempfiles:
- Y: always use temp files (old way)
- A: use temp files, if more than one (auto)
- = if this is both the last and the first
- N: never bother with temp files (new) */
-
- temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
- if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
- yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
- temp_policy);
- temp_policy='A';
- }
-
- if ( ( temp_policy =='N' ) || /* always from memory */
- ( ( temp_policy =='A' ) && /* automatic */
- (zh->reg->key_file_no == 1) && /* this is first time */
- (final) ) ) /* and last (=only) time */
- { /* go directly from memory */
- zh->reg->key_file_no =0; /* signal not to read files */
- zebra_index_merge(zh);
- zh->reg->ptr_i = 0;
- zh->reg->key_buf_used = 0;
- return;
- }
-
- /* Not doing directly from memory, write into a temp file */
- extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
-
- if (!(outf = fopen (out_fname, "wb")))
- {
- yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
- zebra_exit("extract_flushWriteKeys");
- }
- yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
- prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
-
- encode_key_init (&encode_info);
- encode_key_write (cp, &encode_info, outf);
-
- while (--ptr_i > 0)
- {
- cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
- if (strcmp (cp, prevcp))
- {
- encode_key_flush ( &encode_info, outf);
- encode_key_init (&encode_info);
- encode_key_write (cp, &encode_info, outf);
- prevcp = cp;
- }
- else
- encode_key_write (cp + strlen(cp), &encode_info, outf);
- }
- encode_key_flush ( &encode_info, outf);
-#else
- qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
- extract_get_fname_tmp (out_fname, key_file_no);
-
- if (!(outf = fopen (out_fname, "wb")))
- {
- yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
- zebra_exit("extract_flushWriteKeys");
- }
- yaz_log (YLOG_LOG, "writing section %d", key_file_no);
- i = ptr_i;
- prevcp = key_buf[ptr_top-i];
- while (1)
- if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
- {
- key_y_len = strlen(prevcp)+1;
-#if 0
- yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
- key_y_len, prevcp[0], prevcp[1], 2+prevcp);
-#endif
- qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
- sizeof(char*), key_y_compare);
- cp = key_buf[ptr_top-ptr_i];
- --key_y_len;
- encode_key_init (&encode_info);
- encode_key_write (cp, &encode_info, outf);
- while (--ptr_i > i)
- {
- cp = key_buf[ptr_top-ptr_i];
- encode_key_write (cp+key_y_len, &encode_info, outf);
- }
- encode_key_flush ( &encode_info, outf);
- if (!i)
- break;
- prevcp = key_buf[ptr_top-ptr_i];
- }
-#endif
- if (fclose (outf))
- {
- yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
- zebra_exit("extract_flushWriteKeys");
- }
- yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
- zh->reg->ptr_i = 0;
- zh->reg->key_buf_used = 0;
-}