<commit_before>/*
 * Copyright (c) 2002-2003, Index Data.
 * See the file LICENSE for details.
 * 
 * $Id: yaz-z-cache.cpp,v 1.2 2003-07-25 08:57:01 adam Exp $
 */

#include <yaz/log.h>
#include <yaz++/proxy.h>

struct Yaz_RecordCache_Entry {
    int m_offset;
    Z_NamePlusRecord *m_record;
    Z_RecordComposition *m_comp;
    Yaz_RecordCache_Entry *m_next;
};

Yaz_RecordCache::Yaz_RecordCache ()
{
    m_mem = nmem_create();
    m_entries = 0;
    m_presentRequest = 0;
    m_searchRequest = 0;
}

Yaz_RecordCache::~Yaz_RecordCache ()
{
    nmem_destroy(m_mem);
}

void Yaz_RecordCache::clear ()
{
    nmem_destroy(m_mem);
    m_mem = nmem_create();
    m_entries = 0;
    m_presentRequest = 0;
    m_searchRequest = 0;
}

void Yaz_RecordCache::copy_searchRequest(Z_SearchRequest *sr)
{
    ODR encode = odr_createmem(ODR_ENCODE);
    ODR decode = odr_createmem(ODR_DECODE);

    m_searchRequest = 0;
    m_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);
    }
    odr_destroy(encode);
    odr_destroy(decode);
}

void Yaz_RecordCache::copy_presentRequest(Z_PresentRequest *pr)
{
    ODR encode = odr_createmem(ODR_ENCODE);
    ODR decode = odr_createmem(ODR_DECODE);
    
    m_searchRequest = 0;
    m_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);
    }
    odr_destroy(encode);
    odr_destroy(decode);
}

void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start,
			   int hits)
{
    // 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)
    {
	Z_ElementSetNames *esn;

	if (hits <= *m_searchRequest->smallSetUpperBound)
	    esn = m_searchRequest->smallSetElementSetNames;
	else
	    esn = m_searchRequest->mediumSetElementSetNames;
	comp = (Z_RecordComposition *) nmem_malloc(m_mem, sizeof(*comp));
	comp->which = Z_RecordComp_simple;
	comp->u.simple = esn;
    }

    // Z_NamePlusRecordList *npr to be owned by m_mem..
    NMEM tmp_mem = odr_extract_mem(o);
    nmem_transfer(m_mem, tmp_mem);
    nmem_destroy(tmp_mem);
    
    // Insert individual records in cache
    int i;
    for (i = 0; i<npr->num_records; i++)
    {
	Yaz_RecordCache_Entry *entry = (Yaz_RecordCache_Entry *)
	    nmem_malloc(m_mem, sizeof(*entry));
	entry->m_record = npr->records[i];
	entry->m_comp = comp;
	entry->m_offset = i + start;
	entry->m_next = m_entries;
	m_entries = entry;
    }
}

