upscaledb  2.2.1
client1.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 1000
29 
30 void
31 error(const char *foo, ups_status_t st) {
32  printf("%s() returned error %d: %s\n", foo, st, ups_strerror(st));
33  exit(-1);
34 }
35 
36 int
37 main(int argc, char **argv) {
38  int i;
39  ups_status_t st; /* status variable */
40  ups_env_t *env; /* upscaledb Environment object */
41  ups_db_t *db; /* upscaledb Database object */
42  ups_key_t key = {0}; /* the structure for a key */
43  ups_record_t record = {0}; /* the structure for a record */
44 
45  /*
46  * Connect to the server which should listen at 8080. The server is
47  * implemented in server1.c.
48  */
49  st = ups_env_create(&env, "ups://localhost:8080/env1.db", 0, 0, 0);
50  if (st != UPS_SUCCESS)
51  error("ups_env_create", st);
52 
53  /* now open a Database in this Environment */
54  st = ups_env_open_db(env, &db, 13, 0, 0);
55  if (st != UPS_SUCCESS)
56  error("ups_env_open_db", st);
57 
58  /* now we can insert, delete or lookup values in the database */
59  for (i = 0; i < LOOP; i++) {
60  key.data = &i;
61  key.size = sizeof(i);
62 
63  record.size = key.size;
64  record.data = key.data;
65 
66  st = ups_db_insert(db, 0, &key, &record, 0);
67  if (st != UPS_SUCCESS)
68  error("ups_db_insert", st);
69  }
70 
71  /* now lookup all values */
72  for (i = 0; i < LOOP; i++) {
73  key.data = &i;
74  key.size = sizeof(i);
75 
76  st = ups_db_find(db, 0, &key, &record, 0);
77  if (st != UPS_SUCCESS)
78  error("ups_db_find", st);
79 
80  /* check if the value is ok */
81  if (*(int *)record.data != i) {
82  printf("ups_db_find() ok, but returned bad value\n");
83  return (-1);
84  }
85  }
86 
87  /* erase everything */
88  for (i = 0; i < LOOP; i++) {
89  key.data = &i;
90  key.size = sizeof(i);
91 
92  st = ups_db_erase(db, 0, &key, 0);
93  if (st != UPS_SUCCESS)
94  error("ups_db_erase", st);
95  }
96 
97  /* and make sure that the database is empty */
98  for (i = 0; i < LOOP; i++) {
99  key.data = &i;
100  key.size = sizeof(i);
101 
102  st = ups_db_find(db, 0, &key, &record, 0);
103  if (st != UPS_KEY_NOT_FOUND)
104  error("ups_db_find", st);
105  }
106 
107  /* close the database handle */
108  st = ups_db_close(db, 0);
109  if (st != UPS_SUCCESS)
110  error("ups_db_close", st);
111 
112  /* close the environment handle */
113  st = ups_env_close(env, 0);
114  if (st != UPS_SUCCESS)
115  error("ups_env_close", st);
116 
117  printf("success!\n");
118  return (0);
119 }
120 
#define LOOP
Definition: client1.c:28
UPS_EXPORT const char *UPS_CALLCONV ups_strerror(ups_status_t status)
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)
void error(const char *foo, ups_status_t st)
Definition: client1.c:31
int main(int argc, char **argv)
Definition: client1.c:37
Include file for upscaledb embedded database.
uint16_t size
Definition: upscaledb.h:255
struct ups_db_t ups_db_t
Definition: upscaledb.h:156
#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
uint32_t size
Definition: upscaledb.h:204
int ups_status_t
Definition: types.h:139
#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