1 /* $Id: limit.c,v 1.6 2005-11-29 09:37:04 adam Exp $
2 Copyright (C) 1995-2005
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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);
73 for (i = 0; zl->ids[i]; i++)
76 yaz_log(YLOG_LOG, " i=%d ids=" ZINT_FORMAT " mem=" ZINT_FORMAT,
77 i, zl->ids[i], key->mem[1]);
79 if (zl->ids[i] == key->mem[1])
82 yaz_log(YLOG_LOG, " match. Ret=%d", zl->complement_flag ? 0:1);
84 return zl->complement_flag ? 0 : 1;
88 yaz_log(YLOG_LOG, " no match. Ret=%d", zl->complement_flag ? 1:0);
90 return zl->complement_flag ? 1 : 0;
93 static void zebra_limit_destroy_cb(void *data)
95 zebra_limit_destroy(data);
98 void zebra_limit_for_rset(struct zebra_limit *zl,
99 int (**filter_func)(const void *buf, void *data),
100 void (**filter_destroy)(void *data),
103 #if ZEBRA_LIMIT_DEBUG
104 yaz_log(YLOG_LOG, "zebra_limit_for_rset debug enabled zl=%p", zl);
108 struct zebra_limit *hl;
110 #if ZEBRA_LIMIT_DEBUG
111 yaz_log(YLOG_LOG, "enable limit");
113 hl = zebra_limit_create(zl->complement_flag, zl->ids);
115 *filter_func = zebra_limit_filter_cb;
116 *filter_destroy = zebra_limit_destroy_cb;