hamsterdb Embedded Database
2.1.7
Main Page
Modules
Namespaces
Data Structures
Files
File List
Globals
samples
db3.c
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 <stdio.h>
23
#include <string.h>
24
#include <
ham/hamsterdb.h
>
25
26
#define DATABASE_NAME 1
27
28
static
int
29
my_string_compare
(
ham_db_t
*db,
const
ham_u8_t
*lhs,
ham_u32_t
lhs_length,
30
const
ham_u8_t
*rhs,
ham_u32_t
rhs_length) {
31
int
s = strncmp((
const
char
*)lhs, (
const
char
*)rhs,
32
lhs_length < rhs_length ? lhs_length : rhs_length);
33
if
(s < 0)
34
return
-1;
35
if
(s > 0)
36
return
+1;
37
return
0;
38
}
39
40
int
41
main
(
int
argc,
char
**argv) {
42
ham_status_t
st;
/* status variable */
43
ham_env_t
*env;
/* hamsterdb environment object */
44
ham_db_t
*db;
/* hamsterdb database object */
45
ham_cursor_t
*cursor;
/* a database cursor */
46
char
line[1024 * 4];
/* a buffer for reading lines */
47
ham_key_t
key = {0};
48
ham_record_t
record = {0};
49
ham_parameter_t
params[] = {
/* parameters for ham_env_create_db */
50
{
HAM_PARAM_KEY_TYPE
,
HAM_TYPE_CUSTOM
},
51
{
HAM_PARAM_RECORD_SIZE
, 0},
/* we do not store records, only keys */
52
{0, }
53
};
54
55
printf(
"This sample uses hamsterdb to sort data.\n"
);
56
printf(
"Reading from stdin...\n"
);
57
58
/*
59
* Create a new hamsterdb Environment.
60
*/
61
st =
ham_env_create
(&env,
"test.db"
, 0, 0664, 0);
62
if
(st !=
HAM_SUCCESS
) {
63
printf(
"ham_env_create() failed with error %d\n"
, st);
64
return
(-1);
65
}
66
67
/*
68
* Create a new Database in the new Environment. The HAM_TYPE_CUSTOM
69
* parameter allows us to set a custom compare function.
70
*/
71
st =
ham_env_create_db
(env, &db,
DATABASE_NAME
,
72
HAM_ENABLE_DUPLICATE_KEYS
, ¶ms[0]);
73
if
(st !=
HAM_SUCCESS
) {
74
printf(
"ham_env_create_db() failed with error %d\n"
, st);
75
return
(-1);
76
}
77
78
/*
79
* Since we use strings as our database keys we use our own comparison
80
* function based on strcmp instead of the default memcmp function.
81
*/
82
st =
ham_db_set_compare_func
(db,
my_string_compare
);
83
if
(st) {
84
printf(
"ham_set_compare_func() failed with error %d\n"
, st);
85
return
(-1);
86
}
87
88
/*
89
* Now read each line from stdin and split it in words; then each
90
* word is inserted into the database
91
*/
92
while
(fgets(line,
sizeof
(line), stdin)) {
93
char
*start = line, *p;
94
95
/*
96
* strtok is not the best function because it's not threadsafe
97
* and not flexible, but it's good enough for this example.
98
*/
99
while
((p = strtok(start,
" \t\r\n"
))) {
100
key.
data
= p;
101
key.
size
= (
ham_u32_t
)strlen(p) + 1;
/* also store the terminating
102
* 0-byte */
103
104
st =
ham_db_insert
(db, 0, &key, &record, 0);
105
if
(st !=
HAM_SUCCESS
&& st!=
HAM_DUPLICATE_KEY
) {
106
printf(
"ham_db_insert() failed with error %d\n"
, st);
107
return
(-1);
108
}
109
printf(
"."
);
110
111
start = 0;
112
}
113
}
114
115
/* create a cursor */
116
st =
ham_cursor_create
(&cursor, db, 0, 0);
117
if
(st !=
HAM_SUCCESS
) {
118
printf(
"ham_cursor_create() failed with error %d\n"
, st);
119
return
(-1);
120
}
121
122
/* iterate over all items with HAM_CURSOR_NEXT, and print the words */
123
while
(1) {
124
st =
ham_cursor_move
(cursor, &key, &record,
HAM_CURSOR_NEXT
);
125
if
(st !=
HAM_SUCCESS
) {
126
/* reached end of the database? */
127
if
(st ==
HAM_KEY_NOT_FOUND
)
128
break
;
129
else
{
130
printf(
"ham_cursor_next() failed with error %d\n"
, st);
131
return
(-1);
132
}
133
}
134
135
/* print the word */
136
printf(
"%s\n"
, (
const
char
*)key.
data
);
137
}
138
139
/*
140
* Then close the handles; the flag HAM_AUTO_CLEANUP will automatically
141
* close all database and cursors, and we do not need to call
142
* ham_cursor_close and ham_db_close
143
*/
144
st =
ham_env_close
(env,
HAM_AUTO_CLEANUP
);
145
if
(st !=
HAM_SUCCESS
) {
146
printf(
"ham_env_close() failed with error %d\n"
, st);
147
return
(-1);
148
}
149
150
/* success! */
151
return
(0);
152
}
153
Generated by
1.8.4