1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
6 * \file zoom-record-cache.c
7 * \brief Implements ZOOM record caching
18 #include <yaz/diagbib1.h>
19 #include <yaz/record_render.h>
20 #include <yaz/shptr.h>
26 struct ZOOM_record_p {
29 struct WRBUF_shptr *record_wrbuf;
34 Z_NamePlusRecord *npr;
38 const char *diag_message;
39 const char *diag_details;
43 struct ZOOM_record_cache_p {
44 struct ZOOM_record_p rec;
49 ZOOM_record_cache next;
53 static int strcmp_null(const char *v1, const char *v2)
59 return strcmp(v1, v2);
62 static size_t record_hash(int pos)
66 return pos % RECORD_HASH_SIZE;
69 void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
71 const char *syntax, const char *elementSetName,
73 Z_SRW_diagnostic *diag)
75 ZOOM_record_cache rc = 0;
77 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
78 ZOOM_connection_put_event(r->connection, event);
80 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
83 && strcmp_null(r->schema, rc->schema) == 0
84 && strcmp_null(elementSetName,rc->elementSetName) == 0
85 && strcmp_null(syntax, rc->syntax) == 0)
90 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
93 YAZ_SHPTR_INC(r->record_wrbuf);
94 rc->rec.record_wrbuf = r->record_wrbuf;
98 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
100 rc->syntax = odr_strdup_null(r->odr, syntax);
102 rc->schema = odr_strdup_null(r->odr, r->schema);
105 rc->next = r->record_hash[record_hash(pos)];
106 r->record_hash[record_hash(pos)] = rc;
109 rc->rec.schema = odr_strdup_null(r->odr, schema);
110 rc->rec.diag_set = 0;
111 rc->rec.diag_uri = 0;
112 rc->rec.diag_message = 0;
113 rc->rec.diag_details = 0;
119 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
120 if ((cp = strrchr(rc->rec.diag_set, '/')))
122 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
124 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
125 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
129 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
131 const char *elementSetName)
133 ZOOM_record_cache rc;
135 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
139 if (strcmp_null(r->schema, rc->schema))
141 if (strcmp_null(elementSetName,rc->elementSetName))
143 if (strcmp_null(syntax, rc->syntax))
151 ZOOM_API(ZOOM_record)
152 ZOOM_record_clone(ZOOM_record srec)
159 odr_enc = odr_createmem(ODR_ENCODE);
160 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
162 buf = odr_getbuf(odr_enc, &size, 0);
164 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
165 nrec->odr = odr_createmem(ODR_DECODE);
167 nrec->record_wrbuf = 0;
171 odr_setbuf(nrec->odr, buf, size, 0);
172 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
174 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
175 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
176 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
177 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
178 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
179 odr_destroy(odr_enc);
183 static void ZOOM_record_release(ZOOM_record rec)
189 if (rec->record_wrbuf)
190 YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy);
193 wrbuf_destroy(rec->wrbuf);
197 odr_destroy(rec->odr);
201 ZOOM_resultset_cache_reset(ZOOM_resultset r)
204 for (i = 0; i<RECORD_HASH_SIZE; i++)
206 ZOOM_record_cache rc;
207 for (rc = r->record_hash[i]; rc; rc = rc->next)
209 ZOOM_record_release(&rc->rec);
211 r->record_hash[i] = 0;
216 ZOOM_API(const char *)
217 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
222 *len = 0; /* default return */
224 if (!rec || !rec->npr)
228 if (!rec->record_wrbuf)
230 WRBUF w = wrbuf_alloc();
231 YAZ_SHPTR_INIT(rec->record_wrbuf, w);
233 wrbuf = rec->record_wrbuf->ptr;
236 rec->wrbuf = wrbuf_alloc();
239 return yaz_record_render(rec->npr, rec->schema, wrbuf, type_spec, len);
243 ZOOM_record_error(ZOOM_record rec, const char **cp,
244 const char **addinfo, const char **diagset)
246 Z_NamePlusRecord *npr;
255 *cp = rec->diag_message;
257 *addinfo = rec->diag_details;
259 *diagset = rec->diag_set;
260 return ZOOM_uri_to_code(rec->diag_uri);
262 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
264 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
265 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
268 if (diag_rec->which == Z_DiagRec_defaultFormat)
270 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
273 error = *ddf->condition;
276 case Z_DefaultDiagFormat_v2Addinfo:
277 add = ddf->u.v2Addinfo;
279 case Z_DefaultDiagFormat_v3Addinfo:
280 add = ddf->u.v3Addinfo;
285 yaz_oid_to_string(yaz_oid_std(),
286 ddf->diagnosticSetId, &oclass);
294 *addinfo = add ? add : "";
296 *cp = diagbib1_str(error);
303 ZOOM_record_destroy(ZOOM_record rec)
305 ZOOM_record_release(rec);
312 * c-file-style: "Stroustrup"
313 * indent-tabs-mode: nil
315 * vim: shiftwidth=4 tabstop=8 expandtab