int Yaz_RecordCache::match (Yaz_RecordCache_Entry *entry,
			    Odr_oid *syntax, int offset,
			    Z_RecordComposition *comp)
{
    // See if our compspec match...
    int match = 0;
    ODR o1 = odr_createmem(ODR_ENCODE);
    ODR o2 = odr_createmem(ODR_ENCODE);
    
    z_RecordComposition(o1, &comp, 1, 0);
    z_RecordComposition(o2, &entry->m_comp, 1, 0);

    int len1 = -1;
    char *buf1 = odr_getbuf(o1, &len1, 0);
    int len2 = -1;
    char *buf2 = odr_getbuf(o2, &len2, 0);
    
    yaz_log(LOG_LOG, "buf1=%p buf2=%p len1=%d len2=%d", buf1, buf2, len1, len2);
    if (buf1 && buf2 && len1 && len1 == len2 && !memcmp(buf1, buf2, len1))
	match = 1;
    else if (!buf1 && !buf2 && !len1 && !len2)
	match = 1;
    
    odr_destroy(o1);<commit_msg>Dont log cache match<commit_after>/*
 * Copyright (c) 2002-2003, Index Data.
 * See the file LICENSE for details.
 * 
 * $Id: yaz-z-cache.cpp,v 1.3 2003-07-25 19:28:07 adam Exp $
 */

#include <yaz/log.h>
#include <yaz++/proxy.h>

struct Yaz_RecordCache_Entry {
    int m_offset;
    Z_NamePlusRecord *m_record;
    Z_RecordComposition *m_comp;
    Yaz_RecordCache_Entry *m_next;
};

Yaz_RecordCache::Yaz_RecordCache ()
{
    m_mem = nmem_create();
    m_entries = 0;
    m_presentRequest = 0;
    m_searchRequest = 0;
}

Yaz_RecordCache::~Yaz_RecordCache ()
{
    nmem_destroy(m_mem);
}

void Yaz_RecordCache::clear ()
{
    nmem_destroy(m_mem);
    m_mem = nmem_create();
    m_entries = 0;
    m_presentRequest = 0;
    m_searchRequest = 0;
}

void Yaz_RecordCache::copy_searchRequest(Z_SearchRequest *sr)
{
    ODR encode = odr_createmem(ODR_ENCODE);
    ODR decode = odr_createmem(ODR_DECODE);

    m_searchRequest = 0;
    m_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);
    }
    odr_destroy(encode);
    odr_destroy(decode);
}

void Yaz_RecordCache::copy_presentRequest(Z_PresentRequest *pr)
{
    ODR encode = odr_createmem(ODR_ENCODE);
    ODR decode = odr_createmem(ODR_DECODE);
    
    m_searchRequest = 0;
    m_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);
    }
    odr_destroy(encode);
    odr_destroy(decode);
}

void Yaz_RecordCache::add (ODR o, Z_NamePlusRecordList *npr, int start,
			   int hits)
{
    // 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)
    {
	Z_ElementSetNames *esn;

	if (hits <= *m_searchRequest->smallSetUpperBound)
	    esn = m_searchRequest->smallSetElementSetNames;
	else
	    esn = m_searchRequest->mediumSetElementSetNames;
	comp = (Z_RecordComposition *) nmem_malloc(m_mem, sizeof(*comp));
	comp->which = Z_RecordComp_simple;
	comp->u.simple = esn;
    }

    // Z_NamePlusRecordList *npr to be owned by m_mem..
    NMEM tmp_mem = odr_extract_mem(o);
    nmem_transfer(m_mem, tmp_mem);
    nmem_destroy(tmp_mem);
    
    // Insert individual records in cache
    int i;
    for (i = 0; i<npr->num_records; i++)
    {
	Yaz_RecordCache_Entry *entry = (Yaz_RecordCache_Entry *)
	    nmem_malloc(m_mem, sizeof(*entry));
	entry->m_record = npr->records[i];
	entry->m_comp = comp;
	entry->m_offset = i + start;
	entry->m_next = m_entries;
	m_entries = entry;
    }
}

int Yaz_RecordCache::match (Yaz_RecordCache_Entry *entry,
			    Odr_oid *syntax, int offset,
			    Z_RecordComposition *comp)
{
    // See if our compspec match...
    int match = 0;
    ODR o1 = odr_createmem(ODR_ENCODE);
    ODR o2 = odr_createmem(ODR_ENCODE);
    
    z_RecordComposition(o1, &comp, 1, 0);
    z_RecordComposition(o2, &entry->m_comp, 1, 0);

    int len1 = -1;
    char *buf1 = odr_getbuf(o1, &len1, 0);
    int len2 = -1;
    char *buf2 = odr_getbuf(o2, &len2, 0);
    
    if (buf1 && buf2 && len1 && len1 == len2 && !memcmp(buf1, buf2, len1))
	match = 1;
    else if (!buf1 && !buf2 && !len1 && !len2)
	match = 1;
    
    odr_destroy(o1);
{'fraction_non_alphanumeric': 0.13086968602389554, 'fraction_numerical': 0.025006946373992776, 'mean_word_length': 3.5191462649089766, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5353792', 'n_tokens_mistral': 2998, 'n_tokens_neox': 2857, 'n_words': 599}