Fixes for limits - introduced in previos commit
[idzebra-moved-to-github.git] / index / limit.c
1 /* $Id: limit.c,v 1.3 2005-05-09 10:28:09 adam Exp $
2    Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
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 struct zebra_limit {
31     int complement_flag;
32     zint *ids;
33 };
34
35 void zebra_limit_destroy(struct zebra_limit *zl)
36 {
37     if (zl)
38     {
39         xfree(zl->ids);
40         xfree(zl);
41     }
42 }
43
44 struct zebra_limit *zebra_limit_create(int complement_flag, zint *ids)
45 {
46     struct zebra_limit *zl = 0;
47     size_t i;
48     if (ids)
49     {
50         for (i = 0; ids[i]; i++)
51             ;
52         zl = xmalloc(sizeof(*zl));
53         zl->ids = xmalloc((i+1) * sizeof(*ids));
54         memcpy(zl->ids, ids, (i+1) * sizeof(*ids));
55         zl->complement_flag = complement_flag;
56     }
57     return zl;
58 }
59
60 static int zebra_limit_filter_cb(const void *buf, void *data)
61 {
62     struct zebra_limit *zl = data;
63     const struct it_key *key = buf;
64     size_t i;
65
66     if (key->len != 3)
67         return 1;
68     for (i = 0; zl->ids[i]; i++)
69         if (zl->ids[i] == key->mem[1])
70             return zl->complement_flag ? 0 : 1;
71     return zl->complement_flag ? 1 : 0;
72 }
73
74 static void zebra_limit_destroy_cb(void *data)
75 {
76     zebra_limit_destroy(data);
77 }
78
79 void zebra_limit_for_rset(struct zebra_limit *zl,
80                           int (**filter_func)(const void *buf, void *data),
81                           void (**filter_destroy)(void *data),
82                           void **filter_data)
83 {
84     if (zl && zl->ids)
85     {
86         struct zebra_limit *hl;
87
88         hl = zebra_limit_create(zl->complement_flag, zl->ids);
89         *filter_data = hl;
90         *filter_func = zebra_limit_filter_cb;
91         *filter_destroy = zebra_limit_destroy_cb;
92     }
93     else
94     {
95         *filter_data = 0;
96         *filter_func = 0;
97         *filter_destroy = 0;
98     }
99 }
100