This commit is contained in:
Fabio Ganovelli ganovelli 2010-12-06 16:24:40 +00:00
parent 441fe5a5ea
commit 5ee0566e37
2 changed files with 202 additions and 0 deletions

View File

@ -0,0 +1,107 @@
#ifndef __CHUNK_MEM_DISK_
#define __CHUNK_MEM_DISK_
#include "../utils/timing.h"
#include "../../ooc_vector/ooc_chains.h"
#include "../simpledb.h"
#include <limits.h>
template <class TYPE> int Chain<TYPE>::
SaveChunk( typename Chain<TYPE>::Chunk & ck ){
TIM::Begin(13);
unsigned char * local_buffer = 0;
if(ck.savedOnce && this->saveOnce) return 0 ;
RAssert(MemDbg::CheckHeap(0));
const unsigned int & chunk_order = &ck-&(*chunks.begin());
std::string key = this->GetKey(chunk_order);
unsigned int siz = ck.Write(local_buffer);
if(!ck.pos.Void() )
ck.pos = ((SimpleDb*)extMemHnd)->PutSingle(key,ck.pos, local_buffer, siz);
else
ck.pos = ((SimpleDb*)extMemHnd)->PutSingle(key, local_buffer, siz);
ck.Written(local_buffer);
if(ck.size == this->params.chunkSize )
ck.savedOnce = true;
else
STAT::Inc(N_SAVED_PART_CH);
TIM::End(13);
STAT::Inc(N_SAVEDCH);
return ck.SizeOfMem();
}
template <class TYPE> void Chain<TYPE>::
RemoveChunk( typename Chain<TYPE>::Chunk & ck ){
RAssert(MemDbg::CheckHeap(0));
const unsigned int & chunk_order = &ck-&(*chunks.begin());
std::string name = this->GetKey(chunk_order);
((SimpleDb*)extMemHnd)->Del(name);
}
template <class TYPE> void Chain<TYPE>::
FetchChunk( typename Chain<TYPE>::Chunk & ck ){
RAssert(MemDbg::CheckHeap(1));
TIM::Begin(12);
const unsigned int & chunk_order = &ck-&(*chunks.begin());
std::string key = this->GetKey(chunk_order);
unsigned char * local_buffer;
ck.AllocMem(local_buffer);
SimpleDb::Index res;
if(!ck.pos.Void())
res = ((SimpleDb*)extMemHnd)->Get(ck.pos, local_buffer,ck.SizeOfDisk());
else{
res = ((SimpleDb*)extMemHnd)->Get(key, local_buffer, ck.SizeOfDisk());
ck.pos = res;
}
if(res.Void() )
printf("record not found");
ck.Read(local_buffer);
TIM::End(12);
STAT::Inc(N_LOADEDCH);
}
template <class TYPE> void Chain<TYPE>::
LoadAll(){
incore.resize(this->Size());
for(unsigned int ci = 0; ci < chunks.size(); ++ci)
{
typename Chain<TYPE>::Chunk & ck = chunks[ci];
unsigned char * ptr = (unsigned char*) &this->incore[ci*this->params.chunkSize];
if(ck.buffer){
memcpy(ptr,ck.buffer,sizeof(TYPE)*ck.size);
}else
{
RAssert(MemDbg::CheckHeap(1));
unsigned char * local_buffer = NULL;
const unsigned int & chunk_order = &ck-&(*chunks.begin());
std::string key = this->GetKey(chunk_order);
ck.AllocMem(local_buffer,ptr);
SimpleDb::Index res = ((SimpleDb*)extMemHnd)->Get(key, local_buffer, ck.SizeOfDisk());
RAssert(!res.Void());
ck.Read(local_buffer,ptr);
}
}
}
#endif

View File

@ -0,0 +1,95 @@
#include "../ooc_chains.h"
#include "../utils/timing.h"
#include "../simpledb.h"
extern Logging * lgn;
void OOCEnv::
Create( const char * name ){
is_file_owned = true;
std::string iDbName(name); // name of the database
extMemHnd = new SimpleDb(iDbName);
((SimpleDb*)extMemHnd)->Create(iDbName,this->params.blockSizeBytes);
}
void OOCEnv::
Create( void * handle ){
is_file_owned = false;
extMemHnd = (SimpleDb*) handle;
}
void OOCEnv::
Open( const char * name ){
is_file_owned = true;
std::string iDbName(name); // name of the database
extMemHnd = new SimpleDb(name);
((SimpleDb*)extMemHnd)->Open(name);
LoadAT();
}
void OOCEnv::
Open( void * handle ){
is_file_owned = false;
extMemHnd = (SimpleDb*) handle;
LoadAT();
}
void OOCEnv::
Close( bool andsave){
if(andsave){
SaveData();
((SimpleDb*)extMemHnd)->DisableSafeWriting();
SaveAT();
}
lgn->Append("deleting simpledb ");
delete (SimpleDb*)extMemHnd;
lgn->Append("done");
}
int OOCEnv::
SaveAT( ){
int siz = this->SizeOfMem(); // size of the Allocation Table
char * buf = new char[siz]; // allocate memory
#ifdef _DEBUG
char * res =
#endif
this->Serialize(buf); // serialize
#ifdef _DEBUG
RAssert(res-buf == siz);
#endif
std::string key("CHAINMEM_ALLOCATION_TABLE");
((SimpleDb*)extMemHnd)->Put(key,buf,siz);
delete [] buf;
sprintf(lgn->Buf(),"Allocation Table Size: %d",siz);
lgn->Push();
sprintf(lgn->Buf() ,"mem size:%d",siz);
lgn->Push();
return siz;
}
void OOCEnv::
LoadAT( ){
std::string key("CHAINMEM_ALLOCATION_TABLE");
unsigned int size;
void * buf;
SimpleDb::Index res = ((SimpleDb*)extMemHnd)->Get(key, buf);
RAssert(!res.Void());
this->DeSerialize((char*)buf);
size = this->SizeOfMem();
delete [] (char*) buf;
}