hamsterdb Embedded Database
2.1.7
Main Page
Modules
Namespaces
Data Structures
Files
File List
Globals
samples
db6.cpp
Go to the documentation of this file.
1
/*
2
* Copyright (C) 2005-2014 Christoph Rupp (chris@crupp.de).
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
22
#include <iostream>
23
#include <
ham/hamsterdb.hpp
>
24
25
#define LOOP 10
26
27
int
28
run_demo
() {
29
ham_u32_t
i;
30
hamsterdb::env
env;
/* hamsterdb environment object */
31
hamsterdb::db
db;
/* hamsterdb database object */
32
hamsterdb::key
key;
/* a key */
33
hamsterdb::record
record;
/* a record */
34
ham_parameter_t
params[] = {
/* parameters for ham_env_create_db */
35
{
HAM_PARAM_KEY_TYPE
,
HAM_TYPE_UINT32
},
36
{
HAM_PARAM_RECORD_SIZE
,
sizeof
(
ham_u32_t
)},
37
{0, }
38
};
39
40
/* Create a new environment file and a database in this environment */
41
env.
create
(
"test.db"
);
42
db = env.
create_db
(1, 0, ¶ms[0]);
43
44
/*
45
* Now we can insert, delete or lookup values in the database
46
*
47
* for our test program, we just insert a few values, then look them
48
* up, then delete them and try to look them up again (which will fail).
49
*/
50
for
(i = 0; i <
LOOP
; i++) {
51
key.
set_size
(
sizeof
(i));
52
key.
set_data
(&i);
53
54
record.
set_size
(
sizeof
(i));
55
record.
set_data
(&i);
56
57
db.
insert
(&key, &record);
58
}
59
60
/*
61
* Now lookup all values
62
*
63
* for db::find(), we could use the flag HAM_RECORD_USER_ALLOC, if WE
64
* allocate record.data (otherwise the memory is automatically allocated
65
* by hamsterdb)
66
*/
67
for
(i = 0; i <
LOOP
; i++) {
68
key.
set_size
(
sizeof
(i));
69
key.
set_data
(&i);
70
71
record = db.
find
(&key);
72
73
/* Check if the value is ok */
74
if
(*(
ham_u32_t
*)record.
get_data
() != i) {
75
std::cerr <<
"db::find() ok, but returned bad value"
<< std::endl;
76
return
(-1);
77
}
78
}
79
80
/*
81
* close the database handle, then re-open it (just to demonstrate how
82
* to open a database file)
83
*/
84
db.
close
();
85
env.
close
();
86
env.
open
(
"test.db"
);
87
db = env.
open_db
(1);
88
89
/* now erase all values */
90
for
(i = 0; i <
LOOP
; i++) {
91
key.
set_size
(
sizeof
(i));
92
key.
set_data
(&i);
93
94
db.
erase
(&key);
95
}
96
97
/*
98
* Once more we try to find all values. Every db::find() call must
99
* now fail with HAM_KEY_NOT_FOUND
100
*/
101
for
(i = 0; i <
LOOP
; i++) {
102
key.
set_size
(
sizeof
(i));
103
key.
set_data
(&i);
104
105
try
{
106
record = db.
find
(&key);
107
}
108
catch
(
hamsterdb::error
&e) {
109
if
(e.
get_errno
() !=
HAM_KEY_NOT_FOUND
) {
110
std::cerr <<
"db::find() returned error "
<< e.
get_string
()
111
<< std::endl;
112
return
(-1);
113
}
114
}
115
}
116
117
/*
118
* Done! No need to close the database handles, they are closed in their
119
* destructor.
120
*/
121
122
std::cout <<
"success!"
<< std::endl;
123
return
(0);
124
}
125
126
int
127
main
(
int
argc,
char
**argv)
128
{
129
try
{
130
return
(
run_demo
());
131
}
132
catch
(
hamsterdb::error
&e) {
133
std::cerr <<
"run_demo() failed with unexpected error "
134
<< e.
get_errno
() <<
" ('"
135
<< e.
get_string
() <<
"')"
<< std::endl;
136
return
(-1);
137
}
138
}
Generated by
1.8.4