Commit 070c4032 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

fixed bug with CycleStorage (bug 4174)

parent e82c4dc3
...@@ -199,6 +199,32 @@ bool testTable2(void) ...@@ -199,6 +199,32 @@ bool testTable2(void)
return true; return true;
} }
bool reOpen()
{
CycleStorage j;
int i,k=0;
char *str = new char[30];
printf("the same after reopen:\n");
if(!j.open("big_file.test",30,32000,20000))
{
printf("Reopen file error\n");
delete str;
return false;
}
for(i=0;i<20;i++)
{
if(j.readRow(i,str))
{
printf("%s\n",str);
k++;
}
}
delete str;
if(k != 10)
return false;
return true;
}
bool testJournal1(void) bool testJournal1(void)
{ {
CycleStorage j("big_file.test",30,32000,20000,true); CycleStorage j("big_file.test",30,32000,20000,true);
...@@ -282,43 +308,11 @@ bool testJournal1(void) ...@@ -282,43 +308,11 @@ bool testJournal1(void)
} }
k = 0; k = 0;
printf("the same after reopen:\n");
if(!j.open("big_file.test",30,32000,20000))
{
printf("Reopen file error\n");
delete str;
return false;
}
for(i=0;i<20;i++)
{
if(j.readRow(i,str))
{
printf("%s\n",str);
k++;
}
}
if(k != 10)
{
delete str;
return false;
}
k = 0;
printf("the same after reopen:\n"); if(!reOpen()) return false;
printf("opening... %d\n",j.open("big_file.test",30,32000,20000));
for(i=0;i<20;i++) if(!reOpen()) return false;
{
if(j.readRow(i,str))
{
printf("%s\n",str);
k++;
}
}
if(k != 10)
{
delete str;
return false;
}
delete str; delete str;
return true; return true;
} }
...@@ -366,9 +360,10 @@ bool testJournal3() ...@@ -366,9 +360,10 @@ bool testJournal3()
j.getFullSize() j.getFullSize()
); );
JItem ji;
printf("write 30 elements:\n");
for(int i=0;i<30;i++) printf("write 35 elements:\n");
for(int i=0;i<35;i++)
{ {
JItem ji; JItem ji;
ji.id = i; ji.id = i;
...@@ -378,9 +373,8 @@ bool testJournal3() ...@@ -378,9 +373,8 @@ bool testJournal3()
j.addRow(&ji); j.addRow(&ji);
} }
printf("read first 5 elements:\n"); printf("read first 10 elements:\n");
JItem ji;
for( int i=0;i<10;i++) for( int i=0;i<10;i++)
{ {
if( j.readRow(i,&ji) ) if( j.readRow(i,&ji) )
......
...@@ -59,7 +59,8 @@ void CycleStorage::filewrite(CycleStorageElem* jrn,int seek,bool needflush) ...@@ -59,7 +59,8 @@ void CycleStorage::filewrite(CycleStorageElem* jrn,int seek,bool needflush)
{ {
fseek(file,seekpos+seek*full_size,0); fseek(file,seekpos+seek*full_size,0);
fwrite(jrn,full_size,1,file); fwrite(jrn,full_size,1,file);
if(needflush) fflush(file); if(needflush)
fflush(file);
} }
/*! jrn->status: 0 - , 1 - , 2,4 - /*! jrn->status: 0 - , 1 - , 2,4 -
...@@ -71,7 +72,6 @@ bool CycleStorage::findHead() ...@@ -71,7 +72,6 @@ bool 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;
iter=0; iter=0;
seekpos+=sizeof(CycleStorageAttr);
fread(jrn,full_size,1,file); fread(jrn,full_size,1,file);
if(jrn->status==0) if(jrn->status==0)
{ {
...@@ -121,8 +121,10 @@ bool CycleStorage::findHead() ...@@ -121,8 +121,10 @@ bool CycleStorage::findHead()
r=mid; r=mid;
break; break;
} }
else else
{
return false; return false;
}
} }
if(r<size) if(r<size)
head=r; head=r;
...@@ -139,13 +141,13 @@ bool CycleStorage::open(const char* name, int inf_sz, int inf_count, int seek) ...@@ -139,13 +141,13 @@ bool CycleStorage::open(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+");
if(file==NULL) return false; if(file==NULL) return false;
seekpos=seek; if(fseek(file,seek,0)==-1) return false;
if(fseek(file,seekpos,0)==-1) return false;
/*! */ /*! */
CycleStorageAttr csa; CycleStorageAttr csa;
...@@ -157,11 +159,12 @@ bool CycleStorage::open(const char* name, int inf_sz, int inf_count, int seek) ...@@ -157,11 +159,12 @@ bool CycleStorage::open(const char* name, int inf_sz, int inf_count, int seek)
inf_size=inf_sz; inf_size=inf_sz;
full_size=sizeof(CycleStorageElem)+inf_size; full_size=sizeof(CycleStorageElem)+inf_size;
size=inf_count; size=inf_count;
seekpos=seek; seekpos=seek+sizeof(CycleStorageAttr);
seekpos+=sizeof(CycleStorageAttr);
if(!findHead()) if(!findHead())
{
return false; return false;
}
return true; return true;
} }
...@@ -191,15 +194,14 @@ bool CycleStorage::create(const char* name, int inf_sz, int inf_count, int seek) ...@@ -191,15 +194,14 @@ bool CycleStorage::create(const char* name, int inf_sz, int inf_count, int seek)
jrn->status=0; jrn->status=0;
/*! */ /*! */
CycleStorageAttr *csa = new CycleStorageAttr(); CycleStorageAttr csa;
csa->inf_size=inf_size; csa.inf_size=inf_size;
csa->size=size; csa.size=size;
csa->seekpos=seekpos; csa.seekpos=seekpos;
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++)
......
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