1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 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;
52 static size_t record_hash(int pos)
56 return pos % RECORD_HASH_SIZE;
59 void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
61 const char *syntax, const char *elementSetName,
63 Z_SRW_diagnostic *diag)
65 ZOOM_record_cache rc = 0;
67 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
68 ZOOM_connection_put_event(r->connection, event);
70 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
73 && yaz_strcmp_null(schema, rc->schema) == 0
74 && yaz_strcmp_null(elementSetName,rc->elementSetName) == 0
75 && yaz_strcmp_null(syntax, rc->syntax) == 0)
80 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
83 YAZ_SHPTR_INC(r->record_wrbuf);
84 rc->rec.record_wrbuf = r->record_wrbuf;
88 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
90 rc->syntax = odr_strdup_null(r->odr, syntax);
92 rc->schema = odr_strdup_null(r->odr, schema);
95 rc->next = r->record_hash[record_hash(pos)];
96 r->record_hash[record_hash(pos)] = rc;
99 rc->rec.schema = odr_strdup_null(r->odr, schema);
100 rc->rec.diag_set = 0;
101 rc->rec.diag_uri = 0;
102 rc->rec.diag_message = 0;
103 rc->rec.diag_details = 0;
109 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
110 if ((cp = strrchr(rc->rec.diag_set, '/')))
112 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
114 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
115 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
119 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
121 const char *elementSetName,
124 ZOOM_record_cache rc;
126 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
130 if (yaz_strcmp_null(schema, rc->schema))
132 if (yaz_strcmp_null(elementSetName,rc->elementSetName))
134 if (yaz_strcmp_null(syntax, rc->syntax))
142 ZOOM_API(ZOOM_record)
143 ZOOM_record_clone(ZOOM_record srec)
150 odr_enc = odr_createmem(ODR_ENCODE);
151 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
153 buf = odr_getbuf(odr_enc, &size, 0);
155 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
156 nrec->odr = odr_createmem(ODR_DECODE);
158 nrec->record_wrbuf = 0;
162 odr_setbuf(nrec->odr, buf, size, 0);
163 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
165 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
166 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
167 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
168 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
169 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
170 odr_destroy(odr_enc);
174 static void ZOOM_record_release(ZOOM_record rec)
180 if (rec->record_wrbuf)
181 YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy);
184 wrbuf_destroy(rec->wrbuf);
188 odr_destroy(rec->odr);
192 ZOOM_resultset_cache_reset(ZOOM_resultset r)
195 for (i = 0; i<RECORD_HASH_SIZE; i++)
197 ZOOM_record_cache rc;
198 for (rc = r->record_hash[i]; rc; rc = rc->next)
200 ZOOM_record_release(&rc->rec);
202 r->record_hash[i] = 0;
207 ZOOM_API(const char *)
208 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
213 *len = 0; /* default return */
215 if (!rec || !rec->npr)
219 if (!rec->record_wrbuf)
221 WRBUF w = wrbuf_alloc();
222 YAZ_SHPTR_INIT(rec->record_wrbuf, w);
224 wrbuf = rec->record_wrbuf->ptr;
227 rec->wrbuf = wrbuf_alloc();
230 return yaz_record_render(rec->npr, rec->schema, wrbuf, type_spec, len);
234 ZOOM_record_error(ZOOM_record rec, const char **cp,
235 const char **addinfo, const char **diagset)
237 Z_NamePlusRecord *npr;
246 *cp = rec->diag_message;
248 *addinfo = rec->diag_details;
250 *diagset = rec->diag_set;
251 return ZOOM_uri_to_code(rec->diag_uri);
253 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
255 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
256 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
259 if (diag_rec->which == Z_DiagRec_defaultFormat)
261 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
264 error = *ddf->condition;
267 case Z_DefaultDiagFormat_v2Addinfo:
268 add = ddf->u.v2Addinfo;
270 case Z_DefaultDiagFormat_v3Addinfo:
271 add = ddf->u.v3Addinfo;
276 yaz_oid_to_string(yaz_oid_std(),
277 ddf->diagnosticSetId, &oclass);
285 *addinfo = add ? add : "";
287 *cp = diagbib1_str(error);
294 ZOOM_record_destroy(ZOOM_record rec)
296 ZOOM_record_release(rec);
303 * c-file-style: "Stroustrup"
304 * indent-tabs-mode: nil
306 * vim: shiftwidth=4 tabstop=8 expandtab