From 5ee0566e375a5e3314211dd32af32b65faf2b42d Mon Sep 17 00:00:00 2001 From: Fabio Ganovelli ganovelli Date: Mon, 6 Dec 2010 16:24:40 +0000 Subject: [PATCH] --- .../src/ooc_vector/io/ooc_chains.hpp | 107 ++++++++++++++++++ .../src/ooc_vector/io/ooc_chunks.cpp | 95 ++++++++++++++++ 2 files changed, 202 insertions(+) create mode 100755 src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chains.hpp create mode 100755 src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chunks.cpp diff --git a/src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chains.hpp b/src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chains.hpp new file mode 100755 index 000000000..16d448fa2 --- /dev/null +++ b/src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chains.hpp @@ -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 + +template int Chain:: +SaveChunk( typename Chain::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 void Chain:: +RemoveChunk( typename Chain::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 void Chain:: +FetchChunk( typename Chain::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 void Chain:: +LoadAll(){ + incore.resize(this->Size()); + for(unsigned int ci = 0; ci < chunks.size(); ++ci) + { + typename Chain::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 diff --git a/src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chunks.cpp b/src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chunks.cpp new file mode 100755 index 000000000..fb99c8993 --- /dev/null +++ b/src/meshlabplugins/edit_ocme/src/ooc_vector/io/ooc_chunks.cpp @@ -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; +} +