upscaledb  2.2.1
db1.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 <stdlib.h> /* for exit() */
26 #include <ups/upscaledb.h>
27 
28 #define LOOP 10
29 #define DATABASE_NAME 1
30 
31 void
32 error(const char *foo, ups_status_t st) {
33  printf("%s() returned error %d: %s\n", foo, st, ups_strerror(st));
34  exit(-1);
35 }
36 
37 int
38 main(int argc, char **argv) {
39  uint32_t i;
40  ups_status_t st; /* status variable */
41  ups_env_t *env; /* upscaledb environment object */
42  ups_db_t *db; /* upscaledb database object */
43  ups_key_t key = {0}; /* the structure for a key */
44  ups_record_t record = {0}; /* the structure for a record */
45  ups_parameter_t params[] = { /* parameters for ups_env_create_db */
48  {0, }
49  };
50 
51  /* First create a new upscaledb Environment */
52  st = ups_env_create(&env, "test.db", 0, 0664, 0);
53  if (st != UPS_SUCCESS)
54  error("ups_env_create", st);
55 
56  /* And in this Environment we create a new Database for uint32-keys
57  * and uint32-records. */
58  st = ups_env_create_db(env, &db, DATABASE_NAME, 0, &params[0]);
59  if (st != UPS_SUCCESS)
60  error("ups_env_create_db", st);
61 
62  /*
63  * now we can insert, delete or lookup values in the database
64  *
65  * for our test program, we just insert a few values, then look them
66  * up, then delete them and try to look them up again (which will fail).
67  */
68  for (i = 0; i < LOOP; i++) {
69  key.data = &i;
70  key.size = sizeof(i);
71 
72  record.size = key.size;
73  record.data = key.data;
74 
75  st = ups_db_insert(db, 0, &key, &record, 0);
76  if (st != UPS_SUCCESS)
77  error("ups_db_insert", st);
78  }
79 
80  /*
81  * now lookup all values
82  *
83  * for ups_db_find(), we could use the flag UPS_RECORD_USER_ALLOC, if WE
84  * allocate record.data (otherwise the memory is automatically allocated
85  * by upscaledb)
86  */
87  for (i = 0; i < LOOP; i++) {
88  key.data = &i;
89  key.size = sizeof(i);
90 
91  st = ups_db_find(db, 0, &key, &record, 0);
92  if (st != UPS_SUCCESS)
93  error("ups_db_find", st);
94 
95  /*
96  * check if the value is ok
97  */
98  if (*(int *)record.data != i) {
99  printf("ups_db_find() ok, but returned bad value\n");
100  return (-1);
101  }
102  }
103 
104  /*
105  * close the database handle, then re-open it (to demonstrate how to open
106  * an Environment and a Database)
107  */
108  st = ups_db_close(db, 0);
109  if (st != UPS_SUCCESS)
110  error("ups_db_close", st);
111  st = ups_env_close(env, 0);
112  if (st != UPS_SUCCESS)
113  error("ups_env_close", st);
114 
115  st = ups_env_open(&env, "test.db", 0, 0);
116  if (st != UPS_SUCCESS)
117  error("ups_env_open", st);
118  st = ups_env_open_db(env, &db, DATABASE_NAME, 0, 0);
119  if (st != UPS_SUCCESS)
120  error("ups_env_open_db", st);
121 
122  /* now erase all values */
123  for (i = 0; i < LOOP; i++) {
124  key.size = sizeof(i);
125  key.data = &i;
126 
127  st = ups_db_erase(db, 0, &key, 0);
128  if (st != UPS_SUCCESS)
129  error("ups_db_erase", st);
130  }
131 
132  /*
133  * once more we try to find all values... every ups_db_find() call must
134  * now fail with UPS_KEY_NOT_FOUND
135  */
136  for (i = 0; i < LOOP; i++) {
137  key.size = sizeof(i);
138  key.data = &i;
139 
140  st = ups_db_find(db, 0, &key, &record, 0);
141  if (st != UPS_KEY_NOT_FOUND)
142  error("ups_db_find", st);
143  }
144 
145  /* we're done! close the handles. UPS_AUTO_CLEANUP will also close the
146  * 'db' handle */
147  st = ups_env_close(env, UPS_AUTO_CLEANUP);
148  if (st != UPS_SUCCESS)
149  error("ups_env_close", st);
150 
151  printf("success!\n");
152  return (0);
153 }
154 
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_open(ups_env_t **env, const char *filename, uint32_t flags, const ups_parameter_t *param)
UPS_EXPORT const char *UPS_CALLCONV ups_strerror(ups_status_t status)
#define LOOP
Definition: db1.c:28
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_erase(ups_db_t *db, ups_txn_t *txn, ups_key_t *key, 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_db_find(ups_db_t *db, ups_txn_t *txn, ups_key_t *key, ups_record_t *record, uint32_t flags)
#define UPS_TYPE_UINT32
Definition: upscaledb.h:322
unsigned int uint32_t
Definition: msstdint.h:85
int main(int argc, char **argv)
Definition: db1.c:38
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_AUTO_CLEANUP
Definition: upscaledb.h:1883
#define UPS_SUCCESS
Definition: upscaledb.h:341
UPS_EXPORT ups_status_t UPS_CALLCONV ups_env_open_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_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
void * data
Definition: upscaledb.h:207
#define DATABASE_NAME
Definition: db1.c:29
uint32_t size
Definition: upscaledb.h:204
int ups_status_t
Definition: types.h:139
void error(const char *foo, ups_status_t st)
Definition: db1.c:32
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
#define UPS_KEY_NOT_FOUND
Definition: upscaledb.h:361
UPS_EXPORT ups_status_t UPS_CALLCONV ups_db_close(ups_db_t *db, uint32_t flags)
struct ups_env_t ups_env_t
Definition: upscaledb.h:165