1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
6 * \file zoom-record-cache.c
7 * \brief Implements ZOOM record caching
15 #include <yaz/diagbib1.h>
16 #include <yaz/record_render.h>
17 #include <yaz/shptr.h>
23 struct ZOOM_record_p {
26 struct WRBUF_shptr *record_wrbuf;
31 Z_NamePlusRecord *npr;
35 const char *diag_message;
36 const char *diag_details;
40 struct ZOOM_record_cache_p {
41 struct ZOOM_record_p rec;
46 ZOOM_record_cache next;
50 static int strcmp_null(const char *v1, const char *v2)
56 return strcmp(v1, v2);
59 static size_t record_hash(int pos)
63 return pos % RECORD_HASH_SIZE;
66 void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
68 const char *syntax, const char *elementSetName,
70 Z_SRW_diagnostic *diag)
72 ZOOM_record_cache rc = 0;
74 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
75 ZOOM_connection_put_event(r->connection, event);
77 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
80 && strcmp_null(r->schema, rc->schema) == 0
81 && strcmp_null(elementSetName,rc->elementSetName) == 0
82 && strcmp_null(syntax, rc->syntax) == 0)
87 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
90 YAZ_SHPTR_INC(r->record_wrbuf);
91 rc->rec.record_wrbuf = r->record_wrbuf;
95 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
97 rc->syntax = odr_strdup_null(r->odr, syntax);
99 rc->schema = odr_strdup_null(r->odr, r->schema);
102 rc->next = r->record_hash[record_hash(pos)];
103 r->record_hash[record_hash(pos)] = rc;
106 rc->rec.schema = odr_strdup_null(r->odr, schema);
107 rc->rec.diag_set = 0;
108 rc->rec.diag_uri = 0;
109 rc->rec.diag_message = 0;
110 rc->rec.diag_details = 0;
116 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
117 if ((cp = strrchr(rc->rec.diag_set, '/')))
119 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
121 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
122 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
126 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
128 const char *elementSetName)
130 ZOOM_record_cache rc;
132 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
136 if (strcmp_null(r->schema, rc->schema))
138 if (strcmp_null(elementSetName,rc->elementSetName))
140 if (strcmp_null(syntax, rc->syntax))
148 ZOOM_API(ZOOM_record)
149 ZOOM_record_clone(ZOOM_record srec)
156 odr_enc = odr_createmem(ODR_ENCODE);
157 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
159 buf = odr_getbuf(odr_enc, &size, 0);
161 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
162 nrec->odr = odr_createmem(ODR_DECODE);
164 nrec->record_wrbuf = 0;
168 odr_setbuf(nrec->odr, buf, size, 0);
169 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
171 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
172 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
173 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
174 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
175 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
176 odr_destroy(odr_enc);
180 static void ZOOM_record_release(ZOOM_record rec)
186 if (rec->record_wrbuf)
187 YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy);
190 wrbuf_destroy(rec->wrbuf);
194 odr_destroy(rec->odr);
198 ZOOM_resultset_cache_reset(ZOOM_resultset r)
201 for (i = 0; i<RECORD_HASH_SIZE; i++)
203 ZOOM_record_cache rc;
204 for (rc = r->record_hash[i]; rc; rc = rc->next)
206 ZOOM_record_release(&rc->rec);
208 r->record_hash[i] = 0;
213 ZOOM_API(const char *)
214 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
219 *len = 0; /* default return */
221 if (!rec || !rec->npr)
225 if (!rec->record_wrbuf)
227 WRBUF w = wrbuf_alloc();
228 YAZ_SHPTR_INIT(rec->record_wrbuf, w);
230 wrbuf = rec->record_wrbuf->ptr;
233 rec->wrbuf = wrbuf_alloc();
236 return yaz_record_render(rec->npr, rec->schema, wrbuf, type_spec, len);
240 ZOOM_record_error(ZOOM_record rec, const char **cp,
241 const char **addinfo, const char **diagset)
243 Z_NamePlusRecord *npr;
252 *cp = rec->diag_message;
254 *addinfo = rec->diag_details;
256 *diagset = rec->diag_set;
257 return ZOOM_uri_to_code(rec->diag_uri);
259 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
261 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
262 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
265 if (diag_rec->which == Z_DiagRec_defaultFormat)
267 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
270 error = *ddf->condition;
273 case Z_DefaultDiagFormat_v2Addinfo:
274 add = ddf->u.v2Addinfo;
276 case Z_DefaultDiagFormat_v3Addinfo:
277 add = ddf->u.v3Addinfo;
282 yaz_oid_to_string(yaz_oid_std(),
283 ddf->diagnosticSetId, &oclass);
291 *addinfo = add ? add : "";
293 *cp = diagbib1_str(error);
300 ZOOM_record_destroy(ZOOM_record rec)
302 ZOOM_record_release(rec);
309 * c-file-style: "Stroustrup"
310 * indent-tabs-mode: nil
312 * vim: shiftwidth=4 tabstop=8 expandtab