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
|
# File 'ext/rcb_range_scan.cxx', line 105
VALUE
cb_CoreScanResult_next_item(VALUE self)
{
try {
cb_core_scan_result_data* data = nullptr;
TypedData_Get_Struct(self, cb_core_scan_result_data, &cb_core_scan_result_type, data);
std::promise<tl::expected<couchbase::core::range_scan_item, std::error_code>> promise;
auto f = promise.get_future();
data->scan_result->next([promise = std::move(promise)](couchbase::core::range_scan_item item,
std::error_code ec) mutable {
if (ec) {
return promise.set_value(tl::unexpected(ec));
}
return promise.set_value(item);
});
auto resp = cb_wait_for_future(f);
if (!resp.has_value()) {
// If the error code is range_scan_completed return nil without raising an exception (nil
// signifies that there are no more items)
if (resp.error() != couchbase::errc::key_value::range_scan_completed) {
cb_throw_error_code(resp.error(), "unable to fetch next scan item");
}
// Release ownership of scan_result unique pointer
return Qnil;
}
auto item = resp.value();
VALUE res = rb_hash_new();
rb_hash_aset(res, rb_id2sym(rb_intern("id")), cb_str_new(item.key));
if (item.body.has_value()) {
auto body = item.body.value();
rb_hash_aset(res, rb_id2sym(rb_intern("id")), cb_str_new(item.key));
rb_hash_aset(res, rb_id2sym(rb_intern("encoded")), cb_str_new(body.value));
rb_hash_aset(res, rb_id2sym(rb_intern("cas")), cb_cas_to_num(body.cas));
rb_hash_aset(res, rb_id2sym(rb_intern("flags")), UINT2NUM(body.flags));
rb_hash_aset(res, rb_id2sym(rb_intern("expiry")), UINT2NUM(body.expiry));
rb_hash_aset(res, rb_id2sym(rb_intern("id_only")), Qfalse);
} else {
rb_hash_aset(res, rb_id2sym(rb_intern("id_only")), Qtrue);
}
return res;
} catch (const std::system_error& se) {
rb_exc_raise(cb_map_error_code(
se.code(), fmt::format("failed to perform {}: {}", __func__, se.what()), false));
} catch (const ruby_exception& e) {
rb_exc_raise(e.exception_object());
}
return Qnil;
}
|