upscaledb  2.2.1
db5.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 
25 #include <stdio.h>
26 #include <string.h>
27 #include <ups/upscaledb.h>
28 
29 #define DATABASE_NAME 1
30 
31 int
32 main(int argc, char **argv) {
33  ups_status_t st; /* status variable */
34  ups_env_t *env; /* upscaledb environment object */
35  ups_db_t *db; /* upscaledb database object */
36  ups_cursor_t *cursor; /* a database cursor */
37  char line[1024 * 4]; /* a buffer for reading lines */
38  uint32_t lineno = 0; /* the current line number */
39  ups_key_t key;
40  ups_record_t record;
41  ups_parameter_t params[] = { /* we insert 4 byte records only */
43  {0, 0}
44  };
45 
46  memset(&key, 0, sizeof(key));
47  memset(&record, 0, sizeof(record));
48 
49  printf("This sample uses upscaledb and duplicate keys to list all words "
50  "in the\noriginal order, together with their line number.\n");
51  printf("Reading from stdin...\n");
52 
53  /* Create a new Database with support for duplicate keys */
54  st = ups_env_create(&env, 0, UPS_IN_MEMORY, 0664, 0);
55  if (st != UPS_SUCCESS) {
56  printf("ups_env_create() failed with error %d\n", st);
57  return (-1);
58  }
59  st = ups_env_create_db(env, &db, DATABASE_NAME,
60  UPS_ENABLE_DUPLICATE_KEYS, &params[0]);
61  if (st != UPS_SUCCESS) {
62  printf("ups_env_create_db() failed with error %d\n", st);
63  return (-1);
64  }
65 
66  /*
67  * Now read each line from stdin and split it in words; then each
68  * word is inserted into the database
69  */
70  while (fgets(line, sizeof(line), stdin)) {
71  char *start = line, *p;
72  lineno++;
73 
74  /*
75  * strtok is not the best function because it's not threadsafe
76  * and not flexible, but it's good enough for this example.
77  */
78  while ((p = strtok(start, " \t\r\n"))) {
79  key.data = p;
80  key.size = (uint32_t)strlen(p) + 1; /* also store the terminating
81  * 0-byte */
82  record.data = &lineno;
83  record.size = sizeof(lineno);
84 
85  st = ups_db_insert(db, 0, &key, &record, UPS_DUPLICATE);
86  if (st != UPS_SUCCESS) {
87  printf("ups_db_insert() failed with error %d\n", st);
88  return (-1);
89  }
90  printf(".");
91 
92  start = 0;
93  }
94  }
95 
96  /* Create a cursor */
97  st = ups_cursor_create(&cursor, db, 0, 0);
98  if (st != UPS_SUCCESS) {
99  printf("ups_cursor_create() failed with error %d\n", st);
100  return (-1);
101  }
102 
103  /* Iterate over all items and print them */
104  while (1) {
105  st = ups_cursor_move(cursor, &key, &record, UPS_CURSOR_NEXT);
106  if (st != UPS_SUCCESS) {
107  /* reached end of the database? */
108  if (st == UPS_KEY_NOT_FOUND)
109  break;
110  else {
111  printf("ups_cursor_next() failed with error %d\n", st);
112  return (-1);
113  }
114  }
115 
116  /* print the word and the line number */
117  printf("%s: appeared in line %u\n", (const char *)key.data,
118  *(unsigned *)record.data);
119  }
120 
121  /*
122  * Then close the handles; the flag UPS_AUTO_CLEANUP will automatically
123  * close all cursors and we do not need to call ups_cursor_close and
124  * ups_db_close
125  */
126  st = ups_env_close(env, UPS_AUTO_CLEANUP);
127  if (st != UPS_SUCCESS) {
128  printf("ups_env_close() failed with error %d\n", st);
129  return (-1);
130  }
131 
132  /* success! */
133  return (0);
134 }
135 
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)
#define DATABASE_NAME
Definition: db5.c:29
#define UPS_IN_MEMORY
Definition: upscaledb.h:1288
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_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
int main(int argc, char **argv)
Definition: db5.c:32
#define UPS_AUTO_CLEANUP
Definition: upscaledb.h:1883
#define UPS_ENABLE_DUPLICATE_KEYS
Definition: upscaledb.h:1309
#define UPS_SUCCESS
Definition: upscaledb.h:341
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
#define UPS_DUPLICATE
Definition: upscaledb.h:1566
void * data
Definition: upscaledb.h:207
uint32_t size
Definition: upscaledb.h:204
int ups_status_t
Definition: types.h:139
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)
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