2 * Copyright (c) 1998-2003, Index Data.
3 * See the file LICENSE for details.
5 * $Id: yaz-z-cache.cpp,v 1.1 2003-07-18 13:27:20 adam Exp $
9 #include <yaz++/proxy.h>
11 struct Yaz_RecordCache_Entry {
13 Z_NamePlusRecord *m_record;
14 Yaz_RecordCache_Entry *m_next;
18 Yaz_RecordCache::Yaz_RecordCache ()
20 m_mem = nmem_create();
24 Yaz_RecordCache::~Yaz_RecordCache ()
29 void Yaz_RecordCache::clear ()
32 m_mem = nmem_create();
37 void Yaz_RecordCache::prepare_present(Z_RecordComposition *comp)
40 m_recordComposition = 0;
43 m_recordComposition = nmem_malloc(m_mem, sizeof(*m_recordComposition));
44 m_recordComposition->which = comp->which;
45 if (comp->which == Z_RecordComp_simple)
47 m_recordComposition->u.simple = (Z_ElementSetNames *)
48 nmem_malloc(m_mem, sizeof(Z_ElementSetNames));
55 void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start)
57 NMEM tmp_mem = odr_extract_mem(o);
58 nmem_transfer(m_mem, tmp_mem);
59 nmem_destroy(tmp_mem);
62 for (i = 0; i<npr->num_records; i++)
64 Yaz_RecordCache_Entry *entry = (Yaz_RecordCache_Entry *)
65 nmem_malloc(m_mem, sizeof(*entry));
66 entry->m_record = npr->records[i];
67 entry->m_offset = i + start;
68 entry->m_next = m_entries;
73 int Yaz_RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr,
78 yaz_log(LOG_LOG, "cache lookup start=%d num=%d", start, num);
80 for (i = 0; i<num; i++)
82 Yaz_RecordCache_Entry *entry = m_entries;
83 for(; entry; entry = entry->m_next)
84 if (entry->m_offset == start + i &&
85 entry->m_record->which == Z_NamePlusRecord_databaseRecord &&
86 !oid_oidcmp(entry->m_record->u.databaseRecord->direct_reference,
92 *npr = (Z_NamePlusRecordList *) odr_malloc(o, sizeof(**npr));
93 (*npr)->num_records = num;
94 (*npr)->records = (Z_NamePlusRecord **)
95 odr_malloc(o, num * sizeof(Z_NamePlusRecord *));
96 for (i = 0; i<num; i++)
98 Yaz_RecordCache_Entry *entry = m_entries;
99 for(; entry; entry = entry->m_next)
100 if (entry->m_offset == start + i &&
101 entry->m_record->which == Z_NamePlusRecord_databaseRecord &&
102 !oid_oidcmp(entry->m_record->u.databaseRecord->direct_reference,
107 (*npr)->records[i] = entry->m_record;