Skip to content

Commit

Permalink
Client: rewrite response decoding logic with new decoder
Browse files Browse the repository at this point in the history
The commit rewrites response decoding with the new decoder.

Public structure `Data` is reworked. Now you need to call `data.decode`
with tuple format passed as template parameters - the method will return
parsed tuples in std::vector.
  • Loading branch information
drewdzzz committed Nov 24, 2023
1 parent 004d3e8 commit 32875ae
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 562 deletions.
27 changes: 12 additions & 15 deletions examples/Simple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,15 @@ using Net_t = LibevNetProvider<Buf_t, DefaultStream>;
//doclabel16-1
template <class BUFFER>
std::vector<UserTuple>
decodeUserTuple(BUFFER &buf, Data<BUFFER> &data)
decodeUserTuple(Data<BUFFER> &data)
{
auto tuples = data.template decode<std::tuple<uint64_t, std::string, double>>();
std::vector<UserTuple> results;
for(auto& t: data.tuples) {
for(auto& t: tuples) {
UserTuple tuple;
mpp::Dec dec(buf);
dec.SetPosition(t.begin);
dec.SetReader(false, UserTupleReader<BUFFER>{dec, tuple});
mpp::ReadResult_t res = dec.Read();
assert(res == mpp::READ_SUCCESS);
(void) res;
tuple.field1 = std::get<0>(t);
tuple.field2 = std::get<1>(t);
tuple.field3 = std::get<2>(t);
results.push_back(tuple);
}
return results;
Expand All @@ -80,10 +78,10 @@ decodeUserTuple(BUFFER &buf, Data<BUFFER> &data)

template<class BUFFER>
void
printResponse(Connection<BUFFER, Net_t> &conn, Response<BUFFER> &response)
printResponse(Response<BUFFER> &response)
{
if (response.body.error_stack != std::nullopt) {
Error err = (*response.body.error_stack).error;
Error err = (*response.body.error_stack)[0];
std::cout << "RESPONSE ERROR: msg=" << err.msg <<
" line=" << err.file << " file=" << err.file <<
" errno=" << err.saved_errno <<
Expand All @@ -92,12 +90,11 @@ printResponse(Connection<BUFFER, Net_t> &conn, Response<BUFFER> &response)
}
if (response.body.data != std::nullopt) {
Data<BUFFER>& data = *response.body.data;
if (data.tuples.empty()) {
std::vector<UserTuple> tuples = decodeUserTuple(data);
if (tuples.empty()) {
std::cout << "Empty result" << std::endl;
return;
}
std::vector<UserTuple> tuples =
decodeUserTuple(conn.getInBuf(), data);
for (auto const& t : tuples) {
std::cout << t << std::endl;
}
Expand Down Expand Up @@ -204,7 +201,7 @@ main()
* rely on response code storing in the header or check
* Response->body.data and Response->body.error_stack members.
*/
printResponse<Buf_t>(conn, *response);
printResponse<Buf_t>(*response);
//doclabel11-2
/* Let's wait for both futures at once. */
std::vector<rid_t> futures(2);
Expand All @@ -216,7 +213,7 @@ main()
assert(conn.futureIsReady(futures[i]));
response = conn.getResponse(futures[i]);
assert(response != std::nullopt);
printResponse<Buf_t>(conn, *response);
printResponse<Buf_t>(*response);
}
//doclabel11-3
/* Let's create another connection. */
Expand Down
28 changes: 10 additions & 18 deletions src/Client/ResponseDecoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ static constexpr size_t MP_RESPONSE_SIZE = 5;
template<class BUFFER>
class ResponseDecoder {
public:
ResponseDecoder(BUFFER &buf) : m_Dec(buf) {};
ResponseDecoder(BUFFER &buf) : it(buf.begin()) {};
~ResponseDecoder() { };
ResponseDecoder() = delete;
ResponseDecoder(const ResponseDecoder& decoder) = delete;
Expand All @@ -64,18 +64,17 @@ class ResponseDecoder {
private:
int decodeHeader(Header &header);
int decodeBody(Body<BUFFER> &body);
mpp::Dec<BUFFER> m_Dec;
iterator_t<BUFFER> it;
};

template<class BUFFER>
int
ResponseDecoder<BUFFER>::decodeResponseSize()
{
int size = -1;
m_Dec.SetReader(false, mpp::SimpleReader<BUFFER, mpp::MP_INT, int>{size});
mpp::ReadResult_t res = m_Dec.Read();
bool ok = mpp::decode(it, size);
//TODO: raise more detailed error
if (res != mpp::READ_SUCCESS)
if (!ok)
return -1;
return size;
}
Expand All @@ -84,24 +83,17 @@ template<class BUFFER>
int
ResponseDecoder<BUFFER>::decodeHeader(Header &header)
{
m_Dec.SetReader(false, HeaderReader{m_Dec, header});
mpp::ReadResult_t res = m_Dec.Read();
if (res != mpp::READ_SUCCESS)
return -1;
return 0;
bool ok = mpp::decode(it, header);
return ok ? 0 : -1;
}

template<class BUFFER>
int
ResponseDecoder<BUFFER>::decodeBody(Body<BUFFER> &body)
{
m_Dec.SetReader(false, BodyReader{m_Dec, body});
mpp::ReadResult_t res = m_Dec.Read();
if (res != mpp::READ_SUCCESS)
return -1;
if (body.data != std::nullopt)
body.data->end = m_Dec.getPosition();
return 0;

bool ok = mpp::decode(it, body);
return ok ? 0 : -1;
}

template<class BUFFER>
Expand All @@ -123,7 +115,7 @@ template<class BUFFER>
void
ResponseDecoder<BUFFER>::reset(iterator_t<BUFFER> &itr)
{
m_Dec.SetPosition(itr);
it = itr;
}

static inline uint32_t
Expand Down
Loading

0 comments on commit 32875ae

Please sign in to comment.