Commit 6dfe533b authored by Vitaly Lipatov's avatar Vitaly Lipatov

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

parents 028d4e56 72839840
/* 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,14 +19,17 @@ ...@@ -19,14 +19,17 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/*! \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"
#include "UniXML.h"
char* itoa(int val, int base) char* itoa(int val, int base)
{ {
...@@ -46,35 +49,45 @@ char* itoa(int val, int base) ...@@ -46,35 +49,45 @@ 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, 1220, 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;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);
t->AddRow(chr,val);
} }
printf("elements with values=keys added\n"); //printf("elements with values=keys added\nvalues from keys 25-59\n");
for(i=40;i<60;i++) printf("elements with values=keys added:\n");
for(i=0;i<40;i++)
{ {
chr[0]=i%256; chr[0]=i;
if(t->FindKeyValue(chr,val)!=0) printf("%s, ",val);
}
printf("\n");
for(i=9;i<15;i++)
{
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++) printf("elements with keys from 9 to 14 deleted\n");
for(i=9;i<15;i++)
{ {
chr[0]=i; chr[0]=i;
val[0]=i+40; val=itoa(i+40,10);
t->AddRow(chr,val); 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 30 to 50 with values=key+40 added\nvalues from keys 25-59\n");
for(i=25;i<60;i++) 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; 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 +96,11 @@ void testJournal1(void) ...@@ -83,11 +96,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,42 +121,102 @@ void testJournal1(void) ...@@ -108,42 +121,102 @@ 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");
j->ExportToXML("Xml.xml");
} }
void testJournal2(void) 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=1000;k<3000;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");
} }
int main(void) int main(int args, char **argv)
{ {
/*if(args<2)
{
printf("Correct usage: jrntest File_name\n");
return 0;
}
CycleStorage *j = new CycleStorage(argv[1],99,1000,0);
printf("commands:\nadd\ndelete\nview\ntoxml\ndel_all\nenter q or quit to exit\n\n");
char* com=new char[8];
char* str=new char[99];
int num,count;
strcpy(com,"qwerty");
while(strcmp(com,"q")&&strcmp(com,"quit"))
{
scanf("%s",com);
if(!strcmp(com,"add"))
{
printf("string to add: ");
if(scanf("%99s",str)>0)
{
j->AddRow(str);
printf("\n");
}
}
else if(!strcmp(com,"delete"))
{
printf("number of string to delete: ");
if(scanf("%d",&num)>0)
{
j->DelRow(num);
printf("\n");
}
}
else if(!strcmp(com,"view"))
{
printf("start number and count (0 0 for all): ");
if(scanf("%d%d",&num,&count)>1)
{
j->ViewRows(num,count);
printf("\n");
}
}
else if(!strcmp(com,"del_all"))
{
printf("are you sure? (y/n) ");
if(scanf("%s",str)>0)
if(!strcmp(str,"y"))
{
j->DelAllRows();
printf("\n");
}
}
else if(!strcmp(com,"toxml"))
{
printf("enter file name: ");
if(scanf("%25s",str)>0)
{
j->ExportToXML(str);
printf("\n");
}
}
else printf("commands:\nadd\ndelete\nview\ntoxml\ndel_all\nenter q or quit to exit\n\n");
}*/
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 $
*/ */
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
...@@ -29,31 +29,29 @@ ...@@ -29,31 +29,29 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <UniXML.h>
#define str_size 80
#define key_size 20 #define key_size 20
#define val_size 40
struct TableStorageElem struct TableStorageElem
{ {
char status;
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;
int head;
public: public:
int size; int size;
TableStorage(); TableStorage(const char* name, int inf_sz, int sz, int seek);
TableStorage(const char* name, 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,17 +61,17 @@ class TableStorage ...@@ -63,17 +61,17 @@ 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(const char* name, int inf_sz, int sz, int seek);
CycleStorage(const char* name, int sz, int seek);
~CycleStorage(); ~CycleStorage();
int AddRow(char* str); int AddRow(char* str);
int DelRow(int row); int DelRow(int row);
int DelAllRows(void); int DelAllRows(void);
int ViewRows(int beg, int num); int ViewRows(int beg, int num);
int ExportToXML(const char* name);
}; };
#endif #endif
...@@ -75,9 +75,10 @@ public: ...@@ -75,9 +75,10 @@ public:
// , . // , .
static const xmlChar* local2xml(std::string text); static const xmlChar* local2xml(std::string text);
// name node // XML-
void newDoc(const std::string& root_node, std::string xml_ver="1.0"); void newDoc(const std::string& root_node, std::string xml_ver="1.0");
// name node
static std::string getProp(xmlNode* node, const std::string name); static std::string getProp(xmlNode* node, const std::string name);
// name node // name node
......
...@@ -19,45 +19,90 @@ ...@@ -19,45 +19,90 @@
// -------------------------------------------------------------------------- // --------------------------------------------------------------------------
/*! \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"
TableStorage::TableStorage()
{
file = fopen("tbl", "r+");
if(file==NULL)
{
file = fopen("tbl","w");
TableStorageElem *t = new TableStorageElem();
for(int i=0;i<100;i++) fwrite(t,sizeof(*t),1,file);
fclose(file);
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;
int l=-1,r=size,mid;
size=sz/(sizeof(TableStorageElem)+inf_size);
TableStorageElem *t = (TableStorageElem*)malloc(sizeof(TableStorageElem)+inf_size);
if(file==NULL) if(file==NULL)
{ {
file = fopen(name,"w"); file = fopen(name,"w");
TableStorageElem *t = new TableStorageElem(); for(int i=0;i<size;i++) fwrite(t,(sizeof(TableStorageElem)+inf_size),1,file);
for(int i=0;i<size;i++) fwrite(t,sizeof(*t),1,file);
fclose(file); fclose(file);
file = fopen(name,"r+"); file = fopen(name,"r+");
seekpos=0; seekpos=0;
head=-1;
}
else
{
seekpos=seek;
fseek(file,seekpos,0);
fread(t,(sizeof(TableStorageElem)+inf_size),1,file);
if(t->status==0)
{
head=-1;
}
else if((t->status==1)||(t->status==6))
{
head=0;
}
else if((t->status==2)||(t->status==3))
{
while((t->status!=1)&&(t->status!=6)&&(r - l > 1))
{
mid = (l+r)/2;
fseek(file,seekpos+mid*(sizeof(TableStorageElem)+inf_size),0);
fread(t,(sizeof(TableStorageElem)+inf_size),1,file);
if((t->status==2)||(t->status==3))
l = mid;
else if((t->status==4)||(t->status==5))
r = mid;
else
{
r=mid;
break;
}
}
if(r<size)
head=r;
else head=size-1;
}
else
{
while((t->status!=1)&&(t->status!=6)&&(r - l > 1))
{
mid = (l+r)/2;
fseek(file,seekpos+mid*(sizeof(TableStorageElem)+inf_size),0);
fread(t,(sizeof(TableStorageElem)+inf_size),1,file);
if((t->status==2)||(t->status==3))
r = mid;
else if((t->status==4)||(t->status==5))
l = mid;
else
{
r=mid;
break;
}
}
if(r<size)
head=r;
else head=size-1;
}
} }
else seekpos=seek;
} }
TableStorage::~TableStorage() TableStorage::~TableStorage()
...@@ -67,56 +112,118 @@ TableStorage::~TableStorage() ...@@ -67,56 +112,118 @@ 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,j,st;
if(file!=NULL) if(file!=NULL)
{ {
fseek(file,seekpos,0); if(head==-1)
{
fseek(file,seekpos,0);
tbl->status=1;
strcpy(tbl->key,key);
for(k=0;k<inf_size;k++)
*((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
head=0;
return 0;
}
fseek(file,seekpos+head*(sizeof(TableStorageElem)+inf_size),0);
j=head;
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(tbl->status==0) break;
{ if(!strcmp(tbl->key,key)&&((tbl->status==2)||(tbl->status==4)||(tbl->status==1)))
strcpy(tbl->value,value); {
fseek(file,seekpos+i*sizeof(*tbl),0); for(k=0;k<inf_size;k++)
fwrite(tbl,sizeof(*tbl),1,file); *((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k);
fseek(file,seekpos+i*(sizeof(TableStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
return 0; return 0;
} }
j++;
if(j>=size)
{
j=0;
fseek(file,seekpos,0);
}
} }
fseek(file,seekpos,0); fseek(file,seekpos+j*(sizeof(TableStorageElem)+inf_size),0);
for(i=0;i<size;i++) if(j==head)
{
if((tbl->status==2)||(tbl->status==3)) st=2;
else st=4;
if(j==0)
if(st==2) st=4;
else st=2;
tbl->status=st;
strcpy(tbl->key,key);
for(k=0;k<inf_size;k++)
*((char*)(tbl)+sizeof(TableStorageElem)+k)=*(value+k);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
j++;
if(j>=size)
{
j=0;
fseek(file,seekpos,0);
}
fread(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
if((tbl->status==3)||(tbl->status==5)) tbl->status=6;
else tbl->status=1;
fseek(file,seekpos+j*(sizeof(TableStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
head++;
if(head>=size) head=0;
return 0;
}
/*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;
} }
} }*/
} }
return 1; return 1;
} }
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,j;
if(file!=NULL) if(file!=NULL)
{ {
fseek(file,seekpos,0); fseek(file,seekpos+head*(sizeof(TableStorageElem)+inf_size),0);
j=head;
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->status==2)||(tbl->status==4)||(tbl->status==1)))
{ {
tbl->key[0]=0; //tbl->key[0]=0;
fseek(file,seekpos+i*(key_size+val_size),0); if(tbl->status==1) tbl->status=6;
fwrite(tbl,(key_size+val_size),1,file); else if(tbl->status==2) tbl->status=3;
else tbl->status=5;
fseek(file,seekpos+j*(sizeof(TableStorageElem)+inf_size),0);
fwrite(tbl,(sizeof(TableStorageElem)+inf_size),1,file);
return 0; return 0;
} }
j++;
if(j>=size)
{
j=0;
fseek(file,seekpos,0);
}
} }
} }
return 1; return 1;
...@@ -124,17 +231,18 @@ int TableStorage::DelRow(char* key) ...@@ -124,17 +231,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)&&((tbl->status==2)||(tbl->status==4)||(tbl->status==1)))
{ {
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