From 74682526ffcdc625f5d8e7adec296b0d6d23a8b5 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Wed, 11 Aug 2010 14:03:18 +0200 Subject: [PATCH] Refactor record cache to separate source --- src/Makefile.am | 3 +- src/zoom-c.c | 270 +--------------------------------------- src/zoom-p.h | 32 +---- src/zoom-record-cache.c | 313 +++++++++++++++++++++++++++++++++++++++++++++++ win/makefile | 1 + 5 files changed, 327 insertions(+), 292 deletions(-) create mode 100644 src/zoom-record-cache.c diff --git a/src/Makefile.am b/src/Makefile.am index 4b72c8c..aa685af 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -88,7 +88,8 @@ libyaz_la_SOURCES=version.c options.c log.c \ zget.c yaz-ccl.c diag-entry.c diag-entry.h \ logrpn.c \ otherinfo.c pquery.c sortspec.c charneg.c initopt.c \ - zoom-c.c zoom-z3950.c zoom-query.c zoom-socket.c zoom-opt.c zoom-p.h \ + zoom-c.c zoom-z3950.c zoom-query.c zoom-record-cache.c \ + zoom-socket.c zoom-opt.c zoom-p.h \ grs1disp.c zgdu.c soap.c srw.c srwutil.c uri.c \ opacdisp.c cclfind.c ccltoken.c cclerrms.c cclqual.c cclptree.c cclp.h \ cclqfile.c cclstr.c cclxmlconfig.c ccl_stop_words.c \ diff --git a/src/zoom-c.c b/src/zoom-c.c index 75b694a..3c1e14e 100644 --- a/src/zoom-c.c +++ b/src/zoom-c.c @@ -168,7 +168,7 @@ void ZOOM_set_dset_error(ZOOM_connection c, int error, } } -static int uri_to_code(const char *uri) +int ZOOM_uri_to_code(const char *uri) { int code = 0; const char *cp; @@ -177,6 +177,7 @@ static int uri_to_code(const char *uri) return code; } + #if YAZ_HAVE_XML2 static void set_HTTP_error(ZOOM_connection c, int error, const char *addinfo, const char *addinfo2) @@ -188,7 +189,7 @@ static void set_SRU_error(ZOOM_connection c, Z_SRW_diagnostic *d) { const char *uri = d->uri; if (uri) - ZOOM_set_dset_error(c, uri_to_code(uri), uri, d->details, 0); + ZOOM_set_dset_error(c, ZOOM_uri_to_code(uri), uri, d->details, 0); } #endif @@ -898,38 +899,6 @@ ZOOM_API(int) return 0; } -static void ZOOM_record_release(ZOOM_record rec) -{ - if (!rec) - return; - -#if SHPTR - if (rec->record_wrbuf) - YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy); -#else - if (rec->wrbuf) - wrbuf_destroy(rec->wrbuf); -#endif - - if (rec->odr) - odr_destroy(rec->odr); -} - -ZOOM_API(void) - ZOOM_resultset_cache_reset(ZOOM_resultset r) -{ - int i; - for (i = 0; irecord_hash[i]; rc; rc = rc->next) - { - ZOOM_record_release(&rc->rec); - } - r->record_hash[i] = 0; - } -} - ZOOM_API(void) ZOOM_resultset_destroy(ZOOM_resultset r) { @@ -1368,39 +1337,6 @@ static zoom_ret ZOOM_connection_srw_send_search(ZOOM_connection c) #endif ZOOM_API(ZOOM_record) - ZOOM_record_clone(ZOOM_record srec) -{ - char *buf; - int size; - ODR odr_enc; - ZOOM_record nrec; - - odr_enc = odr_createmem(ODR_ENCODE); - if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0)) - return 0; - buf = odr_getbuf(odr_enc, &size, 0); - - nrec = (ZOOM_record) xmalloc(sizeof(*nrec)); - yaz_log(log_details0, "ZOOM_record create"); - nrec->odr = odr_createmem(ODR_DECODE); -#if SHPTR - nrec->record_wrbuf = 0; -#else - nrec->wrbuf = 0; -#endif - odr_setbuf(nrec->odr, buf, size, 0); - z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0); - - nrec->schema = odr_strdup_null(nrec->odr, srec->schema); - nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri); - nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message); - nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details); - nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set); - odr_destroy(odr_enc); - return nrec; -} - -ZOOM_API(ZOOM_record) ZOOM_resultset_record_immediate(ZOOM_resultset s,size_t pos) { const char *syntax = @@ -1432,15 +1368,6 @@ ZOOM_API(ZOOM_record) return rec; } -ZOOM_API(void) - ZOOM_record_destroy(ZOOM_record rec) -{ - ZOOM_record_release(rec); - yaz_log(log_details0, "ZOOM_record destroy"); - xfree(rec); -} - - static yaz_iconv_t iconv_create_charset(const char *record_charset) { char to[40]; @@ -1591,66 +1518,6 @@ static const char *return_record_wrbuf(WRBUF wrbuf, int *len, return 0; } -ZOOM_API(int) - ZOOM_record_error(ZOOM_record rec, const char **cp, - const char **addinfo, const char **diagset) -{ - Z_NamePlusRecord *npr; - - if (!rec) - return 0; - - npr = rec->npr; - if (rec->diag_uri) - { - if (cp) - *cp = rec->diag_message; - if (addinfo) - *addinfo = rec->diag_details; - if (diagset) - *diagset = rec->diag_set; - return uri_to_code(rec->diag_uri); - } - if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic) - { - Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic; - int error = YAZ_BIB1_UNSPECIFIED_ERROR; - const char *add = 0; - - if (diag_rec->which == Z_DiagRec_defaultFormat) - { - Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat; - oid_class oclass; - - error = *ddf->condition; - switch (ddf->which) - { - case Z_DefaultDiagFormat_v2Addinfo: - add = ddf->u.v2Addinfo; - break; - case Z_DefaultDiagFormat_v3Addinfo: - add = ddf->u.v3Addinfo; - break; - } - if (diagset) - *diagset = - yaz_oid_to_string(yaz_oid_std(), - ddf->diagnosticSetId, &oclass); - } - else - { - if (diagset) - *diagset = "Bib-1"; - } - if (addinfo) - *addinfo = add ? add : ""; - if (cp) - *cp = diagbib1_str(error); - return error; - } - return 0; -} - static const char *get_record_format(WRBUF wrbuf, int *len, Z_NamePlusRecord *npr, int marctype, const char *charset, @@ -1682,9 +1549,9 @@ static const char *get_record_format(WRBUF wrbuf, int *len, } -static const char *npr_format(Z_NamePlusRecord *npr, const char *schema, - WRBUF wrbuf, - const char *type_spec, int *len) +const char *ZOOM_npr_format(Z_NamePlusRecord *npr, const char *schema, + WRBUF wrbuf, + const char *type_spec, int *len) { size_t i; char type[40]; @@ -1793,131 +1660,6 @@ static const char *npr_format(Z_NamePlusRecord *npr, const char *schema, return 0; } -ZOOM_API(const char *) - ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len) -{ - WRBUF wrbuf; - - if (len) - *len = 0; /* default return */ - - if (!rec || !rec->npr) - return 0; - -#if SHPTR - if (!rec->record_wrbuf) - { - WRBUF w = wrbuf_alloc(); - YAZ_SHPTR_INIT(rec->record_wrbuf, w); - } - wrbuf = rec->record_wrbuf->ptr; -#else - if (!rec->wrbuf) - rec->wrbuf = wrbuf_alloc(); - wrbuf = rec->wrbuf; -#endif - return npr_format(rec->npr, rec->schema, wrbuf, type_spec, len); -} - -static int strcmp_null(const char *v1, const char *v2) -{ - if (!v1 && !v2) - return 0; - if (!v1 || !v2) - return -1; - return strcmp(v1, v2); -} - -static size_t record_hash(int pos) -{ - if (pos < 0) - pos = 0; - return pos % RECORD_HASH_SIZE; -} - -void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, - int pos, - const char *syntax, const char *elementSetName, - const char *schema, - Z_SRW_diagnostic *diag) -{ - ZOOM_record_cache rc = 0; - - ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD); - ZOOM_connection_put_event(r->connection, event); - - for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next) - { - if (pos == rc->pos - && strcmp_null(r->schema, rc->schema) == 0 - && strcmp_null(elementSetName,rc->elementSetName) == 0 - && strcmp_null(syntax, rc->syntax) == 0) - break; - } - if (!rc) - { - rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc)); - rc->rec.odr = 0; -#if SHPTR - YAZ_SHPTR_INC(r->record_wrbuf); - rc->rec.record_wrbuf = r->record_wrbuf; -#else - rc->rec.wrbuf = 0; -#endif - rc->elementSetName = odr_strdup_null(r->odr, elementSetName); - - rc->syntax = odr_strdup_null(r->odr, syntax); - - rc->schema = odr_strdup_null(r->odr, r->schema); - - rc->pos = pos; - rc->next = r->record_hash[record_hash(pos)]; - r->record_hash[record_hash(pos)] = rc; - } - rc->rec.npr = npr; - rc->rec.schema = odr_strdup_null(r->odr, schema); - rc->rec.diag_set = 0; - rc->rec.diag_uri = 0; - rc->rec.diag_message = 0; - rc->rec.diag_details = 0; - if (diag) - { - if (diag->uri) - { - char *cp; - rc->rec.diag_set = odr_strdup(r->odr, diag->uri); - if ((cp = strrchr(rc->rec.diag_set, '/'))) - *cp = '\0'; - rc->rec.diag_uri = odr_strdup(r->odr, diag->uri); - } - rc->rec.diag_message = odr_strdup_null(r->odr, diag->message); - rc->rec.diag_details = odr_strdup_null(r->odr, diag->details); - } -} - -ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos, - const char *syntax, - const char *elementSetName) -{ - ZOOM_record_cache rc; - - for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next) - { - if (pos == rc->pos) - { - if (strcmp_null(r->schema, rc->schema)) - continue; - if (strcmp_null(elementSetName,rc->elementSetName)) - continue; - if (strcmp_null(syntax, rc->syntax)) - continue; - return &rc->rec; - } - } - return 0; -} - - ZOOM_API(ZOOM_scanset) ZOOM_connection_scan(ZOOM_connection c, const char *start) { diff --git a/src/zoom-p.h b/src/zoom-p.h index f0eb8a8..2763d56 100644 --- a/src/zoom-p.h +++ b/src/zoom-p.h @@ -168,23 +168,6 @@ struct ZOOM_resultset_p { char **facets_names; }; -struct ZOOM_record_p { - ODR odr; -#if SHPTR - struct WRBUF_shptr *record_wrbuf; -#else - WRBUF wrbuf; -#endif - - Z_NamePlusRecord *npr; - const char *schema; - - const char *diag_uri; - const char *diag_message; - const char *diag_details; - const char *diag_set; -}; - struct facet_term_p { char *term; int frequency; @@ -196,16 +179,6 @@ struct ZOOM_facet_field_p { struct facet_term_p *facet_terms; }; - -struct ZOOM_record_cache_p { - struct ZOOM_record_p rec; - char *elementSetName; - char *syntax; - char *schema; - int pos; - ZOOM_record_cache next; -}; - struct ZOOM_scanset_p { int refcount; ODR odr; @@ -315,6 +288,11 @@ Z_Query *ZOOM_query_get_Z_Query(ZOOM_query s); Z_SortKeySpecList *ZOOM_query_get_sortspec(ZOOM_query s); char *ZOOM_query_get_query_string(ZOOM_query s); +const char *ZOOM_npr_format(Z_NamePlusRecord *npr, const char *schema, + WRBUF wrbuf, + const char *type_spec, int *len); +int ZOOM_uri_to_code(const char *uri); + /* * Local variables: * c-basic-offset: 4 diff --git a/src/zoom-record-cache.c b/src/zoom-record-cache.c new file mode 100644 index 0000000..e90ef02 --- /dev/null +++ b/src/zoom-record-cache.c @@ -0,0 +1,313 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2010 Index Data + * See the file LICENSE for details. + */ +/** + * \file zoom-record-cache.c + * \brief Implements ZOOM record caching + */ + +#include +#include +#include +#include "zoom-p.h" + +#include +#include + +#if SHPTR +YAZ_SHPTR_TYPE(WRBUF) +#endif + +struct ZOOM_record_p { + ODR odr; +#if SHPTR + struct WRBUF_shptr *record_wrbuf; +#else + WRBUF wrbuf; +#endif + + Z_NamePlusRecord *npr; + const char *schema; + + const char *diag_uri; + const char *diag_message; + const char *diag_details; + const char *diag_set; +}; + +struct ZOOM_record_cache_p { + struct ZOOM_record_p rec; + char *elementSetName; + char *syntax; + char *schema; + int pos; + ZOOM_record_cache next; +}; + + +static int strcmp_null(const char *v1, const char *v2) +{ + if (!v1 && !v2) + return 0; + if (!v1 || !v2) + return -1; + return strcmp(v1, v2); +} + +static size_t record_hash(int pos) +{ + if (pos < 0) + pos = 0; + return pos % RECORD_HASH_SIZE; +} + +void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, + int pos, + const char *syntax, const char *elementSetName, + const char *schema, + Z_SRW_diagnostic *diag) +{ + ZOOM_record_cache rc = 0; + + ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD); + ZOOM_connection_put_event(r->connection, event); + + for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next) + { + if (pos == rc->pos + && strcmp_null(r->schema, rc->schema) == 0 + && strcmp_null(elementSetName,rc->elementSetName) == 0 + && strcmp_null(syntax, rc->syntax) == 0) + break; + } + if (!rc) + { + rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc)); + rc->rec.odr = 0; +#if SHPTR + YAZ_SHPTR_INC(r->record_wrbuf); + rc->rec.record_wrbuf = r->record_wrbuf; +#else + rc->rec.wrbuf = 0; +#endif + rc->elementSetName = odr_strdup_null(r->odr, elementSetName); + + rc->syntax = odr_strdup_null(r->odr, syntax); + + rc->schema = odr_strdup_null(r->odr, r->schema); + + rc->pos = pos; + rc->next = r->record_hash[record_hash(pos)]; + r->record_hash[record_hash(pos)] = rc; + } + rc->rec.npr = npr; + rc->rec.schema = odr_strdup_null(r->odr, schema); + rc->rec.diag_set = 0; + rc->rec.diag_uri = 0; + rc->rec.diag_message = 0; + rc->rec.diag_details = 0; + if (diag) + { + if (diag->uri) + { + char *cp; + rc->rec.diag_set = odr_strdup(r->odr, diag->uri); + if ((cp = strrchr(rc->rec.diag_set, '/'))) + *cp = '\0'; + rc->rec.diag_uri = odr_strdup(r->odr, diag->uri); + } + rc->rec.diag_message = odr_strdup_null(r->odr, diag->message); + rc->rec.diag_details = odr_strdup_null(r->odr, diag->details); + } +} + +ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos, + const char *syntax, + const char *elementSetName) +{ + ZOOM_record_cache rc; + + for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next) + { + if (pos == rc->pos) + { + if (strcmp_null(r->schema, rc->schema)) + continue; + if (strcmp_null(elementSetName,rc->elementSetName)) + continue; + if (strcmp_null(syntax, rc->syntax)) + continue; + return &rc->rec; + } + } + return 0; +} + +ZOOM_API(ZOOM_record) + ZOOM_record_clone(ZOOM_record srec) +{ + char *buf; + int size; + ODR odr_enc; + ZOOM_record nrec; + + odr_enc = odr_createmem(ODR_ENCODE); + if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0)) + return 0; + buf = odr_getbuf(odr_enc, &size, 0); + + nrec = (ZOOM_record) xmalloc(sizeof(*nrec)); + nrec->odr = odr_createmem(ODR_DECODE); +#if SHPTR + nrec->record_wrbuf = 0; +#else + nrec->wrbuf = 0; +#endif + odr_setbuf(nrec->odr, buf, size, 0); + z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0); + + nrec->schema = odr_strdup_null(nrec->odr, srec->schema); + nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri); + nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message); + nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details); + nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set); + odr_destroy(odr_enc); + return nrec; +} + +static void ZOOM_record_release(ZOOM_record rec) +{ + if (!rec) + return; + +#if SHPTR + if (rec->record_wrbuf) + YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy); +#else + if (rec->wrbuf) + wrbuf_destroy(rec->wrbuf); +#endif + + if (rec->odr) + odr_destroy(rec->odr); +} + +ZOOM_API(void) + ZOOM_resultset_cache_reset(ZOOM_resultset r) +{ + int i; + for (i = 0; irecord_hash[i]; rc; rc = rc->next) + { + ZOOM_record_release(&rc->rec); + } + r->record_hash[i] = 0; + } +} + + +ZOOM_API(const char *) + ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len) +{ + WRBUF wrbuf; + + if (len) + *len = 0; /* default return */ + + if (!rec || !rec->npr) + return 0; + +#if SHPTR + if (!rec->record_wrbuf) + { + WRBUF w = wrbuf_alloc(); + YAZ_SHPTR_INIT(rec->record_wrbuf, w); + } + wrbuf = rec->record_wrbuf->ptr; +#else + if (!rec->wrbuf) + rec->wrbuf = wrbuf_alloc(); + wrbuf = rec->wrbuf; +#endif + return ZOOM_npr_format(rec->npr, rec->schema, wrbuf, type_spec, len); +} + +ZOOM_API(int) + ZOOM_record_error(ZOOM_record rec, const char **cp, + const char **addinfo, const char **diagset) +{ + Z_NamePlusRecord *npr; + + if (!rec) + return 0; + + npr = rec->npr; + if (rec->diag_uri) + { + if (cp) + *cp = rec->diag_message; + if (addinfo) + *addinfo = rec->diag_details; + if (diagset) + *diagset = rec->diag_set; + return ZOOM_uri_to_code(rec->diag_uri); + } + if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic) + { + Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic; + int error = YAZ_BIB1_UNSPECIFIED_ERROR; + const char *add = 0; + + if (diag_rec->which == Z_DiagRec_defaultFormat) + { + Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat; + oid_class oclass; + + error = *ddf->condition; + switch (ddf->which) + { + case Z_DefaultDiagFormat_v2Addinfo: + add = ddf->u.v2Addinfo; + break; + case Z_DefaultDiagFormat_v3Addinfo: + add = ddf->u.v3Addinfo; + break; + } + if (diagset) + *diagset = + yaz_oid_to_string(yaz_oid_std(), + ddf->diagnosticSetId, &oclass); + } + else + { + if (diagset) + *diagset = "Bib-1"; + } + if (addinfo) + *addinfo = add ? add : ""; + if (cp) + *cp = diagbib1_str(error); + return error; + } + return 0; +} + +ZOOM_API(void) + ZOOM_record_destroy(ZOOM_record rec) +{ + ZOOM_record_release(rec); + xfree(rec); +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/win/makefile b/win/makefile index 4935f35..4e16baa 100644 --- a/win/makefile +++ b/win/makefile @@ -473,6 +473,7 @@ MISC_OBJS= \ $(OBJDIR)\srw.obj \ $(OBJDIR)\srwutil.obj \ $(OBJDIR)\zoom-c.obj \ + $(OBJDIR)\zoom-record-cache.obj \ $(OBJDIR)\zoom-z3950.obj \ $(OBJDIR)\zoom-query.obj \ $(OBJDIR)\facet.obj \ -- 1.7.10.4