Commit 8dc14337 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

Added check for byte size of TableBlockStorage

parent 833a4b73
......@@ -32,6 +32,7 @@
#include "UniXML.h"
int seek=0;
int b_size=100000;
void testTable1(void)
{
......@@ -79,7 +80,7 @@ bool testTable2(void)
char *val=new char[40];
TableBlockStorage t;
//t = new TableBlockStorage();
t.create("big_file.test", 4, 40, 100, 5,28,0);
t.create("big_file.test", b_size, 4, 40, 100, 5,28,0);
seek=t.getByteSize();
printf("Table size in bytes = %d\n",seek);
int i;
......@@ -191,7 +192,7 @@ bool testTable2(void)
return false;
}
printf("after reopen:\n");
t.open("big_file.test", 4, 40, 100, 5,28,0);
t.open("big_file.test", b_size, 4, 40, 100, 5,28,0);
for(i=1;i<20;i++)
{
if(t.findKeyValue(&i,val)!=0) printf("%s, ",val);
......@@ -259,7 +260,7 @@ bool testJournal1(void)
k = 0;
printf("size changed to 33000 rows (increased)\n");
j.setSize(33000);
TableBlockStorage t("big_file.test", 4, 40, 100, 5,28,0);
TableBlockStorage t("big_file.test", b_size, 4, 40, 100, 5,28,0);
printf("test of 2 classes working in 1 file together\n");
char *val = new char[40];
for(i=1;i<20;i++)
......
......@@ -92,7 +92,7 @@ class TableBlockStorage
/*! Open, create=true
*/
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(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool create=false);
~TableBlockStorage();
......@@ -109,8 +109,8 @@ class TableBlockStorage
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 open(const char* name, int byte_sz, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek);
bool create(const char* name, int byte_sz, int inf_sz, int key_sz, int inf_count, int block_num, int block_lim, int seek);
/*! , */
bool addRow(void* key, void* val);
......
......@@ -35,13 +35,13 @@ TableBlockStorage::TableBlockStorage()
file=NULL;
}
TableBlockStorage::TableBlockStorage(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool cr)
TableBlockStorage::TableBlockStorage(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek, bool cr)
{
file=NULL;
if(!open(name, key_sz, inf_sz, inf_count, block_num, block_lim, seek))
if(!open(name, byte_sz, key_sz, inf_sz, inf_count, block_num, block_lim, seek))
{
if(cr)
create(name, key_sz, inf_sz, inf_count, block_num, block_lim, seek);
create(name, byte_sz, key_sz, inf_sz, inf_count, block_num, block_lim, seek);
else
file=NULL;
}
......@@ -115,7 +115,7 @@ bool TableBlockStorage::copyToNextBlock(void)
return true;
}
bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek)
bool TableBlockStorage::open(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek)
{
/*! , */
if(file!=NULL) fclose(file);
......@@ -154,6 +154,13 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_c
block_size=inf_count;
size=block_size*block_num;
if( byte_sz<getByteSize() )
{
fclose(file);
file=NULL;
return false;
}
max=-1;
/*! */
......@@ -182,16 +189,11 @@ bool TableBlockStorage::open(const char* name, int key_sz, int inf_sz, int inf_c
return true;
}
bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek)
bool TableBlockStorage::create(const char* name, int byte_sz, int key_sz, int inf_sz, int inf_count, int block_num, int block_lim, int seek)
{
if(file!=NULL) fclose(file);
file = fopen(name, "r+");
if(file==NULL)
{
FILE*f=fopen(name,"w");
fclose(f);
file = fopen(name, "r+");
}
k_size=key_sz;
inf_size=inf_sz;
seekpos=seek;
......@@ -205,6 +207,20 @@ bool TableBlockStorage::create(const char* name, int key_sz, int inf_sz, int inf
size=block_size*block_num;
max=-1;
if( byte_sz<getByteSize() )
{
if( file!=NULL ) fclose(file);
file=NULL;
return false;
}
if(file==NULL)
{
FILE*f=fopen(name,"w");
fclose(f);
file = fopen(name, "r+");
}
if(fseek(file,seekpos,0)==-1) return false;
/*! */
......
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