1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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 static ZOOM_record record_cache_add(ZOOM_resultset r,
60 Z_NamePlusRecord *npr,
63 const char *elementSetName,
65 Z_SRW_diagnostic *diag)
67 ZOOM_record_cache rc = 0;
69 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
70 ZOOM_connection_put_event(r->connection, event);
72 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
75 && yaz_strcmp_null(schema, rc->schema) == 0
76 && yaz_strcmp_null(elementSetName,rc->elementSetName) == 0
77 && yaz_strcmp_null(syntax, rc->syntax) == 0)
82 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
85 YAZ_SHPTR_INC(r->record_wrbuf);
86 rc->rec.record_wrbuf = r->record_wrbuf;
90 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
92 rc->syntax = odr_strdup_null(r->odr, syntax);
94 rc->schema = odr_strdup_null(r->odr, schema);
97 rc->next = r->record_hash[record_hash(pos)];
98 r->record_hash[record_hash(pos)] = rc;
103 rc->rec.schema = odr_strdup_null(r->odr, schema);
104 rc->rec.diag_set = 0;
105 rc->rec.diag_uri = 0;
106 rc->rec.diag_message = 0;
107 rc->rec.diag_details = 0;
113 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
114 if ((cp = strrchr(rc->rec.diag_set, '/')))
116 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
118 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
119 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
124 void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
126 const char *syntax, const char *elementSetName,
128 Z_SRW_diagnostic *diag)
130 record_cache_add(r, npr, pos, syntax, elementSetName, schema, diag);
131 ZOOM_memcached_add(r, npr, pos, syntax, elementSetName, schema, diag);
134 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
136 const char *elementSetName,
139 ZOOM_record_cache rc;
140 Z_NamePlusRecord *npr;
142 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
146 if (yaz_strcmp_null(schema, rc->schema))
148 if (yaz_strcmp_null(elementSetName,rc->elementSetName))
150 if (yaz_strcmp_null(syntax, rc->syntax))
155 npr = ZOOM_memcached_lookup(r, pos, syntax, elementSetName, schema);
157 return record_cache_add(r, npr, pos, syntax, elementSetName,
162 ZOOM_API(ZOOM_record)
163 ZOOM_record_clone(ZOOM_record srec)
170 odr_enc = odr_createmem(ODR_ENCODE);
171 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
173 buf = odr_getbuf(odr_enc, &size, 0);
175 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
176 nrec->odr = odr_createmem(ODR_DECODE);
178 nrec->record_wrbuf = 0;
182 odr_setbuf(nrec->odr, buf, size, 0);
183 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
185 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
186 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
187 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
188 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
189 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
190 odr_destroy(odr_enc);
194 static void ZOOM_record_release(ZOOM_record rec)
200 if (rec->record_wrbuf)
201 YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy);
204 wrbuf_destroy(rec->wrbuf);
208 odr_destroy(rec->odr);
212 ZOOM_resultset_cache_reset(ZOOM_resultset r)
215 for (i = 0; i<RECORD_HASH_SIZE; i++)
217 ZOOM_record_cache rc;
218 for (rc = r->record_hash[i]; rc; rc = rc->next)
220 ZOOM_record_release(&rc->rec);
222 r->record_hash[i] = 0;
227 ZOOM_API(const char *)
228 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
233 *len = 0; /* default return */
235 if (!rec || !rec->npr)
239 if (!rec->record_wrbuf)
241 WRBUF w = wrbuf_alloc();
242 YAZ_SHPTR_INIT(rec->record_wrbuf, w);
244 wrbuf = rec->record_wrbuf->ptr;
247 rec->wrbuf = wrbuf_alloc();
250 return yaz_record_render(rec->npr, rec->schema, wrbuf, type_spec, len);
254 ZOOM_record_error(ZOOM_record rec, const char **cp,
255 const char **addinfo, const char **diagset)
257 Z_NamePlusRecord *npr;
266 *cp = rec->diag_message;
268 *addinfo = rec->diag_details;
270 *diagset = rec->diag_set;
271 return ZOOM_uri_to_code(rec->diag_uri);
273 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
275 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
276 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
279 if (diag_rec->which == Z_DiagRec_defaultFormat)
281 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
284 error = *ddf->condition;
287 case Z_DefaultDiagFormat_v2Addinfo:
288 add = ddf->u.v2Addinfo;
290 case Z_DefaultDiagFormat_v3Addinfo:
291 add = ddf->u.v3Addinfo;
296 yaz_oid_to_string(yaz_oid_std(),
297 ddf->diagnosticSetId, &oclass);
305 *addinfo = add ? add : "";
307 *cp = diagbib1_str(error);
314 ZOOM_record_destroy(ZOOM_record rec)
316 ZOOM_record_release(rec);
323 * c-file-style: "Stroustrup"
324 * indent-tabs-mode: nil
326 * vim: shiftwidth=4 tabstop=8 expandtab