2bebbfd8bc41d8ab11bdb902c3ad165256a86f8a
[idzebra-moved-to-github.git] / index / rpnfacet.c
1 /* $Id: rpnfacet.c,v 1.3 2007-11-05 11:20:39 adam Exp $
2    Copyright (C) 1995-2007
3    Index Data ApS
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #if HAVE_UNISTD_H
26 #include <unistd.h>
27 #endif
28 #include <ctype.h>
29
30 #include <yaz/diagbib1.h>
31 #include "index.h"
32 #include <zebra_xpath.h>
33 #include <yaz/wrbuf.h>
34 #include <attrfind.h>
35 #include <charmap.h>
36 #include <rset.h>
37 #include <yaz/oid_db.h>
38
39 ZEBRA_RES rpn_facet(ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
40                     const Odr_oid *attributeset,
41                     int *position, int *num_entries, 
42                     ZebraScanEntry **list, int *is_partial, 
43                     const char *set_name)
44 {
45     int ord;
46     int use_sort_idx = 1;
47     ZEBRA_RES res = zebra_attr_list_get_ord(zh,
48                                             zapt->attributes,
49                                             zinfo_index_category_sort,
50                                             0 /* index_type */,
51                                             attributeset, &ord);
52     if (res != ZEBRA_OK)
53         return res;
54     else if (use_sort_idx)
55     {
56         const char *index_type = 0;
57         const char *db = 0;
58         const char *string_index = 0;
59         /* for each ord .. */
60         /*   check that sort idx exist for ord */
61         /*   sweep through result set and sort_idx at the same time */
62         char *this_entry_buf = xmalloc(SORT_IDX_ENTRYSIZE);
63         char *dst_buf = xmalloc(SORT_IDX_ENTRYSIZE);
64         size_t sysno_mem_index = 0;
65         RSET rset = resultSetRef(zh, set_name);
66         zint p_this_sys = 0;
67         RSFD rfd;
68         TERMID termid;
69         struct it_key key;
70
71         if (zebraExplain_lookup_ord(zh->reg->zei,
72                                     ord, &index_type, &db, &string_index))
73         {
74             yaz_log(YLOG_WARN, "zebraExplain_lookup_ord failed");
75         }
76         
77         if (zh->m_staticrank)
78             sysno_mem_index = 1;
79         
80         rfd = rset_open(rset, RSETF_READ);
81         while (rset_read(rfd, &key, &termid))
82         {
83             zint sysno = key.mem[sysno_mem_index];
84             if (sysno != p_this_sys)
85             {
86                 p_this_sys = sysno;
87                 zebra_sort_sysno(zh->reg->sort_index, sysno);
88                 zebra_sort_type(zh->reg->sort_index, ord);
89                 zebra_sort_read(zh->reg->sort_index, this_entry_buf);
90
91                 zebra_term_untrans(zh, index_type, dst_buf, this_entry_buf);
92                 yaz_log(YLOG_LOG, "dst_buf=%s", dst_buf);
93             }
94         }
95         rset_close(rfd);
96         xfree(this_entry_buf);
97         xfree(dst_buf);
98         zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "facet not done1");
99         return ZEBRA_FAIL;
100     }
101     else
102     {
103         int num = 100; /* to be customizable */
104         int i;
105
106         ZebraMetaRecord *meta = zebra_meta_records_create_range(
107             zh, set_name, 0, num);
108
109         for (i = 0; i < num; i++)
110         {
111             zint sysno = meta[i].sysno;
112             Record rec = rec_get(zh->reg->records, sysno);
113             if (!rec)
114             {
115                 yaz_log(YLOG_WARN, "rec_get fail on sysno=" ZINT_FORMAT,
116                         sysno);
117                 break;
118             }
119             else
120             {
121                 
122
123                 rec_free(&rec);
124             }
125         }
126         zebra_meta_records_destroy(zh, meta, num);
127         zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "facet not done2");
128         return ZEBRA_FAIL;
129     }
130 }
131
132 /*
133  * Local variables:
134  * c-basic-offset: 4
135  * indent-tabs-mode: nil
136  * End:
137  * vim: shiftwidth=4 tabstop=8 expandtab
138  */
139