From 51d68e30cd2aea2ec189bee63378744a6bcf9ca5 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Mon, 12 Nov 2012 14:04:46 +0100 Subject: [PATCH] Pimpl RecordCache class --- include/yazpp/record-cache.h | 18 +++----- src/yaz-z-cache.cpp | 103 ++++++++++++++++++++++++------------------ 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/include/yazpp/record-cache.h b/include/yazpp/record-cache.h index b7f1e31..8fa2254 100644 --- a/include/yazpp/record-cache.h +++ b/include/yazpp/record-cache.h @@ -30,29 +30,25 @@ #include namespace yazpp_1 { -struct RecordCache_Entry; class YAZ_EXPORT RecordCache { public: RecordCache (); ~RecordCache (); - void add (ODR o, Z_NamePlusRecordList *npr, int start, int hits); + void add(ODR o, Z_NamePlusRecordList *npr, int start, int hits); - int lookup (ODR o, Z_NamePlusRecordList **npr, int start, int num, - Odr_oid *syntax, Z_RecordComposition *comp); + int lookup(ODR o, Z_NamePlusRecordList **npr, int start, int num, + Odr_oid *syntax, Z_RecordComposition *comp); void clear(); void copy_searchRequest(Z_SearchRequest *sr); void copy_presentRequest(Z_PresentRequest *pr); void set_max_size(size_t sz); private: - NMEM m_mem; - RecordCache_Entry *m_entries; - Z_SearchRequest *m_searchRequest; - Z_PresentRequest *m_presentRequest; - int match(RecordCache_Entry *entry, Odr_oid *syntax, int offset, - Z_RecordComposition *comp); - size_t m_max_size; + struct RecordCache_Entry; + struct Rep; + + Rep *m_p; }; }; /* diff --git a/src/yaz-z-cache.cpp b/src/yaz-z-cache.cpp index 3ab0ba6..84a4fef 100644 --- a/src/yaz-z-cache.cpp +++ b/src/yaz-z-cache.cpp @@ -13,7 +13,17 @@ using namespace yazpp_1; -struct yazpp_1::RecordCache_Entry { +struct RecordCache::Rep { + NMEM nmem; + RecordCache_Entry *entries; + Z_SearchRequest *searchRequest; + Z_PresentRequest *presentRequest; + int match(RecordCache_Entry *entry, Odr_oid *syntax, int offset, + Z_RecordComposition *comp); + size_t max_size; +}; + +struct RecordCache::RecordCache_Entry { int m_offset; Z_NamePlusRecord *m_record; Z_RecordComposition *m_comp; @@ -22,30 +32,32 @@ struct yazpp_1::RecordCache_Entry { RecordCache::RecordCache () { - m_mem = nmem_create(); - m_entries = 0; - m_presentRequest = 0; - m_searchRequest = 0; - m_max_size = 200000; + m_p = new Rep; + m_p->nmem = nmem_create(); + m_p->entries = 0; + m_p->presentRequest = 0; + m_p->searchRequest = 0; + m_p->max_size = 200000; } RecordCache::~RecordCache () { - nmem_destroy(m_mem); + nmem_destroy(m_p->nmem); + delete m_p; } void RecordCache::set_max_size(size_t sz) { - m_max_size = sz; + m_p->max_size = sz; } void RecordCache::clear () { - nmem_destroy(m_mem); - m_mem = nmem_create(); - m_entries = 0; - m_presentRequest = 0; - m_searchRequest = 0; + nmem_destroy(m_p->nmem); + m_p->nmem = nmem_create(); + m_p->entries = 0; + m_p->presentRequest = 0; + m_p->searchRequest = 0; } void RecordCache::copy_searchRequest(Z_SearchRequest *sr) @@ -53,16 +65,16 @@ void RecordCache::copy_searchRequest(Z_SearchRequest *sr) ODR encode = odr_createmem(ODR_ENCODE); ODR decode = odr_createmem(ODR_DECODE); - m_searchRequest = 0; - m_presentRequest = 0; + m_p->searchRequest = 0; + m_p->presentRequest = 0; int v = z_SearchRequest (encode, &sr, 1, 0); if (v) { int len; char *buf = odr_getbuf(encode, &len, 0); odr_setbuf(decode, buf, len, 0); - z_SearchRequest(decode, &m_searchRequest, 1, 0); - nmem_transfer(m_mem, decode->mem); + z_SearchRequest(decode, &m_p->searchRequest, 1, 0); + nmem_transfer(m_p->nmem, decode->mem); } odr_destroy(encode); odr_destroy(decode); @@ -73,40 +85,40 @@ void RecordCache::copy_presentRequest(Z_PresentRequest *pr) ODR encode = odr_createmem(ODR_ENCODE); ODR decode = odr_createmem(ODR_DECODE); - m_searchRequest = 0; - m_presentRequest = 0; + m_p->searchRequest = 0; + m_p->presentRequest = 0; int v = z_PresentRequest (encode, &pr, 1, 0); if (v) { int len; char *buf = odr_getbuf(encode, &len, 0); odr_setbuf(decode, buf, len, 0); - z_PresentRequest(decode, &m_presentRequest, 1, 0); - nmem_transfer(m_mem, decode->mem); + z_PresentRequest(decode, &m_p->presentRequest, 1, 0); + nmem_transfer(m_p->nmem, decode->mem); } odr_destroy(encode); odr_destroy(decode); } -void RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start, - int hits) +void RecordCache::add(ODR o, Z_NamePlusRecordList *npr, int start, + int hits) { - if (nmem_total(m_mem) > m_max_size) + if (nmem_total(m_p->nmem) > m_p->max_size) return; // Build appropriate compspec for this response Z_RecordComposition *comp = 0; - if (hits == -1 && m_presentRequest) - comp = m_presentRequest->recordComposition; - else if (hits > 0 && m_searchRequest) + if (hits == -1 && m_p->presentRequest) + comp = m_p->presentRequest->recordComposition; + else if (hits > 0 && m_p->searchRequest) { Z_ElementSetNames *esn; - if (hits <= *m_searchRequest->smallSetUpperBound) - esn = m_searchRequest->smallSetElementSetNames; + if (hits <= *m_p->searchRequest->smallSetUpperBound) + esn = m_p->searchRequest->smallSetElementSetNames; else - esn = m_searchRequest->mediumSetElementSetNames; - comp = (Z_RecordComposition *) nmem_malloc(m_mem, sizeof(*comp)); + esn = m_p->searchRequest->mediumSetElementSetNames; + comp = (Z_RecordComposition *) nmem_malloc(m_p->nmem, sizeof(*comp)); comp->which = Z_RecordComp_simple; comp->u.simple = esn; } @@ -116,16 +128,17 @@ void RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start, for (i = 0; inum_records; i++) { RecordCache_Entry *entry = (RecordCache_Entry *) - nmem_malloc(m_mem, sizeof(*entry)); - entry->m_record = yaz_clone_z_NamePlusRecord(npr->records[i], m_mem); - entry->m_comp = yaz_clone_z_RecordComposition(comp, m_mem); + nmem_malloc(m_p->nmem, sizeof(*entry)); + entry->m_record = + yaz_clone_z_NamePlusRecord(npr->records[i], m_p->nmem); + entry->m_comp = yaz_clone_z_RecordComposition(comp, m_p->nmem); entry->m_offset = i + start; - entry->m_next = m_entries; - m_entries = entry; + entry->m_next = m_p->entries; + m_p->entries = entry; } } -int RecordCache::match (RecordCache_Entry *entry, +int RecordCache::Rep::match(RecordCache_Entry *entry, Odr_oid *syntax, int offset, Z_RecordComposition *comp) { @@ -170,19 +183,19 @@ int RecordCache::match (RecordCache_Entry *entry, return 0; } -int RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr, - int start, int num, - Odr_oid *syntax, - Z_RecordComposition *comp) +int RecordCache::lookup(ODR o, Z_NamePlusRecordList **npr, + int start, int num, + Odr_oid *syntax, + Z_RecordComposition *comp) { int i; yaz_log(YLOG_DEBUG, "cache lookup start=%d num=%d", start, num); for (i = 0; ientries; for(; entry; entry = entry->m_next) - if (match(entry, syntax, start+i, comp)) + if (m_p->match(entry, syntax, start+i, comp)) break; if (!entry) return 0; @@ -193,9 +206,9 @@ int RecordCache::lookup (ODR o, Z_NamePlusRecordList **npr, odr_malloc(o, num * sizeof(Z_NamePlusRecord *)); for (i = 0; ientries; for(; entry; entry = entry->m_next) - if (match(entry, syntax, start+i, comp)) + if (m_p->match(entry, syntax, start+i, comp)) break; if (!entry) return 0; -- 1.7.10.4