IDZEBRA  2.2.7
limit.c
Go to the documentation of this file.
1 /* This file is part of the Zebra server.
2  Copyright (C) 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 {
35 };
36 
38 {
39  if (zl)
40  {
41  xfree(zl->ids);
42  xfree(zl);
43  }
44 }
45 
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));
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 
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;
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 
static int zebra_limit_filter_cb(const void *buf, void *data)
Definition: limit.c:62
void zebra_limit_for_rset(struct zebra_limit *zl, int(**filter_func)(const void *buf, void *data), void(**filter_destroy)(void *data), void **filter_data)
Definition: limit.c:96
static void zebra_limit_destroy_cb(void *data)
Definition: limit.c:91
struct zebra_limit * zebra_limit_create(int complement_flag, zint *ids)
Definition: limit.c:46
void zebra_limit_destroy(struct zebra_limit *zl)
Definition: limit.c:37
static void filter_destroy(void *clientData)
Definition: mod_alvis.c:333
Definition: it_key.h:30
int len
Definition: it_key.h:31
zint mem[IT_KEY_LEVEL_MAX]
Definition: it_key.h:32
zint * ids
Definition: limit.c:34
int complement_flag
Definition: limit.c:33
long zint
Zebra integer.
Definition: util.h:66
#define ZINT_FORMAT
Definition: util.h:72