upscaledb  2.2.1
db6.cpp
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 <iostream>
24 #include <ups/upscaledb.hpp>
25 
26 #define LOOP 10
27 
28 int
30  uint32_t i;
31  upscaledb::env env; /* upscaledb environment object */
32  upscaledb::db db; /* upscaledb database object */
33  upscaledb::key key; /* a key */
34  upscaledb::record record; /* a record */
35  ups_parameter_t params[] = { /* parameters for ups_env_create_db */
38  {0, }
39  };
40 
41  /* Create a new environment file and a database in this environment */
42  env.create("test.db");
43  db = env.create_db(1, 0, &params[0]);
44 
45  /*
46  * Now we can insert, delete or lookup values in the database
47  *
48  * for our test program, we just insert a few values, then look them
49  * up, then delete them and try to look them up again (which will fail).
50  */
51  for (i = 0; i < LOOP; i++) {
52  key.set_size(sizeof(i));
53  key.set_data(&i);
54 
55  record.set_size(sizeof(i));
56  record.set_data(&i);
57 
58  db.insert(&key, &record);
59  }
60 
61  /*
62  * Now lookup all values
63  *
64  * for db::find(), we could use the flag UPS_RECORD_USER_ALLOC, if WE
65  * allocate record.data (otherwise the memory is automatically allocated
66  * by upscaledb)
67  */
68  for (i = 0; i < LOOP; i++) {
69  key.set_size(sizeof(i));
70  key.set_data(&i);
71 
72  record = db.find(&key);
73 
74  /* Check if the value is ok */
75  if (*(uint32_t *)record.get_data() != i) {
76  std::cerr << "db::find() ok, but returned bad value" << std::endl;
77  return (-1);
78  }
79  }
80 
81  /*
82  * close the database handle, then re-open it (just to demonstrate how
83  * to open a database file)
84  */
85  db.close();
86  env.close();
87  env.open("test.db");
88  db = env.open_db(1);
89 
90  /* now erase all values */
91  for (i = 0; i < LOOP; i++) {
92  key.set_size(sizeof(i));
93  key.set_data(&i);
94 
95  db.erase(&key);
96  }
97 
98  /*
99  * Once more we try to find all values. Every db::find() call must
100  * now fail with UPS_KEY_NOT_FOUND
101  */
102  for (i = 0; i < LOOP; i++) {
103  key.set_size(sizeof(i));
104  key.set_data(&i);
105 
106  try {
107  record = db.find(&key);
108  }
109  catch (upscaledb::error &e) {
110  if (e.get_errno() != UPS_KEY_NOT_FOUND) {
111  std::cerr << "db::find() returned error " << e.get_string()
112  << std::endl;
113  return (-1);
114  }
115  }
116  }
117 
118  /*
119  * Done! No need to close the database handles, they are closed in their
120  * destructor.
121  */
122 
123  std::cout << "success!" << std::endl;
124  return (0);
125 }
126 
127 int
128 main(int argc, char **argv)
129 {
130  try {
131  return (run_demo());
132  }
133  catch (upscaledb::error &e) {
134  std::cerr << "run_demo() failed with unexpected error "
135  << e.get_errno() << " ('"
136  << e.get_string() << "')" << std::endl;
137  return (-1);
138  }
139 }
void * get_data() const
Definition: upscaledb.hpp:187
void create(const char *filename, uint32_t flags=0, uint32_t mode=0644, const ups_parameter_t *param=0)
Definition: upscaledb.hpp:620
void set_size(uint32_t size)
Definition: upscaledb.hpp:202
void erase(key *k, uint32_t flags=0)
Definition: upscaledb.hpp:371
ups_status_t get_errno() const
Definition: upscaledb.hpp:74
#define UPS_TYPE_UINT32
Definition: upscaledb.h:322
unsigned int uint32_t
Definition: msstdint.h:85
db create_db(uint16_t name, uint32_t flags=0, const ups_parameter_t *param=0)
Definition: upscaledb.hpp:643
#define UPS_PARAM_RECORD_SIZE
Definition: upscaledb.h:1725
void close(uint32_t flags=0)
Definition: upscaledb.hpp:691
void insert(txn *t, key *k, record *r, uint32_t flags=0)
Definition: upscaledb.hpp:358
int main(int argc, char **argv)
Definition: db6.cpp:128
const char * get_string() const
Definition: upscaledb.hpp:79
void set_size(uint16_t size)
Definition: upscaledb.hpp:130
#define LOOP
Definition: db6.cpp:26
int run_demo()
Definition: db6.cpp:29
void set_data(void *data)
Definition: upscaledb.hpp:192
record find(txn *t, key *k, uint32_t flags=0)
Definition: upscaledb.hpp:334
void close(uint32_t flags=0)
Definition: upscaledb.hpp:403
void open(const char *filename, uint32_t flags=0, const ups_parameter_t *param=0)
Definition: upscaledb.hpp:628
db open_db(uint16_t name, uint32_t flags=0, const ups_parameter_t *param=0)
Definition: upscaledb.hpp:655
void set_data(void *data)
Definition: upscaledb.hpp:120
#define UPS_PARAM_KEY_TYPE
Definition: upscaledb.h:1710
#define UPS_KEY_NOT_FOUND
Definition: upscaledb.h:361