#define SORT_IDX_ENTRYSIZE 64
+/** \var zebra_sort_index_t
+ \brief sort index handle
+*/
typedef struct zebra_sort_index *zebra_sort_index_t;
#define ZEBRA_SORT_TYPE_FLAT 1
#define ZEBRA_SORT_TYPE_ISAMB 2
+
+/** \brief creates sort handle
+ \param bfs block files handle
+ \param write_flag (0=read-only, 1=write and read)
+ \param sort_type one of ZEBRA_SORT_TYPE_..
+ \return sort index handle
+*/
zebra_sort_index_t zebra_sort_open(BFiles bfs, int write_flag, int sort_type);
+
+/** \brief frees sort handle
+*/
void zebra_sort_close(zebra_sort_index_t si);
+
+/** \brief sets type for sort usage
+ \param si sort index handle
+ \param type opaque type .. A sort file for each type is created
+*/
int zebra_sort_type(zebra_sort_index_t si, int type);
+
+/** \brief sets sort system number for read / add / delete
+ \param si sort index handle
+ \param sysno system number
+*/
void zebra_sort_sysno(zebra_sort_index_t si, zint sysno);
+
+/** \brief adds content to sort file
+ \param si sort index handle
+ \param buf buffer content
+ \param len length
+
+ zebra_sort_type and zebra_sort_sysno must be called prior to this
+*/
void zebra_sort_add(zebra_sort_index_t si, const char *buf, int len);
+
+
+/** \brief delete sort entry
+ \param si sort index handle
+
+ zebra_sort_type and zebra_sort_sysno must be called prior to this
+*/
void zebra_sort_delete(zebra_sort_index_t si);
-void zebra_sort_read(zebra_sort_index_t si, char *buf);
+
+/** \brief reads sort entry
+ \param si sort index handle
+ \param buf resulting buffer
+ \retval 0 could not be read
+ \retval 1 could be read (found)
+*/
+int zebra_sort_read(zebra_sort_index_t si, char *buf);
YAZ_END_CDECL
yaz_log(level, "%s " ZINT_FORMAT " %s", txt, a1.sysno, a1.term);
}
-int sort_term_compare(const void *a, const void *b)
+static int sort_term_compare(const void *a, const void *b)
{
struct sort_term a1, b1;
return 0;
}
-void *sort_term_code_start(void)
+static void *sort_term_code_start(void)
{
return 0;
}
-void sort_term_encode(void *p, char **dst, const char **src)
+static void sort_term_encode(void *p, char **dst, const char **src)
{
struct sort_term a1;
*dst += strlen(a1.term) + 1;
}
-void sort_term_decode(void *p, char **dst, const char **src)
+static void sort_term_decode(void *p, char **dst, const char **src)
{
struct sort_term a1;
*dst += sizeof(a1);
}
-void sort_term_code_reset(void *p)
+static void sort_term_code_reset(void *p)
{
}
-void sort_term_code_stop(void *p)
+static void sort_term_code_stop(void *p)
{
}
-
struct sort_term_stream {
int no;
int insert_flag;
struct sort_term st;
};
-int sort_term_code_read(void *vp, char **dst, int *insertMode)
+static int sort_term_code_read(void *vp, char **dst, int *insertMode)
{
struct sort_term_stream *s = (struct sort_term_stream *) vp;
return 1;
}
-
struct sortFileHead {
zint sysno_max;
};
int zebra_sort_type(zebra_sort_index_t si, int id)
{
int isam_block_size = 4096;
+
ISAMC_M method;
char fname[80];
struct sortFile *sf;
sf = (struct sortFile *) xmalloc(sizeof(*sf));
sf->id = id;
- method.compare_item = sort_term_compare;
- method.log_item = sort_term_log_item;
- method.codec.start = sort_term_code_start;
- method.codec.encode = sort_term_encode;
- method.codec.decode = sort_term_decode;
- method.codec.reset = sort_term_code_reset;
- method.codec.stop = sort_term_code_stop;
-
switch(si->type)
{
case ZEBRA_SORT_TYPE_FLAT:
}
break;
case ZEBRA_SORT_TYPE_ISAMB:
- sprintf(fname, "sortb%d", id);
+ method.compare_item = sort_term_compare;
+ method.log_item = sort_term_log_item;
+ method.codec.start = sort_term_code_start;
+ method.codec.encode = sort_term_encode;
+ method.codec.decode = sort_term_decode;
+ method.codec.reset = sort_term_code_reset;
+ method.codec.stop = sort_term_code_stop;
+ sprintf(fname, "sortb%d", id);
sf->u.isamb = isamb_open2(si->bfs, fname, si->write_flag, &method,
/* cache */ 0,
/* no_cat */ 1, &isam_block_size,
}
}
-void zebra_sort_read(zebra_sort_index_t si, char *buf)
+int zebra_sort_read(zebra_sort_index_t si, char *buf)
{
int r;
struct sortFile *sf = si->current_file;
assert(sf);
+ assert(sf->u.bf);
switch(si->type)
{
r = bf_read(sf->u.bf, si->sysno+1, 0, 0, buf);
if (!r)
memset(buf, 0, SORT_IDX_ENTRYSIZE);
+ if (buf[0] == 0)
+ return 0;
break;
case ZEBRA_SORT_TYPE_ISAMB:
memset(buf, 0, SORT_IDX_ENTRYSIZE);
- assert(sf->u.bf);
- if (sf->u.bf)
+ if (!sf->isam_p)
+ return 0;
+ else
{
struct sort_term st, st_untilbuf;
if (!sf->isam_pp)
sf->isam_pp = isamb_pp_open(sf->u.isamb, sf->isam_p, 1);
if (!sf->isam_pp)
- return;
+ return 0;
-#if 0
- while (1)
- {
- r = isamb_pp_read(sf->isam_pp, &st);
- if (!r)
- break;
- if (st.sysno == si->sysno)
- break;
- yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
- ZINT_FORMAT, st.sysno, si->sysno);
- }
-#else
st_untilbuf.sysno = si->sysno;
st_untilbuf.term[0] = '\0';
r = isamb_pp_forward(sf->isam_pp, &st, &st_untilbuf);
if (!r)
- return;
-#endif
+ return 0;
if (r)
{
if (st.sysno != si->sysno)
{
yaz_log(YLOG_LOG, "Received sysno=" ZINT_FORMAT " looking for "
ZINT_FORMAT, st.sysno, si->sysno);
- return;
+ return 0;
}
if (strlen(st.term) < SORT_IDX_ENTRYSIZE)
strcpy(buf, st.term);
}
break;
}
+ return 1;
}
/*
* Local variables:
test_sortindex
test_sort1
test_sort2
+test_sortidx
*.mf
*.LCK
*.log
test_rank test_private_attset \
test_scan test_create_databases test_resources test_update_record \
test_zebra_fork test_special_elements test_icu_indexing \
- test_sortindex test_safari test_sort1 test_sort2
+ test_sortindex test_safari test_sort1 test_sort2 test_sortidx
TESTS = $(check_PROGRAMS)
test_safari_SOURCES = test_safari.c
test_sort1_SOURCES = test_sort1.c
test_sort2_SOURCES = test_sort2.c
+test_sortidx_SOURCES = test_sortidx.c
AM_CPPFLAGS = -I$(top_srcdir)/include $(YAZINC)
,
"<sort1>\n"
" <title>3rd computer</title>\n"
-" <dateTime>a^3</dateTime>\n"
+ " <dateTime>a^3</dateTime>\n"
" <cost>15</cost>\n"
"</sort1>\n"
,
--- /dev/null
+/* This file is part of the Zebra server.
+ Copyright (C) 1995-2008 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
+Software Foundation; either version 2, or (at your option) any later
+version.
+
+Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
+WARRANTY; without even the implied warranty of MERCHANTABILITY or
+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 this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+
+*/
+
+#include <idzebra/bfile.h>
+#include <sortidx.h>
+#include "testlib.h"
+
+static void tst1(zebra_sort_index_t si)
+{
+ zint sysno = 12; /* just some sysno */
+ int my_type = 2; /* just some type ID */
+ char read_buf[SORT_IDX_ENTRYSIZE];
+
+ zebra_sort_type(si, my_type);
+
+ zebra_sort_sysno(si, sysno);
+ YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+
+ zebra_sort_add(si, "abcde1", 6);
+
+ zebra_sort_sysno(si, sysno);
+ YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 1);
+ YAZ_CHECK(!strcmp(read_buf, "abcde1"));
+
+ zebra_sort_sysno(si, sysno+1);
+ YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+
+ zebra_sort_sysno(si, sysno-1);
+ YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+
+ zebra_sort_sysno(si, sysno);
+ zebra_sort_delete(si);
+ YAZ_CHECK_EQ(zebra_sort_read(si, read_buf), 0);
+}
+
+static void tst(int argc, char **argv)
+{
+ BFiles bfs = bfs_create(".:50M", 0);
+ zebra_sort_index_t si;
+
+ YAZ_CHECK(bfs);
+ if (bfs)
+ {
+ bf_reset(bfs);
+ si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_ISAMB);
+ YAZ_CHECK(si);
+ if (si)
+ {
+ tst1(si);
+ zebra_sort_close(si);
+ }
+ }
+
+ if (bfs)
+ {
+ bf_reset(bfs);
+ si = zebra_sort_open(bfs, 1, ZEBRA_SORT_TYPE_FLAT);
+ YAZ_CHECK(si);
+ if (si)
+ {
+ tst1(si);
+ zebra_sort_close(si);
+ }
+ }
+ if (bfs)
+ bfs_destroy(bfs);
+}
+
+TL_MAIN
+/*
+ * Local variables:
+ * c-basic-offset: 4
+ * indent-tabs-mode: nil
+ * End:
+ * vim: shiftwidth=4 tabstop=8 expandtab
+ */
+