Repository navigation
Expand file tree
/
Copy pathbackend_msgpack.cpp
More file actions
68 lines (60 loc) · 2.41 KB
/
Copy pathbackend_msgpack.cpp
File metadata and controls
68 lines (60 loc) · 2.41 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
#include "bench.h"
#include <msgpack.hpp>
#include <map>
#include <string>
// msgpack-cxx has no in-memory document: packing reads directly from the
// prepared KV pool, unpacking materializes the wire bytes into a map.
// (Noted in methodology: the "add" phase here is trivial by design.)
class MsgPackBackend : public Backend {
public:
const char* name() const override { return "msgpack-cxx"; }
void reset() override {}
void populate(size_t params_count, bool with_attachment, size_t attachment_bytes) override {
if (m_pool.size() != params_count)
m_pool = make_kvs(params_count);
m_with_attachment = with_attachment;
if (with_attachment && m_blob.size() != attachment_bytes)
m_blob.assign(attachment_bytes, 0xAB);
}
void serialize(std::vector<uint8_t>& out) override {
m_sbuf.clear();
msgpack::packer<msgpack::sbuffer> pk(m_sbuf);
pk.pack_map(m_with_attachment ? 2u : 1u);
pk.pack(std::string("params"));
pk.pack_map(static_cast<uint32_t>(m_pool.size()));
for (const KV& kv : m_pool) {
pk.pack(kv.key);
switch (kv.type) {
case KV::Type::Double: pk.pack(kv.d); break;
case KV::Type::Int: pk.pack(kv.i); break;
case KV::Type::Bool: pk.pack(kv.b); break;
case KV::Type::Str: pk.pack(kv.s); break;
}
}
if (m_with_attachment) {
pk.pack(std::string("attachment"));
pk.pack_bin(m_blob.size());
pk.pack_bin_body(reinterpret_cast<const char*>(m_blob.data()), m_blob.size());
}
out.assign(m_sbuf.data(), m_sbuf.data() + m_sbuf.size());
}
void deserialize(const uint8_t* data, size_t size) override {
m_root.clear();
msgpack::object_handle oh = msgpack::unpack(reinterpret_cast<const char*>(data), size);
const msgpack::object obj = oh.get();
const msgpack::object_kv* p = obj.via.map.ptr;
for (uint32_t i = 0; i < obj.via.map.size; ++i) {
std::string key = p[i].key.as<std::string>();
m_root.emplace(std::move(key), p[i].val);
}
}
private:
msgpack::sbuffer m_sbuf;
std::vector<KV> m_pool;
std::vector<uint8_t> m_blob;
bool m_with_attachment = false;
std::map<std::string, msgpack::object> m_root;
};
Backend* make_msgpack_backend() {
return new MsgPackBackend();
}