2 * Copyright (c) 2002-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-cache.cpp,v 1.5 2003-10-01 13:13:51 adam Exp $
9 #include <yaz++/proxy.h>
11 struct Yaz_RecordCache_Entry {
13 Z_NamePlusRecord *m_record;
14 Z_RecordComposition *m_comp;
15 Yaz_RecordCache_Entry *m_next;
18 Yaz_RecordCache::Yaz_RecordCache ()
20 m_mem = nmem_create();
26 Yaz_RecordCache::~Yaz_RecordCache ()
31 void Yaz_RecordCache::clear ()
34 m_mem = nmem_create();
40 void Yaz_RecordCache::copy_searchRequest(Z_SearchRequest *sr)
42 ODR encode = odr_createmem(ODR_ENCODE);
43 ODR decode = odr_createmem(ODR_DECODE);
47 int v = z_SearchRequest (encode, &sr, 1, 0);
51 char *buf = odr_getbuf(encode, &len, 0);
52 odr_setbuf(decode, buf, len, 0);
53 z_SearchRequest(decode, &m_searchRequest, 1, 0);
54 nmem_transfer(m_mem, decode->mem);
60 void Yaz_RecordCache::copy_presentRequest(Z_PresentRequest *pr)
62 ODR encode = odr_createmem(ODR_ENCODE);
63 ODR decode = odr_createmem(ODR_DECODE);
67 int v = z_PresentRequest (encode, &pr, 1, 0);
71 char *buf = odr_getbuf(encode, &len, 0);
72 odr_setbuf(decode, buf, len, 0);
73 z_PresentRequest(decode, &m_presentRequest, 1, 0);
74 nmem_transfer(m_mem, decode->mem);
80 void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start,
83 // Build appropriate compspec for this response
84 Z_RecordComposition *comp = 0;
85 if (hits == -1 && m_presentRequest)
86 comp = m_presentRequest->recordComposition;
87 else if (hits > 0 && m_searchRequest)
89 Z_ElementSetNames *esn;
91 if (hits <= *m_searchRequest->smallSetUpperBound)
92 esn = m_searchRequest->smallSetElementSetNames;
94 esn = m_searchRequest->mediumSetElementSetNames;
95 comp = (Z_RecordComposition *) nmem_malloc(m_mem, sizeof(*comp));
96 comp->which = Z_RecordComp_simple;
100 // Z_NamePlusRecordList *npr to be owned by m_mem..
101 NMEM tmp_mem = odr_extract_mem(o);
102 nmem_transfer(m_mem, tmp_mem);
103 nmem_destroy(tmp_mem);
105 // Insert individual records in cache
107 for (i = 0; i<npr->num_records; i++)
109 Yaz_RecordCache_Entry *entry = (Yaz_RecordCache_Entry *)
110 nmem_malloc(m_mem, sizeof(*entry));
111 entry->m_record = npr->records[i];
112 entry->m_comp = comp;
113 entry->m_offset = i + start;
114 entry->m_next = m_entries;
119 int Yaz_RecordCache::match (Yaz_RecordCache_Entry *entry,
120 Odr_oid *syntax, int offset,
121 Z_RecordComposition *comp)
123 // See if our compspec match...
125 ODR o1 = odr_createmem(ODR_ENCODE);
126 ODR o2 = odr_createmem(ODR_ENCODE);
128 z_RecordComposition(o1, &comp, 1, 0);
129 z_RecordComposition(o2, &entry->m_comp, 1, 0);
132 char *buf1 = odr_getbuf(o1, &len1, 0);
134 char *buf2 = odr_getbuf(o2, &len2, 0);
136 if (buf1 && buf2 && len1 && len1 == len2 && !memcmp(buf1, buf2, len1))
138 else if (!buf1 && !buf2 && !len1 && !len2)
148 // See if offset, OID match..
149 if (entry->m_offset == offset &&
150 entry->m_record->which == Z_NamePlusRecord_databaseRecord &&
151 !oid_oidcmp(entry->m_record->u.databaseRecord->direct_reference,
157 int Yaz_RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr,
160 Z_RecordComposition *comp)
163 yaz_log(LOG_DEBUG, "cache lookup start=%d num=%d", start, num);
165 for (i = 0; i<num; i++)
167 Yaz_RecordCache_Entry *entry = m_entries;
168 for(; entry; entry = entry->m_next)
169 if (match(entry, syntax, start+i, comp))
174 *npr = (Z_NamePlusRecordList *) odr_malloc(o, sizeof(**npr));
175 (*npr)->num_records = num;
176 (*npr)->records = (Z_NamePlusRecord **)
177 odr_malloc(o, num * sizeof(Z_NamePlusRecord *));
178 for (i = 0; i<num; i++)
180 Yaz_RecordCache_Entry *entry = m_entries;
181 for(; entry; entry = entry->m_next)
182 if (match(entry, syntax, start+i, comp))
186 (*npr)->records[i] = entry->m_record;