Commit b21a332f authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

the candidate for final version of TableBlockStorage and CycleStorage

parent 13c5133e
...@@ -91,8 +91,9 @@ class TableBlockStorage ...@@ -91,8 +91,9 @@ class TableBlockStorage
private: private:
void filewrite(int seek, bool needflush=true); void filewrite(int seek, bool needflush=true);
bool CopyToNextBlock(); bool CopyToNextBlock();
void* KeyPointer(void* pnt); bool KeyCompare(int i, void* key);
void* ValPointer(void* pnt); void* KeyPointer(int num);
void* ValPointer(int num);
public: public:
bool Open(const char* name, int inf_sz, int key_sz, int sz, int block_num, int block_lim, int seek); bool Open(const char* name, int inf_sz, int key_sz, int sz, int block_num, int block_lim, int seek);
bool Create(const char* name, int inf_sz, int key_sz, int sz, int block_num, int block_lim, int seek); bool Create(const char* name, int inf_sz, int key_sz, int sz, int block_num, int block_lim, int seek);
...@@ -118,7 +119,6 @@ class CycleStorage ...@@ -118,7 +119,6 @@ class CycleStorage
bool AddRow(void* str); bool AddRow(void* str);
bool DelRow(int row); bool DelRow(int row);
bool DelAllRows(void); bool DelAllRows(void);
//bool ViewRows(int beg, int num);
void* ViewRow(int num, void* str); void* ViewRow(int num, void* str);
bool ExportToXML(const char* name); bool ExportToXML(const char* name);
}; };
......
...@@ -43,10 +43,12 @@ CycleStorage::CycleStorage(const char* name, int inf_sz, int sz, int seek, bool ...@@ -43,10 +43,12 @@ CycleStorage::CycleStorage(const char* name, int inf_sz, int sz, int seek, bool
{ {
file=NULL; file=NULL;
if(!Open(name,inf_sz, sz, seek)) if(!Open(name,inf_sz, sz, seek))
{
if(create) if(create)
Create(name,inf_sz, sz, seek); Create(name,inf_sz, sz, seek);
else else
file=NULL; file=NULL;
}
} }
CycleStorage::~CycleStorage() CycleStorage::~CycleStorage()
...@@ -220,7 +222,7 @@ bool CycleStorage::AddRow(void* str) ...@@ -220,7 +222,7 @@ bool CycleStorage::AddRow(void* str)
{ {
if(file==NULL) return false; if(file==NULL) return false;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size]; CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
int i=0,k; int i=0;
/*! 2 - (head=-1), 1 (head=tail=0) ) /*! 2 - (head=-1), 1 (head=tail=0) )
*/ */
...@@ -327,7 +329,7 @@ bool CycleStorage::DelAllRows() ...@@ -327,7 +329,7 @@ bool CycleStorage::DelAllRows()
{ {
if(file==NULL) return false; if(file==NULL) return false;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size]; CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
int i,j; int i;
fseek(file,seekpos,0); fseek(file,seekpos,0);
for(i=0;i<size;i++) for(i=0;i<size;i++)
...@@ -347,7 +349,7 @@ bool CycleStorage::DelAllRows() ...@@ -347,7 +349,7 @@ bool CycleStorage::DelAllRows()
void* CycleStorage::ViewRow(int num, void* str) void* CycleStorage::ViewRow(int num, void* str)
{ {
int i,j=(head+num)%size; int j=(head+num)%size;
if((file==NULL)||(num>size)) return false; if((file==NULL)||(num>size)) return false;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size]; CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
fseek(file,seekpos+j*full_size,0); fseek(file,seekpos+j*full_size,0);
......
...@@ -37,11 +37,6 @@ ...@@ -37,11 +37,6 @@
#define block_begin -5 #define block_begin -5
#define empty_elem -1 #define empty_elem -1
bool KeyCompare(void* key1, void* key2, int cnt)
{
return !memcmp((char*)key1+sizeof(TableBlockStorageElem),key2,cnt);
}
TableBlockStorage::TableBlockStorage() TableBlockStorage::TableBlockStorage()
{ {
file=NULL; file=NULL;
...@@ -51,10 +46,12 @@ TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, i ...@@ -51,10 +46,12 @@ TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, i
{ {
file=NULL; file=NULL;
if(!Open(name, key_sz, inf_sz, sz, block_num, block_lim, seek)) if(!Open(name, key_sz, inf_sz, sz, block_num, block_lim, seek))
{
if(create) if(create)
Create(name, key_sz, inf_sz, sz, block_num, block_lim, seek); Create(name, key_sz, inf_sz, sz, block_num, block_lim, seek);
else else
file=NULL; file=NULL;
}
} }
TableBlockStorage::~TableBlockStorage() TableBlockStorage::~TableBlockStorage()
...@@ -68,14 +65,19 @@ TableBlockStorage::~TableBlockStorage() ...@@ -68,14 +65,19 @@ TableBlockStorage::~TableBlockStorage()
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
} }
void* TableBlockStorage::KeyPointer(void* pnt) bool TableBlockStorage::KeyCompare(int i, void* key)
{ {
return (char*)pnt+sizeof(TableBlockStorageElem); return !memcmp((char*)mem[i]+sizeof(TableBlockStorageElem),key,k_size);
} }
void* TableBlockStorage::ValPointer(void* pnt) void* TableBlockStorage::KeyPointer(int num)
{ {
return (char*)pnt+sizeof(TableBlockStorageElem)+k_size; return (char*)mem[num]+sizeof(TableBlockStorageElem);
}
void* TableBlockStorage::ValPointer(int num)
{
return (char*)mem[num]+sizeof(TableBlockStorageElem)+k_size;
} }
void TableBlockStorage::filewrite(int seek, bool needflush) void TableBlockStorage::filewrite(int seek, bool needflush)
...@@ -260,22 +262,22 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz, ...@@ -260,22 +262,22 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
bool TableBlockStorage::AddRow(void* key, void* value) bool TableBlockStorage::AddRow(void* key, void* value)
{ {
int i=0,pos=-1,empty=-1,k; int i=0,pos=-1,empty=-1;
if(file==NULL) return false; if(file==NULL) return false;
CopyToNextBlock(); CopyToNextBlock();
for(i=0;i<block_size;i++) for(i=0;i<block_size;i++)
{ {
if(mem[i]->count>=0) if(mem[i]->count>=0)
if(KeyCompare(mem[i],key,k_size)) pos = i; if(KeyCompare(i,key)) pos = i;
if((mem[i]->count<0)&&(empty<0)) empty=i; if((mem[i]->count<0)&&(empty<0)) empty=i;
} }
if(pos>=0) empty=pos; if(pos>=0) empty=pos;
else memcpy(KeyPointer(mem[empty]),key,k_size); else memcpy(KeyPointer(empty),key,k_size);
mem[empty]->count=++max; mem[empty]->count=++max;
memcpy(ValPointer(mem[empty]),value,inf_size); memcpy(ValPointer(empty),value,inf_size);
filewrite(empty); filewrite(empty);
return true; return true;
} }
...@@ -292,32 +294,32 @@ bool TableBlockStorage::DelRow(void* key) ...@@ -292,32 +294,32 @@ bool TableBlockStorage::DelRow(void* key)
for(i=0;i<block_size;i++) for(i=0;i<block_size;i++)
{ {
if(mem[i]->count>=0) if(mem[i]->count>=0)
if(KeyCompare(mem[i],key,k_size)) if(KeyCompare(i,key))
{ {
mem[i]->count=++max; mem[i]->count=++max;
for(int k=0;k<k_size;k++) memset(KeyPointer(i),0,k_size);
*((char*)KeyPointer(mem[i])+k)=0; //for(int k=0;k<k_size;k++)
// *((char*)KeyPointer(i)+k)=0;
filewrite(i); filewrite(i);
return true; return true;
} }
} }
return false;
} }
void* TableBlockStorage::FindKeyValue(void* key, void* val) void* TableBlockStorage::FindKeyValue(void* key, void* val)
{ {
int i,k; int i;
if(file!=NULL) if(file==NULL) return 0;
{
for(i=0;i<block_size;i++) for(i=0;i<block_size;i++)
{ {
if(mem[i]->count>=0) if(mem[i]->count>=0)
if(KeyCompare(mem[i],key,k_size)) if(KeyCompare(i,key))
{ {
memcpy(val,ValPointer(mem[i]),inf_size); memcpy(val,ValPointer(i),inf_size);
return val; return val;
} }
} }
} return false;
return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment