X-Git-Url: http://sru.miketaylor.org.uk/?a=blobdiff_plain;f=src%2Fyaz-z-cache.cpp;h=366d19bbf411535b666811386b92743af0c1e9fa;hb=eea2caea1263bed0aae268f72d985cbb1822ac30;hp=8e521e6cd564894583c9b7cb15be70adf316c035;hpb=28588b9224eb5189af32b10f440ef2a917a05ea2;p=yazpp-moved-to-github.git diff --git a/src/yaz-z-cache.cpp b/src/yaz-z-cache.cpp index 8e521e6..366d19b 100644 --- a/src/yaz-z-cache.cpp +++ b/src/yaz-z-cache.cpp @@ -1,34 +1,43 @@ /* - * Copyright (c) 2002-2003, Index Data. + * Copyright (c) 2002-2004, Index Data. * See the file LICENSE for details. * - * $Id: yaz-z-cache.cpp,v 1.5 2003-10-01 13:13:51 adam Exp $ + * $Id: yaz-z-cache.cpp,v 1.13 2005-06-08 13:28:06 adam Exp $ */ #include -#include +#include +#include -struct Yaz_RecordCache_Entry { +using namespace yazpp_1; + +struct yazpp_1::RecordCache_Entry { int m_offset; Z_NamePlusRecord *m_record; Z_RecordComposition *m_comp; - Yaz_RecordCache_Entry *m_next; + RecordCache_Entry *m_next; }; -Yaz_RecordCache::Yaz_RecordCache () +RecordCache::RecordCache () { m_mem = nmem_create(); m_entries = 0; m_presentRequest = 0; m_searchRequest = 0; + m_max_size = 200000; } -Yaz_RecordCache::~Yaz_RecordCache () +RecordCache::~RecordCache () { nmem_destroy(m_mem); } -void Yaz_RecordCache::clear () +void RecordCache::set_max_size(int sz) +{ + m_max_size = sz; +} + +void RecordCache::clear () { nmem_destroy(m_mem); m_mem = nmem_create(); @@ -37,7 +46,7 @@ void Yaz_RecordCache::clear () m_searchRequest = 0; } -void Yaz_RecordCache::copy_searchRequest(Z_SearchRequest *sr) +void RecordCache::copy_searchRequest(Z_SearchRequest *sr) { ODR encode = odr_createmem(ODR_ENCODE); ODR decode = odr_createmem(ODR_DECODE); @@ -57,7 +66,7 @@ void Yaz_RecordCache::copy_searchRequest(Z_SearchRequest *sr) odr_destroy(decode); } -void Yaz_RecordCache::copy_presentRequest(Z_PresentRequest *pr) +void RecordCache::copy_presentRequest(Z_PresentRequest *pr) { ODR encode = odr_createmem(ODR_ENCODE); ODR decode = odr_createmem(ODR_DECODE); @@ -77,9 +86,11 @@ void Yaz_RecordCache::copy_presentRequest(Z_PresentRequest *pr) odr_destroy(decode); } -void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start, +void RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start, int hits) { + if (nmem_total(m_mem) > m_max_size) + return; // Build appropriate compspec for this response Z_RecordComposition *comp = 0; if (hits == -1 && m_presentRequest) @@ -106,9 +117,13 @@ void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start, int i; for (i = 0; inum_records; i++) { - Yaz_RecordCache_Entry *entry = (Yaz_RecordCache_Entry *) + RecordCache_Entry *entry = (RecordCache_Entry *) nmem_malloc(m_mem, sizeof(*entry)); - entry->m_record = npr->records[i]; + entry->m_record = (Z_NamePlusRecord *) + nmem_malloc(m_mem, sizeof(*entry->m_record)); + entry->m_record->databaseName = npr->records[i]->databaseName; + entry->m_record->which = npr->records[i]->which; + entry->m_record->u.databaseRecord = npr->records[i]->u.databaseRecord; entry->m_comp = comp; entry->m_offset = i + start; entry->m_next = m_entries; @@ -116,7 +131,7 @@ void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start, } } -int Yaz_RecordCache::match (Yaz_RecordCache_Entry *entry, +int RecordCache::match (RecordCache_Entry *entry, Odr_oid *syntax, int offset, Z_RecordComposition *comp) { @@ -142,7 +157,6 @@ int Yaz_RecordCache::match (Yaz_RecordCache_Entry *entry, odr_destroy(o2); if (!match) return 0; - if (!syntax) return 0; // See if offset, OID match.. @@ -151,20 +165,28 @@ int Yaz_RecordCache::match (Yaz_RecordCache_Entry *entry, !oid_oidcmp(entry->m_record->u.databaseRecord->direct_reference, syntax)) return 1; +#if 0 + char mstr1[100]; + oid_to_dotstring(entry->m_record->u.databaseRecord->direct_reference, mstr1); + char mstr2[100]; + oid_to_dotstring(syntax, mstr2); + yaz_log(YLOG_LOG, "match fail 3 d=%s s=%s", mstr1, mstr2); +#endif + return 0; } -int Yaz_RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr, +int RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num, Odr_oid *syntax, Z_RecordComposition *comp) { int i; - yaz_log(LOG_DEBUG, "cache lookup start=%d num=%d", start, num); + yaz_log(YLOG_DEBUG, "cache lookup start=%d num=%d", start, num); for (i = 0; im_next) if (match(entry, syntax, start+i, comp)) break; @@ -177,13 +199,18 @@ int Yaz_RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr, odr_malloc(o, num * sizeof(Z_NamePlusRecord *)); for (i = 0; im_next) if (match(entry, syntax, start+i, comp)) break; if (!entry) return 0; - (*npr)->records[i] = entry->m_record; + (*npr)->records[i] = (Z_NamePlusRecord *) + odr_malloc(o, sizeof(Z_NamePlusRecord)); + (*npr)->records[i]->databaseName = entry->m_record->databaseName; + (*npr)->records[i]->which = entry->m_record->which; + (*npr)->records[i]->u.databaseRecord = + entry->m_record->u.databaseRecord; } return 1; }