Commit 99be11fc authored by Vitaly Lipatov's avatar Vitaly Lipatov

Merge branch 'master' of git.eter:/people/yv/packages/uniset

parents fddc943c 01464e43
...@@ -47,7 +47,7 @@ char* itoa(int val, int base) ...@@ -47,7 +47,7 @@ char* itoa(int val, int base)
} }
void testTable(void) void testTable1(void)
{ {
char *chr=(char*)malloc(20); char *chr=(char*)malloc(20);
char *val=(char*)malloc(40); char *val=(char*)malloc(40);
...@@ -92,6 +92,84 @@ void testTable(void) ...@@ -92,6 +92,84 @@ void testTable(void)
printf("\n"); printf("\n");
} }
void testTable2(void)
{
char *chr=(char*)malloc(20);
char *val=(char*)malloc(40);
TableBlockStorage *t;
t = new TableBlockStorage("blocktable.test", 40, 20000, 5,40);
int i;
printf("testTable\nsize = %d\n",t->block_size);
for(i=1;i<16;i++)
{
chr[0]=i;
val=itoa(i,10);
t->AddRow(chr,val);
}
printf("current block = %d, elements with values=keys added:\n",t->cur_block);
for(i=1;i<16;i++)
{
chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
}
printf("\ncurrent block = %d\n",t->cur_block);
for(i=1;i<16;i++)
{
chr[0]=i;
val=itoa(i+10,10);
t->AddRow(chr,val);
}
for(i=1;i<16;i++)
{
chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
}
printf("\ncurrent block = %d\n",t->cur_block);
for(i=20;i<30;i++)
{
chr[0]=i;
val=itoa(i,10);
t->AddRow(chr,val);
}
for(i=1;i<40;i++)
{
chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
}
printf("\ncurrent block = %d\n",t->cur_block);
chr[0]=30;
strcpy(val,"new block");
t->AddRow(chr,val);
for(i=1;i<40;i++)
{
chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
}
printf("\ncurrent block = %d\n",t->cur_block);
/*for(i=9;i<15;i++)
{
chr[0]=i;
t->DelRow(chr);
}
//printf("elements with keys from 40 to 60 deleted\n");
printf("elements with keys from 9 to 14 deleted\n");
for(i=9;i<15;i++)
{
chr[0]=i;
val=itoa(i+40,10);
t->AddRow(chr,val);
}
//printf("elements with keys from 30 to 50 with values=key+40 added\nvalues from keys 25-59\n");
printf("elements with keys from 9 to 14 with values=key+40 added, all elements:\n");
for(i=0;i<40;i++)
{
chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
}
printf("\n");*/
}
void testJournal1(void) void testJournal1(void)
{ {
CycleStorage *j; CycleStorage *j;
...@@ -154,7 +232,8 @@ int main(int args, char **argv) ...@@ -154,7 +232,8 @@ int main(int args, char **argv)
{ {
/*if(args<2) /*if(args<2)
{ {
printf("Correct usage: jrntest File_name\n"); printf("Correct usage: jrntest [command] [param]\n");
printf(" commands:\n add - add row with text = param\n delete - delete row with number = param\n view - view param2 from row = param1 \n toxml - export journal to XML file with name = param\n del_all - delete all journal\n q or quit - exit\n\n");
return 0; return 0;
} }
CycleStorage *j = new CycleStorage(argv[1],99,1000,0); CycleStorage *j = new CycleStorage(argv[1],99,1000,0);
...@@ -214,7 +293,8 @@ int main(int args, char **argv) ...@@ -214,7 +293,8 @@ int main(int args, char **argv)
} }
else printf("commands:\nadd\ndelete\nview\ntoxml\ndel_all\nenter q or quit to exit\n\n"); else printf("commands:\nadd\ndelete\nview\ntoxml\ndel_all\nenter q or quit to exit\n\n");
}*/ }*/
testTable(); testTable1();
testTable2();
testJournal1(); testJournal1();
testJournal2(); testJournal2();
......
...@@ -39,6 +39,12 @@ struct TableStorageElem ...@@ -39,6 +39,12 @@ struct TableStorageElem
char key[key_size]; char key[key_size];
} __attribute__((__packed__)); } __attribute__((__packed__));
struct TableBlockStorageElem
{
int count;
char key[key_size];
} __attribute__((__packed__));
struct CycleStorageElem struct CycleStorageElem
{ {
char status; char status;
...@@ -58,6 +64,20 @@ class TableStorage ...@@ -58,6 +64,20 @@ class TableStorage
char* FindKeyValue(char* key, char* val); char* FindKeyValue(char* key, char* val);
}; };
class TableBlockStorage
{
FILE *file;
int inf_size;
int max,lim;
public:
int size,cur_block,block_size;
TableBlockStorage(const char* name, int inf_sz, int sz, int block_num, int block_lim);
~TableBlockStorage();
int AddRow(char* key, char* val);
int DelRow(char* key);
char* FindKeyValue(char* key, char* val);
};
class CycleStorage class CycleStorage
{ {
FILE *file; FILE *file;
......
...@@ -11,7 +11,7 @@ libVarious_la_SOURCES = DebugStream.cc Debug.cc UniXML.cc MessageType.cc Config ...@@ -11,7 +11,7 @@ libVarious_la_SOURCES = DebugStream.cc Debug.cc UniXML.cc MessageType.cc Config
ISRestorer.cc ISRestorer_XML.cc \ ISRestorer.cc ISRestorer_XML.cc \
RunLock.cc Mutex.cc SViewer.cc SMonitor.cc LT_Object.cc \ RunLock.cc Mutex.cc SViewer.cc SMonitor.cc LT_Object.cc \
MessageInterface_XML.cc MessageInterface_idXML.cc WDTInterface.cc \ MessageInterface_XML.cc MessageInterface_idXML.cc WDTInterface.cc \
CycleStorage.cc TableStorage.cc CycleStorage.cc TableStorage.cc TableBlockStorage.cc
include $(top_builddir)/conf/setting.mk include $(top_builddir)/conf/setting.mk
......
/* This file is part of the UniSet project
* Copyright (c) 2009 Free Software Foundation, Inc.
* Copyright (c) 2009 Ivan Donchevskiy
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// --------------------------------------------------------------------------
/*! \file
* \author Ivan Donchevskiy
* \date $Date: 2009/07/15 15:55:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/15 15:55:00vpashka Exp $
*/
// --------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Storages.h"
TableBlockStorage::TableBlockStorage(const char* name, int inf_sz, int sz, int block_num, int block_lim)
{
file = fopen(name, "r+");
inf_size=inf_sz;
int i;
max=-1;
lim=block_lim;
size=sz/(sizeof(TableBlockStorageElem)+inf_size);
block_size=size/block_num;
size=block_size*block_num;
TableBlockStorageElem *t = (TableBlockStorageElem*)malloc(sizeof(TableBlockStorageElem)+inf_size);
for(int k=0;k<inf_size;k++)
*((char*)(t)+sizeof(TableBlockStorageElem)+k)=0;
if(file==NULL)
{
file = fopen(name,"w");
cur_block=0;
for(i=0;i<size;i++)
{
if(i%block_size==0)
t->count=-5;
else t->count=-1;
fwrite(t,(sizeof(TableBlockStorageElem)+inf_size),1,file);
}
fclose(file);
file = fopen(name,"r+");
}
else
{
for(i=0;i<block_num;i++)
{
fseek(file,i*block_size*(sizeof(TableBlockStorageElem)+inf_size),0);
fread(t,(sizeof(TableBlockStorageElem)+inf_size),1,file);
if(t->count>=0)
{
cur_block=i;
break;
}
}
fseek(file,(cur_block*block_size)*(sizeof(TableBlockStorageElem)+inf_size),0);
for(i=0;i<block_size;i++);
{
fread(t,(sizeof(TableBlockStorageElem)+inf_size),1,file);
if(t->count>max) max=t->count;
}
}
}
TableBlockStorage::~TableBlockStorage()
{
fclose(file);
}
int TableBlockStorage::AddRow(char* key, char* value)
{
TableBlockStorageElem *tbl = (TableBlockStorageElem*)malloc(sizeof(TableBlockStorageElem)+inf_size);
int i=0,pos=-1,empty=-1,k,j;
if(file!=NULL)
{
fseek(file,cur_block*block_size*(sizeof(TableBlockStorageElem)+inf_size),0);
if(max==lim-1)
{
if((cur_block+2)*block_size<size)
{
max=-1;
j=0;
for(i=0;i<block_size;i++)
{
fseek(file,(cur_block*block_size+i)*(sizeof(TableBlockStorageElem)+inf_size),0);
fread(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
if(tbl->count>=0)
{
tbl->count=++max;
fseek(file,((cur_block+1)*block_size+j)*(sizeof(TableBlockStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
j++;
}
}
fseek(file,((cur_block+1)*block_size+j)*(sizeof(TableBlockStorageElem)+inf_size),0);
tbl->count=++max;
strcpy(tbl->key,key);
for(k=0;k<inf_size;k++)
*((char*)(tbl)+sizeof(TableBlockStorageElem)+k)=*(value+k);
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
tbl->count=-5;
fseek(file,cur_block*block_size*(sizeof(TableBlockStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
cur_block++;
return 0;
}
}
for(i=0;i<block_size;i++)
{
fread(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
if((tbl->key[0]!=0)&&(tbl->count>=0))
if(!strcmp(tbl->key,key))
pos = i;
if((tbl->count<0)&&(empty<0)) empty=i;
}
if(pos>=0)
{
fseek(file,(cur_block*block_size+pos)*(sizeof(TableBlockStorageElem)+inf_size),0);
fread(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
tbl->count=++max;
for(k=0;k<inf_size;k++)
*((char*)(tbl)+sizeof(TableBlockStorageElem)+k)=*(value+k);
fseek(file,(cur_block*block_size+pos)*(sizeof(TableBlockStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
return 0;
}
fseek(file,(cur_block*block_size+empty)*(sizeof(TableBlockStorageElem)+inf_size),0);
fread(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
tbl->count=++max;
strcpy(tbl->key,key);
for(k=0;k<inf_size;k++)
*((char*)(tbl)+sizeof(TableBlockStorageElem)+k)=*(value+k);
fseek(file,(cur_block*block_size+empty)*(sizeof(TableBlockStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
return 0;
}
return 1;
}
int TableBlockStorage::DelRow(char* key)
{
TableBlockStorageElem *tbl = (TableBlockStorageElem*)malloc(sizeof(TableBlockStorageElem)+inf_size);
int i,j;
if(file!=NULL)
{
fseek(file,cur_block*block_size*(sizeof(TableBlockStorageElem)+inf_size),0);
if(max==lim-1)
{
if((cur_block+2)*block_size<size)
{
max=-1;
j=0;
for(i=0;i<block_size;i++)
{
fseek(file,(cur_block*block_size+i)*(sizeof(TableBlockStorageElem)+inf_size),0);
fread(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
if(tbl->count>=0)
{
tbl->count=++max;
fseek(file,((cur_block+1)*block_size+j)*(sizeof(TableBlockStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
j++;
}
}
tbl->count=-5;
fseek(file,cur_block*block_size*(sizeof(TableBlockStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
cur_block++;
fseek(file,cur_block*block_size*(sizeof(TableBlockStorageElem)+inf_size),0);
}
}
for(i=0;i<block_size;i++)
{
fread(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
if((tbl->key[0]!=0)&&(tbl->count>=0))
if(!strcmp(tbl->key,key))
{
fseek(file,(cur_block*block_size+i)*(sizeof(TableBlockStorageElem)+inf_size),0);
tbl->count=++max;
tbl->key[0]=0;
fwrite(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
return 0;
}
}
}
return 1;
}
char* TableBlockStorage::FindKeyValue(char* key, char* val)
{
TableBlockStorageElem *tbl = (TableBlockStorageElem*)malloc(sizeof(TableBlockStorageElem)+inf_size);
int i,k;
if(file!=NULL)
{
fseek(file,cur_block*block_size*(sizeof(TableBlockStorageElem)+inf_size),0);
for(i=0;i<block_size;i++)
{
fread(tbl,(sizeof(TableBlockStorageElem)+inf_size),1,file);
if((tbl->key[0]!=0)&&(tbl->count>=0))
if(!strcmp(tbl->key,key))
{
for(k=0;k<inf_size;k++)
*(val+k)=*((char*)(tbl)+sizeof(TableBlockStorageElem)+k);
return val;
}
}
}
return 0;
}
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