Commit 5ab9f645 authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

Some comments for CycleStorage and TableBlockStorage

parent b4113aa4
......@@ -75,11 +75,10 @@ void testTable1(void)
void testTable2(void)
{
char *val=(char*)malloc(40);
char *val=new char[40];
TableBlockStorage *t;
t = new TableBlockStorage("big_file.test", 4, 40, 20000, 5,28,0);
int i;
printf("testTable\nsize = %d\n",t->block_size);
for(i=1;i<20;i++)
{
if(t->FindKeyValue((char*)&i,val)!=0) printf("%s, ",val);
......@@ -133,59 +132,62 @@ void testTable2(void)
t->Open("big_file.test", 4, 40, 20000, 5,28,0);
i=11;
sprintf(val,"%d",i);
//t->AddRow((char*)&i,val);
t->AddRow((char*)&i,val);
for(i=1;i<20;i++)
{
if(t->FindKeyValue((char*)&i,val)!=0) printf("%s, ",val);
}
printf("\ncurrent block = %d\n",t->cur_block);
delete t;
}
void testJournal1(void)
{
CycleStorage *j;
int i;
char *str = (char*)malloc(30);
char *str = new char[30];
printf("journal test 1\n");
j = new CycleStorage("big_file.test",30,1000000,20000);
printf("size = %d\n",j->size);
for(i=1;i<40000;i++)
for(i=1;i<33000;i++)
{
sprintf(str,"%d",i);
j->AddRow(str);
}
printf("\n20 elements from 10-th:\n");
j->ViewRows(10,20);
printf("\nfirst 30 elements:\n");
j->ViewRows(0,30);
printf("test of 2 classes working in 1 file together\n");
TableBlockStorage *t = new TableBlockStorage("big_file.test", 4, 40, 20000, 5,28,0);
char *val = (char*)malloc(40);
char *val = new char[40];
for(i=1;i<20;i++)
{
if(t->FindKeyValue((char*)&i,val)!=0) printf("%s, ",val);
}
printf("\ncurrent block = %d\n\n",t->cur_block);
printf("\n20 elements from 10-th after deleting first 20:\n");
printf("\nfirst 30 elements after deleting first 20:\n");
for(i=0;i<20;i++)
{
j->DelRow(i);
}
j->ViewRows(10,20);
j->ViewRows(0,30);
printf("\nfirst 20 after adding 10 elements\n");
for(i=1;i<11;i++)
for(i=10001;i<10011;i++)
{
sprintf(str,"%d",i);
j->AddRow(str);
}
j->ViewRows(0,20);
printf("\nthe same after reopen:\n");
delete j;
j = new CycleStorage();
j->Open("big_file.test",30,1000000,20000);
j->ViewRows(0,20);
printf("\n");
j->ExportToXML("Xml.xml");
delete t;
delete j;
}
void testJournal2(void)
......@@ -209,12 +211,13 @@ void testJournal2(void)
printf("iterations = %d\n",j->iter);
}
printf("\n");
delete j;
}
int main(int args, char **argv)
{
testTable1();
//testTable1();
testTable2();
testJournal1();
......
......@@ -80,10 +80,11 @@ class TableBlockStorage
{
int max;
TableBlockStorageElem** mem;
FILE *file;
int inf_size, k_size, lim,seekpos;
int size,block_size,block_number,full_size;
public:
FILE *file;
int inf_size, k_size, lim,seekpos;
int size,cur_block,block_size,block_number;
int cur_block;
TableBlockStorage();
TableBlockStorage(const char* name, int key_sz, int inf_sz, int sz, int block_num, int block_lim, int seek);
~TableBlockStorage();
......
......@@ -24,6 +24,10 @@
*/
// --------------------------------------------------------------------------
/*! CycleStorage, ,
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -56,6 +60,9 @@ void CycleStorage::filewrite(CycleStorageElem* jrn,int seek,bool needflush)
bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
{
/*! ,
*/
if(file!=NULL) fclose(file);
file = fopen(name, "r+");
if(file==NULL) return false;
......@@ -85,6 +92,12 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
iter=0;
seekpos+=sizeof(CycleStorageAttr);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
/*! jrn->status: 0 - , 1 - , 2,4 -
( 2 - 4 , ), 3 - 2, 5 - 4, 6 - 1.
|
*/
if(jrn->status==0)
{
head=-1;
......@@ -198,6 +211,9 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
}
fflush(file);
/*! , ,
*/
int emp = sz-size*(sizeof(CycleStorageElem)+inf_size)-sizeof(CycleStorageAttr);
if(emp>0)
{
......@@ -218,6 +234,10 @@ bool CycleStorage::AddRow(char* str)
if(file==NULL) return false;
CycleStorageElem *jrn = (CycleStorageElem*)new char[(sizeof(CycleStorageElem)+inf_size)];
int i=0,k;
/*! 2 - (head=-1), 1 (head=tail=0) )
*/
if(head==-1)
{
jrn->status=1;
......@@ -239,9 +259,17 @@ bool CycleStorage::AddRow(char* str)
}
fseek(file,seekpos+tail*(sizeof(CycleStorageElem)+inf_size),0);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
if((jrn->status==2)||(jrn->status==3))
i=2;
/*! 2, 3 -> 2; 4, 5 -> 4
*/
if((jrn->status==2)||(jrn->status==3)) i=2;
else i=4;
/*! ,
2->4, 4->2
*/
if(tail==size-1)
{
fseek(file,seekpos,0);
......@@ -259,6 +287,10 @@ bool CycleStorage::AddRow(char* str)
}
else
{
/*! , ,
. . 1
*/
head++;
if(head>=size) head=0;
jrn->status=i;
......@@ -282,6 +314,10 @@ bool CycleStorage::DelRow(int row)
CycleStorageElem *jrn = (CycleStorageElem*)new char[(sizeof(CycleStorageElem)+inf_size)];
fseek(file,seekpos+i*(sizeof(CycleStorageElem)+inf_size),0);
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
/*! 1->6, 2->3, 4->5 false
*/
if(jrn->status==1)
{
jrn->status=6;
......@@ -367,7 +403,7 @@ bool CycleStorage::ExportToXML(const char* name)
fseek(file,seekpos,0);
}
fread(jrn,(sizeof(CycleStorageElem)+inf_size),1,file);
if((jrn->status!=0)&&(jrn->status!=3)&&(jrn->status!=5)&&(jrn->status!=6))
if((jrn->status==1)||(jrn->status==2)||(jrn->status==4))
{
f->createNext(f->cur,"!",((char*)(jrn)+sizeof(CycleStorageElem)));
}
......
......@@ -24,6 +24,10 @@
*/
// --------------------------------------------------------------------------
/*! TableBlockStorage, - -
, ,
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
......@@ -216,6 +220,11 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
fflush(file);
seekpos+=sizeof(StorageAttr);
/*! :
(-5) - , , ,
(-1) -
*/
for(i=0;i<size;i++)
{
if(i%block_size==0)
......@@ -272,6 +281,9 @@ bool TableBlockStorage::DelRow(char* key)
int i;
if(file==NULL) return false;
/*!
*/
CopyToNextBlock();
for(i=0;i<block_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