-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathdb5.c
135 lines (118 loc) · 4.04 KB
/
db5.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
* Copyright (C) 2005-2016 Christoph Rupp (chris@crupp.de).
* All Rights Reserved.
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See the file COPYING for License information.
*/
/**
* This sample demonstrates the use of duplicate items. Every line is
* split into words, and each word is inserted with its line number.
* Then a cursor is used to print all words in a sorted order, with the
* lines in which the word occurred.
*/
#include <stdio.h>
#include <string.h>
#include <ups/upscaledb.h>
#define DATABASE_NAME 1
int
main(int argc, char **argv) {
ups_status_t st; /* status variable */
ups_env_t *env; /* upscaledb environment object */
ups_db_t *db; /* upscaledb database object */
ups_cursor_t *cursor; /* a database cursor */
char line[1024 * 4]; /* a buffer for reading lines */
uint32_t lineno = 0; /* the current line number */
ups_key_t key;
ups_record_t record;
ups_parameter_t params[] = { /* we insert 4 byte records only */
{UPS_PARAM_RECORD_SIZE, sizeof(uint32_t)},
{0, 0}
};
memset(&key, 0, sizeof(key));
memset(&record, 0, sizeof(record));
printf("This sample uses upscaledb and duplicate keys to list all words "
"in the\noriginal order, together with their line number.\n");
printf("Reading from stdin...\n");
/* Create a new Database with support for duplicate keys */
st = ups_env_create(&env, 0, UPS_IN_MEMORY, 0664, 0);
if (st != UPS_SUCCESS) {
printf("ups_env_create() failed with error %d\n", st);
return (-1);
}
st = ups_env_create_db(env, &db, DATABASE_NAME,
UPS_ENABLE_DUPLICATE_KEYS, ¶ms[0]);
if (st != UPS_SUCCESS) {
printf("ups_env_create_db() failed with error %d\n", st);
return (-1);
}
/*
* Now read each line from stdin and split it in words; then each
* word is inserted into the database
*/
while (fgets(line, sizeof(line), stdin)) {
char *start = line, *p;
lineno++;
/*
* strtok is not the best function because it's not threadsafe
* and not flexible, but it's good enough for this example.
*/
while ((p = strtok(start, " \t\r\n"))) {
key.data = p;
key.size = (uint32_t)strlen(p) + 1; /* also store the terminating
* 0-byte */
record.data = &lineno;
record.size = sizeof(lineno);
st = ups_db_insert(db, 0, &key, &record, UPS_DUPLICATE);
if (st != UPS_SUCCESS) {
printf("ups_db_insert() failed with error %d\n", st);
return (-1);
}
printf(".");
start = 0;
}
}
/* Create a cursor */
st = ups_cursor_create(&cursor, db, 0, 0);
if (st != UPS_SUCCESS) {
printf("ups_cursor_create() failed with error %d\n", st);
return (-1);
}
/* Iterate over all items and print them */
while (1) {
st = ups_cursor_move(cursor, &key, &record, UPS_CURSOR_NEXT);
if (st != UPS_SUCCESS) {
/* reached end of the database? */
if (st == UPS_KEY_NOT_FOUND)
break;
else {
printf("ups_cursor_next() failed with error %d\n", st);
return (-1);
}
}
/* print the word and the line number */
printf("%s: appeared in line %u\n", (const char *)key.data,
*(unsigned *)record.data);
}
/*
* Then close the handles; the flag UPS_AUTO_CLEANUP will automatically
* close all cursors and we do not need to call ups_cursor_close and
* ups_db_close
*/
st = ups_env_close(env, UPS_AUTO_CLEANUP);
if (st != UPS_SUCCESS) {
printf("ups_env_close() failed with error %d\n", st);
return (-1);
}
/* success! */
return (0);
}