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
94204758
Commit
94204758
authored
Jul 30, 2009
by
Ivan Donchevskiy
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
code changes in CycleStorage and TableBlockStorage
parent
c0997d17
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
129 additions
and
102 deletions
+129
-102
JrnTest.cc
Utilities/JrnTests/JrnTest.cc
+4
-5
Storages.h
include/Storages.h
+7
-3
CycleStorage.cc
src/Various/CycleStorage.cc
+55
-39
TableBlockStorage.cc
src/Various/TableBlockStorage.cc
+63
-55
No files found.
Utilities/JrnTests/JrnTest.cc
View file @
94204758
...
...
@@ -153,7 +153,7 @@ void testJournal1(void)
printf
(
"
\n
first 30 elements:
\n
"
);
for
(
i
=
0
;
i
<
30
;
i
++
)
{
if
(
j
->
View
Row
(
i
,
str
))
if
(
j
->
Read
Row
(
i
,
str
))
printf
(
"%s
\n
"
,
str
);
}
...
...
@@ -173,7 +173,7 @@ void testJournal1(void)
}
for
(
i
=
0
;
i
<
30
;
i
++
)
{
if
(
j
->
View
Row
(
i
,
str
))
if
(
j
->
Read
Row
(
i
,
str
))
printf
(
"%s
\n
"
,
str
);
}
printf
(
"
\n
first 20 after adding 10 elements
\n
"
);
...
...
@@ -184,7 +184,7 @@ void testJournal1(void)
}
for
(
i
=
0
;
i
<
20
;
i
++
)
{
if
(
j
->
View
Row
(
i
,
str
))
if
(
j
->
Read
Row
(
i
,
str
))
printf
(
"%s
\n
"
,
str
);
}
printf
(
"
\n
the same after reopen:
\n
"
);
...
...
@@ -193,11 +193,10 @@ void testJournal1(void)
j
->
Open
(
"big_file.test"
,
30
,
1000000
,
20000
);
for
(
i
=
0
;
i
<
20
;
i
++
)
{
if
(
j
->
View
Row
(
i
,
str
))
if
(
j
->
Read
Row
(
i
,
str
))
printf
(
"%s
\n
"
,
str
);
}
printf
(
"
\n
"
);
j
->
ExportToXML
(
"Xml.xml"
);
delete
t
;
delete
j
;
}
...
...
include/Storages.h
View file @
94204758
...
...
@@ -32,7 +32,10 @@
#include <string.h>
#include <string>
#include "UniXML.h"
/*! 2 TableBlockStorage count,
TableBlockStorageElem */
#define EMPTY_BLOCK -5
#define EMPTY_ELEM -1
#define key_size 20
...
...
@@ -153,10 +156,10 @@ class CycleStorage
bool
DelAllRows
(
void
);
/*! num */
void
*
View
Row
(
int
num
,
void
*
str
);
void
*
Read
Row
(
int
num
,
void
*
str
);
/*! Xml- name */
bool
ExportToXML
(
const
char
*
name
);
//
bool ExportToXML(const char* name);
/*! - / ( ) */
int
GetIter
(
void
);
...
...
@@ -169,6 +172,7 @@ class CycleStorage
int
full_size
;
void
filewrite
(
CycleStorageElem
*
jrn
,
int
seek
,
bool
needflush
=
true
);
void
*
ValPointer
(
void
*
pnt
);
void
FindHead
();
};
#endif
src/Various/CycleStorage.cc
View file @
94204758
...
...
@@ -64,44 +64,17 @@ void CycleStorage::filewrite(CycleStorageElem* jrn,int seek,bool needflush)
if
(
needflush
)
fflush
(
file
);
}
bool
CycleStorage
::
Open
(
const
char
*
name
,
int
inf_sz
,
int
sz
,
int
seek
)
/*! jrn->status: 0 - , 1 - , 2,4 -
( 2 - 4 , ), 3 - 2, 5 - 4, 6 - 1.
|
*/
void
CycleStorage
::
FindHead
()
{
/*! ,
*/
if
(
file
!=
NULL
)
fclose
(
file
);
file
=
fopen
(
name
,
"r+"
);
if
(
file
==
NULL
)
return
false
;
seekpos
=
seek
;
if
(
fseek
(
file
,
seekpos
,
0
)
==-
1
)
return
false
;
CycleStorageAttr
*
csa
=
new
CycleStorageAttr
();
fread
(
csa
,
sizeof
(
CycleStorageAttr
),
1
,
file
);
if
((
csa
->
size
!=
((
sz
-
sizeof
(
CycleStorageAttr
))
/
(
sizeof
(
CycleStorageElem
)
+
inf_sz
)))
||
(
csa
->
inf_size
!=
inf_sz
)
||
(
csa
->
seekpos
!=
seek
))
{
delete
csa
;
return
false
;
}
delete
csa
;
inf_size
=
inf_sz
;
full_size
=
sizeof
(
CycleStorageElem
)
+
inf_size
;
size
=
(
sz
-
sizeof
(
CycleStorageAttr
))
/
full_size
;
seekpos
=
seek
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[
full_size
];
int
l
=-
1
,
r
=
size
,
mid
;
iter
=
0
;
seekpos
+=
sizeof
(
CycleStorageAttr
);
fread
(
jrn
,
full_size
,
1
,
file
);
/*! jrn->status: 0 - , 1 - , 2,4 -
( 2 - 4 , ), 3 - 2, 5 - 4, 6 - 1.
|
*/
if
(
jrn
->
status
==
0
)
{
head
=-
1
;
...
...
@@ -109,7 +82,9 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
}
else
if
((
jrn
->
status
==
1
)
||
(
jrn
->
status
==
6
))
{
/*! - */
head
=
0
;
/*! , */
while
(
r
-
l
>
1
)
{
mid
=
(
l
+
r
)
/
2
;
...
...
@@ -129,6 +104,8 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
else
{
int
i
,
j
;
/*! i 2-, 4-, 2-, 4-
*/
i
=
jrn
->
status
-
jrn
->
status
%
2
;
if
(
i
==
2
)
j
=
4
;
else
j
=
2
;
while
((
jrn
->
status
!=
1
)
&&
(
jrn
->
status
!=
6
)
&&
(
r
-
l
>
1
))
...
...
@@ -141,7 +118,7 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
l
=
mid
;
else
if
((
jrn
->
status
==
j
)
||
(
jrn
->
status
==
j
+
1
))
r
=
mid
;
else
else
{
r
=
mid
;
break
;
...
...
@@ -150,10 +127,46 @@ bool CycleStorage::Open(const char* name, int inf_sz, int sz, int seek)
if
(
r
<
size
)
head
=
r
;
else
head
=
size
-
1
;
/*! 1 , .. , */
tail
=
head
-
1
;
if
(
tail
<
0
)
tail
=
size
-
1
;
}
delete
jrn
;
}
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
;
seekpos
=
seek
;
if
(
fseek
(
file
,
seekpos
,
0
)
==-
1
)
return
false
;
/*! */
CycleStorageAttr
*
csa
=
new
CycleStorageAttr
();
fread
(
csa
,
sizeof
(
CycleStorageAttr
),
1
,
file
);
/*! */
if
((
csa
->
size
!=
(
int
)((
sz
-
sizeof
(
CycleStorageAttr
))
/
(
sizeof
(
CycleStorageElem
)
+
inf_sz
)))
||
(
csa
->
inf_size
!=
inf_sz
)
||
(
csa
->
seekpos
!=
seek
))
{
delete
csa
;
return
false
;
}
delete
csa
;
inf_size
=
inf_sz
;
full_size
=
sizeof
(
CycleStorageElem
)
+
inf_size
;
size
=
(
sz
-
sizeof
(
CycleStorageAttr
))
/
full_size
;
seekpos
=
seek
;
seekpos
+=
sizeof
(
CycleStorageAttr
);
FindHead
();
return
true
;
}
...
...
@@ -161,6 +174,7 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
{
if
(
file
!=
NULL
)
fclose
(
file
);
file
=
fopen
(
name
,
"r+"
);
/*! , */
if
(
file
==
NULL
)
{
FILE
*
f
=
fopen
(
name
,
"w"
);
...
...
@@ -180,6 +194,7 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[
full_size
];
jrn
->
status
=
0
;
/*! */
CycleStorageAttr
*
csa
=
new
CycleStorageAttr
();
csa
->
inf_size
=
inf_size
;
csa
->
size
=
size
;
...
...
@@ -189,15 +204,14 @@ bool CycleStorage::Create(const char* name, int inf_sz, int sz, int seek)
fflush
(
file
);
seekpos
+=
sizeof
(
CycleStorageAttr
);
/*! */
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
filewrite
(
jrn
,
i
,
false
);
}
fflush
(
file
);
/*! , ,
*/
/*! , , */
int
emp
=
sz
-
size
*
full_size
-
sizeof
(
CycleStorageAttr
);
if
(
emp
>
0
)
{
...
...
@@ -322,6 +336,7 @@ bool CycleStorage::DelRow(int row)
bool
CycleStorage
::
DelAllRows
()
{
/*! */
if
(
file
==
NULL
)
return
false
;
CycleStorageElem
*
jrn
=
(
CycleStorageElem
*
)
new
char
[
full_size
];
int
i
;
...
...
@@ -342,8 +357,9 @@ bool CycleStorage::DelAllRows()
return
true
;
}
void
*
CycleStorage
::
View
Row
(
int
num
,
void
*
str
)
void
*
CycleStorage
::
Read
Row
(
int
num
,
void
*
str
)
{
/*! */
int
j
=
(
head
+
num
)
%
size
;
if
((
file
==
NULL
)
||
(
num
>=
size
))
return
0
;
...
...
@@ -361,7 +377,7 @@ void* CycleStorage::ViewRow(int num, void* str)
return
0
;
}
bool
CycleStorage
::
ExportToXML
(
const
char
*
name
)
/*
bool CycleStorage::ExportToXML(const char* name)
{
int i,j=head;
if(file==NULL) return false;
...
...
@@ -391,7 +407,7 @@ bool CycleStorage::ExportToXML(const char* name)
f->save(name);
delete jrn;
return true;
}
}
*/
int
CycleStorage
::
GetIter
()
{
...
...
src/Various/TableBlockStorage.cc
View file @
94204758
...
...
@@ -30,9 +30,6 @@
#include "Storages.h"
#define block_begin -5
#define empty_elem -1
TableBlockStorage
::
TableBlockStorage
()
{
file
=
NULL
;
...
...
@@ -78,6 +75,7 @@ void* TableBlockStorage::ValPointer(int num)
void
TableBlockStorage
::
filewrite
(
int
seek
,
bool
needflush
)
{
/*! i */
fseek
(
file
,
seekpos
+
(
seek
+
cur_block
*
block_size
)
*
full_size
,
0
);
fwrite
(
mem
[
seek
],
full_size
,
1
,
file
);
if
(
needflush
)
fflush
(
file
);
...
...
@@ -85,37 +83,40 @@ void TableBlockStorage::filewrite(int seek, bool needflush)
bool
TableBlockStorage
::
CopyToNextBlock
(
void
)
{
int
i
;
TableBlockStorageElem
*
tbl
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
];
/*! */
max
=-
1
;
int
tmp
=
mem
[
0
]
->
count
;
mem
[
0
]
->
count
=
EMPTY_BLOCK
;
filewrite
(
0
,
false
);
mem
[
0
]
->
count
=
tmp
;
if
(
max
==
lim
-
1
)
if
(
cur_block
>=
block_number
-
1
)
cur_block
=
0
;
else
cur_block
++
;
/*! */
for
(
int
i
=
0
;
i
<
block_size
;
i
++
)
{
if
(
(
cur_block
+
2
)
*
block_size
<=
size
)
if
(
mem
[
i
]
->
count
>=
0
)
{
max
=-
1
;;
for
(
i
=
0
;
i
<
block_size
;
i
++
)
{
if
(
mem
[
i
]
->
count
>=
0
)
{
mem
[
i
]
->
count
=++
max
;
cur_block
++
;
filewrite
(
i
,
false
);
cur_block
--
;
}
}
tbl
->
count
=
block_begin
;
fseek
(
file
,
seekpos
+
cur_block
*
block_size
*
full_size
,
0
);
fwrite
(
tbl
,
full_size
,
1
,
file
);
fflush
(
file
);
cur_block
++
;
mem
[
i
]
->
count
=++
max
;
filewrite
(
i
,
false
);
}
}
delete
tbl
;
fflush
(
file
);
/*! , false */
if
(
cur_block
>=
block_number
-
1
)
return
false
;
return
true
;
}
bool
TableBlockStorage
::
Open
(
const
char
*
name
,
int
key_sz
,
int
inf_sz
,
int
sz
,
int
block_num
,
int
block_lim
,
int
seek
)
{
/*! , */
if
(
file
!=
NULL
)
fclose
(
file
);
file
=
fopen
(
name
,
"r+"
);
...
...
@@ -124,6 +125,7 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
seekpos
=
seek
;
if
(
fseek
(
file
,
seekpos
,
0
)
==-
1
)
return
false
;
/*! */
StorageAttr
*
sa
=
new
StorageAttr
();
fread
(
sa
,
sizeof
(
StorageAttr
),
1
,
file
);
...
...
@@ -133,6 +135,7 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
int
tmpblock
=
tmpsize
/
block_num
;
tmpsize
=
tmpblock
*
block_num
;
/*! */
if
((
sa
->
k_size
!=
key_sz
)
||
(
sa
->
inf_size
!=
inf_sz
)
||
(
sa
->
size
!=
tmpsize
)
||
(
sa
->
block_number
!=
block_num
)
||
(
sa
->
lim
!=
block_lim
)
||
(
sa
->
seekpos
!=
seek
))
{
delete
sa
;
...
...
@@ -149,10 +152,10 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
size
=
block_size
*
block_num
;
max
=-
1
;
int
i
;
/*! */
mem
=
new
TableBlockStorageElem
*
[
block_size
];
for
(
i
=
0
;
i
<
block_size
;
i
++
)
for
(
i
nt
i
=
0
;
i
<
block_size
;
i
++
)
{
mem
[
i
]
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
];
}
...
...
@@ -160,18 +163,18 @@ bool TableBlockStorage::Open(const char* name, int key_sz, int inf_sz, int sz, i
TableBlockStorageElem
*
t
=
(
TableBlockStorageElem
*
)
new
char
[
full_size
];
seekpos
+=
sizeof
(
StorageAttr
);
for
(
i
=
0
;
i
<
block_num
;
i
++
)
/*! , , 0 */
for
(
cur_block
=
0
;
cur_block
<
block_num
;
cur_block
++
)
{
fseek
(
file
,
seekpos
+
i
*
block_size
*
(
full_size
),
0
);
fseek
(
file
,
seekpos
+
cur_block
*
block_size
*
(
full_size
),
0
);
fread
(
t
,(
full_size
),
1
,
file
);
if
(
t
->
count
>=
0
)
{
cur_block
=
i
;
if
(
t
->
count
>=
0
)
break
;
}
}
/*! */
fseek
(
file
,
seekpos
+
(
cur_block
*
block_size
)
*
(
full_size
),
0
);
for
(
i
=
0
;
i
<
block_size
;
i
++
)
for
(
i
nt
i
=
0
;
i
<
block_size
;
i
++
)
{
fread
(
mem
[
i
],(
full_size
),
1
,
file
);
if
(
mem
[
i
]
->
count
>
max
)
max
=
mem
[
i
]
->
count
;
...
...
@@ -206,6 +209,7 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
if
(
fseek
(
file
,
seekpos
,
0
)
==-
1
)
return
false
;
/*! */
mem
=
new
TableBlockStorageElem
*
[
block_size
];
for
(
i
=
0
;
i
<
block_size
;
i
++
)
{
...
...
@@ -220,18 +224,19 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
sa
->
lim
=
lim
;
sa
->
seekpos
=
seekpos
;
/*! */
cur_block
=
0
;
fwrite
(
sa
,
sizeof
(
StorageAttr
),
1
,
file
);
fflush
(
file
);
seekpos
+=
sizeof
(
StorageAttr
);
/*! :
block_begin=(-5) - , , , empty_elem
=(-1) -
EMPTY_BLOCK=(-5) - , , , EMPTY_ELEM
=(-1) -
*/
mem
[
0
]
->
count
=
block_begin
;
mem
[
0
]
->
count
=
EMPTY_BLOCK
;
for
(
i
=
1
;
i
<
block_size
;
i
++
)
mem
[
i
]
->
count
=
empty_elem
;
mem
[
i
]
->
count
=
EMPTY_ELEM
;
/*! */
for
(
i
=
0
;
i
<
size
;
i
++
)
...
...
@@ -242,6 +247,7 @@ bool TableBlockStorage::Create(const char* name, int key_sz, int inf_sz, int sz,
cur_block
=
0
;
fflush
(
file
);
/*! , , */
int
emp
=
sz
-
size
*
full_size
-
sizeof
(
StorageAttr
);
if
(
emp
>
0
)
{
...
...
@@ -258,7 +264,7 @@ bool TableBlockStorage::AddRow(void* key, void* value)
int
i
=
0
,
pos
=-
1
,
empty
=-
1
;
if
(
file
==
NULL
)
return
false
;
CopyToNextBlock
();
if
(
max
==
lim
-
1
)
CopyToNextBlock
();
for
(
i
=
0
;
i
<
block_size
;
i
++
)
{
if
(
mem
[
i
]
->
count
>=
0
)
...
...
@@ -266,6 +272,7 @@ bool TableBlockStorage::AddRow(void* key, void* value)
if
((
mem
[
i
]
->
count
<
0
)
&&
(
empty
<
0
))
empty
=
i
;
}
/*! , pos>=0, , empty */
if
(
pos
>=
0
)
empty
=
pos
;
else
memcpy
(
KeyPointer
(
empty
),
key
,
k_size
);
...
...
@@ -280,20 +287,19 @@ bool TableBlockStorage::DelRow(void* key)
int
i
;
if
(
file
==
NULL
)
return
false
;
/*!
*/
CopyToNextBlock
();
/*! */
if
(
max
==
lim
-
1
)
CopyToNextBlock
();
for
(
i
=
0
;
i
<
block_size
;
i
++
)
{
if
(
mem
[
i
]
->
count
>=
0
)
if
(
KeyCompare
(
i
,
key
))
{
mem
[
i
]
->
count
=++
max
;
memset
(
KeyPointer
(
i
),
0
,
k_size
);
filewrite
(
i
);
return
true
;
}
if
(
mem
[
i
]
->
count
<
0
)
continue
;
if
(
KeyCompare
(
i
,
key
))
{
mem
[
i
]
->
count
=++
max
;
memset
(
KeyPointer
(
i
),
0
,
k_size
);
filewrite
(
i
);
return
true
;
}
}
return
false
;
}
...
...
@@ -304,12 +310,14 @@ void* TableBlockStorage::FindKeyValue(void* key, void* val)
if
(
file
==
NULL
)
return
0
;
for
(
i
=
0
;
i
<
block_size
;
i
++
)
{
if
(
mem
[
i
]
->
count
>=
0
)
if
(
KeyCompare
(
i
,
key
))
{
memcpy
(
val
,
ValPointer
(
i
),
inf_size
);
return
val
;
}
/*! >= 0, .. */
if
(
mem
[
i
]
->
count
<
0
)
continue
;
if
(
KeyCompare
(
i
,
key
))
{
memcpy
(
val
,
ValPointer
(
i
),
inf_size
);
return
val
;
}
}
return
0
;
}
...
...
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