Break long lines in debian/control
[idzebra-moved-to-github.git] / index / limit.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 <stdio.h>
24 #include <assert.h>
25
26 #include <yaz/xmalloc.h>
27 #include <yaz/diagbib1.h>
28 #include "index.h"
29
30 #define ZEBRA_LIMIT_DEBUG 0
31
32 struct zebra_limit {
33     int complement_flag;
34     zint *ids;
35 };
36
37 void zebra_limit_destroy(struct zebra_limit *zl)
38 {
39     if (zl)
40     {
41         xfree(zl->ids);
42         xfree(zl);
43     }
44 }
45
46 struct zebra_limit *zebra_limit_create(int complement_flag, zint *ids)
47 {
48     struct zebra_limit *zl = 0;
49     size_t i;
50     if (ids)
51     {
52         for (i = 0; ids[i]; i++)
53             ;
54         zl = xmalloc(sizeof(*zl));
55         zl->ids = xmalloc((i+1) * sizeof(*ids));
56         memcpy(zl->ids, ids, (i+1) * sizeof(*ids));
57         zl->complement_flag = complement_flag;
58     }
59     return zl;
60 }
61
62 static int zebra_limit_filter_cb(const void *buf, void *data)
63 {
64     struct zebra_limit *zl = data;
65     const struct it_key *key = buf;
66     size_t i;
67
68 #if ZEBRA_LIMIT_DEBUG
69     yaz_log(YLOG_LOG, "zebra_limit_filter_cb zl=%p key->len=%d", zl, key->len);
70 #endif
71     for (i = 0; zl->ids[i]; i++)
72     {
73 #if ZEBRA_LIMIT_DEBUG
74         yaz_log(YLOG_LOG, " i=%d ids=" ZINT_FORMAT " mem=" ZINT_FORMAT,
75                 i, zl->ids[i], key->mem[1]);
76 #endif
77         if (zl->ids[i] == key->mem[1])
78         {
79 #if ZEBRA_LIMIT_DEBUG
80             yaz_log(YLOG_LOG, " match. Ret=%d", zl->complement_flag ? 0:1);
81 #endif
82             return zl->complement_flag ? 0 : 1;
83         }
84     }
85 #if ZEBRA_LIMIT_DEBUG
86     yaz_log(YLOG_LOG, " no match. Ret=%d", zl->complement_flag ? 1:0);
87 #endif
88     return zl->complement_flag ? 1 : 0;
89 }
90
91 static void zebra_limit_destroy_cb(void *data)
92 {
93     zebra_limit_destroy(data);
94 }
95
96 void zebra_limit_for_rset(struct zebra_limit *zl,
97                           int (**filter_func)(const void *buf, void *data),
98                           void (**filter_destroy)(void *data),
99                           void **filter_data)
100 {
101 #if ZEBRA_LIMIT_DEBUG
102     yaz_log(YLOG_LOG, "zebra_limit_for_rset debug enabled zl=%p", zl);
103 #endif
104     if (zl && zl->ids)
105     {
106         struct zebra_limit *hl;
107
108 #if ZEBRA_LIMIT_DEBUG
109         yaz_log(YLOG_LOG, "enable limit");
110 #endif
111         hl = zebra_limit_create(zl->complement_flag, zl->ids);
112         *filter_data = hl;
113         *filter_func = zebra_limit_filter_cb;
114         *filter_destroy = zebra_limit_destroy_cb;
115     }
116     else
117     {
118         *filter_data = 0;
119         *filter_func = 0;
120         *filter_destroy = 0;
121     }
122 }
123                           
124 /*
125  * Local variables:
126  * c-basic-offset: 4
127  * c-file-style: "Stroustrup"
128  * indent-tabs-mode: nil
129  * End:
130  * vim: shiftwidth=4 tabstop=8 expandtab
131  */
132