Changes on QC

removed KV_Btree_Array.cpp
removed query_SQL from MySQL_Data_Stream
added some documentation for QC_entry_t
Query_Cache::get() and Query_Cache::set() now requires also the length of the key
Query_Cache::get() and Query_Cache::set() now process hk (the hash key) using the length of the key
SQLiteServer
René Cannaò 10 years ago
parent d22bf01737
commit 60ddf5d8e4

@ -105,7 +105,7 @@ class MySQL_Data_Stream
PtrSizeArray *PSarrayOUTpending;
PtrSizeArray *resultset;
unsigned int resultset_length;
unsigned char * query_SQL;
// unsigned char * query_SQL;
int active_transaction; // 1 if there is an active transaction
int active; // data stream is active. If not, shutdown+close needs to be called

@ -18,15 +18,15 @@ class KV_BtreeArray;
typedef struct __QC_entry_t QC_entry_t;
struct __QC_entry_t {
uint64_t key;
char *value;
KV_BtreeArray *kv;
QC_entry_t *self;
uint32_t klen;
uint32_t length;
time_t expire;
time_t access;
uint32_t ref_count;
uint64_t key; // primary key
char *value; // pointer to value
KV_BtreeArray *kv; // pointer to the KV_BtreeArray where the entry is stored
QC_entry_t *self; // pointer to itself
uint32_t klen; // length of the key : FIXME: not sure if still relevant
uint32_t length; // length of the value
time_t expire; // when the entry will expire: FIXME: should have a millisecond granularity
time_t access; // when the entry was read last: FIXME: should have a millisecond granularity
uint32_t ref_count; // reference counter
};
typedef btree::btree_map<uint64_t, QC_entry_t *> BtMap_cache;
@ -72,8 +72,8 @@ class Query_Cache {
Query_Cache();
~Query_Cache();
void print_version();
bool set(unsigned char *, uint32_t, unsigned char *, uint32_t, time_t);
unsigned char * get(const unsigned char *, uint32_t *);
bool set(const unsigned char *, uint32_t, unsigned char *, uint32_t, time_t);
unsigned char * get(const unsigned char *, const uint32_t, uint32_t *);
uint64_t flush();
};
#endif /* __CLASS_QUERY_CACHE_H */

@ -1,176 +0,0 @@
#include "btree_map.h"
#include "proxysql.h"
#include "cpp.h"
using namespace std;
/*
AdvancedKV::AdvancedKV() {
// dataSize=0;
};
*/
/*
struct classcomp1 {
bool operator() (char *a, char *b) const
{return strcmp(a,b); }
};
//typedef btree::btree_map<char *, char *, classcomp> MyMap;
*/
KV_BtreeArray::KV_BtreeArray() {
spinlock_rwlock_init(&lock);
}
/*
KV_BtreeAray::set((char *kp, void *entry)
//spin_wrlock(&fdb_hashes[i].lock);
spin_wrlock(&rwlock);
ptrArray->add(entry);
btree::btree_map<const char *, char *, classcomp>::iterator lookup;
lookup = bt_map.find(key);
if (lookup != bt_map.end()) {
g_hash_table_replace(fdb_hashes[i].hash, (void *)entry->key, (void *)entry);
spin_wrunlock(&lock);
*/
/*
void SimpleKV::insert(const char *key, char *value) {
if (lock_enabled)
spin_wrlock(&rwlock);
//g_hash_table_insert(hash, key, value);
bt_map.insert(std::make_pair(key,value));
if (lock_enabled)
spin_wrunlock(&rwlock);
}
void SimpleKV::insert_copy(const char *key, char * value) {
//std::string *key_copy=key;
char * key_copy=strdup(key);
char * value_copy=strdup(value);
if (lock_enabled)
spin_wrlock(&rwlock);
//g_hash_table_insert(hash, key_copy, value_copy);
bt_map.insert(std::make_pair(key_copy,value_copy));
if (lock_enabled)
spin_wrunlock(&rwlock);
}
void SimpleKV::replace(const char *key, char * value) {
if (lock_enabled)
spin_wrlock(&rwlock);
//g_hash_table_replace(hash, key, value);
//btree::btree_map<std::string *, char *>::iterator lookup;
btree::btree_map<const char *, char *, classcomp>::iterator lookup;
//btree::btree_map<char *, char *, classcomp>::iterator lookup ;
//btree::btree_map<char *, char *, classcomp>::iterator lookup ;
lookup = bt_map.find(key);
//printf("%s\n", *lookup->first) ;
if (lookup != bt_map.end()) {
const char *f=lookup->first;
free(lookup->second);
bt_map.erase(lookup);
free((void *)f);
}
bt_map.insert(std::make_pair(key,value));
if (lock_enabled)
spin_wrunlock(&rwlock);
}
void SimpleKV::replace_copy(char *key, char * value) {
char * key_copy=strdup((const char *)key);
char * value_copy=strdup((const char *)value);
replace(key_copy, value_copy);
if (lock_enabled)
spin_wrlock(&rwlock);
g_hash_table_replace(hash, key_copy, value_copy);
if (lock_enabled)
spin_wrunlock(&rwlock);
}
char * SimpleKV::lookup(const char * key) {
char *v=NULL;
if (lock_enabled)
spin_rdlock(&rwlock);
btree::btree_map<const char *, char *, classcomp>::iterator lookup;
lookup = bt_map.find(key);
if (lookup != bt_map.end()) {
v=lookup->second;
}
if (lock_enabled)
spin_rdunlock(&rwlock);
return v;
}
char * SimpleKV::lookup_copy(const char * key) {
char *v=NULL;
char *r=NULL;
if (lock_enabled)
spin_rdlock(&rwlock);
btree::btree_map<const char *, char *, classcomp>::iterator lookup;
lookup = bt_map.find(key);
if (lookup != bt_map.end()) {
r=lookup->second;
}
if (r)
v=strdup((const char *)r);
if (lock_enabled)
spin_rdunlock(&rwlock);
return v;
}
void SimpleKV::remove(const char *key) {
if (lock_enabled)
spin_wrlock(&rwlock);
btree::btree_map<const char *, char *, classcomp>::iterator lookup;
lookup = bt_map.find(key);
if (lookup != bt_map.end()) {
const char *f=lookup->first;
free(lookup->second);
bt_map.erase(lookup);
free((void *)f);
}
if (lock_enabled)
spin_wrunlock(&rwlock);
}
void SimpleKV::empty() {
if (lock_enabled)
spin_wrlock(&rwlock);
btree::btree_map<const char *, char *, classcomp>::iterator lookup;
while (bt_map.size()) {
lookup = bt_map.begin();
if ( lookup != bt_map.end() ) {
//free((char *)lookup.first);
const char *f=lookup->first;
free(lookup->second);
bt_map.erase(lookup);
free((void *)f);
}
}
if (lock_enabled)
spin_wrunlock(&rwlock);
}
SimpleKV::~SimpleKV() {
//g_hash_table_destroy(hash);
empty();
}
int SimpleKV::size() {
return bt_map.size();
}
*/

