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(r->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, r->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)
123 ZOOM_record_cache rc;
125 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
129 if (yaz_strcmp_null(r->schema, rc->schema))
131 if (yaz_strcmp_null(elementSetName,rc->elementSetName))
133 if (yaz_strcmp_null(syntax, rc->syntax))
141 ZOOM_API(ZOOM_record)
142 ZOOM_record_clone(ZOOM_record srec)
149 odr_enc = odr_createmem(ODR_ENCODE);
150 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
152 buf = odr_getbuf(odr_enc, &size, 0);
154 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
155 nrec->odr = odr_createmem(ODR_DECODE);
157 nrec->record_wrbuf = 0;
161 odr_setbuf(nrec->odr, buf, size, 0);
162 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
164 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
165 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
166 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
167 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
168 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
169 odr_destroy(odr_enc);
173 static void ZOOM_record_release(ZOOM_record rec)
179 if (rec->record_wrbuf)
180 YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy);
183 wrbuf_destroy(rec->wrbuf);
187 odr_destroy(rec->odr);
191 ZOOM_resultset_cache_reset(ZOOM_resultset r)
194 for (i = 0; i<RECORD_HASH_SIZE; i++)
196 ZOOM_record_cache rc;
197 for (rc = r->record_hash[i]; rc; rc = rc->next)
199 ZOOM_record_release(&rc->rec);
201 r->record_hash[i] = 0;
206 ZOOM_API(const char *)
207 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
212 *len = 0; /* default return */
214 if (!rec || !rec->npr)
218 if (!rec->record_wrbuf)
220 WRBUF w = wrbuf_alloc();
221 YAZ_SHPTR_INIT(rec->record_wrbuf, w);
223 wrbuf = rec->record_wrbuf->ptr;
226 rec->wrbuf = wrbuf_alloc();
229 return yaz_record_render(rec->npr, rec->schema, wrbuf, type_spec, len);
233 ZOOM_record_error(ZOOM_record rec, const char **cp,
234 const char **addinfo, const char **diagset)
236 Z_NamePlusRecord *npr;
245 *cp = rec->diag_message;
247 *addinfo = rec->diag_details;
249 *diagset = rec->diag_set;
250 return ZOOM_uri_to_code(rec->diag_uri);
252 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
254 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
255 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
258 if (diag_rec->which == Z_DiagRec_defaultFormat)
260 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
263 error = *ddf->condition;
266 case Z_DefaultDiagFormat_v2Addinfo:
267 add = ddf->u.v2Addinfo;
269 case Z_DefaultDiagFormat_v3Addinfo:
270 add = ddf->u.v3Addinfo;
275 yaz_oid_to_string(yaz_oid_std(),
276 ddf->diagnosticSetId, &oclass);
284 *addinfo = add ? add : "";
286 *cp = diagbib1_str(error);
293 ZOOM_record_destroy(ZOOM_record rec)
295 ZOOM_record_release(rec);
302 * c-file-style: "Stroustrup"
303 * indent-tabs-mode: nil
305 * vim: shiftwidth=4 tabstop=8 expandtab