Commit 231f2434 authored by Pavel Vainerman's avatar Pavel Vainerman

revision StorageInterface...

parent e320830d
......@@ -348,11 +348,61 @@ void testJournal2(void)
delete j;
}
struct JItem
{
long id;
long val[10];
} __attribute__((__packed__));
bool testJournal3()
{
CycleStorage j("journal3.test",sizeof(JItem),10,0,true);
if( !j.isOpen() )
{
printf("create journal3.test failed\n");
return false;
}
printf("Joural size=%d inf_size=%d full_size=%d\n",
j.getSize(),
j.getInfSize(),
j.getFullSize()
);
printf("write 30 elements:\n");
for(int i=0;i<30;i++)
{
JItem ji;
ji.id = i;
for( int k=0; k<10; k++ )
ji.val[k] = i;
j.addRow(&ji);
}
printf("read first 5 elements:\n");
JItem ji;
for( int i=0;i<10;i++)
{
if( j.readRow(i,&ji) )
printf("read i=%d j.id=%d\n",i,ji.id);
else
printf("read num=%d FAILED!\n",i);
}
return true;
}
int main(int args, char **argv)
{
//testTable1();
bool ok = true;
/*
if(testTable2())
printf("\nTest for TableBlockStorage passed\n\n");
else
......@@ -368,7 +418,9 @@ int main(int args, char **argv)
printf("\nTest for CycleStorage failed\n\n");
ok = false;
}
*/
testJournal3();
/*
if(ok)
{
testJournal2();
......@@ -376,5 +428,6 @@ int main(int args, char **argv)
}
else
printf("TEST FAILED :(\n");
*/
return 0;
}
\ No newline at end of file
......@@ -137,14 +137,19 @@ class CycleStorage
/*! Open, create=true
*/
CycleStorage(const char* name, int inf_sz, int sz, int seek,bool create=false);
CycleStorage(const char* name, int inf_sz, int inf_count, int seek,bool create=false);
~CycleStorage();
/*! inf_sz - , sz - ,
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);
/*!
\param inf_sz - ,
\param int_count - ( inf_sz)
\param seek - ( , )
*/
bool open(const char* name, int inf_sz, int inf_count, int seek);
bool create(const char* name, int inf_sz, int inf_count, int seek);
bool isOpen(){ return (file!=NULL); }
/*! */
bool addRow(void* str);
......@@ -155,11 +160,15 @@ class CycleStorage
/*! */
bool delAllRows(void);
/*! num */
/*! \return num */
void* readRow(int num, void* str);
/*! - / ( ) */
int getIter(void);
inline int getSize(){ return size; }
inline int getInfSize(){ return inf_size; }
inline int getFullSize(){ return full_size; }
protected:
FILE *file;
int inf_size;
......
......@@ -35,15 +35,13 @@ CycleStorage::CycleStorage()
file=NULL;
}
CycleStorage::CycleStorage(const char* name, int inf_sz, int sz, int seek, bool cr)
CycleStorage::CycleStorage(const char* name, int inf_sz, int inf_count, int seek, bool cr):
file(NULL)
{
file=NULL;
if(!open(name,inf_sz, sz, seek))
if(!open(name,inf_sz, inf_count, seek))
{
if(cr)
create(name,inf_sz, sz, seek);
else
file=NULL;
create(name,inf_sz, inf_count, seek);
}
}
......@@ -134,7 +132,7 @@ void CycleStorage::findHead()
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 inf_count, int seek)
{
/*! ,
*/
......@@ -147,30 +145,26 @@ bool CycleStorage::open(const char* name, int inf_sz, int sz, int seek)
if(fseek(file,seekpos,0)==-1) return false;
/*! */
CycleStorageAttr *csa = new CycleStorageAttr();
fread(csa,sizeof(CycleStorageAttr),1,file);
CycleStorageAttr csa;
fread(&csa,sizeof(csa),1,file);
int sz = inf_sz * inf_count;
/*! */
if((csa->size!=(int)((sz-sizeof(CycleStorageAttr))/(sizeof(CycleStorageElem)+inf_sz)))||(csa->inf_size!=inf_sz)||(csa->seekpos!=seek))
{
delete csa;
if((csa.size!=(int)((sz-sizeof(csa))/(sizeof(CycleStorageElem)+inf_sz)))||(csa.inf_size!=inf_sz)||(csa.seekpos!=seek))
return false;
}
delete csa;
inf_size=inf_sz;
full_size=sizeof(CycleStorageElem)+inf_size;
size=(sz-sizeof(CycleStorageAttr))/full_size;
size=(sz-sizeof(csa))/full_size;
seekpos=seek;
seekpos+=sizeof(CycleStorageAttr);
findHead();
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 inf_count, int seek)
{
if(file!=NULL) fclose(file);
file = fopen(name, "r+");
......@@ -182,6 +176,8 @@ bool CycleStorage::create(const char* name, int inf_sz, int sz, int seek)
file = fopen(name, "r+");
}
int sz = inf_sz * inf_count;
inf_size=inf_sz;
full_size=sizeof(CycleStorageElem)+inf_size;
......@@ -360,14 +356,18 @@ bool CycleStorage::delAllRows()
/*! TODO: str, */
void* CycleStorage::readRow(int num, void* str)
{
if( size<=0 ) return 0;
/*! */
int j=(head+num)%size;
if((file==NULL)||(num>=size)) return 0;
if((head!=tail+1)&&(num>tail)) return 0;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
fseek(file,seekpos+j*full_size,0);
fread(jrn,full_size,1,file);
if((jrn->status==1)||(jrn->status==2)||(jrn->status==4))
{
memcpy(str,valPointer(jrn),inf_size);
......
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