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
private:
void filewrite(int seek, bool needflush=true);
bool CopyToNextBlock();
void* KeyPointer(void* pnt);
void* ValPointer(void* pnt);
bool KeyCompare(int i, void* key);
void* KeyPointer(int num);
void* ValPointer(int num);
public:
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);
......@@ -118,7 +119,6 @@ class CycleStorage
bool AddRow(void* str);
bool DelRow(int row);
bool DelAllRows(void);
//bool ViewRows(int beg, int num);
void* ViewRow(int num, void* str);
bool ExportToXML(const char* name);
};
......
......@@ -43,10 +43,12 @@ CycleStorage::CycleStorage(const char* name, int inf_sz, int sz, int seek, bool
{
file=NULL;
if(!Open(name,inf_sz, sz, seek))
{
if(create)
Create(name,inf_sz, sz, seek);
else
file=NULL;
}
}
CycleStorage::~CycleStorage()
......@@ -220,7 +222,7 @@ bool CycleStorage::AddRow(void* str)
{
if(file==NULL) return false;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
int i=0,k;
int i=0;
/*! 2 - (head=-1), 1 (head=tail=0) )
*/
......@@ -327,7 +329,7 @@ bool CycleStorage::DelAllRows()
{
if(file==NULL) return false;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
int i,j;
int i;
fseek(file,seekpos,0);
for(i=0;i<size;i++)
......@@ -347,7 +349,7 @@ bool CycleStorage::DelAllRows()
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;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
fseek(file,seekpos+j*full_size,0);
......
......@@ -37,11 +37,6 @@
#define block_begin -5
#define empty_elem -1
bool KeyCompare(void* key1, void* key2, int cnt)
{
return !memcmp((char*)key1+sizeof(TableBlockStorageElem),key2,cnt);
}
TableBlockStorage::TableBlockStorage()
{
file=NULL;
......@@ -51,10 +46,12 @@ TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, i
{
file=NULL;
if(!Open(name, key_sz, inf_sz, sz, block_num, block_lim, seek))
{
if(create)
Create(name, key_sz, inf_sz, sz, block_num, block_lim, seek);
else
file=NULL;
}
}
TableBlockStorage::~TableBlockStorage()
......@@ -68,14 +65,19 @@ TableBlockStorage::~TableBlockStorage()
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)
......@@ -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)
{
int i=0,pos=-1,empty=-1,k;
int i=0,pos=-1,empty=-1;
if(file==NULL) return false;
CopyToNextBlock();
for(i=0;i<block_size;i++)
{
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(pos>=0) empty=pos;
else memcpy(KeyPointer(mem[empty]),key,k_size);
else memcpy(KeyPointer(empty),key,k_size);
mem[empty]->count=++max;
memcpy(ValPointer(mem[empty]),value,inf_size);
memcpy(ValPointer(empty),value,inf_size);
filewrite(empty);
return true;
}
......@@ -292,32 +294,32 @@ bool TableBlockStorage::DelRow(void* key)
for(i=0;i<block_size;i++)
{
if(mem[i]->count>=0)
if(KeyCompare(mem[i],key,k_size))
if(KeyCompare(i,key))
{
mem[i]->count=++max;
for(int k=0;k<k_size;k++)
*((char*)KeyPointer(mem[i])+k)=0;
memset(KeyPointer(i),0,k_size);
//for(int k=0;k<k_size;k++)
// *((char*)KeyPointer(i)+k)=0;
filewrite(i);
return true;
}
}
return false;
}
void* TableBlockStorage::FindKeyValue(void* key, void* val)
{
int i,k;
if(file!=NULL)
{
int i;
if(file==NULL) return 0;
for(i=0;i<block_size;i++)
{
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 0;
return false;
}
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