upscaledb  2.2.1
uqi1.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 
22 #include <stdio.h>
23 #include <string.h>
24 #include <stdlib.h> /* for exit() */
25 
26 #include <ups/upscaledb.h>
27 #include <ups/upscaledb_uqi.h>
28 
29 #define DATABASE_NAME 1
30 
31 void
32 error(const char *foo, ups_status_t st)
33 {
34  printf("%s() returned error %d: %s\n", foo, st, ups_strerror(st));
35  exit(-1);
36 }
37 
38 int
39 main(int argc, char **argv)
40 {
41  ups_status_t st; /* status variable */
42  ups_env_t *env; /* upscaledb environment object */
43  ups_db_t *db; /* upscaledb database object */
44  ups_parameter_t params[] = { /* parameters for ups_env_create_db */
46  {UPS_PARAM_RECORD_TYPE, UPS_TYPE_UINT32},
47  {0, 0}
48  };
49 
50  /* First create a new upscaledb Environment */
51  st = ups_env_create(&env, "test.db", 0, 0664, 0);
52  if (st != UPS_SUCCESS)
53  error("ups_env_create", st);
54 
55  /* And in this Environment we create a new Database for uint32-keys
56  * and uint32-records. */
58  &params[0]);
59  if (st != UPS_SUCCESS)
60  error("ups_env_create_db", st);
61 
62  /*
63  * now insert a bunch of key/value pairs. The IDs are automatically
64  * assigned, the record is a pseudo-random value
65  */
66  for (int i = 0; i < 10000; i++) {
67  uint32_t value = 50 + i % 30;
68  ups_key_t key = {0};
69  ups_record_t record = ups_make_record(&value, sizeof(value));
70 
71  st = ups_db_insert(db, 0, &key, &record, 0);
72  if (st != UPS_SUCCESS)
73  error("ups_db_insert", st);
74  }
75 
76  uqi_result_t *result;
77  ups_key_t key = {0};
78  ups_record_t record = {0};
79 
80  /* now calculate the maximum record value */
81  st = uqi_select(env, "MAX($record) FROM DATABASE 1", &result);
82  if (st != UPS_SUCCESS)
83  error("uqi_select", st);
84  uqi_result_get_key(result, 0, &key);
85  uqi_result_get_record(result, 0, &record);
86  printf("MAX($record): key %2u, record %u\n",
87  *(uint32_t *)key.data, *(uint32_t *)record.data);
88  uqi_result_close(result);
89 
90  /* ... and the minimum record value */
91  st = uqi_select(env, "MIN($record) FROM DATABASE 1", &result);
92  if (st != UPS_SUCCESS)
93  error("uqi_select", st);
94  uqi_result_get_key(result, 0, &key);
95  uqi_result_get_record(result, 0, &record);
96  printf("MIN($record): key %2u, record %u\n",
97  *(uint32_t *)key.data, *(uint32_t *)record.data);
98  uqi_result_close(result);
99 
100  /* ... and the average record value */
101  st = uqi_select(env, "AVERAGE($record) FROM DATABASE 1", &result);
102  if (st != UPS_SUCCESS)
103  error("uqi_select", st);
104  uqi_result_get_record(result, 0, &record);
105  printf("AVERAGE($record): %f\n", *(double *)record.data);
106  uqi_result_close(result);
107 
108  /* we're done! close the handles. UPS_AUTO_CLEANUP will also close the
109  * 'db' handle */
110  st = ups_env_close(env, UPS_AUTO_CLEANUP);
111  if (st != UPS_SUCCESS)
112  error("ups_env_close", st);
113 
114  printf("success!\n");
115  return 0;
116 }
117 
#define ups_make_record(PTR, SIZE)
Definition: upscaledb.h:225
void error(const char *foo, ups_status_t st)
Definition: uqi1.c:32
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_insert(ups_db_t *db, ups_txn_t *txn, ups_key_t *key, ups_record_t *record, uint32_t flags)
UPS_EXPORT void UPS_CALLCONV uqi_result_get_key(uqi_result_t *result, uint32_t row, ups_key_t *key)
Include file for upscaledb Query Interface.
#define UPS_TYPE_UINT32
Definition: upscaledb.h:322
UPS_EXPORT ups_status_t UPS_CALLCONV uqi_select(ups_env_t *env, const char *query, uqi_result_t **result)
unsigned int uint32_t
Definition: msstdint.h:85
UPS_EXPORT void UPS_CALLCONV uqi_result_get_record(uqi_result_t *result, uint32_t row, ups_record_t *record)
#define DATABASE_NAME
Definition: uqi1.c:29
#define UPS_PARAM_RECORD_TYPE
Definition: upscaledb.h:1739
UPS_EXPORT void UPS_CALLCONV uqi_result_close(uqi_result_t *result)
Include file for upscaledb embedded database.
int main(int argc, char **argv)
Definition: uqi1.c:39
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_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
struct uqi_result_t uqi_result_t
Definition: upscaledb_uqi.h:41
#define UPS_RECORD_NUMBER32
Definition: upscaledb.h:1301
void * data
Definition: upscaledb.h:207
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 UPS_PARAM_KEY_TYPE
Definition: upscaledb.h:1710
struct ups_env_t ups_env_t
Definition: upscaledb.h:165