Commit 6e74886d authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

function setSize in CycleStorage and few changes in JrnTest to test it

parent f2377dd4
......@@ -211,7 +211,7 @@ bool reOpen()
int i,k=0;
char *str = new char[30];
printf("the same after reopen:\n");
if(!j.open("big_file.test",30,32000,seek))
if(!j.open("big_file.test",30,33000,seek))
{
printf("Reopen file error\n");
delete str;
......@@ -237,7 +237,7 @@ bool testJournal1(void)
int i,k=0;
char *str = new char[30];
printf("journal test 1\n");
for(i=1;i<33000;i++)
for(i=1;i<64001;i++)
{
sprintf(str,"%d",i);
j.addRow(str);
......@@ -257,7 +257,7 @@ bool testJournal1(void)
return false;
}
k = 0;
j.setSize(33000);
TableBlockStorage t("big_file.test", 4, 40, 100, 5,28,0);
printf("test of 2 classes working in 1 file together\n");
char *val = new char[40];
......@@ -294,7 +294,7 @@ bool testJournal1(void)
k = 0;
printf("first 20 after adding 10 elements\n");
for(i=10001;i<10011;i++)
for(i=10000001;i<10000011;i++)
{
sprintf(str,"%d",i);
j.addRow(str);
......@@ -319,6 +319,20 @@ bool testJournal1(void)
if(!reOpen()) return false;
j.setSize(32000);
for(i=0;i<32000;i++)
{
if(j.readRow(i,str))
{
printf("%s\n",str);
k++;
}
}
if( k>0 )
{
delete str;
return false;
}
delete str;
return true;
}
......
......@@ -182,6 +182,9 @@ class CycleStorage
/*! - / ( ) */
int getIter(void);
/*! ( ) */
bool setSize(int count);
inline int getByteSize() { return (size*full_size + sizeof(CycleStorageAttr)); }
inline int getSize(){ return size; }
......
......@@ -365,7 +365,7 @@ void* CycleStorage::readRow(int num, void* str)
int j=(head+num)%size;
if((file==NULL)||(num>=size)) return 0;
if((head!=tail+1)&&(num>tail)) return 0;
if((head!=tail+1)&&(num>tail)&&(head!=tail)) return 0;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
fseek(file,seekpos+j*full_size,0);
......@@ -385,3 +385,70 @@ int CycleStorage::getIter()
{
return iter;
}
/*! . , .
, , . , . .
, */
bool CycleStorage::setSize(int count)
{
if( file==NULL ) return false;
if( count==size ) return true;
fseek(file,seekpos-sizeof(CycleStorageAttr),0);
/*! */
CycleStorageAttr csa;
csa.inf_size=inf_size;
csa.size=count;
csa.seekpos=seekpos-sizeof(CycleStorageAttr);
fwrite(&csa,sizeof(CycleStorageAttr),1,file);
fflush(file);
if(count<size)
{
size=count;
return delAllRows();
}
int num = count - size;
size = count;
CycleStorageElem *jrn = (CycleStorageElem*)new char[full_size];
if(( head==tail+1 ) || (( tail==size-num-1 ) && ( head==0 )))
{
if( head<size-num-1 )
fseek(file, seekpos + (size-num-1)*full_size, 0);
else
fseek(file, seekpos, 0);
fread(jrn, full_size, 1, file);
if(( jrn->status==2 ) || (jrn->status==3 ))
jrn->status = ( head<size-num-1 ) ? 3 : 5;
else if(( jrn->status==4 ) || (jrn->status==5 ))
jrn->status = ( head<size-num-1 ) ? 5 : 3;
else
{
delete[] jrn;
return false;
}
for( int i=num; i>0; i-- )
{
filewrite(jrn, size-i, false);
}
fflush(file);
if( head==0 )
tail = size-1;
}
else
{
jrn->status=0;
for( int i=num; i>0; i-- )
filewrite(jrn, size-i, false);
fflush(file);
}
delete[] jrn;
return true;
}
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