forked from datastax/cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuture.cpp
200 lines (167 loc) · 5.81 KB
/
future.cpp
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
/*
Copyright (c) DataStax, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "future.hpp"
#include "external.hpp"
#include "prepared.hpp"
#include "request_handler.hpp"
#include "result_response.hpp"
#include "scoped_ptr.hpp"
extern "C" {
void cass_future_free(CassFuture* future) {
// Futures can be deleted without being waited on
// because they'll be cleaned up by the notifying thread
future->dec_ref();
}
CassError cass_future_set_callback(CassFuture* future,
CassFutureCallback callback,
void* data) {
if (!future->set_callback(callback, data)) {
return CASS_ERROR_LIB_CALLBACK_ALREADY_SET;
}
return CASS_OK;
}
cass_bool_t cass_future_ready(CassFuture* future) {
return static_cast<cass_bool_t>(future->ready());
}
void cass_future_wait(CassFuture* future) {
future->wait();
}
cass_bool_t cass_future_wait_timed(CassFuture* future, cass_duration_t wait_us) {
return static_cast<cass_bool_t>(future->wait_for(wait_us));
}
const CassResult* cass_future_get_result(CassFuture* future) {
if (future->type() != cass::CASS_FUTURE_TYPE_RESPONSE) {
return NULL;
}
cass::Response::Ptr response(
static_cast<cass::ResponseFuture*>(future->from())->response());
if (!response || response->opcode() == CQL_OPCODE_ERROR) {
return NULL;
}
response->inc_ref();
return CassResult::to(
static_cast<cass::ResultResponse*>(response.get()));
}
const CassPrepared* cass_future_get_prepared(CassFuture* future) {
if (future->type() != cass::CASS_FUTURE_TYPE_RESPONSE) {
return NULL;
}
cass::ResponseFuture* response_future =
static_cast<cass::ResponseFuture*>(future->from());
cass::SharedRefPtr<cass::ResultResponse> result(response_future->response());
if (!result || result->kind() != CASS_RESULT_KIND_PREPARED) {
return NULL;
}
cass::Prepared* prepared = new cass::Prepared(result,
response_future->prepare_request,
*response_future->schema_metadata);
prepared->inc_ref();
return CassPrepared::to(prepared);
}
const CassErrorResult* cass_future_get_error_result(CassFuture* future) {
if (future->type() != cass::CASS_FUTURE_TYPE_RESPONSE) {
return NULL;
}
cass::Response::Ptr response(
static_cast<cass::ResponseFuture*>(future->from())->response());
if (!response || response->opcode() != CQL_OPCODE_ERROR) {
return NULL;
}
response->inc_ref();
return CassErrorResult::to(
static_cast<cass::ErrorResponse*>(response.get()));
}
CassError cass_future_error_code(CassFuture* future) {
const cass::Future::Error* error = future->error();
if (error != NULL) {
return error->code;
} else {
return CASS_OK;
}
}
void cass_future_error_message(CassFuture* future,
const char** message,
size_t* message_length) {
const cass::Future::Error* error = future->error();
if (error != NULL) {
const std::string& m = error->message;
*message = m.data();
*message_length = m.length();
} else {
*message = "";
*message_length = 0;
}
}
size_t cass_future_custom_payload_item_count(CassFuture* future) {
if (future->type() != cass::CASS_FUTURE_TYPE_RESPONSE) {
return 0;
}
cass::Response::Ptr response(
static_cast<cass::ResponseFuture*>(future->from())->response());
if (!response) return 0;
return response->custom_payload().size();
}
CassError cass_future_custom_payload_item(CassFuture* future,
size_t index,
const char** name,
size_t* name_length,
const cass_byte_t** value,
size_t* value_size) {
if (future->type() != cass::CASS_FUTURE_TYPE_RESPONSE) {
return CASS_ERROR_LIB_INVALID_FUTURE_TYPE;
}
cass::Response::Ptr response(
static_cast<cass::ResponseFuture*>(future->from())->response());
if (!response) return CASS_ERROR_LIB_NO_CUSTOM_PAYLOAD;
const cass::Response::CustomPayloadVec& custom_payload =
response->custom_payload();
if (index >= custom_payload.size()) {
return CASS_ERROR_LIB_INDEX_OUT_OF_BOUNDS;
}
const cass::Response::CustomPayloadItem& item = custom_payload[index];
*name = item.name.data();
*name_length = item.name.size();
*value = reinterpret_cast<const cass_byte_t*>(item.value.data());
*value_size = item.value.size();
return CASS_OK;
}
} // extern "C"
namespace cass {
bool Future::set_callback(Future::Callback callback, void* data) {
ScopedMutex lock(&mutex_);
if (callback_) {
return false; // Callback is already set
}
callback_ = callback;
data_ = data;
if (is_set_) {
// Run the callback if the future is already set
lock.unlock();
callback(CassFuture::to(this), data);
}
return true;
}
void Future::internal_set(ScopedMutex& lock) {
is_set_ = true;
if (callback_) {
Callback callback = callback_;
void* data = data_;
lock.unlock();
callback(CassFuture::to(this), data);
lock.lock();
}
// Broadcast after we've run the callback so that threads waiting
// on this future see the side effects of the callback.
uv_cond_broadcast(&cond_);
}
} // namespace cass