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/shptr.h>
22 struct ZOOM_record_p {
25 struct WRBUF_shptr *record_wrbuf;
30 Z_NamePlusRecord *npr;
34 const char *diag_message;
35 const char *diag_details;
39 struct ZOOM_record_cache_p {
40 struct ZOOM_record_p rec;
45 ZOOM_record_cache next;
49 static int strcmp_null(const char *v1, const char *v2)
55 return strcmp(v1, v2);
58 static size_t record_hash(int pos)
62 return pos % RECORD_HASH_SIZE;
65 void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr,
67 const char *syntax, const char *elementSetName,
69 Z_SRW_diagnostic *diag)
71 ZOOM_record_cache rc = 0;
73 ZOOM_Event event = ZOOM_Event_create(ZOOM_EVENT_RECV_RECORD);
74 ZOOM_connection_put_event(r->connection, event);
76 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
79 && strcmp_null(r->schema, rc->schema) == 0
80 && strcmp_null(elementSetName,rc->elementSetName) == 0
81 && strcmp_null(syntax, rc->syntax) == 0)
86 rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
89 YAZ_SHPTR_INC(r->record_wrbuf);
90 rc->rec.record_wrbuf = r->record_wrbuf;
94 rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
96 rc->syntax = odr_strdup_null(r->odr, syntax);
98 rc->schema = odr_strdup_null(r->odr, r->schema);
101 rc->next = r->record_hash[record_hash(pos)];
102 r->record_hash[record_hash(pos)] = rc;
105 rc->rec.schema = odr_strdup_null(r->odr, schema);
106 rc->rec.diag_set = 0;
107 rc->rec.diag_uri = 0;
108 rc->rec.diag_message = 0;
109 rc->rec.diag_details = 0;
115 rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
116 if ((cp = strrchr(rc->rec.diag_set, '/')))
118 rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
120 rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
121 rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
125 ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos,
127 const char *elementSetName)
129 ZOOM_record_cache rc;
131 for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
135 if (strcmp_null(r->schema, rc->schema))
137 if (strcmp_null(elementSetName,rc->elementSetName))
139 if (strcmp_null(syntax, rc->syntax))
147 ZOOM_API(ZOOM_record)
148 ZOOM_record_clone(ZOOM_record srec)
155 odr_enc = odr_createmem(ODR_ENCODE);
156 if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
158 buf = odr_getbuf(odr_enc, &size, 0);
160 nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
161 nrec->odr = odr_createmem(ODR_DECODE);
163 nrec->record_wrbuf = 0;
167 odr_setbuf(nrec->odr, buf, size, 0);
168 z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
170 nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
171 nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
172 nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
173 nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
174 nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
175 odr_destroy(odr_enc);
179 static void ZOOM_record_release(ZOOM_record rec)
185 if (rec->record_wrbuf)
186 YAZ_SHPTR_DEC(rec->record_wrbuf, wrbuf_destroy);
189 wrbuf_destroy(rec->wrbuf);
193 odr_destroy(rec->odr);
197 ZOOM_resultset_cache_reset(ZOOM_resultset r)
200 for (i = 0; i<RECORD_HASH_SIZE; i++)
202 ZOOM_record_cache rc;
203 for (rc = r->record_hash[i]; rc; rc = rc->next)
205 ZOOM_record_release(&rc->rec);
207 r->record_hash[i] = 0;
212 ZOOM_API(const char *)
213 ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
218 *len = 0; /* default return */
220 if (!rec || !rec->npr)
224 if (!rec->record_wrbuf)
226 WRBUF w = wrbuf_alloc();
227 YAZ_SHPTR_INIT(rec->record_wrbuf, w);
229 wrbuf = rec->record_wrbuf->ptr;
232 rec->wrbuf = wrbuf_alloc();
235 return ZOOM_npr_format(rec->npr, rec->schema, wrbuf, type_spec, len);
239 ZOOM_record_error(ZOOM_record rec, const char **cp,
240 const char **addinfo, const char **diagset)
242 Z_NamePlusRecord *npr;
251 *cp = rec->diag_message;
253 *addinfo = rec->diag_details;
255 *diagset = rec->diag_set;
256 return ZOOM_uri_to_code(rec->diag_uri);
258 if (npr && npr->which == Z_NamePlusRecord_surrogateDiagnostic)
260 Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
261 int error = YAZ_BIB1_UNSPECIFIED_ERROR;
264 if (diag_rec->which == Z_DiagRec_defaultFormat)
266 Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
269 error = *ddf->condition;
272 case Z_DefaultDiagFormat_v2Addinfo:
273 add = ddf->u.v2Addinfo;
275 case Z_DefaultDiagFormat_v3Addinfo:
276 add = ddf->u.v3Addinfo;
281 yaz_oid_to_string(yaz_oid_std(),
282 ddf->diagnosticSetId, &oclass);
290 *addinfo = add ? add : "";
292 *cp = diagbib1_str(error);
299 ZOOM_record_destroy(ZOOM_record rec)
301 ZOOM_record_release(rec);
308 * c-file-style: "Stroustrup"
309 * indent-tabs-mode: nil
311 * vim: shiftwidth=4 tabstop=8 expandtab