upscaledb  2.2.1
db4.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  ups_key_t key;
39  ups_record_t record;
40 
41  memset(&key, 0, sizeof(key));
42  memset(&record, 0, sizeof(record));
43 
44  printf("This sample uses upscaledb to list all words in the "
45  "original order.\n");
46  printf("Reading from stdin...\n");
47 
48  /*
49  * Create a new upscaledb "record number" Database.
50  * We could create an in-memory-Environment to speed up the sorting.
51  */
52  st = ups_env_create(&env, "test.db", 0, 0664, 0);
53  if (st != UPS_SUCCESS) {
54  printf("ups_env_create() failed with error %d\n", st);
55  return (-1);
56  }
57 
59  if (st != UPS_SUCCESS) {
60  printf("ups_env_create_db() failed with error %d\n", st);
61  return (-1);
62  }
63 
64  /*
65  * Now read each line from stdin and split it in words; then each
66  * word is inserted into the database
67  */
68  while (fgets(line, sizeof(line), stdin)) {
69  char *start = line, *p;
70 
71  /*
72  * strtok is not the best function because it's not threadsafe
73  * and not flexible, but it's good enough for this example.
74  */
75  while ((p = strtok(start, " \t\r\n"))) {
76  uint32_t recno;
77 
79  key.data = &recno;
80  key.size = sizeof(recno);
81 
82  record.data = p;
83  record.size = (uint32_t)strlen(p) + 1; /* also store
84  * terminating 0 */
85 
86  st = ups_db_insert(db, 0, &key, &record, 0);
87  if (st != UPS_SUCCESS && st != UPS_DUPLICATE_KEY) {
88  printf("ups_db_insert() failed with error %d\n", st);
89  return (-1);
90  }
91  printf(".");
92 
93  start = 0;
94  }
95  }
96 
97  /* Create a cursor */
98  st = ups_cursor_create(&cursor, db, 0, 0);
99  if (st != UPS_SUCCESS) {
100  printf("ups_cursor_create() failed with error %d\n", st);
101  return (-1);
102  }
103 
104  memset(&key, 0, sizeof(key));
105 
106  /* Iterate over all items and print the records */
107  while (1) {
108  st = ups_cursor_move(cursor, &key, &record, UPS_CURSOR_NEXT);
109  if (st != UPS_SUCCESS) {
110  /* reached end of the database? */
111  if (st == UPS_KEY_NOT_FOUND)
112  break;
113  else {
114  printf("ups_cursor_next() failed with error %d\n", st);
115  return (-1);
116  }
117  }
118 
119  /* print the record number and the word */
120  printf("%u: %s\n", *(uint32_t *)key.data, (const char *)record.data);
121  }
122 
123  /*
124  * Then close the handles; the flag UPS_AUTO_CLEANUP will automatically
125  * close all databases and cursors and we do not need to
126  * call ups_cursor_close and ups_db_close
127  */
128  st = ups_env_close(env, UPS_AUTO_CLEANUP);
129  if (st != UPS_SUCCESS) {
130  printf("ups_env_close() failed with error %d\n", st);
131  return (-1);
132  }
133 
134  /* success! */
135  return (0);
136 }
137 
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)
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
uint32_t flags
Definition: upscaledb.h:261
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 UPS_AUTO_CLEANUP
Definition: upscaledb.h:1883
#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_RECORD_NUMBER32
Definition: upscaledb.h:1301
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)
#define DATABASE_NAME
Definition: db4.c:29
#define UPS_KEY_USER_ALLOC
Definition: upscaledb.h:279
int main(int argc, char **argv)
Definition: db4.c:32
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