+ scan_info->list = (struct scan_info_entry *)
+ odr_malloc(stream, (before+after) * sizeof(*scan_info->list));
+ for (j = 0; j<before+after; j++)
+ scan_info->list[j].term = NULL;
+
+ prefix_len += key_SU_encode (ords[i], termz + prefix_len);
+#if REG_TYPE_PREFIX
+ termz[prefix_len++] = reg_id;
+#endif
+ termz[prefix_len] = 0;
+ strcpy(scan_info->prefix, termz);
+
+ if (trans_scan_term(zh, zapt, termz+prefix_len, reg_id) == ZEBRA_FAIL)
+ return ZEBRA_FAIL;
+
+ dict_scan(zh->reg->dict, termz, &before_tmp, &after_tmp,
+ scan_info, scan_handle);
+ }
+ glist = (ZebraScanEntry *)
+ odr_malloc(stream, (before+after)*sizeof(*glist));
+
+ rset_nmem = nmem_create();
+ kc = zebra_key_control_create(zh);
+
+ /* consider terms after main term */
+ for (i = 0; i < ord_no; i++)
+ ptr[i] = before;
+
+ *is_partial = 0;
+ for (i = 0; i<after; i++)
+ {
+ int j, j0 = -1;
+ const char *mterm = NULL;
+ const char *tst;
+ RSET rset = 0;
+ int lo = i + pos-1; /* offset in result list */
+
+ /* find: j0 is the first of the minimal values */
+ for (j = 0; j < ord_no; j++)
+ {
+ if (ptr[j] < before+after && ptr[j] >= 0 &&
+ (tst = scan_info_array[j].list[ptr[j]].term) &&
+ (!mterm || strcmp (tst, mterm) < 0))
+ {
+ j0 = j;
+ mterm = tst;
+ }
+ }
+ if (j0 == -1)
+ break; /* no value found, stop */
+
+ /* get result set for first one , but only if it's within bounds */
+ if (lo >= 0)
+ {
+ /* get result set for first term */
+ zebra_term_untrans_iconv(zh, stream->mem, reg_id,
+ &glist[lo].term, mterm);
+ rset = rset_trunc(zh, &scan_info_array[j0].list[ptr[j0]].isam_p, 1,
+ glist[lo].term, strlen(glist[lo].term),
+ NULL, 0, zapt->term->which, rset_nmem,
+ kc, kc->scope, 0, reg_id, 0 /* hits_limit */,
+ 0 /* term_ref_id_str */);
+ }
+ ptr[j0]++; /* move index for this set .. */
+ /* get result set for remaining scan terms */
+ for (j = j0+1; j<ord_no; j++)
+ {
+ if (ptr[j] < before+after && ptr[j] >= 0 &&
+ (tst = scan_info_array[j].list[ptr[j]].term) &&
+ !strcmp (tst, mterm))
+ {
+ if (lo >= 0)
+ {
+ RSET rsets[2];
+
+ rsets[0] = rset;
+ rsets[1] =
+ rset_trunc(
+ zh, &scan_info_array[j].list[ptr[j]].isam_p, 1,
+ glist[lo].term,
+ strlen(glist[lo].term), NULL, 0,
+ zapt->term->which,rset_nmem,
+ kc, kc->scope, 0, reg_id, 0 /* hits_limit */,
+ 0 /* term_ref_id_str */ );
+ rset = rsmulti_or_create(rset_nmem, kc,
+ kc->scope, 0 /* termid */,
+ 2, rsets);
+ }
+ ptr[j]++;
+ }
+ }
+ if (lo >= 0)
+ {
+ zint count;
+ /* merge with limit_set if given */
+ if (limit_set)
+ {
+ RSET rsets[2];
+ rsets[0] = rset;
+ rsets[1] = rset_dup(limit_set);
+
+ rset = rsmulti_and_create(rset_nmem, kc,
+ kc->scope,
+ 2, rsets);
+ }
+ /* count it */
+ count_set(zh, rset, &count);
+ glist[lo].occurrences = count;
+ rset_delete(rset);
+ }
+ }
+ if (i < after)
+ {
+ *num_entries -= (after-i);
+ *is_partial = 1;
+ if (*num_entries < 0)
+ {
+ (*kc->dec)(kc);
+ nmem_destroy(rset_nmem);
+ *num_entries = 0;
+ return ZEBRA_OK;
+ }
+ }
+ /* consider terms before main term */
+ for (i = 0; i<ord_no; i++)
+ ptr[i] = 0;
+
+ for (i = 0; i<before; i++)
+ {
+ int j, j0 = -1;
+ const char *mterm = NULL;
+ const char *tst;
+ RSET rset;
+ int lo = before-1-i; /* offset in result list */
+ zint count;
+
+ for (j = 0; j <ord_no; j++)
+ {
+ if (ptr[j] < before && ptr[j] >= 0 &&
+ (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
+ (!mterm || strcmp (tst, mterm) > 0))
+ {
+ j0 = j;
+ mterm = tst;
+ }
+ }
+ if (j0 == -1)
+ break;
+
+ zebra_term_untrans_iconv(zh, stream->mem, reg_id,
+ &glist[lo].term, mterm);
+
+ rset = rset_trunc
+ (zh, &scan_info_array[j0].list[before-1-ptr[j0]].isam_p, 1,
+ glist[lo].term, strlen(glist[lo].term),
+ NULL, 0, zapt->term->which, rset_nmem,
+ kc, kc->scope, 0, reg_id, 0 /* hits_limit */,
+ 0 /* term_ref_id_str */);
+
+ ptr[j0]++;
+
+ for (j = j0+1; j<ord_no; j++)
+ {
+ if (ptr[j] < before && ptr[j] >= 0 &&
+ (tst = scan_info_array[j].list[before-1-ptr[j]].term) &&
+ !strcmp (tst, mterm))
+ {
+ RSET rsets[2];
+
+ rsets[0] = rset;
+ rsets[1] = rset_trunc(
+ zh,
+ &scan_info_array[j].list[before-1-ptr[j]].isam_p, 1,
+ glist[lo].term,
+ strlen(glist[lo].term), NULL, 0,
+ zapt->term->which, rset_nmem,
+ kc, kc->scope, 0, reg_id, 0 /* hits_limit */,
+ 0 /* term_ref_id_str */);
+ rset = rsmulti_or_create(rset_nmem, kc,
+ kc->scope, 0 /* termid */, 2, rsets);
+
+ ptr[j]++;
+ }
+ }
+ if (limit_set)
+ {
+ RSET rsets[2];
+ rsets[0] = rset;
+ rsets[1] = rset_dup(limit_set);
+
+ rset = rsmulti_and_create(rset_nmem, kc,
+ kc->scope, 2, rsets);
+ }
+ count_set(zh, rset, &count);
+ glist[lo].occurrences = count;
+ rset_delete (rset);
+ }
+ (*kc->dec)(kc);
+ nmem_destroy(rset_nmem);
+ i = before-i;