Beginnings of facets
[idzebra-moved-to-github.git] / index / rpnfacet.c
1 /* $Id: rpnfacet.c,v 1.2 2007-11-01 16:01:33 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, RSET rset)
43 {
44     int ord;
45     ZEBRA_RES res = zebra_attr_list_get_ord(zh,
46                                             zapt->attributes,
47                                             zinfo_index_category_sort,
48                                             0 /* index_type */,
49                                             attributeset, &ord);
50     if (res != ZEBRA_OK)
51         return res;
52     else
53     {
54         const char *index_type = 0;
55         const char *db = 0;
56         const char *string_index = 0;
57         /* for each ord .. */
58         /*   check that sort idx exist for ord */
59         /*   sweep through result set and sort_idx at the same time */
60         char *this_entry_buf = xmalloc(SORT_IDX_ENTRYSIZE);
61         char *dst_buf = xmalloc(SORT_IDX_ENTRYSIZE);
62         size_t sysno_mem_index = 0;
63         
64         zint p_this_sys = 0;
65         RSFD rfd;
66         TERMID termid;
67         struct it_key key;
68
69         if (zebraExplain_lookup_ord(zh->reg->zei,
70                                     ord, &index_type, &db, &string_index))
71         {
72             yaz_log(YLOG_WARN, "zebraExplain_lookup_ord failed");
73         }
74         
75         if (zh->m_staticrank)
76             sysno_mem_index = 1;
77         
78         rfd = rset_open(rset, RSETF_READ);
79         while (rset_read(rfd, &key, &termid))
80         {
81             zint sysno = key.mem[sysno_mem_index];
82             if (sysno != p_this_sys)
83             {
84                 p_this_sys = sysno;
85                 zebra_sort_sysno(zh->reg->sort_index, sysno);
86                 zebra_sort_type(zh->reg->sort_index, ord);
87                 zebra_sort_read(zh->reg->sort_index, this_entry_buf);
88
89                 zebra_term_untrans(zh, index_type, dst_buf, this_entry_buf);
90                 yaz_log(YLOG_LOG, "dst_buf=%s", dst_buf);
91             }
92         }
93         rset_close(rfd);
94         xfree(this_entry_buf);
95         xfree(dst_buf);
96         zebra_setError(zh, YAZ_BIB1_TEMPORARY_SYSTEM_ERROR, "facet not done");
97         return ZEBRA_FAIL;
98     }
99 }
100
101 /*
102  * Local variables:
103  * c-basic-offset: 4
104  * indent-tabs-mode: nil
105  * End:
106  * vim: shiftwidth=4 tabstop=8 expandtab
107  */
108