Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
U
uniset2
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
1
Issues
1
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
UniSet project repositories
uniset2
Commits
2619eb7e
Commit
2619eb7e
authored
Jul 15, 2009
by
Ivan Donchevskiy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make StorageTable and CycleStorage work with any types, not only strings
parent
c57ad32b
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
73 additions
and
66 deletions
+73
-66
JrnTest.cc
Utilities/JrnTests/JrnTest.cc
+30
-25
Storages.h
include/Storages.h
+7
-11
CycleStorage.cc
src/Various/CycleStorage.cc
+0
-0
TableStorage.cc
src/Various/TableStorage.cc
+36
-30
No files found.
Utilities/JrnTests/JrnTest.cc
View file @
2619eb7e
/* This file is part of the UniSet project
* Copyright (c) 200
2
Free Software Foundation, Inc.
* Copyright (c) 200
2
Ivan Donchevskiy
* Copyright (c) 200
9
Free Software Foundation, Inc.
* Copyright (c) 200
9
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
...
...
@@ -19,12 +19,13 @@
// --------------------------------------------------------------------------
/*! \file
* \author Ivan Donchevskiy
* \date $Date: 2009/07/1
4 16:59
:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/1
4 16:59
:00 vpashka Exp $
* \date $Date: 2009/07/1
5 15:55
:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/1
5 15:55
:00 vpashka Exp $
*/
// --------------------------------------------------------------------------
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "Storages.h"
...
...
@@ -46,35 +47,42 @@ char* itoa(int val, int base)
void
testTable
(
void
)
{
char
*
chr
=
new
char
[
2
]
;
char
*
val
=
new
char
[
2
]
;
char
*
chr
=
(
char
*
)
malloc
(
20
)
;
char
*
val
=
(
char
*
)
malloc
(
40
)
;
TableStorage
*
t
;
t
=
new
TableStorage
(
"table.test"
,
6000
,
0
);
t
=
new
TableStorage
(
"table.test"
,
40
,
6000
,
0
);
int
i
;
printf
(
"testTable
\n
size = %d
\n
"
,
t
->
size
);
for
(
i
=
0
;
i
<
t
->
size
+
5
;
i
++
)
{
chr
[
0
]
=
i
%
256
;
if
(
t
->
AddRow
(
chr
,
chr
)
==
1
)
printf
(
"elem number %d - no space in TableStorage
\n
"
,
i
);
chr
[
0
]
=
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
\n
values 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
++
)
{
chr
[
0
]
=
i
%
256
;
chr
[
0
]
=
i
;
t
->
DelRow
(
chr
);
}
printf
(
"elements with keys from 40 to 60 deleted
\n
"
);
for
(
i
=
30
;
i
<
50
;
i
++
)
{
chr
[
0
]
=
i
;
val
[
0
]
=
i
+
40
;
t
->
AddRow
(
chr
,
val
);
val
=
itoa
(
i
+
40
,
10
)
;
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
\n
values from keys 25-59
\n
"
);
for
(
i
=
25
;
i
<
60
;
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
"
);
}
...
...
@@ -83,11 +91,11 @@ void testJournal1(void)
{
CycleStorage
*
j
;
int
i
;
char
*
str
=
new
char
[
6
]
;
char
*
str
=
(
char
*
)
malloc
(
30
)
;
printf
(
"journal test 1
\n
"
);
j
=
new
CycleStorage
(
"journal.test"
,
2
000000
,
0
);
j
=
new
CycleStorage
(
"journal.test"
,
30
,
1
000000
,
0
);
printf
(
"size = %d
\n
"
,
j
->
size
);
for
(
i
=
1
;
i
<
3
0000
;
i
++
)
for
(
i
=
1
;
i
<
4
0000
;
i
++
)
{
str
=
itoa
(
i
,
10
);
j
->
AddRow
(
str
);
...
...
@@ -108,8 +116,7 @@ void testJournal1(void)
}
j
->
ViewRows
(
0
,
20
);
printf
(
"
\n
the same after reopen:
\n
"
);
delete
j
;
j
=
new
CycleStorage
(
"journal.test"
,
2000000
,
0
);
j
=
new
CycleStorage
(
"journal.test"
,
30
,
2000000
,
0
);
j
->
ViewRows
(
0
,
20
);
printf
(
"
\n
"
);
}
...
...
@@ -118,20 +125,19 @@ void testJournal2(void)
{
CycleStorage
*
j
;
int
i
,
k
;
char
*
str
=
new
char
[
4
]
;
j
=
new
CycleStorage
(
"journal.test"
,
2
000000
,
0
);
char
*
str
=
(
char
*
)
malloc
(
30
)
;
j
=
new
CycleStorage
(
"journal.test"
,
30
,
1
000000
,
0
);
printf
(
"journal test 2 - checking number of iterations to find head/tail
\n
"
);
printf
(
"size = %d
\n\n
"
,
j
->
size
);
printf
(
"iterations = %d
\n
"
,
j
->
iter
);
for
(
i
=
0
;
i
<
20
;
i
++
)
{
for
(
k
=
0
;
k
<
1
000
;
k
++
)
for
(
k
=
0
;
k
<
2
000
;
k
++
)
{
str
=
itoa
(
k
,
10
);
j
->
AddRow
(
str
);
}
delete
j
;
j
=
new
CycleStorage
(
"journal.test"
,
2000000
,
0
);
j
=
new
CycleStorage
(
"journal.test"
,
30
,
2000000
,
0
);
printf
(
"iterations = %d
\n
"
,
j
->
iter
);
}
printf
(
"
\n
"
);
...
...
@@ -143,7 +149,6 @@ int main(void)
testTable
();
testJournal1
();
testJournal2
();
return
0
;
...
...
include/Storages.h
View file @
2619eb7e
...
...
@@ -19,8 +19,8 @@
// --------------------------------------------------------------------------
/*! \file
* \author Ivan Donchevskiy
* \date $Date: 2009/07/1
4 16:59
:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/1
4 16:59
:00 vpashka Exp $
* \date $Date: 2009/07/1
5 15:55
:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/1
5 15:55
:00 vpashka Exp $
*/
// --------------------------------------------------------------------------
...
...
@@ -30,30 +30,26 @@
#include <stdio.h>
#include <string.h>
#define str_size 80
#define key_size 20
#define val_size 40
struct
TableStorageElem
{
char
key
[
key_size
];
char
value
[
val_size
];
}
__attribute__
((
__packed__
));
struct
CycleStorageElem
{
char
str
[
str_size
];
int
status
;
char
status
;
}
__attribute__
((
__packed__
));
class
TableStorage
{
FILE
*
file
;
int
seekpos
;
int
seekpos
,
inf_size
;
public
:
int
size
;
TableStorage
();
TableStorage
(
const
char
*
name
,
int
sz
,
int
seek
);
TableStorage
(
const
char
*
name
,
int
inf_sz
,
int
sz
,
int
seek
);
~
TableStorage
();
int
AddRow
(
char
*
key
,
char
*
val
);
int
DelRow
(
char
*
key
);
...
...
@@ -63,12 +59,12 @@ class TableStorage
class
CycleStorage
{
FILE
*
file
;
int
seekpos
;
int
seekpos
,
inf_size
;
int
head
,
tail
;
public
:
int
size
,
iter
;
CycleStorage
();
CycleStorage
(
const
char
*
name
,
int
sz
,
int
seek
);
CycleStorage
(
const
char
*
name
,
int
inf_sz
,
int
sz
,
int
seek
);
~
CycleStorage
();
int
AddRow
(
char
*
str
);
int
DelRow
(
int
row
);
...
...
src/Various/CycleStorage.cc
View file @
2619eb7e
This diff is collapsed.
Click to expand it.
src/Various/TableStorage.cc
View file @
2619eb7e
...
...
@@ -19,12 +19,13 @@
// --------------------------------------------------------------------------
/*! \file
* \author Ivan Donchevskiy
* \date $Date: 2009/07/1
4 16:59
:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/1
4 16:59:00
vpashka Exp $
* \date $Date: 2009/07/1
5 15:55
:00 $
* \version $Id: Jrn.h,v 1.0 2009/07/1
5 15:55:00
vpashka Exp $
*/
// --------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Storages.h"
...
...
@@ -32,27 +33,29 @@
TableStorage
::
TableStorage
()
{
file
=
fopen
(
"tbl"
,
"r+"
);
inf_size
=
60
;
size
=
100
;
seekpos
=
0
;
if
(
file
==
NULL
)
{
file
=
fopen
(
"tbl"
,
"w"
);
TableStorageElem
*
t
=
new
TableStorageElem
(
);
for
(
int
i
=
0
;
i
<
100
;
i
++
)
fwrite
(
t
,
sizeof
(
*
t
),
1
,
file
);
TableStorageElem
*
t
=
(
TableStorageElem
*
)
malloc
(
sizeof
(
TableStorageElem
)
+
inf_size
);
for
(
int
i
=
0
;
i
<
size
;
i
++
)
fwrite
(
t
,(
sizeof
(
TableStorageElem
)
+
inf_size
),
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+"
);
size
=
sz
/
sizeof
(
TableStorageElem
);
inf_size
=
inf_sz
;
size
=
sz
/
(
sizeof
(
TableStorageElem
)
+
inf_size
);
if
(
file
==
NULL
)
{
file
=
fopen
(
name
,
"w"
);
TableStorageElem
*
t
=
new
TableStorageElem
(
);
for
(
int
i
=
0
;
i
<
size
;
i
++
)
fwrite
(
t
,
sizeof
(
*
t
),
1
,
file
);
TableStorageElem
*
t
=
(
TableStorageElem
*
)
malloc
(
sizeof
(
TableStorageElem
)
+
inf_size
);
for
(
int
i
=
0
;
i
<
size
;
i
++
)
fwrite
(
t
,
(
sizeof
(
TableStorageElem
)
+
inf_size
),
1
,
file
);
fclose
(
file
);
file
=
fopen
(
name
,
"r+"
);
seekpos
=
0
;
...
...
@@ -67,32 +70,34 @@ TableStorage::~TableStorage()
int
TableStorage
::
AddRow
(
char
*
key
,
char
*
value
)
{
TableStorageElem
*
tbl
=
new
TableStorageElem
(
);
int
i
;
TableStorageElem
*
tbl
=
(
TableStorageElem
*
)
malloc
(
sizeof
(
TableStorageElem
)
+
inf_size
);
int
i
,
k
;
if
(
file
!=
NULL
)
{
fseek
(
file
,
seekpos
,
0
);
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
))
{
strcpy
(
tbl
->
value
,
value
);
fseek
(
file
,
seekpos
+
i
*
sizeof
(
*
tbl
),
0
);
fwrite
(
tbl
,
sizeof
(
*
tbl
),
1
,
file
);
{
for
(
k
=
0
;
k
<
inf_size
;
k
++
)
*
((
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
;
}
}
fseek
(
file
,
seekpos
,
0
);
for
(
i
=
0
;
i
<
size
;
i
++
)
{
fread
(
tbl
,
sizeof
(
*
tbl
),
1
,
file
);
if
(
*
(
tbl
->
key
)
==
0
)
fread
(
tbl
,
(
sizeof
(
TableStorageElem
)
+
inf_size
),
1
,
file
);
if
(
tbl
->
key
[
0
]
==
0
)
{
strcpy
(
tbl
->
key
,
key
);
strcpy
(
tbl
->
value
,
value
);
fseek
(
file
,
seekpos
+
i
*
sizeof
(
*
tbl
),
0
);
fwrite
(
tbl
,
sizeof
(
*
tbl
),
1
,
file
);
for
(
k
=
0
;
k
<
inf_size
;
k
++
)
*
((
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
;
}
}
...
...
@@ -102,19 +107,19 @@ int TableStorage::AddRow(char* key, char* value)
int
TableStorage
::
DelRow
(
char
*
key
)
{
TableStorageElem
*
tbl
=
new
TableStorageElem
(
);
TableStorageElem
*
tbl
=
(
TableStorageElem
*
)
malloc
(
sizeof
(
TableStorageElem
)
+
inf_size
);
int
i
;
if
(
file
!=
NULL
)
{
fseek
(
file
,
seekpos
,
0
);
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
))
{
tbl
->
key
[
0
]
=
0
;
fseek
(
file
,
seekpos
+
i
*
(
key_size
+
val
_size
),
0
);
fwrite
(
tbl
,(
key_size
+
val
_size
),
1
,
file
);
fseek
(
file
,
seekpos
+
i
*
(
sizeof
(
TableStorageElem
)
+
inf
_size
),
0
);
fwrite
(
tbl
,(
sizeof
(
TableStorageElem
)
+
inf
_size
),
1
,
file
);
return
0
;
}
}
...
...
@@ -124,17 +129,18 @@ int TableStorage::DelRow(char* key)
char
*
TableStorage
::
FindKeyValue
(
char
*
key
,
char
*
val
)
{
TableStorageElem
*
tbl
=
new
TableStorageElem
(
);
int
i
;
TableStorageElem
*
tbl
=
(
TableStorageElem
*
)
malloc
(
sizeof
(
TableStorageElem
)
+
inf_size
);
int
i
,
k
;
if
(
file
!=
NULL
)
{
fseek
(
file
,
seekpos
,
0
);
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
))
{
strcpy
(
val
,
tbl
->
value
);
for
(
k
=
0
;
k
<
inf_size
;
k
++
)
*
(
val
+
k
)
=*
((
char
*
)(
tbl
)
+
sizeof
(
TableStorageElem
)
+
k
);
return
val
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment