1 /* $Id: limit.c,v 1.10 2007-01-15 15:10:16 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
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
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
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
26 #include <yaz/xmalloc.h>
27 #include <yaz/diagbib1.h>
30 #define ZEBRA_LIMIT_DEBUG 0
37 void zebra_limit_destroy(struct zebra_limit *zl)
46 struct zebra_limit *zebra_limit_create(int complement_flag, zint *ids)
48 struct zebra_limit *zl = 0;
52 for (i = 0; ids[i]; i++)
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;
62 static int zebra_limit_filter_cb(const void *buf, void *data)
64 struct zebra_limit *zl = data;
65 const struct it_key *key = buf;
69 yaz_log(YLOG_LOG, "zebra_limit_filter_cb zl=%p key->len=%d", zl, key->len);
71 for (i = 0; zl->ids[i]; i++)
74 yaz_log(YLOG_LOG, " i=%d ids=" ZINT_FORMAT " mem=" ZINT_FORMAT,
75 i, zl->ids[i], key->mem[1]);
77 if (zl->ids[i] == key->mem[1])
80 yaz_log(YLOG_LOG, " match. Ret=%d", zl->complement_flag ? 0:1);
82 return zl->complement_flag ? 0 : 1;
86 yaz_log(YLOG_LOG, " no match. Ret=%d", zl->complement_flag ? 1:0);
88 return zl->complement_flag ? 1 : 0;
91 static void zebra_limit_destroy_cb(void *data)
93 zebra_limit_destroy(data);
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),
101 #if ZEBRA_LIMIT_DEBUG
102 yaz_log(YLOG_LOG, "zebra_limit_for_rset debug enabled zl=%p", zl);
106 struct zebra_limit *hl;
108 #if ZEBRA_LIMIT_DEBUG
109 yaz_log(YLOG_LOG, "enable limit");
111 hl = zebra_limit_create(zl->complement_flag, zl->ids);
113 *filter_func = zebra_limit_filter_cb;
114 *filter_destroy = zebra_limit_destroy_cb;
127 * indent-tabs-mode: nil
129 * vim: shiftwidth=4 tabstop=8 expandtab