upscaledb  2.2.1
db3.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2016 Christoph Rupp (chris@crupp.de).
3  * All Rights Reserved.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * See the file COPYING for License information.
16  */
17 
23 #include <stdio.h>
24 #include <string.h>
25 #include <ups/upscaledb.h>
26 
27 #define DATABASE_NAME 1
28 
29 static int
30 my_string_compare(ups_db_t *db, const uint8_t *lhs, uint32_t lhs_length,
31  const uint8_t *rhs, uint32_t rhs_length) {
32  int s = strncmp((const char *)lhs, (const char *)rhs,
33  lhs_length < rhs_length ? lhs_length : rhs_length);
34  if (s < 0)
35  return -1;
36  if (s > 0)
37  return +1;
38  return 0;
39 }
40 
41 int
42 main(int argc, char **argv) {
43  ups_status_t st; /* status variable */
44  ups_env_t *env; /* upscaledb environment object */
45  ups_db_t *db; /* upscaledb database object */
46  ups_cursor_t *cursor; /* a database cursor */
47  char line[1024 * 4]; /* a buffer for reading lines */
48  ups_key_t key = {0};
49  ups_record_t record = {0};
50  ups_parameter_t params[] = { /* parameters for ups_env_create_db */
52  {UPS_PARAM_RECORD_SIZE, 0}, /* we do not store records, only keys */
53  {0, }
54  };
55 
56  printf("This sample uses upscaledb to sort data.\n");
57  printf("Reading from stdin...\n");
58 
59  /*
60  * Create a new upscaledb Environment.
61  */
62  st = ups_env_create(&env, "test.db", 0, 0664, 0);
63  if (st != UPS_SUCCESS) {
64  printf("ups_env_create() failed with error %d\n", st);
65  return (-1);
66  }
67 
68  /*
69  * Create a new Database in the new Environment. The UPS_TYPE_CUSTOM
70  * parameter allows us to set a custom compare function.
71  */
72  st = ups_env_create_db(env, &db, DATABASE_NAME,
73  UPS_ENABLE_DUPLICATE_KEYS, &params[0]);
74  if (st != UPS_SUCCESS) {
75  printf("ups_env_create_db() failed with error %d\n", st);
76  return (-1);
77  }
78 
79  /*
80  * Since we use strings as our database keys we use our own comparison
81  * function based on strcmp instead of the default memcmp function.
82  */
84  if (st) {
85  printf("ups_set_compare_func() failed with error %d\n", st);
86  return (-1);
87  }
88 
89  /*
90  * Now read each line from stdin and split it in words; then each
91  * word is inserted into the database
92  */
93  while (fgets(line, sizeof(line), stdin)) {
94  char *start = line, *p;
95 
96  /*
97  * strtok is not the best function because it's not threadsafe
98  * and not flexible, but it's good enough for this example.
99  */
100  while ((p = strtok(start, " \t\r\n"))) {
101  key.data = p;
102  key.size = (uint32_t)strlen(p) + 1; /* also store the terminating
103  * 0-byte */
104 
105  st = ups_db_insert(db, 0, &key, &record, 0);
106  if (st != UPS_SUCCESS && st!=UPS_DUPLICATE_KEY) {
107  printf("ups_db_insert() failed with error %d\n", st);
108  return (-1);
109  }
110  printf(".");
111 
112  start = 0;
113  }
114  }
115 
116  /* create a cursor */
117  st = ups_cursor_create(&cursor, db, 0, 0);
118  if (st != UPS_SUCCESS) {
119  printf("ups_cursor_create() failed with error %d\n", st);
120  return (-1);
121  }
122 
123  /* iterate over all items with UPS_CURSOR_NEXT, and print the words */
124  while (1) {
125  st = ups_cursor_move(cursor, &key, &record, UPS_CURSOR_NEXT);
126  if (st != UPS_SUCCESS) {
127  /* reached end of the database? */
128  if (st == UPS_KEY_NOT_FOUND)
129  break;
130  else {
131  printf("ups_cursor_next() failed with error %d\n", st);
132  return (-1);
133  }
134  }
135 
136  /* print the word */
137  printf("%s\n", (const char *)key.data);
138  }
139 
140  /*
141  * Then close the handles; the flag UPS_AUTO_CLEANUP will automatically
142  * close all database and cursors, and we do not need to call
143  * ups_cursor_close and ups_db_close
144  */
145  st = ups_env_close(env, UPS_AUTO_CLEANUP);
146  if (st != UPS_SUCCESS) {
147  printf("ups_env_close() failed with error %d\n", st);
148  return (-1);
149  }
150 
151  /* success! */
152  return (0);
153 }
154 
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_close(ups_env_t *env, uint32_t flags)
UPS_EXPORT ups_status_t UPS_CALLCONV ups_db_insert(ups_db_t *db, ups_txn_t *txn, ups_key_t *key, ups_record_t *record, uint32_t flags)
unsigned char uint8_t
Definition: msstdint.h:83
UPS_EXPORT ups_status_t UPS_CALLCONV ups_cursor_move(ups_cursor_t *cursor, ups_key_t *key, ups_record_t *record, uint32_t flags)
unsigned int uint32_t
Definition: msstdint.h:85
Include file for upscaledb embedded database.
uint16_t size
Definition: upscaledb.h:255
#define UPS_DUPLICATE_KEY
Definition: upscaledb.h:363
#define UPS_PARAM_RECORD_SIZE
Definition: upscaledb.h:1725
struct ups_db_t ups_db_t
Definition: upscaledb.h:156
#define UPS_CURSOR_NEXT
Definition: upscaledb.h:2051
struct ups_cursor_t ups_cursor_t
Definition: upscaledb.h:177
#define DATABASE_NAME
Definition: db3.c:27
#define UPS_AUTO_CLEANUP
Definition: upscaledb.h:1883
#define UPS_ENABLE_DUPLICATE_KEYS
Definition: upscaledb.h:1309
#define UPS_SUCCESS
Definition: upscaledb.h:341
int main(int argc, char **argv)
Definition: db3.c:42
#define UPS_TYPE_CUSTOM
Definition: upscaledb.h:316
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_create(ups_env_t **env, const char *filename, uint32_t flags, uint32_t mode, const ups_parameter_t *param)
void * data
Definition: upscaledb.h:258
int ups_status_t
Definition: types.h:139
UPS_EXPORT ups_status_t UPS_CALLCONV ups_db_set_compare_func(ups_db_t *db, ups_compare_func_t foo)
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_create_db(ups_env_t *env, ups_db_t **db, uint16_t name, uint32_t flags, const ups_parameter_t *params)
#define UPS_PARAM_KEY_TYPE
Definition: upscaledb.h:1710
static int my_string_compare(ups_db_t *db, const uint8_t *lhs, uint32_t lhs_length, const uint8_t *rhs, uint32_t rhs_length)
Definition: db3.c:30
UPS_EXPORT ups_status_t UPS_CALLCONV ups_cursor_create(ups_cursor_t **cursor, ups_db_t *db, ups_txn_t *txn, uint32_t flags)
#define UPS_KEY_NOT_FOUND
Definition: upscaledb.h:361
struct ups_env_t ups_env_t
Definition: upscaledb.h:165