YAZ  5.34.0
zoom-record-cache.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12 
13 #include <assert.h>
14 #include <string.h>
15 #include <errno.h>
16 #include "zoom-p.h"
17 
18 #include <yaz/diagbib1.h>
19 #include <yaz/record_render.h>
20 #include <yaz/shptr.h>
21 
22 #if SHPTR
24 #endif
25 
26 struct ZOOM_record_p {
28 #if SHPTR
29  struct WRBUF_shptr *record_wrbuf;
30 #else
31  WRBUF wrbuf;
32 #endif
33 
35  const char *schema;
36 
37  const char *diag_uri;
38  const char *diag_message;
39  const char *diag_details;
40  const char *diag_set;
41 };
42 
44  struct ZOOM_record_p rec;
46  char *syntax;
47  char *schema;
48  int pos;
50 };
51 
52 static size_t record_hash(int pos)
53 {
54  if (pos < 0)
55  pos = 0;
56  return pos % RECORD_HASH_SIZE;
57 }
58 
61  int pos,
62  const char *syntax,
63  const char *elementSetName,
64  const char *schema,
65  Z_SRW_diagnostic *diag)
66 {
67  ZOOM_record_cache rc = 0;
68 
71 
72  for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
73  {
74  if (pos == rc->pos
75  && yaz_strcmp_null(schema, rc->schema) == 0
76  && yaz_strcmp_null(elementSetName,rc->elementSetName) == 0
77  && yaz_strcmp_null(syntax, rc->syntax) == 0)
78  break;
79  }
80  if (!rc)
81  {
82  rc = (ZOOM_record_cache) odr_malloc(r->odr, sizeof(*rc));
83  rc->rec.odr = 0;
84 #if SHPTR
86  rc->rec.record_wrbuf = r->record_wrbuf;
87 #else
88  rc->rec.wrbuf = 0;
89 #endif
90  rc->elementSetName = odr_strdup_null(r->odr, elementSetName);
91 
92  rc->syntax = odr_strdup_null(r->odr, syntax);
93 
94  rc->schema = odr_strdup_null(r->odr, schema);
95 
96  rc->pos = pos;
97  rc->next = r->record_hash[record_hash(pos)];
98  r->record_hash[record_hash(pos)] = rc;
99 
100  }
101 
102  rc->rec.npr = npr;
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;
108  if (diag)
109  {
110  if (diag->uri)
111  {
112  char *cp;
113  rc->rec.diag_set = odr_strdup(r->odr, diag->uri);
114  if ((cp = strrchr(rc->rec.diag_set, '/')))
115  *cp = '\0';
116  rc->rec.diag_uri = odr_strdup(r->odr, diag->uri);
117  }
118  rc->rec.diag_message = odr_strdup_null(r->odr, diag->message);
119  rc->rec.diag_details = odr_strdup_null(r->odr, diag->details);
120  }
121  return &rc->rec;
122 }
123 
125  int pos,
126  const char *syntax, const char *elementSetName,
127  const char *schema,
128  Z_SRW_diagnostic *diag)
129 {
130  record_cache_add(r, npr, pos, syntax, elementSetName, schema, diag);
131  ZOOM_memcached_add(r, npr, pos, syntax, elementSetName, schema, diag);
132 }
133 
135  const char *syntax,
136  const char *elementSetName,
137  const char *schema)
138 {
140 
141  for (rc = r->record_hash[record_hash(pos)]; rc; rc = rc->next)
142  {
143  if (pos == rc->pos)
144  {
145  if (yaz_strcmp_null(schema, rc->schema))
146  continue;
147  if (yaz_strcmp_null(elementSetName,rc->elementSetName))
148  continue;
149  if (yaz_strcmp_null(syntax, rc->syntax))
150  continue;
151  return &rc->rec;
152  }
153  }
154  return 0;
155 }
156 
158  const char *syntax,
159  const char *elementSetName,
160  const char *schema)
161 {
163  ZOOM_record rec = ZOOM_record_cache_lookup_i(r, pos, syntax,
164  elementSetName, schema);
165  if (rec)
166  {
169  return rec;
170  }
171  npr = ZOOM_memcached_lookup(r, pos, syntax, elementSetName, schema);
172  if (npr)
173  return record_cache_add(r, npr, pos, syntax, elementSetName,
174  schema, 0);
175  return 0;
176 }
177 
180 {
181  char *buf;
182  int size;
183  ODR odr_enc;
184  ZOOM_record nrec;
185 
186  odr_enc = odr_createmem(ODR_ENCODE);
187  if (!z_NamePlusRecord(odr_enc, &srec->npr, 0, 0))
188  return 0;
189  buf = odr_getbuf(odr_enc, &size, 0);
190 
191  nrec = (ZOOM_record) xmalloc(sizeof(*nrec));
192  nrec->odr = odr_createmem(ODR_DECODE);
193 #if SHPTR
194  nrec->record_wrbuf = 0;
195 #else
196  nrec->wrbuf = 0;
197 #endif
198  odr_setbuf(nrec->odr, buf, size, 0);
199  z_NamePlusRecord(nrec->odr, &nrec->npr, 0, 0);
200 
201  nrec->schema = odr_strdup_null(nrec->odr, srec->schema);
202  nrec->diag_uri = odr_strdup_null(nrec->odr, srec->diag_uri);
203  nrec->diag_message = odr_strdup_null(nrec->odr, srec->diag_message);
204  nrec->diag_details = odr_strdup_null(nrec->odr, srec->diag_details);
205  nrec->diag_set = odr_strdup_null(nrec->odr, srec->diag_set);
206  odr_destroy(odr_enc);
207  return nrec;
208 }
209 
211 {
212  if (!rec)
213  return;
214 
215 #if SHPTR
216  if (rec->record_wrbuf)
218 #else
219  if (rec->wrbuf)
220  wrbuf_destroy(rec->wrbuf);
221 #endif
222 
223  if (rec->odr)
224  odr_destroy(rec->odr);
225 }
226 
227 ZOOM_API(void)
229 {
230  int i;
231  for (i = 0; i<RECORD_HASH_SIZE; i++)
232  {
234  for (rc = r->record_hash[i]; rc; rc = rc->next)
235  {
236  ZOOM_record_release(&rc->rec);
237  }
238  r->record_hash[i] = 0;
239  }
240 }
241 
242 
243 ZOOM_API(const char *)
244  ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
245 {
246  WRBUF wrbuf;
247 
248  if (len)
249  *len = 0; /* default return */
250 
251  if (!rec || !rec->npr)
252  return 0;
253 
254 #if SHPTR
255  if (!rec->record_wrbuf)
256  {
257  WRBUF w = wrbuf_alloc();
258  YAZ_SHPTR_INIT(rec->record_wrbuf, w);
259  }
260  wrbuf = rec->record_wrbuf->ptr;
261 #else
262  if (!rec->wrbuf)
263  rec->wrbuf = wrbuf_alloc();
264  wrbuf = rec->wrbuf;
265 #endif
266  return yaz_record_render(rec->npr, rec->schema, wrbuf, type_spec, len);
267 }
268 
269 ZOOM_API(int)
270  ZOOM_record_error(ZOOM_record rec, const char **cp,
271  const char **addinfo, const char **diagset)
272 {
274 
275  if (!rec)
276  return 0;
277 
278  npr = rec->npr;
279  if (rec->diag_uri)
280  {
281  if (cp)
282  *cp = rec->diag_message;
283  if (addinfo)
284  *addinfo = rec->diag_details;
285  if (diagset)
286  *diagset = rec->diag_set;
287  return ZOOM_uri_to_code(rec->diag_uri);
288  }
290  {
291  Z_DiagRec *diag_rec = npr->u.surrogateDiagnostic;
292  int error = YAZ_BIB1_UNSPECIFIED_ERROR;
293  const char *add = 0;
294 
295  if (diag_rec->which == Z_DiagRec_defaultFormat)
296  {
297  Z_DefaultDiagFormat *ddf = diag_rec->u.defaultFormat;
298  oid_class oclass;
299 
300  error = *ddf->condition;
301  switch (ddf->which)
302  {
304  add = ddf->u.v2Addinfo;
305  break;
307  add = ddf->u.v3Addinfo;
308  break;
309  }
310  if (diagset)
311  *diagset =
313  ddf->diagnosticSetId, &oclass);
314  }
315  else
316  {
317  if (diagset)
318  *diagset = "Bib-1";
319  }
320  if (addinfo)
321  *addinfo = add ? add : "";
322  if (cp)
323  *cp = diagbib1_str(error);
324  return error;
325  }
326  return 0;
327 }
328 
329 ZOOM_API(void)
331 {
332  ZOOM_record_release(rec);
333  xfree(rec);
334 }
335 
336 /*
337  * Local variables:
338  * c-basic-offset: 4
339  * c-file-style: "Stroustrup"
340  * indent-tabs-mode: nil
341  * End:
342  * vim: shiftwidth=4 tabstop=8 expandtab
343  */
344 
const char * diagbib1_str(int code)
Definition: diagbib1.c:192
Diagnostics: Generated by csvtodiag.tcl from ./bib1.csv.
#define YAZ_BIB1_UNSPECIFIED_ERROR
Definition: diagbib1.h:44
Header for errno utilities.
int yaz_strcmp_null(const char *v1, const char *v2)
strcmp, null may be passed
Definition: matchstr.c:95
ODR odr_createmem(int direction)
Definition: odr.c:200
void odr_setbuf(ODR o, char *buf, int len, int can_grow)
Definition: odr.c:267
char * odr_getbuf(ODR o, int *len, int *size)
Definition: odr.c:277
void odr_destroy(ODR o)
Definition: odr.c:253
#define ODR_DECODE
Definition: odr.h:95
#define ODR_ENCODE
Definition: odr.h:96
char * odr_strdup_null(ODR o, const char *str)
Definition: odr_mem.c:41
char * odr_strdup(ODR o, const char *str)
Definition: odr_mem.c:36
void * odr_malloc(ODR o, size_t size)
Definition: odr_mem.c:31
yaz_oid_db_t yaz_oid_std(void)
returns standard OID database
Definition: oid_db.c:33
const char * yaz_oid_to_string(yaz_oid_db_t oid_db, const Odr_oid *oid, oid_class *oclass)
maps raw OID to string
Definition: oid_db.c:78
oid_class
Definition: oid_db.h:46
const char * yaz_record_render(Z_NamePlusRecord *npr, const char *schema, WRBUF wrbuf, const char *type_spec, int *len)
render records (ZOOM style)
Record render header.
Shared pointer macros.
#define YAZ_SHPTR_INIT(p, n)
Definition: shptr.h:50
#define YAZ_SHPTR_DEC(p, destroy)
Definition: shptr.h:64
#define YAZ_SHPTR_INC(p)
Definition: shptr.h:58
#define YAZ_SHPTR_TYPE(type)
Definition: shptr.h:41
ZOOM_record_cache next
struct ZOOM_record_p rec
const char * schema
const char * diag_message
const char * diag_uri
const char * diag_set
const char * diag_details
Z_NamePlusRecord * npr
struct WRBUF_shptr * record_wrbuf
ZOOM_record_cache record_hash[RECORD_HASH_SIZE]
Definition: zoom-p.h:142
ZOOM_connection connection
Definition: zoom-p.h:144
struct WRBUF_shptr * record_wrbuf
Definition: zoom-p.h:149
union Z_DefaultDiagFormat::@57 u
Z_InternationalString * v3Addinfo
Definition: z-core.h:747
char * v2Addinfo
Definition: z-core.h:746
Odr_int * condition
Definition: z-core.h:743
Odr_oid * diagnosticSetId
Definition: z-core.h:742
int which
Definition: z-core.h:732
Z_DefaultDiagFormat * defaultFormat
Definition: z-core.h:734
union Z_DiagRec::@56 u
union Z_NamePlusRecord::@54 u
Z_DiagRec * surrogateDiagnostic
Definition: z-core.h:709
char * message
Definition: srw.h:66
char * details
Definition: srw.h:65
char * uri
Definition: srw.h:64
Definition: odr.h:125
string buffer
Definition: wrbuf.h:43
void wrbuf_destroy(WRBUF b)
destroy WRBUF and its buffer
Definition: wrbuf.c:38
WRBUF wrbuf_alloc(void)
construct WRBUF
Definition: wrbuf.c:25
struct wrbuf wrbuf
string buffer
#define xfree(x)
utility macro which calls xfree_f
Definition: xmalloc.h:53
#define xmalloc(x)
utility macro which calls malloc_f
Definition: xmalloc.h:49
#define Z_DiagRec_defaultFormat
Definition: z-core.h:736
#define Z_NamePlusRecord_surrogateDiagnostic
Definition: z-core.h:714
#define Z_DefaultDiagFormat_v3Addinfo
Definition: z-core.h:749
int z_NamePlusRecord(ODR o, Z_NamePlusRecord **p, int opt, const char *name)
Definition: z-core.c:593
#define Z_DefaultDiagFormat_v2Addinfo
Definition: z-core.h:748
int ZOOM_uri_to_code(const char *uri)
Definition: zoom-c.c:89
ZOOM_Event ZOOM_Event_create(int kind)
Definition: zoom-event.c:42
void ZOOM_connection_put_event(ZOOM_connection c, ZOOM_Event event)
Definition: zoom-event.c:73
Z_NamePlusRecord * ZOOM_memcached_lookup(ZOOM_resultset r, int pos, const char *syntax, const char *elementSetName, const char *schema)
void ZOOM_memcached_add(ZOOM_resultset r, Z_NamePlusRecord *npr, int pos, const char *syntax, const char *elementSetName, const char *schema, Z_SRW_diagnostic *diag)
Internal header for ZOOM implementation.
struct ZOOM_record_cache_p * ZOOM_record_cache
Definition: zoom-p.h:129
#define RECORD_HASH_SIZE
Definition: zoom-p.h:131
ZOOM_record_get(ZOOM_record rec, const char *type_spec, int *len)
ZOOM_resultset_cache_reset(ZOOM_resultset r)
static void ZOOM_record_release(ZOOM_record rec)
ZOOM_record ZOOM_record_cache_lookup(ZOOM_resultset r, int pos, const char *syntax, const char *elementSetName, const char *schema)
ZOOM_record_clone(ZOOM_record srec)
ZOOM_record_error(ZOOM_record rec, const char **cp, const char **addinfo, const char **diagset)
void ZOOM_record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, int pos, const char *syntax, const char *elementSetName, const char *schema, Z_SRW_diagnostic *diag)
static size_t record_hash(int pos)
ZOOM_record_destroy(ZOOM_record rec)
ZOOM_record ZOOM_record_cache_lookup_i(ZOOM_resultset r, int pos, const char *syntax, const char *elementSetName, const char *schema)
static ZOOM_record record_cache_add(ZOOM_resultset r, Z_NamePlusRecord *npr, int pos, const char *syntax, const char *elementSetName, const char *schema, Z_SRW_diagnostic *diag)
struct ZOOM_record_p * ZOOM_record
Definition: zoom.h:54
#define ZOOM_EVENT_RECV_RECORD
Definition: zoom.h:159
#define ZOOM_API(x)
Definition: zoom.h:42