X-Git-Url: http://git.indexdata.com/?p=idzebra-moved-to-github.git;a=blobdiff_plain;f=index%2Freckeys.c;h=39a3f4a3344cc5b3bc0a6aa11f7bfd3aa049623c;hp=174e8954b682335c27c04b6e3a8a2adb714ef2b5;hb=ba0720e26f508ba3396e232d2f82037c0e701698;hpb=8a57ec471c6e6fc6979dfad0415a5665fdb43320 diff --git a/index/reckeys.c b/index/reckeys.c index 174e895..39a3f4a 100644 --- a/index/reckeys.c +++ b/index/reckeys.c @@ -1,8 +1,5 @@ -/* $Id: reckeys.c,v 1.2 2005-11-09 08:27:28 adam Exp $ - Copyright (C) 1995-2005 - Index Data ApS - -This file is part of the Zebra server. +/* This file is part of the Zebra server. + Copyright (C) 1994-2011 Index Data Zebra is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free @@ -15,18 +12,30 @@ FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License -along with Zebra; see the file LICENSE.zebra. If not, write to the -Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA -02111-1307, USA. +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ +#if HAVE_CONFIG_H +#include +#endif #include #include +#include #include #include -#include "index.h" #include "reckeys.h" +#include +#include + +struct zebra_rec_key_entry { + char *buf; + size_t len; + struct it_key key; + struct zebra_rec_key_entry *next; +}; struct zebra_rec_keys_t_ { size_t buf_used; @@ -36,9 +45,47 @@ struct zebra_rec_keys_t_ { void *encode_handle; void *decode_handle; char owner_of_buffer; + zint custom_record_id; + + NMEM nmem; + size_t hash_size; + struct zebra_rec_key_entry **entries; }; -zebra_rec_keys_t zebra_rec_keys_open() + +struct zebra_rec_key_entry **zebra_rec_keys_mk_hash(zebra_rec_keys_t p, + const char *buf, + size_t len, + const struct it_key *key) +{ + unsigned h = 0; + size_t i; + int j; +#if 0 + h = key->mem[key->len-1]; +#else + for (i = 0; ilen; j++) + h = h * 65509 + CAST_ZINT_TO_INT(key->mem[j]); +#endif + return &p->entries[h % (unsigned) p->hash_size]; +} + +static void init_hash(zebra_rec_keys_t p) +{ + p->entries = 0; + nmem_reset(p->nmem); + if (p->hash_size) + { + size_t i; + p->entries = nmem_malloc(p->nmem, p->hash_size * sizeof(*p->entries)); + for (i = 0; ihash_size; i++) + p->entries[i] = 0; + } +} + +zebra_rec_keys_t zebra_rec_keys_open(void) { zebra_rec_keys_t p = xmalloc(sizeof(*p)); p->buf_used = 0; @@ -48,9 +95,17 @@ zebra_rec_keys_t zebra_rec_keys_open() p->owner_of_buffer = 1; p->encode_handle = iscz1_start(); p->decode_handle = iscz1_start(); + + p->custom_record_id = 0; + p->nmem = nmem_create(); + p->hash_size = 32767; + p->entries = 0; + + init_hash(p); + return p; } - + void zebra_rec_keys_set_buf(zebra_rec_keys_t p, char *buf, size_t sz, int copy_buf) { @@ -96,11 +151,41 @@ void zebra_rec_keys_close(zebra_rec_keys_t p) iscz1_stop(p->encode_handle); if (p->decode_handle) iscz1_stop(p->decode_handle); + nmem_destroy(p->nmem); xfree(p); } +int zebra_rec_keys_add_hash(zebra_rec_keys_t keys, + const char *str, size_t slen, + const struct it_key *key) +{ + struct zebra_rec_key_entry **kep_first + = zebra_rec_keys_mk_hash(keys, str, slen, key); + struct zebra_rec_key_entry **kep = kep_first; + while (*kep) + { + struct zebra_rec_key_entry *e = *kep; + if (slen == e->len && !memcmp(str, e->buf, slen) && + !key_compare(key, &e->key)) + { + *kep = (*kep)->next; /* out of queue */ + e->next = *kep_first; /* move to front */ + *kep_first = e; + + return 0; + } + kep = &(*kep)->next; + } + *kep = nmem_malloc(keys->nmem, sizeof(**kep)); + (*kep)->next = 0; + (*kep)->len = slen; + memcpy(&(*kep)->key, key, sizeof(*key)); + (*kep)->buf = nmem_malloc(keys->nmem, slen); + memcpy((*kep)->buf, str, slen); + return 1; +} + void zebra_rec_keys_write(zebra_rec_keys_t keys, - int reg_type, const char *str, size_t slen, const struct it_key *key) { @@ -109,6 +194,21 @@ void zebra_rec_keys_write(zebra_rec_keys_t keys, assert(keys->owner_of_buffer); + if (key->mem[1]) /* record_id custom */ + { + keys->custom_record_id = key->mem[1]; + } +#if 1 + if (!zebra_rec_keys_add_hash(keys, str, slen, key)) + { +#if 0 + yaz_log(YLOG_LOG, "dup key slen=%d %.*s " + "ord=" ZINT_FORMAT " seq=" ZINT_FORMAT, + slen, slen, str, key->mem[0], key->mem[key->len-1]); +#endif + return; /* key already there . Omit it */ + } +#endif if (keys->buf_used+1024 > keys->buf_max) { char *b = (char *) xmalloc (keys->buf_max += 128000); @@ -121,9 +221,6 @@ void zebra_rec_keys_write(zebra_rec_keys_t keys, iscz1_encode(keys->encode_handle, &dst, &src); -#if REG_TYPE_PREFIX - *dst++ = reg_type; -#endif memcpy (dst, str, slen); dst += slen; *dst++ = '\0'; @@ -136,12 +233,16 @@ void zebra_rec_keys_reset(zebra_rec_keys_t keys) keys->buf_used = 0; iscz1_reset(keys->encode_handle); + + init_hash(keys); } int zebra_rec_keys_rewind(zebra_rec_keys_t keys) { assert(keys); iscz1_reset(keys->decode_handle); + + keys->fetch_offset = 0; if (keys->buf_used == 0) return 0; @@ -181,3 +282,18 @@ int zebra_rec_keys_read(zebra_rec_keys_t keys, } return 1; } + +zint zebra_rec_keys_get_custom_record_id(zebra_rec_keys_t keys) +{ + return keys->custom_record_id; +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ +