eis/inc/zlib/DB2Mem.hpp

93 lines
1.6 KiB
C++

/************************************************
*
* read table data to memory
*
* create zoufuzhou 20201001
*
************************************************/
#ifndef _DATABASE_TO_MEM_CACHE_HPP_
#define _DATABASE_TO_MEM_CACHE_HPP_
#include <glob/GlobMem.h>
template <class T,typename S> class DB2Mem
{
typedef struct{
unsigned int maxsize;
unsigned int size;
unsigned int count;
}HEAD;
private:
HEAD* p_head;
S* p_ptr;
public:
DB2Mem(const string& tableName)
{
char* ptr = (char*)GlobMem::GetInstancePtr()->GetTablePtr(tableName);
if(ptr == nullptr)
{
p_ptr = nullptr;
p_head = nullptr;
return;
}
p_head = (HEAD*)(ptr);
p_ptr = (S*)(ptr+sizeof(HEAD));
p_head->maxsize = 100;
}
~DB2Mem()
{
return;
}
public:
S* operator()(void)
{
return p_ptr;
};
S* operator[](int i)
{
if(p_ptr == nullptr) return nullptr;
return p_ptr+i;
};
long count(void)
{
if(p_head == nullptr)return 0;
return p_head->size;
};
/* Read DB Table Data to memory */
int readDB(char* dbwhere = nullptr,char* order = nullptr)
{
if( p_ptr == nullptr ) {
return -1;
}
p_head->size = 0;
T tb;
char* dbMsg = tb.openSetDB(dbwhere,order);
if( dbMsg != nullptr)
{
tb.closeSetDB();
return -1;
}
while( ( dbMsg = tb.getSetDB()) == nullptr )
{
tb.fillStructure();
memcpy(p_ptr+p_head->size,&tb.structTable,sizeof(S));
p_head->size = p_head->size+1;
p_head->count++;
}
p_head->maxsize = p_head->size+1;
return 0;
}
};
#endif