-
Notifications
You must be signed in to change notification settings - Fork 57
/
vedis_intro.c
205 lines (184 loc) · 5.84 KB
/
vedis_intro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* Compile this file together with the Vedis datastore engine source code
* to generate the executable. For example:
* gcc -W -Wall -O6 vedis_intro.c vedis.c -o vedis_intro
*/
/*
* This simple program is a quick introduction on how to embed and start
* experimenting with Vedis without having to do a lot of tedious
* reading and configuration.
*
* Introduction to the Command Execution Interfaces (CEI):
*
* Structured data storage is presented to clients via the command execution interface.
* Basically, you execute one or more commands ala Redis (i.e. SET key value; HSET ds key value, GET key)
* via [vedis_exec()] and you extract the execution result (The return value of the command) via
* [vedis_exec_result()]. You could also register foreign commands via [vedis_register_commmand()].
* You may also be tempted by installing a command consumer callback in case you
* want to invoke multiple commands (pipelined request) in a single [vedis_exec()] call rather
* than calling [vedis_exec_result()] which return only the execution result of the last executed
* command.
*
* For an introduction to the Vedis C/C++ interface, please refer to:
* http://vedis.symisc.net/api_intro.html
* For the full C/C++ API reference guide, please refer to:
* http://vedis.symisc.net/c_api.html
* Vedis in 5 Minutes or Less:
* http://vedis.symisc.net/intro.html
* Built-in Vedis Commands:
* http://vedis.symisc.net/commands.html
*/
/* $SymiscID: vedis_intro.c v1.0 Win7 2013-09-12 02:05 stable <[email protected]> $ */
/*
* Make sure you have the latest release of Vedis from:
* http://vedis.symisc.net/downloads.html
*/
#include <stdio.h> /* puts() */
#include <stdlib.h> /* exit() */
/* Make sure this header file is available.*/
#include "vedis.h"
/*
* Banner.
*/
static const char zBanner[] = {
"============================================================\n"
"Vedis Command Execution Intro \n"
" http://vedis.symisc.net/\n"
"============================================================\n"
};
/*
* Extract the datastore error log and exit.
*/
static void Fatal(vedis *pStore,const char *zMsg)
{
if( pStore ){
const char *zErr;
int iLen = 0; /* Stupid cc warning */
/* Extract the datastore error log */
vedis_config(pStore,VEDIS_CONFIG_ERR_LOG,&zErr,&iLen);
if( iLen > 0 ){
/* Output the error log */
puts(zErr); /* Always null terminated */
}
}else{
if( zMsg ){
puts(zMsg);
}
}
/* Manually shutdown the library */
vedis_lib_shutdown();
/* Exit immediately */
exit(0);
}
int main(int argc,char *argv[])
{
vedis *pStore; /* Vedis handle */
vedis_value *pResult; /* Return value of the last executed command */
int rc;
puts(zBanner);
/* Create our datastore */
rc = vedis_open(&pStore,argc > 1 ? argv[1] /* On-disk DB */ : ":mem:" /* In-mem DB */);
if( rc != VEDIS_OK ){
Fatal(0,"Out of memory");
}
/* Execute the simplest command */
rc = vedis_exec(pStore,"SET test 'Hello World'",-1);
if( rc != VEDIS_OK ){
/* Seriously? */
Fatal(pStore,0);
}
/* Another simple command (Multiple set) */
rc = vedis_exec(pStore,"MSET username james age 27 mail [email protected]",-1);
if( rc != VEDIS_OK ){
Fatal(pStore,0);
}
/* A configuration hashtable */
rc = vedis_exec(pStore,"HSET config path '/usr/local/etc'",-1);
if( rc != VEDIS_OK ){
Fatal(pStore,0);
}
/* A quite complex command (Multiple hash set) using foreign data */
rc = vedis_exec_fmt(pStore,
"HMSET config pid %d user %s os %s scm %s",
1024 /* pid */,
"dean", /* user */
"FreeBSD", /* OS */
"Git" /* SCM */
);
if( rc != VEDIS_OK ){
Fatal(pStore,0);
}
puts("Insertion..OK");
/* Fetch some data */
vedis_exec(pStore,"GET test",-1);
/* Extract the return value of the last executed command (i.e. GET test) " */
rc = vedis_exec_result(pStore,&pResult);
if( rc != VEDIS_OK ){
/* Seriously? */
Fatal(pStore,0);
}else{
const char *zResponse;
/* Cast the vedis object to a string */
zResponse = vedis_value_to_string(pResult,0);
/* Output */
printf(" test ==> %s\n",zResponse); /* test ==> 'Hello world' */
}
vedis_exec(pStore,"GET mail",-1);
/* 'GET mail' return value */
rc = vedis_exec_result(pStore,&pResult);
if( rc != VEDIS_OK ){
Fatal(pStore,0);
}else{
const char *zResponse;
/* Cast the vedis object to a string */
zResponse = vedis_value_to_string(pResult,0);
/* Output */
printf(" mail ==> %s\n",zResponse); /* Should be '[email protected]' */
}
/*
* A command which return multiple value in array.
*/
vedis_exec(pStore,"MGET username age",-1); /* james 27*/
vedis_exec_result(pStore,&pResult);
if( vedis_value_is_array(pResult) ){
/* Iterate over the elements of the returned array */
vedis_value *pEntry;
puts("Array entries:");
while((pEntry = vedis_array_next_elem(pResult)) != 0 ){
const char *zEntry;
/* Cast to string and output */
zEntry = vedis_value_to_string(pEntry,0);
/* Output */
printf("\t%s\n",zEntry);
}
}
/* Extract hashtable data (i.e. pid field) */
vedis_exec(pStore,"HGET config pid",-1); /* 1024 */
vedis_exec_result(pStore,&pResult);
{
int pid;
/* Cast to integer */
pid = vedis_value_to_int(pResult);
/* Output */
printf("pid ==> %d\n",pid); /* Should be 1024 */
}
/* All hashtable data */
vedis_exec(pStore,"HVALS config",-1); /* All 'config' values */
vedis_exec_result(pStore,&pResult);
if( vedis_value_is_array(pResult) ){
/* Iterate over the elements of the returned array */
vedis_value *pEntry;
puts("Array entries:");
while((pEntry = vedis_array_next_elem(pResult)) != 0 ){
const char *zEntry;
/* Cast to string and output */
zEntry = vedis_value_to_string(pEntry,0);
/* Output */
printf("\t%s\n",zEntry);
}
}
puts("All done!");
/* Auto-commit the transaction and close our datastore */
vedis_close(pStore);
return 0;
}