hamsterdb Embedded Database
2.1.7
Main Page
Modules
Namespaces
Data Structures
Files
File List
Globals
samples
db5.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
24
#include <stdio.h>
25
#include <string.h>
26
#include <
ham/hamsterdb.h
>
27
28
#define DATABASE_NAME 1
29
30
int
31
main
(
int
argc,
char
**argv) {
32
ham_status_t
st;
/* status variable */
33
ham_env_t
*env;
/* hamsterdb environment object */
34
ham_db_t
*db;
/* hamsterdb database object */
35
ham_cursor_t
*cursor;
/* a database cursor */
36
char
line[1024 * 4];
/* a buffer for reading lines */
37
ham_u32_t
lineno = 0;
/* the current line number */
38
ham_key_t
key;
39
ham_record_t
record;
40
ham_parameter_t
params[] = {
/* we insert 4 byte records only */
41
{
HAM_PARAM_RECORD_SIZE
,
sizeof
(
ham_u32_t
)},
42
{0, 0}
43
};
44
45
memset(&key, 0,
sizeof
(key));
46
memset(&record, 0,
sizeof
(record));
47
48
printf(
"This sample uses hamsterdb and duplicate keys to list all words "
49
"in the\noriginal order, together with their line number.\n"
);
50
printf(
"Reading from stdin...\n"
);
51
52
/* Create a new Database with support for duplicate keys */
53
st =
ham_env_create
(&env, 0,
HAM_IN_MEMORY
, 0664, 0);
54
if
(st !=
HAM_SUCCESS
) {
55
printf(
"ham_env_create() failed with error %d\n"
, st);
56
return
(-1);
57
}
58
st =
ham_env_create_db
(env, &db,
DATABASE_NAME
,
59
HAM_ENABLE_DUPLICATE_KEYS
, ¶ms[0]);
60
if
(st !=
HAM_SUCCESS
) {
61
printf(
"ham_env_create_db() failed with error %d\n"
, st);
62
return
(-1);
63
}
64
65
/*
66
* Now read each line from stdin and split it in words; then each
67
* word is inserted into the database
68
*/
69
while
(fgets(line,
sizeof
(line), stdin)) {
70
char
*start = line, *p;
71
lineno++;
72
73
/*
74
* strtok is not the best function because it's not threadsafe
75
* and not flexible, but it's good enough for this example.
76
*/
77
while
((p = strtok(start,
" \t\r\n"
))) {
78
key.
data
= p;
79
key.
size
= (
ham_u32_t
)strlen(p) + 1;
/* also store the terminating
80
* 0-byte */
81
record.
data
= &lineno;
82
record.
size
=
sizeof
(lineno);
83
84
st =
ham_db_insert
(db, 0, &key, &record,
HAM_DUPLICATE
);
85
if
(st !=
HAM_SUCCESS
) {
86
printf(
"ham_db_insert() failed with error %d\n"
, st);
87
return
(-1);
88
}
89
printf(
"."
);
90
91
start = 0;
92
}
93
}
94
95
/* Create a cursor */
96
st =
ham_cursor_create
(&cursor, db, 0, 0);
97
if
(st !=
HAM_SUCCESS
) {
98
printf(
"ham_cursor_create() failed with error %d\n"
, st);
99
return
(-1);
100
}
101
102
/* Iterate over all items and print them */
103
while
(1) {
104
st =
ham_cursor_move
(cursor, &key, &record,
HAM_CURSOR_NEXT
);
105
if
(st !=
HAM_SUCCESS
) {
106
/* reached end of the database? */
107
if
(st ==
HAM_KEY_NOT_FOUND
)
108
break
;
109
else
{
110
printf(
"ham_cursor_next() failed with error %d\n"
, st);
111
return
(-1);
112
}
113
}
114
115
/* print the word and the line number */
116
printf(
"%s: appeared in line %u\n"
, (
const
char
*)key.
data
,
117
*(
unsigned
*)record.
data
);
118
}
119
120
/*
121
* Then close the handles; the flag HAM_AUTO_CLEANUP will automatically
122
* close all cursors and we do not need to call ham_cursor_close and
123
* ham_db_close
124
*/
125
st =
ham_env_close
(env,
HAM_AUTO_CLEANUP
);
126
if
(st !=
HAM_SUCCESS
) {
127
printf(
"ham_env_close() failed with error %d\n"
, st);
128
return
(-1);
129
}
130
131
/* success! */
132
return
(0);
133
}
134
Generated by
1.8.4