-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathAsyncHelloWorld.cpp
More file actions
145 lines (125 loc) · 4.69 KB
/
AsyncHelloWorld.cpp
File metadata and controls
145 lines (125 loc) · 4.69 KB
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
/* This file is part of VoltDB.
* Copyright (C) 2008-2016 VoltDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#if !defined (__STDC_CONSTANT_MACROS)
#define __STDC_CONSTANT_MACROS
#endif
#if !defined (__STDC_LIMIT_MACROS)
#define __STDC_LIMIT_MACROS
#endif
#include <vector>
#include <boost/shared_ptr.hpp>
#include "Client.h"
#include "Table.h"
#include "TableIterator.h"
#include "Row.hpp"
#include "WireType.h"
#include "Parameter.hpp"
#include "ParameterSet.hpp"
#include "ProcedureCallback.hpp"
#include "ClientConfig.h"
/*
* A callback that counts the number of times that it is invoked and returns true
* when the counter reaches zero to instruct the client library to break out of the event loop.
*/
class CountingCallback : public voltdb::ProcedureCallback {
public:
CountingCallback(int32_t count) : m_count(count) {}
bool callback(voltdb::InvocationResponse response) throw (voltdb::Exception) {
m_count--;
//Print the error response if there was a problem
if (response.failure()) {
std::cout << response.toString();
}
//If the callback has been invoked count times, return true to break event loop
if (m_count == 0) {
return true;
} else {
return false;
}
}
private:
int32_t m_count;
};
/*
* A callback that prints the response it receives and then requests the event loop
* break
*/
class PrintingCallback : public voltdb::ProcedureCallback {
public:
bool callback(voltdb::InvocationResponse response) throw (voltdb::Exception) {
std::cout << response.toString();
return true;
}
};
int main(int argc, char **argv) {
/*
* Instantiate a client and connect to the database.
* SHA-256 can be used as of VoltDB5.2 by specifying voltdb::HASH_SHA256
*/
voltdb::ClientConfig config("myusername", "mypassword", voltdb::HASH_SHA1);
voltdb::Client client = voltdb::Client::create(config);
client.createConnection("localhost");
/*
* Describe the stored procedure to be invoked
*/
std::vector<voltdb::Parameter> parameterTypes(3);
parameterTypes[0] = voltdb::Parameter(voltdb::WIRE_TYPE_STRING);
parameterTypes[1] = voltdb::Parameter(voltdb::WIRE_TYPE_STRING);
parameterTypes[2] = voltdb::Parameter(voltdb::WIRE_TYPE_STRING);
voltdb::Procedure procedure("Insert", parameterTypes);
boost::shared_ptr<CountingCallback> callback(new CountingCallback(5));
/*
* Load the database.
*/
voltdb::ParameterSet* params = procedure.params();
params->addString("English").addString("Hello").addString("World");
client.invoke(procedure, callback);
params->addString("French").addString("Bonjour").addString("Monde");
client.invoke(procedure, callback);
params->addString("Spanish").addString("Hola").addString("Mundo");
client.invoke(procedure, callback);
params->addString("Danish").addString("Hej").addString("Verden");
client.invoke(procedure, callback);
params->addString("Italian").addString("Ciao").addString("Mondo");
client.invoke(procedure, callback);
/*
* Run the client event loop to poll the network and invoke callbacks.
* The event loop will break on an error or when a callback returns true
*/
client.run();
/*
* Describe procedure to retrieve message
*/
parameterTypes.resize( 1, voltdb::Parameter(voltdb::WIRE_TYPE_STRING));
voltdb::Procedure selectProc("Select", parameterTypes);
/*
* Retrieve the message
*/
selectProc.params()->addString("Spanish");
client.invoke(selectProc, boost::shared_ptr<PrintingCallback>(new PrintingCallback()));
/*
* Invoke event loop
*/
client.run();
return 0;
}