X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=index%2Frecindex.c;h=8de2ca8f82f5cd07ad2fc35c1993529677cdd018;hb=8896080000b8d31751ca253b59aaae425ebd9e48;hp=a888c85143e62486dd41321d9d3568f5e9275871;hpb=cdcde0c689ec4b66f1884dbd455284b7a35fb425;p=idzebra-moved-to-github.git diff --git a/index/recindex.c b/index/recindex.c index a888c85..8de2ca8 100644 --- a/index/recindex.c +++ b/index/recindex.c @@ -1,5 +1,5 @@ -/* $Id: recindex.c,v 1.49 2006-05-10 12:30:02 adam Exp $ - Copyright (C) 1995-2006 +/* $Id: recindex.c,v 1.57 2007-11-23 13:11:08 adam Exp $ + Copyright (C) 1995-2007 Index Data ApS This file is part of the Zebra server. @@ -15,9 +15,9 @@ 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 + */ #define RIDX_CHUNK 128 @@ -61,26 +61,36 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #define USUAL_RANGE 2000000000LL #endif -static SYSNO rec_sysno_to_ext(SYSNO sysno) +static zint rec_sysno_to_ext(zint sysno) { assert(sysno >= 0 && sysno <= USUAL_RANGE); return sysno + FAKE_OFFSET; } -SYSNO rec_sysno_to_int(SYSNO sysno) +zint rec_sysno_to_int(zint sysno) { assert(sysno >= FAKE_OFFSET && sysno <= FAKE_OFFSET + USUAL_RANGE); return sysno - FAKE_OFFSET; } -static ZEBRA_RES rec_write_head(Records p) +static int rec_read_head(recindex_t p, void *buf) +{ + return bf_read(p->index_BFile, 0, 0, 0, buf); +} + +static const char *recindex_get_fname(recindex_t p) +{ + return p->index_fname; +} + +static ZEBRA_RES rec_write_head(recindex_t p, const void *buf, size_t len) { int r; assert(p); assert(p->index_BFile); - r = bf_write(p->index_BFile, 0, 0, sizeof(p->head), &p->head); + r = bf_write(p->index_BFile, 0, 0, len, buf); if (r) { yaz_log(YLOG_FATAL|YLOG_ERRNO, "write head of %s", p->index_fname); @@ -101,12 +111,12 @@ static void rec_tmp_expand(Records p, int size) } } -static int read_indx(Records p, SYSNO sysno, void *buf, int itemsize, - int ignoreError) +static int read_indx(recindex_t p, zint sysno, void *buf, int itemsize, + int ignoreError) { int r; zint pos = (sysno-1)*itemsize; - int off = (int) (pos%RIDX_CHUNK); + int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK); int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */ if (sz1 > itemsize) @@ -124,10 +134,10 @@ static int read_indx(Records p, SYSNO sysno, void *buf, int itemsize, return r; } -static void write_indx(Records p, SYSNO sysno, void *buf, int itemsize) +static void write_indx(recindex_t p, zint sysno, void *buf, int itemsize) { zint pos = (sysno-1)*itemsize; - int off = (int) (pos%RIDX_CHUNK); + int off = CAST_ZINT_TO_INT(pos%RIDX_CHUNK); int sz1 = RIDX_CHUNK - off; /* sz1 is size of buffer to read.. */ if (sz1 > itemsize) @@ -139,7 +149,7 @@ static void write_indx(Records p, SYSNO sysno, void *buf, int itemsize) (char*) buf + sz1); } -static ZEBRA_RES rec_release_blocks(Records p, SYSNO sysno) +static ZEBRA_RES rec_release_blocks(Records p, zint sysno) { struct record_index_entry entry; zint freeblock; @@ -147,12 +157,12 @@ static ZEBRA_RES rec_release_blocks(Records p, SYSNO sysno) int dst_type; int first = 1; - if (read_indx(p, sysno, &entry, sizeof(entry), 1) != 1) + if (read_indx(p->recindex, sysno, &entry, sizeof(entry), 1) != 1) return ZEBRA_FAIL; freeblock = entry.next; assert(freeblock > 0); - dst_type = (int) (freeblock & 7); + dst_type = CAST_ZINT_TO_INT(freeblock & 7); assert(dst_type < REC_BLOCK_TYPES); freeblock = freeblock / 8; while (freeblock) @@ -202,17 +212,19 @@ static ZEBRA_RES rec_delete_single(Records p, Record rec) { struct record_index_entry entry; + /* all data in entry must be reset, since it's written verbatim */ + memset(&entry, '\0', sizeof(entry)); if (rec_release_blocks(p, rec_sysno_to_int(rec->sysno)) != ZEBRA_OK) return ZEBRA_FAIL; entry.next = p->head.index_free; entry.size = 0; p->head.index_free = rec_sysno_to_int(rec->sysno); - write_indx(p, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry)); + write_indx(p->recindex, rec_sysno_to_int(rec->sysno), &entry, sizeof(entry)); return ZEBRA_OK; } -static ZEBRA_RES rec_write_tmp_buf(Records p, int size, SYSNO *sysnos) +static ZEBRA_RES rec_write_tmp_buf(Records p, int size, zint *sysnos) { struct record_index_entry entry; int no_written = 0; @@ -221,6 +233,9 @@ static ZEBRA_RES rec_write_tmp_buf(Records p, int size, SYSNO *sysnos) int dst_type = 0; int i; + /* all data in entry must be reset, since it's written verbatim */ + memset(&entry, '\0', sizeof(entry)); + for (i = 1; i= p->head.block_move[i]) dst_type = i; @@ -248,7 +263,7 @@ static ZEBRA_RES rec_write_tmp_buf(Records p, int size, SYSNO *sysnos) p->head.total_bytes += size; while (*sysnos > 0) { - write_indx(p, *sysnos, &entry, sizeof(entry)); + write_indx(p->recindex, *sysnos, &entry, sizeof(entry)); sysnos++; } } @@ -259,7 +274,8 @@ static ZEBRA_RES rec_write_tmp_buf(Records p, int size, SYSNO *sysnos) cptr = p->tmp_buf + no_written; } block_prev = block_free; - no_written += (int)(p->head.block_size[dst_type]) - sizeof(zint); + no_written += CAST_ZINT_TO_INT(p->head.block_size[dst_type]) + - sizeof(zint); p->head.block_used[dst_type]++; } assert(block_prev != -1); @@ -270,6 +286,31 @@ static ZEBRA_RES rec_write_tmp_buf(Records p, int size, SYSNO *sysnos) return ZEBRA_OK; } +recindex_t recindex_open(BFiles bfs, int rw) +{ + recindex_t p = xmalloc(sizeof(*p)); + p->index_fname = "reci"; + p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw); + if (p->index_BFile == NULL) + { + yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname); + xfree(p); + return 0; + } + return p; +} + +void recindex_close(recindex_t p) +{ + if (p) + { + if (p->index_BFile) + bf_close(p->index_BFile); + xfree(p); + } +} + + Records rec_open(BFiles bfs, int rw, int compression_method) { Records p; @@ -278,19 +319,13 @@ Records rec_open(BFiles bfs, int rw, int compression_method) ZEBRA_RES ret = ZEBRA_OK; p = (Records) xmalloc(sizeof(*p)); + memset(&p->head, '\0', sizeof(p->head)); p->compression_method = compression_method; p->rw = rw; p->tmp_size = 1024; - p->index_fname = "reci"; - p->index_BFile = bf_open(bfs, p->index_fname, RIDX_CHUNK, rw); - if (p->index_BFile == NULL) - { - yaz_log(YLOG_FATAL|YLOG_ERRNO, "open %s", p->index_fname); - xfree(p); - return 0; - } + p->recindex = recindex_open(bfs, rw); p->tmp_buf = (char *) xmalloc(p->tmp_size); - r = bf_read(p->index_BFile, 0, 0, 0, p->tmp_buf); + r = rec_read_head(p->recindex, p->tmp_buf); switch (r) { case 0: @@ -315,7 +350,7 @@ Records rec_open(BFiles bfs, int rw, int compression_method) } if (rw) { - if (rec_write_head(p) != ZEBRA_OK) + if (rec_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK) ret = ZEBRA_FAIL; } break; @@ -323,14 +358,16 @@ Records rec_open(BFiles bfs, int rw, int compression_method) memcpy(&p->head, p->tmp_buf, sizeof(p->head)); if (memcmp(p->head.magic, REC_HEAD_MAGIC, sizeof(p->head.magic))) { - yaz_log(YLOG_FATAL, "file %s has bad format", p->index_fname); + yaz_log(YLOG_FATAL, "file %s has bad format", + recindex_get_fname(p->recindex)); ret = ZEBRA_FAIL; } version = atoi(p->head.version); if (version != REC_VERSION) { yaz_log(YLOG_FATAL, "file %s is version %d, but version" - " %d is required", p->index_fname, version, REC_VERSION); + " %d is required", + recindex_get_fname(p->recindex), version, REC_VERSION); ret = ZEBRA_FAIL; } break; @@ -345,12 +382,13 @@ Records rec_open(BFiles bfs, int rw, int compression_method) } for (i = 0; idata_BFile[i] = bf_open(bfs, p->data_fname[i], - (int) (p->head.block_size[i]), - rw))) + if (!(p->data_BFile[i] = + bf_open(bfs, p->data_fname[i], + CAST_ZINT_TO_INT(p->head.block_size[i]), rw))) { yaz_log(YLOG_FATAL|YLOG_ERRNO, "bf_open %s", p->data_fname[i]); ret = ZEBRA_FAIL; + break; } } p->cache_max = 400; @@ -432,7 +470,7 @@ static void rec_cache_flush_block1(Records p, Record rec, Record last_rec, for (i = 0; isize[i] + 20 > *out_size) + if (*out_offset + CAST_ZINT_TO_INT(rec->size[i]) + 20 > *out_size) { int new_size = *out_offset + rec->size[i] + 65536; char *np = (char *) xmalloc(new_size); @@ -482,8 +520,8 @@ static ZEBRA_RES rec_write_multiple(Records p, int saveCount) int out_size = 1000; int out_offset = 0; char *out_buf = (char *) xmalloc(out_size); - SYSNO *sysnos = (SYSNO *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1)); - SYSNO *sysnop = sysnos; + zint *sysnos = (zint *) xmalloc(sizeof(*sysnos) * (p->cache_cur + 1)); + zint *sysnop = sysnos; ZEBRA_RES ret = ZEBRA_OK; for (i = 0; icache_cur - saveCount; i++) @@ -590,7 +628,7 @@ static ZEBRA_RES rec_cache_flush(Records p, int saveCount) for (i = 0; icache_cur - saveCount; i++) { struct record_cache_entry *e = p->record_cache + i; - rec_rm(&e->rec); + rec_free(&e->rec); } /* i still being used ... */ for (j = 0; jrw) { - if (rec_write_head(p) != ZEBRA_OK) + if (rec_write_head(p->recindex, &p->head, sizeof(p->head)) != ZEBRA_OK) ret = ZEBRA_FAIL; } - if (p->index_BFile) - bf_close(p->index_BFile); + recindex_close(p->recindex); for (i = 0; irecindex, rec_sysno_to_int(sysno), &entry, sizeof(entry), 1) < 1) return NULL; /* record is not there! */ if (!entry.size) @@ -826,7 +863,7 @@ static Record rec_get_int(Records p, SYSNO sysno) return rec; } -Record rec_get(Records p, SYSNO sysno) +Record rec_get(Records p, zint sysno) { Record rec; zebra_mutex_lock(&p->mutex); @@ -844,7 +881,7 @@ Record rec_get_root(Records p) static Record rec_new_int(Records p) { int i; - SYSNO sysno; + zint sysno; Record rec; assert(p); @@ -855,7 +892,7 @@ static Record rec_new_int(Records p) { struct record_index_entry entry; - if (read_indx(p, p->head.index_free, &entry, sizeof(entry), 0) < 1) + if (read_indx(p->recindex, p->head.index_free, &entry, sizeof(entry), 0) < 1) { xfree(rec); return 0; @@ -893,13 +930,13 @@ ZEBRA_RES rec_del(Records p, Record *recpp) (p->head.no_records)--; if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagDelete))) { - rec_rm(recp); + rec_free(recp); *recp = *recpp; } else { ret = rec_cache_insert(p, *recpp, recordFlagDelete); - rec_rm(recpp); + rec_free(recpp); } zebra_mutex_unlock(&p->mutex); *recpp = NULL; @@ -914,20 +951,20 @@ ZEBRA_RES rec_put(Records p, Record *recpp) zebra_mutex_lock(&p->mutex); if ((recp = rec_cache_lookup(p, (*recpp)->sysno, recordFlagWrite))) { - rec_rm(recp); + rec_free(recp); *recp = *recpp; } else { ret = rec_cache_insert(p, *recpp, recordFlagWrite); - rec_rm(recpp); + rec_free(recpp); } zebra_mutex_unlock(&p->mutex); *recpp = NULL; return ret; } -void rec_rm(Record *recpp) +void rec_free(Record *recpp) { int i;