Commit 2ee860c5 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

parameter sz removed with inf_count in TableBlockStorage. Added functions…

parameter sz removed with inf_count in TableBlockStorage. Added functions getByteSize to CycleStorage and TableBlockStorage.
parent 299ba4e4
...@@ -31,6 +31,8 @@ ...@@ -31,6 +31,8 @@
#include "Storages.h" #include "Storages.h"
#include "UniXML.h" #include "UniXML.h"
int seek=0;
void testTable1(void) void testTable1(void)
{ {
char *chr=new char[20]; char *chr=new char[20];
...@@ -77,7 +79,9 @@ bool testTable2(void) ...@@ -77,7 +79,9 @@ 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, 100, 5,28,0);
seek=t.getByteSize();
printf("Table size in bytes = %d\n",seek);
int i; int i;
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
...@@ -187,7 +191,7 @@ bool testTable2(void) ...@@ -187,7 +191,7 @@ bool testTable2(void)
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, 100, 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);
...@@ -207,7 +211,7 @@ bool reOpen() ...@@ -207,7 +211,7 @@ bool reOpen()
int i,k=0; int i,k=0;
char *str = new char[30]; char *str = new char[30];
printf("the same after reopen:\n"); printf("the same after reopen:\n");
if(!j.open("big_file.test",30,32000,20000)) if(!j.open("big_file.test",30,32000,seek))
{ {
printf("Reopen file error\n"); printf("Reopen file error\n");
delete str; delete str;
...@@ -229,7 +233,7 @@ bool reOpen() ...@@ -229,7 +233,7 @@ bool reOpen()
bool testJournal1(void) bool testJournal1(void)
{ {
CycleStorage j("big_file.test",30,32000,20000,true); CycleStorage j("big_file.test",30,32000,seek,true);
int i,k=0; int i,k=0;
char *str = new char[30]; char *str = new char[30];
printf("journal test 1\n"); printf("journal test 1\n");
...@@ -254,8 +258,8 @@ bool testJournal1(void) ...@@ -254,8 +258,8 @@ bool testJournal1(void)
} }
k = 0; k = 0;
TableBlockStorage t("big_file.test", 4, 40, 100, 5,28,0);
printf("test of 2 classes working in 1 file together\n"); printf("test of 2 classes working in 1 file together\n");
TableBlockStorage t("big_file.test", 4, 40, 20000, 5,28,0);
char *val = new char[40]; char *val = new char[40];
for(i=1;i<20;i++) for(i=1;i<20;i++)
{ {
...@@ -321,20 +325,20 @@ bool testJournal1(void) ...@@ -321,20 +325,20 @@ bool testJournal1(void)
void testJournal2(void) void testJournal2(void)
{ {
CycleStorage j("big_file.test",30,32000,20000); CycleStorage j("big_file.test",30,32000,seek);
int i,k; int i,k;
char *str = new char[30]; char *str = new char[30];
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<2999;k++)
{ {
sprintf(str,"%d",k); sprintf(str,"%d",k);
j.addRow(str); j.addRow(str);
} }
j.open("big_file.test",30,32000,20000); j.open("big_file.test",30,32000,seek);
printf("iterations = %d\n",j.getIter()); printf("i=%d, iterations = %d\n", i, j.getIter());
} }
printf("\n"); printf("\n");
delete str; delete str;
...@@ -356,10 +360,11 @@ bool testJournal3() ...@@ -356,10 +360,11 @@ bool testJournal3()
return false; return false;
} }
printf("Joural size=%d inf_size=%d full_size=%d\n", printf("Joural size=%d inf_size=%d full_size=%d byte_size=%d\n",
j.getSize(), j.getSize(),
j.getInfSize(), j.getInfSize(),
j.getFullSize() j.getFullSize(),
j.getByteSize()
); );
JItem ji; JItem ji;
......
...@@ -92,16 +92,25 @@ class TableBlockStorage ...@@ -92,16 +92,25 @@ class TableBlockStorage
/*! Open, create=true /*! Open, create=true
*/ */
TableBlockStorage(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek, bool create=false); TableBlockStorage(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool create=false);
~TableBlockStorage(); ~TableBlockStorage();
/*! inf_sz - , key_sz - , sz - /*!
block_num - - ( = sz/block_num \param inf_sz - ,
, ), block_lim - , \param key_sz - ,
seek - ( , ) */ \param inf_count - - ( inf_sz+key_sz
bool open(const char* name, int inf_sz, int key_sz, int sz, int block_num, int block_lim, int seek); +sizeof(int) )
bool create(const char* name, int inf_sz, int key_sz, int sz, int block_num, int block_lim, int seek); \param block_num - - ( - = inf_count*block_num,
\param block_lim - ,
\param seek - ( , )
inf_count*block_num*(inf_sz+key_sz+sizeof(int)) +
sizeof(StorageAttr), - ,
, getByteSize()
*/
bool open(const char* name, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek);
bool create(const char* name, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek);
/*! , */ /*! , */
bool addRow(void* key, void* val); bool addRow(void* key, void* val);
...@@ -114,6 +123,9 @@ class TableBlockStorage ...@@ -114,6 +123,9 @@ class TableBlockStorage
/*! ( ) */ /*! ( ) */
int getCurBlock(void); int getCurBlock(void);
inline int getByteSize() { return (size*full_size + sizeof(StorageAttr)); }
protected: protected:
FILE *file; FILE *file;
int inf_size; int inf_size;
...@@ -148,6 +160,7 @@ class CycleStorage ...@@ -148,6 +160,7 @@ class CycleStorage
inf_count*(inf_sz+1) + sizeof(CycleStorageAttr), inf_count*(inf_sz+1) + sizeof(CycleStorageAttr),
- -
, getByteSize()
*/ */
bool open(const char* name, int inf_sz, int inf_count, int 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 create(const char* name, int inf_sz, int inf_count, int seek);
...@@ -169,6 +182,7 @@ class CycleStorage ...@@ -169,6 +182,7 @@ class CycleStorage
/*! - / ( ) */ /*! - / ( ) */
int getIter(void); int getIter(void);
inline int getByteSize() { return (size*full_size + sizeof(CycleStorageAttr)); }
inline int getSize(){ return size; } inline int getSize(){ return size; }
inline int getInfSize(){ return inf_size; } inline int getInfSize(){ return inf_size; }
inline int getFullSize(){ return full_size; } inline int getFullSize(){ return full_size; }
......
...@@ -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 cr) TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, int inf_count, 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, inf_count, block_num, block_lim, seek))
{ {
if(cr) if(cr)
create(name, key_sz, inf_sz, sz, block_num, block_lim, seek); create(name, key_sz, inf_sz, inf_count, block_num, block_lim, seek);
else else
file=NULL; file=NULL;
} }
...@@ -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 inf_count, int block_num, int block_lim, int seek)
{ {
/*! , */ /*! , */
if(file!=NULL) fclose(file); if(file!=NULL) fclose(file);
...@@ -136,9 +136,7 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, i ...@@ -136,9 +136,7 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, i
full_size = sizeof(TableBlockStorageElem)+key_sz+inf_sz; full_size = sizeof(TableBlockStorageElem)+key_sz+inf_sz;
int tmpsize=(sz-sizeof(StorageAttr))/(full_size); int tmpsize=inf_count*block_num;
int tmpblock=tmpsize/block_num;
tmpsize=tmpblock*block_num;
/*! */ /*! */
if((sa.k_size!=key_sz)||(sa.inf_size!=inf_sz)||(sa.size!=tmpsize)||(sa.block_number!=block_num)||(sa.lim!=block_lim)||(sa.seekpos!=seek)) if((sa.k_size!=key_sz)||(sa.inf_size!=inf_sz)||(sa.size!=tmpsize)||(sa.block_number!=block_num)||(sa.lim!=block_lim)||(sa.seekpos!=seek))
...@@ -151,9 +149,8 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, i ...@@ -151,9 +149,8 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, i
k_size=key_sz; k_size=key_sz;
inf_size=inf_sz; inf_size=inf_sz;
lim=block_lim; lim=block_lim;
size=(sz-sizeof(StorageAttr))/(full_size);
block_number=block_num; block_number=block_num;
block_size=size/block_num; block_size=inf_count;
size=block_size*block_num; size=block_size*block_num;
max=-1; max=-1;
...@@ -188,7 +185,7 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int sz, i ...@@ -188,7 +185,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 inf_count, 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+");
...@@ -205,10 +202,9 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int sz, ...@@ -205,10 +202,9 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int sz,
full_size = sizeof(TableBlockStorageElem)+k_size+inf_size; full_size = sizeof(TableBlockStorageElem)+k_size+inf_size;
int i; int i;
size=(sz-sizeof(StorageAttr))/(full_size);
block_number=block_num; block_number=block_num;
block_size=size/block_num; block_size=inf_count;
size=block_size*block_num; size=block_size*block_num;
max=-1; max=-1;
...@@ -252,15 +248,6 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int sz, ...@@ -252,15 +248,6 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int sz,
cur_block=0; cur_block=0;
fflush(file); fflush(file);
/*! , , */
int emp = sz-size*full_size-sizeof(StorageAttr);
if(emp>0)
{
char* empty=new char[emp];
fwrite(empty,emp,1,file);
fflush(file);
delete empty;
}
return true; 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