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

revision StorageInterface...

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