Record cache. syntax used for matching. Compspec is not (yet)
[yazpp-moved-to-github.git] / src / yaz-z-cache.cpp
1 /*
2  * Copyright (c) 1998-2003, Index Data.
3  * See the file LICENSE for details.
4  * 
5  * $Id: yaz-z-cache.cpp,v 1.1 2003-07-18 13:27:20 adam Exp $
6  */
7
8 #include <yaz/log.h>
9 #include <yaz++/proxy.h>
10
11 struct Yaz_RecordCache_Entry {
12     int m_offset;
13     Z_NamePlusRecord *m_record;
14     Yaz_RecordCache_Entry *m_next;
15 };
16
17
18 Yaz_RecordCache::Yaz_RecordCache ()
19 {
20     m_mem = nmem_create();
21     m_entries = 0;
22 }
23
24 Yaz_RecordCache::~Yaz_RecordCache ()
25 {
26     nmem_destroy(m_mem);
27 }
28
29 void Yaz_RecordCache::clear ()
30 {
31     nmem_destroy(m_mem);
32     m_mem = nmem_create();
33     m_entries = 0;
34 }
35
36 #if 0
37 void Yaz_RecordCache::prepare_present(Z_RecordComposition *comp)
38 {
39     if (!comp)
40         m_recordComposition = 0;
41     else
42     {
43         m_recordComposition = nmem_malloc(m_mem, sizeof(*m_recordComposition));
44         m_recordComposition->which = comp->which;
45         if (comp->which == Z_RecordComp_simple)
46         {
47             m_recordComposition->u.simple = (Z_ElementSetNames *)
48                 nmem_malloc(m_mem, sizeof(Z_ElementSetNames));
49         }
50     }
51
52 }
53 #endif
54
55 void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start)
56 {
57     NMEM tmp_mem = odr_extract_mem(o);
58     nmem_transfer(m_mem, tmp_mem);
59     nmem_destroy(tmp_mem);
60
61     int i;
62     for (i = 0; i<npr->num_records; i++)
63     {
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;
69         m_entries = entry;
70     }
71 }
72
73 int Yaz_RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr,
74                              int start, int num,
75                              Odr_oid *syntax)
76 {
77     int i;
78     yaz_log(LOG_LOG, "cache lookup start=%d num=%d", start, num);
79
80     for (i = 0; i<num; i++)
81     {
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,
87                             syntax))
88                 break;
89         if (!entry)
90             return 0;
91     }
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++)
97     {
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,
103                             syntax))
104                 break;
105         if (!entry)
106             return 0;
107         (*npr)->records[i] = entry->m_record;
108     }
109     return 1;
110 }