Commit 66eb4a98 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

function names changed in CycleStorage, TableBlockStorage

parent 34fb5e84
...@@ -42,32 +42,32 @@ void testTable1(void) ...@@ -42,32 +42,32 @@ void testTable1(void)
{ {
chr[0]=i; chr[0]=i;
sprintf(val,"%d",i); sprintf(val,"%d",i);
t->AddRow(chr,val); t->addRow(chr,val);
} }
printf("elements with values=keys added:\n"); printf("elements with values=keys added:\n");
for(i=0;i<40;i++) for(i=0;i<40;i++)
{ {
chr[0]=i; chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val); if(t->findKeyValue(chr,val)!=0) printf("%s, ",val);
} }
printf("\n"); printf("\n");
for(i=9;i<15;i++) for(i=9;i<15;i++)
{ {
chr[0]=i; chr[0]=i;
t->DelRow(chr); t->delRow(chr);
} }
printf("elements with keys from 9 to 14 deleted\n"); printf("elements with keys from 9 to 14 deleted\n");
for(i=9;i<15;i++) for(i=9;i<15;i++)
{ {
chr[0]=i; chr[0]=i;
sprintf(val,"%d",i+40); sprintf(val,"%d",i+40);
t->AddRow(chr,val); t->addRow(chr,val);
} }
printf("elements with keys from 9 to 14 with values=key+40 added, all elements:\n"); printf("elements with keys from 9 to 14 with values=key+40 added, all elements:\n");
for(i=0;i<40;i++) for(i=0;i<40;i++)
{ {
chr[0]=i; chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val); if(t->findKeyValue(chr,val)!=0) printf("%s, ",val);
} }
printf("\n"); printf("\n");
} }
...@@ -77,14 +77,14 @@ bool testTable2(void) ...@@ -77,14 +77,14 @@ bool testTable2(void)
char *val=new char[40]; char *val=new char[40];
TableBlockStorage *t; TableBlockStorage *t;
t = new TableBlockStorage(); t = new TableBlockStorage();
t->Create("big_file.test", 4, 40, 20000, 5,28,0); t->create("big_file.test", 4, 40, 20000, 5,28,0);
int i; int i;
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t->FindKeyValue(&i,val)!=0) printf("%s, ",val); if(t->findKeyValue(&i,val)!=0) printf("%s, ",val);
} }
printf("\n"); printf("\n");
if(t->GetCurBlock()!=0) if(t->getCurBlock()!=0)
{ {
delete t; delete t;
return false; return false;
...@@ -92,16 +92,16 @@ bool testTable2(void) ...@@ -92,16 +92,16 @@ bool testTable2(void)
for(i=1;i<11;i++) for(i=1;i<11;i++)
{ {
sprintf(val,"%d",i); sprintf(val,"%d",i);
t->AddRow((char*)&i,val); t->addRow((char*)&i,val);
} }
if(t->GetCurBlock()!=0) if(t->getCurBlock()!=0)
{ {
delete t; delete t;
return false; return false;
} }
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t->FindKeyValue(&i,val)!=0) printf("%s, ",val); if(t->findKeyValue(&i,val)!=0) printf("%s, ",val);
if(val[0]==0) if(val[0]==0)
{ {
delete t; delete t;
...@@ -109,7 +109,7 @@ bool testTable2(void) ...@@ -109,7 +109,7 @@ bool testTable2(void)
} }
} }
printf("\n"); printf("\n");
if(t->GetCurBlock()!=0) if(t->getCurBlock()!=0)
{ {
delete t; delete t;
return false; return false;
...@@ -117,16 +117,16 @@ bool testTable2(void) ...@@ -117,16 +117,16 @@ bool testTable2(void)
for(i=1;i<8;i++) for(i=1;i<8;i++)
{ {
sprintf(val,"%d",i+10); sprintf(val,"%d",i+10);
t->AddRow(&i,val); t->addRow(&i,val);
} }
printf("deleteing 8-10 elements\n"); printf("deleteing 8-10 elements\n");
for(i=8;i<11;i++) for(i=8;i<11;i++)
{ {
t->DelRow(&i); t->delRow(&i);
} }
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t->FindKeyValue(&i,val)!=0) if(t->findKeyValue(&i,val)!=0)
{ {
printf("%s, ",val); printf("%s, ",val);
if((i > 7)&&(i <11)) if((i > 7)&&(i <11))
...@@ -142,7 +142,7 @@ bool testTable2(void) ...@@ -142,7 +142,7 @@ bool testTable2(void)
} }
} }
printf("\nrewriting 3-10 elements with values=keys+40\n"); printf("\nrewriting 3-10 elements with values=keys+40\n");
if(t->GetCurBlock()!=0) if(t->getCurBlock()!=0)
{ {
delete t; delete t;
return false; return false;
...@@ -150,11 +150,11 @@ bool testTable2(void) ...@@ -150,11 +150,11 @@ bool testTable2(void)
for(i=3;i<11;i++) for(i=3;i<11;i++)
{ {
sprintf(val,"%d",i+40); sprintf(val,"%d",i+40);
t->AddRow(&i,val); t->addRow(&i,val);
} }
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t->FindKeyValue(&i,val)!=0) printf("%s, ",val); if(t->findKeyValue(&i,val)!=0) printf("%s, ",val);
if((atoi(val) != i+40) && (i>2) && (i<11)) if((atoi(val) != i+40) && (i>2) && (i<11))
{ {
delete t; delete t;
...@@ -166,7 +166,7 @@ bool testTable2(void) ...@@ -166,7 +166,7 @@ bool testTable2(void)
return false; return false;
} }
} }
if(t->GetCurBlock()!=0) if(t->getCurBlock()!=0)
{ {
delete t; delete t;
return false; return false;
...@@ -174,23 +174,23 @@ bool testTable2(void) ...@@ -174,23 +174,23 @@ bool testTable2(void)
strcpy(val,"new block"); strcpy(val,"new block");
i=9; i=9;
t->AddRow(&i,val); t->addRow(&i,val);
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t->FindKeyValue((char*)&i,val)!=0) printf("%s, ",val); if(t->findKeyValue((char*)&i,val)!=0) printf("%s, ",val);
} }
if(t->GetCurBlock()!=1) if(t->getCurBlock()!=1)
{ {
delete t; delete t;
return false; return false;
} }
printf("after reopen:\n"); printf("after reopen:\n");
t->Open("big_file.test", 4, 40, 20000, 5,28,0); t->open("big_file.test", 4, 40, 20000, 5,28,0);
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t->FindKeyValue(&i,val)!=0) printf("%s, ",val); if(t->findKeyValue(&i,val)!=0) printf("%s, ",val);
} }
if(t->GetCurBlock()!=1) if(t->getCurBlock()!=1)
{ {
delete t; delete t;
return false; return false;
...@@ -209,12 +209,12 @@ bool testJournal1(void) ...@@ -209,12 +209,12 @@ bool testJournal1(void)
for(i=1;i<33000;i++) for(i=1;i<33000;i++)
{ {
sprintf(str,"%d",i); sprintf(str,"%d",i);
j->AddRow(str); j->addRow(str);
} }
printf("first 30 elements:\n"); printf("first 30 elements:\n");
for(i=0;i<30;i++) for(i=0;i<30;i++)
{ {
if(j->ReadRow(i,str)) if(j->readRow(i,str))
{ {
printf("%s\n",str); printf("%s\n",str);
k++; k++;
...@@ -232,7 +232,7 @@ bool testJournal1(void) ...@@ -232,7 +232,7 @@ bool testJournal1(void)
char *val = new char[40]; char *val = new char[40];
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
if(t->FindKeyValue((char*)&i,val)!=0) printf("%s, ",val); if(t->findKeyValue((char*)&i,val)!=0) printf("%s, ",val);
if((atoi(val) != i+10) && (i<3)) if((atoi(val) != i+10) && (i<3))
{ {
delete t; delete t;
...@@ -244,11 +244,11 @@ bool testJournal1(void) ...@@ -244,11 +244,11 @@ bool testJournal1(void)
printf("\nfirst 30 elements after deleting first 20:\n"); printf("\nfirst 30 elements after deleting first 20:\n");
for(i=0;i<20;i++) for(i=0;i<20;i++)
{ {
j->DelRow(i); j->delRow(i);
} }
for(i=0;i<30;i++) for(i=0;i<30;i++)
{ {
if(j->ReadRow(i,str)) if(j->readRow(i,str))
{ {
printf("%s\n",str); printf("%s\n",str);
k++; k++;
...@@ -266,11 +266,11 @@ bool testJournal1(void) ...@@ -266,11 +266,11 @@ bool testJournal1(void)
for(i=10001;i<10011;i++) for(i=10001;i<10011;i++)
{ {
sprintf(str,"%d",i); sprintf(str,"%d",i);
j->AddRow(str); j->addRow(str);
} }
for(i=0;i<20;i++) for(i=0;i<20;i++)
{ {
if(j->ReadRow(i,str)) if(j->readRow(i,str))
{ {
printf("%s\n",str); printf("%s\n",str);
k++; k++;
...@@ -285,12 +285,11 @@ bool testJournal1(void) ...@@ -285,12 +285,11 @@ bool testJournal1(void)
k = 0; k = 0;
printf("the same after reopen:\n"); printf("the same after reopen:\n");
delete j;
j = new CycleStorage(); j = new CycleStorage();
j->Open("big_file.test",30,1000000,20000); j->open("big_file.test",30,1000000,20000);
for(i=0;i<20;i++) for(i=0;i<20;i++)
{ {
if(j->ReadRow(i,str)) if(j->readRow(i,str))
{ {
printf("%s\n",str); printf("%s\n",str);
k++; k++;
...@@ -305,12 +304,11 @@ bool testJournal1(void) ...@@ -305,12 +304,11 @@ bool testJournal1(void)
k = 0; k = 0;
printf("the same after reopen:\n"); printf("the same after reopen:\n");
delete j;
j = new CycleStorage(); j = new CycleStorage();
j->Open("big_file.test",30,1000000,20000); j->open("big_file.test",30,1000000,20000);
for(i=0;i<20;i++) for(i=0;i<20;i++)
{ {
if(j->ReadRow(i,str)) if(j->readRow(i,str))
{ {
printf("%s\n",str); printf("%s\n",str);
k++; k++;
...@@ -320,7 +318,7 @@ bool testJournal1(void) ...@@ -320,7 +318,7 @@ bool testJournal1(void)
{ {
delete t; delete t;
delete j; delete j;
//return false; return false;
} }
delete t; delete t;
...@@ -335,16 +333,16 @@ void testJournal2(void) ...@@ -335,16 +333,16 @@ void testJournal2(void)
char *str = (char*)malloc(30); char *str = (char*)malloc(30);
j = new CycleStorage("big_file.test",30,1000000,20000); j = new CycleStorage("big_file.test",30,1000000,20000);
printf("journal test 2 - checking number of iterations to find head/tail\n"); printf("journal test 2 - checking number of iterations to find head/tail\n");
printf("iterations = %d\n",j->GetIter()); printf("iterations = %d\n",j->getIter());
for(i=0;i<20;i++) for(i=0;i<20;i++)
{ {
for(k=1000;k<3000;k++) for(k=1000;k<3000;k++)
{ {
sprintf(str,"%d",k); sprintf(str,"%d",k);
j->AddRow(str); j->addRow(str);
} }
j->Open("big_file.test",30,1000000,20000); j->open("big_file.test",30,1000000,20000);
printf("iterations = %d\n",j->GetIter()); printf("iterations = %d\n",j->getIter());
} }
printf("\n"); printf("\n");
delete j; delete j;
......
...@@ -79,9 +79,9 @@ class TableStorage ...@@ -79,9 +79,9 @@ class TableStorage
public: public:
TableStorage(const char* name, int inf_sz, int sz, int seek); TableStorage(const char* name, int inf_sz, int sz, int seek);
~TableStorage(); ~TableStorage();
int AddRow(char* key, char* val); int addRow(char* key, char* val);
int DelRow(char* key); int delRow(char* key);
char* FindKeyValue(char* key, char* val); char* findKeyValue(char* key, char* val);
}; };
class TableBlockStorage class TableBlockStorage
...@@ -100,20 +100,20 @@ class TableBlockStorage ...@@ -100,20 +100,20 @@ class TableBlockStorage
block_num - - ( = sz/block_num block_num - - ( = sz/block_num
, ), block_lim - , , ), block_lim - ,
seek - ( , ) */ seek - ( , ) */
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);
/*! , */ /*! , */
bool AddRow(void* key, void* val); bool addRow(void* key, void* val);
/*! , , */ /*! , , */
bool DelRow(void* key); bool delRow(void* key);
/*! , 0 */ /*! , 0 */
void* FindKeyValue(void* key, void* val); void* findKeyValue(void* key, void* val);
/*! ( ) */ /*! ( ) */
int GetCurBlock(void); int getCurBlock(void);
protected: protected:
FILE *file; FILE *file;
int inf_size; int inf_size;
...@@ -123,10 +123,10 @@ class TableBlockStorage ...@@ -123,10 +123,10 @@ class TableBlockStorage
int k_size, lim,seekpos; int k_size, lim,seekpos;
int size,block_size,block_number,full_size; int size,block_size,block_number,full_size;
void filewrite(int seek, bool needflush=true); void filewrite(int seek, bool needflush=true);
bool CopyToNextBlock(); bool copyToNextBlock();
bool KeyCompare(int i, void* key); bool keyCompare(int i, void* key);
void* KeyPointer(int num); void* keyPointer(int num);
void* ValPointer(int num); void* valPointer(int num);
}; };
class CycleStorage class CycleStorage
...@@ -143,26 +143,23 @@ class CycleStorage ...@@ -143,26 +143,23 @@ class CycleStorage
/*! inf_sz - , sz - , /*! inf_sz - , sz - ,
seek - ( , ) */ seek - ( , ) */
bool Open(const char* name, int inf_sz, int sz, int seek); bool open(const char* name, int inf_sz, int sz, int seek);
bool Create(const char* name, int inf_sz, int sz, int seek); bool create(const char* name, int inf_sz, int sz, int seek);
/*! */ /*! */
bool AddRow(void* str); bool addRow(void* str);
/*! row */ /*! row */
bool DelRow(int row); bool delRow(int row);
/*! */ /*! */
bool DelAllRows(void); bool delAllRows(void);
/*! num */ /*! num */
void* ReadRow(int num, void* str); void* readRow(int num, void* str);
/*! Xml- name */
//bool ExportToXML(const char* name);
/*! - / ( ) */ /*! - / ( ) */
int GetIter(void); int getIter(void);
protected: protected:
FILE *file; FILE *file;
int inf_size; int inf_size;
...@@ -171,8 +168,8 @@ class CycleStorage ...@@ -171,8 +168,8 @@ class CycleStorage
int size,seekpos, iter; int size,seekpos, iter;
int full_size; int full_size;
void filewrite(CycleStorageElem* jrn,int seek, bool needflush=true); void filewrite(CycleStorageElem* jrn,int seek, bool needflush=true);
void* ValPointer(void* pnt); void* valPointer(void* pnt);
void FindHead(); void findHead();
}; };
#endif #endif
...@@ -35,13 +35,13 @@ CycleStorage::CycleStorage() ...@@ -35,13 +35,13 @@ CycleStorage::CycleStorage()
file=NULL; file=NULL;
} }
CycleStorage::CycleStorage(const char* name, int inf_sz, int sz, int seek, bool create) CycleStorage::CycleStorage(const char* name, int inf_sz, int sz, int seek, bool cr)
{ {
file=NULL; file=NULL;
if(!Open(name,inf_sz, sz, seek)) if(!open(name,inf_sz, sz, seek))
{ {
if(create) if(cr)
Create(name,inf_sz, sz, seek); create(name,inf_sz, sz, seek);
else else
file=NULL; file=NULL;
} }
...@@ -52,7 +52,7 @@ CycleStorage::~CycleStorage() ...@@ -52,7 +52,7 @@ CycleStorage::~CycleStorage()
fclose(file); fclose(file);
} }
void* CycleStorage::ValPointer(void* pnt) void* CycleStorage::valPointer(void* pnt)
{ {
return (char*)pnt+sizeof(CycleStorageElem); return (char*)pnt+sizeof(CycleStorageElem);
} }
...@@ -68,7 +68,7 @@ void CycleStorage::filewrite(CycleStorageElem* jrn,int seek,bool needflush) ...@@ -68,7 +68,7 @@ void CycleStorage::filewrite(CycleStorageElem* jrn,int seek,bool needflush)
( 2 - 4 , ), 3 - 2, 5 - 4, 6 - 1. ( 2 - 4 , ), 3 - 2, 5 - 4, 6 - 1.
| |
*/ */
void CycleStorage::FindHead() void CycleStorage::findHead()
{ {
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size]; CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
int l=-1,r=size,mid; int l=-1,r=size,mid;
...@@ -134,7 +134,7 @@ void CycleStorage::FindHead() ...@@ -134,7 +134,7 @@ void CycleStorage::FindHead()
delete jrn; delete jrn;
} }
bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek) bool CycleStorage::open(const char* name, int inf_sz, int sz, int seek)
{ {
/*! , /*! ,
*/ */
...@@ -165,12 +165,12 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek) ...@@ -165,12 +165,12 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
seekpos+=sizeof(CycleStorageAttr); seekpos+=sizeof(CycleStorageAttr);
FindHead(); findHead();
return true; return true;
} }
bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek) bool CycleStorage::create(const char* name, int inf_sz, int sz, int seek)
{ {
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
file = fopen(name, "r+"); file = fopen(name, "r+");
...@@ -203,6 +203,7 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek) ...@@ -203,6 +203,7 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
fwrite(csa,sizeof(CycleStorageAttr),1,file); fwrite(csa,sizeof(CycleStorageAttr),1,file);
fflush(file); fflush(file);
seekpos+=sizeof(CycleStorageAttr); seekpos+=sizeof(CycleStorageAttr);
delete csa;
/*! */ /*! */
for(int i=0;i<size;i++) for(int i=0;i<size;i++)
...@@ -222,12 +223,11 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek) ...@@ -222,12 +223,11 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
} }
head=tail=-1; head=tail=-1;
delete csa;
delete jrn; delete jrn;
return true; return true;
} }
bool CycleStorage::AddRow(void* str) 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];
...@@ -236,7 +236,7 @@ bool CycleStorage::AddRow(void* str) ...@@ -236,7 +236,7 @@ bool CycleStorage::AddRow(void* str)
/*! 2 - (head=-1), 1 (head=tail=0) ) /*! 2 - (head=-1), 1 (head=tail=0) )
*/ */
memcpy(ValPointer(jrn),str,inf_size); memcpy(valPointer(jrn),str,inf_size);
if(head==-1) if(head==-1)
{ {
jrn->status=1; jrn->status=1;
...@@ -276,7 +276,7 @@ bool CycleStorage::AddRow(void* str) ...@@ -276,7 +276,7 @@ bool CycleStorage::AddRow(void* str)
} }
else tail++; else tail++;
fread(jrn,full_size,1,file); fread(jrn,full_size,1,file);
memcpy(ValPointer(jrn),str,inf_size); memcpy(valPointer(jrn),str,inf_size);
if(jrn->status==0) if(jrn->status==0)
{ {
jrn->status=2; jrn->status=2;
...@@ -302,7 +302,7 @@ bool CycleStorage::AddRow(void* str) ...@@ -302,7 +302,7 @@ bool CycleStorage::AddRow(void* str)
return true; return true;
} }
bool CycleStorage::DelRow(int row) bool CycleStorage::delRow(int row)
{ {
int i=(head+row)%size,j; int i=(head+row)%size,j;
if( row >= size ) return false; if( row >= size ) return false;
...@@ -334,7 +334,7 @@ bool CycleStorage::DelRow(int row) ...@@ -334,7 +334,7 @@ bool CycleStorage::DelRow(int row)
return true; return true;
} }
bool CycleStorage::DelAllRows() bool CycleStorage::delAllRows()
{ {
/*! */ /*! */
if(file==NULL) return false; if(file==NULL) return false;
...@@ -357,7 +357,8 @@ bool CycleStorage::DelAllRows() ...@@ -357,7 +357,8 @@ bool CycleStorage::DelAllRows()
return true; return true;
} }
void* CycleStorage::ReadRow(int num, void* str) /*! TODO: str, */
void* CycleStorage::readRow(int num, void* str)
{ {
/*! */ /*! */
int j=(head+num)%size; int j=(head+num)%size;
...@@ -369,47 +370,15 @@ void* CycleStorage::ReadRow(int num, void* str) ...@@ -369,47 +370,15 @@ void* CycleStorage::ReadRow(int num, void* str)
fread(jrn,full_size,1,file); fread(jrn,full_size,1,file);
if((jrn->status==1)||(jrn->status==2)||(jrn->status==4)) if((jrn->status==1)||(jrn->status==2)||(jrn->status==4))
{ {
memcpy(str,ValPointer(jrn),inf_size); memcpy(str,valPointer(jrn),inf_size);
delete jrn; delete jrn;
return str; return str;
} }
delete jrn; delete jrn;
return 0; return NULL;
} }
/*bool CycleStorage::ExportToXML(const char* name) int CycleStorage::getIter()
{
int i,j=head;
if(file==NULL) return false;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
UniXML* f = new UniXML();
xmlNode* xn;
f->newDoc("journal");
fseek(file,seekpos+j*full_size,0);
char* num = new char[10];
for(i=0;i<size;i++)
{
if(j==size)
{
j=0;
fseek(file,seekpos,0);
}
fread(jrn,full_size,1,file);
if((jrn->status==1)||(jrn->status==2)||(jrn->status==4))
{
sprintf(num,"%d",i);
xn=f->createChild(f->cur,"item","");
f->setProp(xn,"key",num);
f->setProp(xn,"text",(char*)ValPointer(jrn));
}
j++;
}
f->save(name);
delete jrn;
return true;
}*/
int CycleStorage::GetIter()
{ {
return iter; return iter;
} }
\ No newline at end of file
...@@ -35,13 +35,13 @@ TableBlockStorage::TableBlockStorage() ...@@ -35,13 +35,13 @@ TableBlockStorage::TableBlockStorage()
file=NULL; file=NULL;
} }
TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek, bool create) TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek, bool cr)
{ {
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(cr)
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;
} }
...@@ -58,17 +58,17 @@ TableBlockStorage::~TableBlockStorage() ...@@ -58,17 +58,17 @@ TableBlockStorage::~TableBlockStorage()
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
} }
bool TableBlockStorage::KeyCompare(int i, void* key) bool TableBlockStorage::keyCompare(int i, void* key)
{ {
return !memcmp((char*)mem[i]+sizeof(TableBlockStorageElem),key,k_size); return !memcmp((char*)mem[i]+sizeof(TableBlockStorageElem),key,k_size);
} }
void* TableBlockStorage::KeyPointer(int num) void* TableBlockStorage::keyPointer(int num)
{ {
return (char*)mem[num]+sizeof(TableBlockStorageElem); return (char*)mem[num]+sizeof(TableBlockStorageElem);
} }
void* TableBlockStorage::ValPointer(int num) void* TableBlockStorage::valPointer(int num)
{ {
return (char*)mem[num]+sizeof(TableBlockStorageElem)+k_size; return (char*)mem[num]+sizeof(TableBlockStorageElem)+k_size;
} }
...@@ -81,7 +81,7 @@ void TableBlockStorage::filewrite(int seek, bool needflush) ...@@ -81,7 +81,7 @@ void TableBlockStorage::filewrite(int seek, bool needflush)
if(needflush) fflush(file); if(needflush) fflush(file);
} }
bool TableBlockStorage::CopyToNextBlock(void) bool TableBlockStorage::copyToNextBlock(void)
{ {
/*! */ /*! */
max=-1; max=-1;
...@@ -114,7 +114,7 @@ bool TableBlockStorage::CopyToNextBlock(void) ...@@ -114,7 +114,7 @@ bool TableBlockStorage::CopyToNextBlock(void)
return true; return true;
} }
bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek) bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek)
{ {
/*! , */ /*! , */
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
...@@ -183,7 +183,7 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i ...@@ -183,7 +183,7 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
return true; return true;
} }
bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek) bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek)
{ {
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
file = fopen(name, "r+"); file = fopen(name, "r+");
...@@ -229,6 +229,7 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz, ...@@ -229,6 +229,7 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
fwrite(sa,sizeof(StorageAttr),1,file); fwrite(sa,sizeof(StorageAttr),1,file);
fflush(file); fflush(file);
seekpos+=sizeof(StorageAttr); seekpos+=sizeof(StorageAttr);
delete sa;
/*! : /*! :
EMPTY_BLOCK=(-5) - , , , EMPTY_ELEM=(-1) - EMPTY_BLOCK=(-5) - , , , EMPTY_ELEM=(-1) -
...@@ -255,48 +256,47 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz, ...@@ -255,48 +256,47 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
fwrite(empty,emp,1,file); fwrite(empty,emp,1,file);
fflush(file); fflush(file);
} }
delete sa;
return true; return true;
} }
bool TableBlockStorage::AddRow(void* key, void* value) bool TableBlockStorage::addRow(void* key, void* value)
{ {
int i=0,pos=-1,empty=-1; int i=0,pos=-1,empty=-1;
if(file==NULL) return false; if(file==NULL) return false;
if(max==lim-1) CopyToNextBlock(); if(max==lim-1) 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(i,key)) 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;
} }
/*! , pos>=0, , empty */ /*! , pos>=0, , empty */
if(pos>=0) empty=pos; if(pos>=0) empty=pos;
else memcpy(KeyPointer(empty),key,k_size); else memcpy(keyPointer(empty),key,k_size);
mem[empty]->count=++max; mem[empty]->count=++max;
memcpy(ValPointer(empty),value,inf_size); memcpy(valPointer(empty),value,inf_size);
filewrite(empty); filewrite(empty);
return true; return true;
} }
bool TableBlockStorage::DelRow(void* key) bool TableBlockStorage::delRow(void* key)
{ {
int i; int i;
if(file==NULL) return false; if(file==NULL) return false;
/*! */ /*! */
if(max==lim-1) CopyToNextBlock(); if(max==lim-1) 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)
continue; continue;
if(KeyCompare(i,key)) if(keyCompare(i,key))
{ {
mem[i]->count=++max; mem[i]->count=++max;
memset(KeyPointer(i),0,k_size); memset(keyPointer(i),0,k_size);
filewrite(i); filewrite(i);
return true; return true;
} }
...@@ -304,7 +304,8 @@ bool TableBlockStorage::DelRow(void* key) ...@@ -304,7 +304,8 @@ bool TableBlockStorage::DelRow(void* key)
return false; return false;
} }
void* TableBlockStorage::FindKeyValue(void* key, void* val) /*! TODO: val, */
void* TableBlockStorage::findKeyValue(void* key, void* val)
{ {
int i; int i;
if(file==NULL) return 0; if(file==NULL) return 0;
...@@ -313,16 +314,16 @@ void* TableBlockStorage::FindKeyValue(void* key, void* val) ...@@ -313,16 +314,16 @@ void* TableBlockStorage::FindKeyValue(void* key, void* val)
/*! >= 0, .. */ /*! >= 0, .. */
if(mem[i]->count < 0) if(mem[i]->count < 0)
continue; continue;
if(KeyCompare(i,key)) if(keyCompare(i,key))
{ {
memcpy(val,ValPointer(i),inf_size); memcpy(val,valPointer(i),inf_size);
return val; return val;
} }
} }
return 0; return NULL;
} }
int TableBlockStorage::GetCurBlock() int TableBlockStorage::getCurBlock()
{ {
return cur_block; return cur_block;
} }
......
...@@ -110,10 +110,10 @@ TableStorage::~TableStorage() ...@@ -110,10 +110,10 @@ TableStorage::~TableStorage()
fclose(file); fclose(file);
} }
int TableStorage::AddRow(char* key, char* value) int TableStorage::addRow(char* key, char* value)
{ {
TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size); TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
int i,k,j; int i,k,j,st;
if(file!=NULL) if(file!=NULL)
{ {
if(head==-1) if(head==-1)
...@@ -151,19 +151,14 @@ int TableStorage::AddRow(char* key, char* value) ...@@ -151,19 +151,14 @@ int TableStorage::AddRow(char* key, char* value)
fseek(file,seekpos+j*(sizeof(TableStorageElem)+inf_size),0); fseek(file,seekpos+j*(sizeof(TableStorageElem)+inf_size),0);
if(j==head) if(j==head)
{ {
if((tbl->status==2)||(tbl->status==3)) if((tbl->status==2)||(tbl->status==3)) st=2;
tbl->status=2; else st=4;
else
tbl->status=4;
if(j==0) if(j==0)
{ if(st==2) st=4;
if(tbl->status==2) else st=2;
tbl->status=4;
else
tbl->status=2;
}
tbl->status=st;
strcpy(tbl->key,key); strcpy(tbl->key,key);
for(k=0;k<inf_size;k++) for(k=0;k<inf_size;k++)
*((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k); *((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k);
...@@ -184,25 +179,11 @@ int TableStorage::AddRow(char* key, char* value) ...@@ -184,25 +179,11 @@ int TableStorage::AddRow(char* key, char* value)
if(head>=size) head=0; if(head>=size) head=0;
return 0; return 0;
} }
/*for(i=0;i<size;i++)
{
fread(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
if(tbl->key[0]==0)
{
strcpy(tbl->key,key);
for(k=0;k<inf_size;k++)
*((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k);
fseek(file,seekpos+i*(sizeof(TableStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
return 0;
}
}*/
} }
return 1; return 1;
} }
int TableStorage::DelRow(char* key) int TableStorage::delRow(char* key)
{ {
TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size); TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
int i,j; int i,j;
...@@ -234,7 +215,7 @@ int TableStorage::DelRow(char* key) ...@@ -234,7 +215,7 @@ int TableStorage::DelRow(char* key)
return 1; return 1;
} }
char* TableStorage::FindKeyValue(char* key, char* val) char* TableStorage::findKeyValue(char* key, char* val)
{ {
TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size); TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
int i,k; int i,k;
......
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