Commit 2619eb7e authored by Ivan Donchevskiy's avatar Ivan Donchevskiy

make StorageTable and CycleStorage work with any types, not only strings

parent c57ad32b
/* This file is part of the UniSet project /* This file is part of the UniSet project
* Copyright (c) 2002 Free Software Foundation, Inc. * Copyright (c) 2009 Free Software Foundation, Inc.
* Copyright (c) 2002 Ivan Donchevskiy * Copyright (c) 2009 Ivan Donchevskiy
* *
* This program is free software; you can redistribute it and/or modify * 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 * it under the terms of the GNU General Public License as published by
...@@ -19,12 +19,13 @@ ...@@ -19,12 +19,13 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/*! \file /*! \file
* \author Ivan Donchevskiy * \author Ivan Donchevskiy
* \date $Date: 2009/07/14 16:59:00 $ * \date $Date: 2009/07/15 15:55:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/14 16:59:00 vpashka Exp $ * \version $Id: Jrn.h,v 1.0 2009/07/15 15:55:00 vpashka Exp $
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#include "stdio.h" #include "stdio.h"
#include "stdlib.h"
#include "string.h" #include "string.h"
#include "Storages.h" #include "Storages.h"
...@@ -46,35 +47,42 @@ char* itoa(int val, int base) ...@@ -46,35 +47,42 @@ char* itoa(int val, int base)
void testTable(void) void testTable(void)
{ {
char *chr=new char[2]; char *chr=(char*)malloc(20);
char *val=new char[2]; char *val=(char*)malloc(40);
TableStorage *t; TableStorage *t;
t = new TableStorage("table.test", 6000, 0); t = new TableStorage("table.test", 40, 6000, 0);
int i; int i;
printf("testTable\nsize = %d\n",t->size); printf("testTable\nsize = %d\n",t->size);
for(i=0;i<t->size+5;i++) for(i=0;i<t->size+5;i++)
{ {
chr[0]=i%256; chr[0]=i;
if(t->AddRow(chr,chr)==1) printf("elem number %d - no space in TableStorage\n",i); val=itoa(i,10);
if(t->AddRow(chr,val)==1) printf("elem number %d - no space in TableStorage\n",i);
}
printf("elements with values=keys added\nvalues from keys 25-59\n");
for(i=25;i<60;i++)
{
chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
} }
printf("elements with values=keys added\n"); printf("\n");
for(i=40;i<60;i++) for(i=40;i<60;i++)
{ {
chr[0]=i%256; chr[0]=i;
t->DelRow(chr); t->DelRow(chr);
} }
printf("elements with keys from 40 to 60 deleted\n"); printf("elements with keys from 40 to 60 deleted\n");
for(i=30;i<50;i++) for(i=30;i<50;i++)
{ {
chr[0]=i; chr[0]=i;
val[0]=i+40; val=itoa(i+40,10);
t->AddRow(chr,val); if(t->AddRow(chr,val)==1) printf("elem number %d - no space in TableStorage\n",i);
} }
printf("elements with keys from 30 to 50 with values=key+40 added\nvalues from keys 25-59\n"); printf("elements with keys from 30 to 50 with values=key+40 added\nvalues from keys 25-59\n");
for(i=25;i<60;i++) for(i=25;i<60;i++)
{ {
chr[0]=i; chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%d, ",val[0]); if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
} }
printf("\n"); printf("\n");
} }
...@@ -83,11 +91,11 @@ void testJournal1(void) ...@@ -83,11 +91,11 @@ void testJournal1(void)
{ {
CycleStorage *j; CycleStorage *j;
int i; int i;
char *str=new char[6]; char *str = (char*)malloc(30);
printf("journal test 1\n"); printf("journal test 1\n");
j = new CycleStorage("journal.test",2000000,0); j = new CycleStorage("journal.test",30,1000000,0);
printf("size = %d\n",j->size); printf("size = %d\n",j->size);
for(i=1;i<30000;i++) for(i=1;i<40000;i++)
{ {
str = itoa(i,10); str = itoa(i,10);
j->AddRow(str); j->AddRow(str);
...@@ -108,8 +116,7 @@ void testJournal1(void) ...@@ -108,8 +116,7 @@ void testJournal1(void)
} }
j->ViewRows(0,20); j->ViewRows(0,20);
printf("\nthe same after reopen:\n"); printf("\nthe same after reopen:\n");
delete j; j = new CycleStorage("journal.test",30,2000000,0);
j = new CycleStorage("journal.test",2000000,0);
j->ViewRows(0,20); j->ViewRows(0,20);
printf("\n"); printf("\n");
} }
...@@ -118,20 +125,19 @@ void testJournal2(void) ...@@ -118,20 +125,19 @@ void testJournal2(void)
{ {
CycleStorage *j; CycleStorage *j;
int i,k; int i,k;
char *str=new char[4]; char *str = (char*)malloc(30);
j = new CycleStorage("journal.test",2000000,0); j = new CycleStorage("journal.test",30,1000000,0);
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("size = %d\n\n",j->size); printf("size = %d\n\n",j->size);
printf("iterations = %d\n",j->iter); printf("iterations = %d\n",j->iter);
for(i=0;i<20;i++) for(i=0;i<20;i++)
{ {
for(k=0;k<1000;k++) for(k=0;k<2000;k++)
{ {
str = itoa(k,10); str = itoa(k,10);
j->AddRow(str); j->AddRow(str);
} }
delete j; j = new CycleStorage("journal.test",30,2000000,0);
j = new CycleStorage("journal.test",2000000,0);
printf("iterations = %d\n",j->iter); printf("iterations = %d\n",j->iter);
} }
printf("\n"); printf("\n");
...@@ -143,7 +149,6 @@ int main(void) ...@@ -143,7 +149,6 @@ int main(void)
testTable(); testTable();
testJournal1(); testJournal1();
testJournal2(); testJournal2();
return 0; return 0;
......
...@@ -19,8 +19,8 @@ ...@@ -19,8 +19,8 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/*! \file /*! \file
* \author Ivan Donchevskiy * \author Ivan Donchevskiy
* \date $Date: 2009/07/14 16:59:00 $ * \date $Date: 2009/07/15 15:55:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/14 16:59:00 vpashka Exp $ * \version $Id: Jrn.h,v 1.0 2009/07/15 15:55:00 vpashka Exp $
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -30,30 +30,26 @@ ...@@ -30,30 +30,26 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define str_size 80
#define key_size 20 #define key_size 20
#define val_size 40
struct TableStorageElem struct TableStorageElem
{ {
char key[key_size]; char key[key_size];
char value[val_size];
} __attribute__((__packed__)); } __attribute__((__packed__));
struct CycleStorageElem struct CycleStorageElem
{ {
char str[str_size]; char status;
int status;
} __attribute__((__packed__)); } __attribute__((__packed__));
class TableStorage class TableStorage
{ {
FILE *file; FILE *file;
int seekpos; int seekpos, inf_size;
public: public:
int size; int size;
TableStorage(); TableStorage();
TableStorage(const char* name, int sz, int seek); TableStorage(const char* name, int inf_sz, int sz, int seek);
~TableStorage(); ~TableStorage();
int AddRow(char* key, char* val); int AddRow(char* key, char* val);
int DelRow(char* key); int DelRow(char* key);
...@@ -63,12 +59,12 @@ class TableStorage ...@@ -63,12 +59,12 @@ class TableStorage
class CycleStorage class CycleStorage
{ {
FILE *file; FILE *file;
int seekpos; int seekpos, inf_size;
int head,tail; int head,tail;
public: public:
int size, iter; int size, iter;
CycleStorage(); CycleStorage();
CycleStorage(const char* name, int sz, int seek); CycleStorage(const char* name, int inf_sz, int sz, int seek);
~CycleStorage(); ~CycleStorage();
int AddRow(char* str); int AddRow(char* str);
int DelRow(int row); int DelRow(int row);
......
...@@ -19,12 +19,13 @@ ...@@ -19,12 +19,13 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/*! \file /*! \file
* \author Ivan Donchevskiy * \author Ivan Donchevskiy
* \date $Date: 2009/07/14 16:59:00 $ * \date $Date: 2009/07/15 15:55:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/14 16:59:00 vpashka Exp $ * \version $Id: Jrn.h,v 1.0 2009/07/15 15:55:00vpashka Exp $
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "Storages.h" #include "Storages.h"
...@@ -32,27 +33,29 @@ ...@@ -32,27 +33,29 @@
TableStorage::TableStorage() TableStorage::TableStorage()
{ {
file = fopen("tbl", "r+"); file = fopen("tbl", "r+");
inf_size=60;
size=100;
seekpos=0;
if(file==NULL) if(file==NULL)
{ {
file = fopen("tbl","w"); file = fopen("tbl","w");
TableStorageElem *t = new TableStorageElem(); TableStorageElem *t = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
for(int i=0;i<100;i++) fwrite(t,sizeof(*t),1,file); for(int i=0;i<size;i++) fwrite(t,(sizeof(TableStorageElem)+inf_size),1,file);
fclose(file); fclose(file);
file = fopen("tbl","r+"); file = fopen("tbl","r+");
} }
size=100;
seekpos=0;
} }
TableStorage::TableStorage(const char* name, int sz, int seek) TableStorage::TableStorage(const char* name, int inf_sz, int sz, int seek)
{ {
file = fopen(name, "r+"); file = fopen(name, "r+");
size=sz/sizeof(TableStorageElem); inf_size=inf_sz;
size=sz/(sizeof(TableStorageElem)+inf_size);
if(file==NULL) if(file==NULL)
{ {
file = fopen(name,"w"); file = fopen(name,"w");
TableStorageElem *t = new TableStorageElem(); TableStorageElem *t = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
for(int i=0;i<size;i++) fwrite(t,sizeof(*t),1,file); for(int i=0;i<size;i++) fwrite(t,(sizeof(TableStorageElem)+inf_size),1,file);
fclose(file); fclose(file);
file = fopen(name,"r+"); file = fopen(name,"r+");
seekpos=0; seekpos=0;
...@@ -67,32 +70,34 @@ TableStorage::~TableStorage() ...@@ -67,32 +70,34 @@ TableStorage::~TableStorage()
int TableStorage::AddRow(char* key, char* value) int TableStorage::AddRow(char* key, char* value)
{ {
TableStorageElem *tbl = new TableStorageElem(); TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
int i; int i,k;
if(file!=NULL) if(file!=NULL)
{ {
fseek(file,seekpos,0); fseek(file,seekpos,0);
for(i=0;i<size;i++) for(i=0;i<size;i++)
{ {
fread(tbl,sizeof(*tbl),1,file); fread(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
if(!strcmp(tbl->key,key)) if(!strcmp(tbl->key,key))
{ {
strcpy(tbl->value,value); for(k=0;k<inf_size;k++)
fseek(file,seekpos+i*sizeof(*tbl),0); *((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k);
fwrite(tbl,sizeof(*tbl),1,file); fseek(file,seekpos+i*(sizeof(TableStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
return 0; return 0;
} }
} }
fseek(file,seekpos,0); fseek(file,seekpos,0);
for(i=0;i<size;i++) for(i=0;i<size;i++)
{ {
fread(tbl,sizeof(*tbl),1,file); fread(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
if(*(tbl->key)==0) if(tbl->key[0]==0)
{ {
strcpy(tbl->key,key); strcpy(tbl->key,key);
strcpy(tbl->value,value); for(k=0;k<inf_size;k++)
fseek(file,seekpos+i*sizeof(*tbl),0); *((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k);
fwrite(tbl,sizeof(*tbl),1,file); fseek(file,seekpos+i*(sizeof(TableStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
return 0; return 0;
} }
} }
...@@ -102,19 +107,19 @@ int TableStorage::AddRow(char* key, char* value) ...@@ -102,19 +107,19 @@ int TableStorage::AddRow(char* key, char* value)
int TableStorage::DelRow(char* key) int TableStorage::DelRow(char* key)
{ {
TableStorageElem *tbl = new TableStorageElem(); TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
int i; int i;
if(file!=NULL) if(file!=NULL)
{ {
fseek(file,seekpos,0); fseek(file,seekpos,0);
for(i=0;i<size;i++) for(i=0;i<size;i++)
{ {
fread(tbl,(key_size+val_size),1,file); fread(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
if(!strcmp(tbl->key,key)) if(!strcmp(tbl->key,key))
{ {
tbl->key[0]=0; tbl->key[0]=0;
fseek(file,seekpos+i*(key_size+val_size),0); fseek(file,seekpos+i*(sizeof(TableStorageElem)+inf_size),0);
fwrite(tbl,(key_size+val_size),1,file); fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
return 0; return 0;
} }
} }
...@@ -124,17 +129,18 @@ int TableStorage::DelRow(char* key) ...@@ -124,17 +129,18 @@ int TableStorage::DelRow(char* key)
char* TableStorage::FindKeyValue(char* key, char* val) char* TableStorage::FindKeyValue(char* key, char* val)
{ {
TableStorageElem *tbl = new TableStorageElem(); TableStorageElem *tbl = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
int i; int i,k;
if(file!=NULL) if(file!=NULL)
{ {
fseek(file,seekpos,0); fseek(file,seekpos,0);
for(i=0;i<size;i++) for(i=0;i<size;i++)
{ {
fread(tbl,(key_size+val_size),1,file); fread(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
if(!strcmp(tbl->key,key)) if(!strcmp(tbl->key,key))
{ {
strcpy(val,tbl->value); for(k=0;k<inf_size;k++)
*(val+k)=*((char*)(tbl)+sizeof(TableStorageElem)+k);
return val; return val;
} }
} }
......
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