Skip to content

Commit 08f5eb9

Browse files
committed
MB-47318: [BP] Add a make_wire_encoded_string factory method in xattr utils
Add code that can generate a xattr encoding value into xattr utils so it is easily accessible to all modules. Change-Id: I5743d0afd0308b57edb0f78cc7789e18a28710e3 Reviewed-on: https://review.couchbase.org/c/kv_engine/+/165018 Tested-by: Build Bot <[email protected]> Reviewed-by: Dave Rigby <[email protected]> Well-Formed: Restriction Checker
1 parent e6fe669 commit 08f5eb9

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

include/xattr/utils.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <xattr/visibility.h>
2121

2222
#include <string>
23+
#include <unordered_map>
2324

2425
namespace cb {
2526
/**
@@ -119,5 +120,18 @@ size_t get_system_xattr_size(uint8_t datatype, const cb::const_char_buffer doc);
119120
*/
120121
XATTR_PUBLIC_API
121122
size_t get_body_size(uint8_t datatype, cb::const_char_buffer value);
123+
124+
/**
125+
* Make a wire encoded XATTR value in a std::string. This will encode the given
126+
* 'body' and xattrs as per the mcbp protocol
127+
*
128+
* @param body the 'body' of the document
129+
* @param xattMap map of XATTR key -> value pairs to encode in the output
130+
* document/string.
131+
*/
132+
XATTR_PUBLIC_API
133+
std::string make_wire_encoded_string(
134+
const std::string& body,
135+
const std::unordered_map<std::string, std::string>& xattrMap);
122136
}
123137
}

xattr/utils.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,5 +181,23 @@ size_t get_body_size(uint8_t datatype, cb::const_char_buffer value) {
181181

182182
return value.size() - get_body_offset(value);
183183
}
184+
185+
std::string make_wire_encoded_string(
186+
const std::string& body,
187+
const std::unordered_map<std::string, std::string>& xattrSet) {
188+
Blob xattrs;
189+
for (auto itr = xattrSet.begin(); itr != xattrSet.end(); ++itr) {
190+
xattrs.set(itr->first, itr->second);
191+
}
192+
auto data = xattrs.finalize();
193+
std::string encoded{data.data(), data.size()};
194+
if (!cb::xattr::validate(encoded)) {
195+
throw std::logic_error(
196+
"cb::xattr::make_wire_encoded_string Invalid xattr encoding");
197+
}
198+
encoded.append(body);
199+
return encoded;
200+
}
201+
184202
}
185203
}

0 commit comments

Comments
 (0)