@ -1637,14 +1637,14 @@ bool MySQL_Session::handler___status_WAITING_CLIENT_DATA___STATE_SLEEP___MYSQL_C
delete qpo->new_query;
}
if (qpo->cache_ttl>0) {
client_myds->query_SQL=(unsigned char *)l_alloc(pkt->size-sizeof(mysql_hdr));
memcpy(client_myds->query_SQL,(unsigned char *)pkt->ptr+sizeof(mysql_hdr)+1,pkt->size-sizeof(mysql_hdr)-1);
client_myds->query_SQL[pkt->size-sizeof(mysql_hdr)-1]=0;
// client_myds->query_SQL=(unsigned char *)l_alloc(pkt->size-sizeof(mysql_hdr));
// memcpy(client_myds->query_SQL,(unsigned char *)pkt->ptr+sizeof(mysql_hdr)+1,pkt->size-sizeof(mysql_hdr)-1);
// client_myds->query_SQL[pkt->size-sizeof(mysql_hdr)-1]=0;
uint32_t resbuf=0;
unsigned char *aa=GloQC->get(client_myds->query_SQL,&resbuf);
unsigned char *aa=GloQC->get((const unsigned char *)client_myds->mysql_real_query.QueryPtr , client_myds->mysql_real_query.QuerySize , &resbuf);
if (aa) {
l_free(pkt->size,pkt->ptr);
l_free(strlen((char *)client_myds->query_SQL)+1,client_myds->query_SQL);
// l_free(strlen((char *)client_myds->query_SQL)+1,client_myds->query_SQL);
client_myds->buffer2resultset(aa,resbuf);
free(aa);
client_myds->PSarrayOUT->copy_add(client_myds->resultset,0,client_myds->resultset->len);
@ -1798,10 +1798,10 @@ void MySQL_Session::MySQL_Result_to_MySQL_wire(MYSQL *mysql, MySQL_ResultSet *My
client_myds->resultset_length=MyRS->resultset_size;
unsigned char *aa=client_myds->resultset2buffer(false);
while (client_myds->resultset->len) client_myds->resultset->remove_index(client_myds->resultset->len-1,NULL);
GloQC->set((unsigned char *)client_myds->query_SQL,strlen((char *)client_myds->query_SQL)+1,aa,client_myds->resultset_length,qpo->cache_ttl);
GloQC->set((const unsigned char *)client_myds->mysql_real_query.QueryPtr , client_myds->mysql_real_query.QuerySize , aa,client_myds->resultset_length,qpo->cache_ttl);
l_free(client_myds->resultset_length,aa);
client_myds->resultset_length=0;
l_free(strlen((char *)client_myds->query_SQL)+1,client_myds->query_SQL);
// l_free(strlen((char *)client_myds->query_SQL)+1,client_myds->query_SQL);
}
}
}

@ -75,7 +75,7 @@ typedef btree::btree_map<uint64_t, QC_entry_t *> BtMap;
KV_BtreeArray::KV_BtreeArray() {
KV_BtreeArray::KV_BtreeArray() {
freeable_memory=0;
tottopurge=0;
spinlock_rwlock_init(&lock);
@ -259,10 +259,10 @@ Query_Cache::~Query_Cache() {
unsigned char * Query_Cache::get(const unsigned char *kp, uint32_t *lv) {
unsigned char * Query_Cache::get(const unsigned char *kp, const uint32_t kl, uint32_t *lv) {
unsigned char *result=NULL;
uint64_t hk=SpookyHash::Hash64(kp,strlen((const char *)kp),0);
uint64_t hk=SpookyHash::Hash64(kp, kl,0);
unsigned char i=hk%SHARED_QUERY_CACHE_HASH_TABLES;
QC_entry_t *entry=KVs[i].lookup(hk);
@ -280,7 +280,7 @@ unsigned char * Query_Cache::get(const unsigned char *kp, uint32_t *lv) {
return result;
}
bool Query_Cache::set(unsigned char *kp, uint32_t kl, unsigned char *vp, uint32_t vl, time_t expire) {
bool Query_Cache::set(const unsigned char *kp, uint32_t kl, unsigned char *vp, uint32_t vl, time_t expire) {
QC_entry_t *entry = (QC_entry_t *)malloc(sizeof(QC_entry_t));
entry->klen=kl;
entry->length=vl;
@ -295,7 +295,7 @@ bool Query_Cache::set(unsigned char *kp, uint32_t kl, unsigned char *vp, uint32_
} else {
entry->expire=QCnow+expire; // expire is seconds
}
uint64_t hk=SpookyHash::Hash64(kp,strlen((const char *)kp),0);
uint64_t hk=SpookyHash::Hash64(kp, kl, 0);
unsigned char i=hk%SHARED_QUERY_CACHE_HASH_TABLES;
entry->key=hk;
KVs[i].replace(hk, entry);

@ -109,7 +109,7 @@ MySQL_Data_Stream::MySQL_Data_Stream() {
connect_tries=0;
poll_fds_idx=-1;
resultset_length=0;
query_SQL=NULL;
// query_SQL=NULL;
PSarrayIN=NULL;
PSarrayOUT=NULL;

Loading…
Cancel
Save