2323#include < google/protobuf/util/json_util.h>
2424
2525#include < bit>
26+ #include < chrono>
2627#include < fstream>
2728#include < iomanip>
2829#include < sstream>
@@ -240,15 +241,16 @@ static std::unordered_map<std::string_view,
240241};
241242// clang-format on
242243
243- static MetaServiceResponseStatus encode_key (const brpc::URI& uri, std::string& key) {
244+ static MetaServiceResponseStatus encode_key (const brpc::URI& uri, std::string* key,
245+ std::string* key_type = nullptr ) {
244246 MetaServiceResponseStatus status;
245247 status.set_code (MetaServiceCode::OK);
246- std::string_view key_type = http_query (uri, " key_type" );
247- auto it = param_set.find (key_type);
248+ std::string_view kt = http_query (uri, " key_type" );
249+ if (key_type != nullptr ) *key_type = kt;
250+ auto it = param_set.find (kt);
248251 if (it == param_set.end ()) {
249252 status.set_code (MetaServiceCode::INVALID_ARGUMENT);
250- status.set_msg (fmt::format (" key_type not supported: {}" ,
251- (key_type.empty () ? " (empty)" : key_type)));
253+ status.set_msg (fmt::format (" key_type not supported: {}" , (kt.empty () ? " (empty)" : kt)));
252254 return status;
253255 }
254256 auto & key_params = std::get<0 >(it->second );
@@ -264,16 +266,18 @@ static MetaServiceResponseStatus encode_key(const brpc::URI& uri, std::string& k
264266 params.emplace_back (p);
265267 }
266268 auto & key_encoding_function = std::get<1 >(it->second );
267- key = key_encoding_function (params);
269+ * key = key_encoding_function (params);
268270 return status;
269271}
270272
271273HttpResponse process_http_get_value (TxnKv* txn_kv, const brpc::URI& uri) {
272274 std::string key;
275+ std::string key_type;
273276 if (auto hex_key = http_query (uri, " key" ); !hex_key.empty ()) {
274277 key = unhex (hex_key);
278+ key_type = http_query (uri, " key_type" );
275279 } else { // Encode key from params
276- auto st = encode_key (uri, key);
280+ auto st = encode_key (uri, & key, &key_type );
277281 if (st.code () != MetaServiceCode::OK) {
278282 return http_json_reply (st);
279283 }
@@ -285,7 +289,6 @@ HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) {
285289 fmt::format (" failed to create txn, err={}" , err));
286290 }
287291
288- std::string_view key_type = http_query (uri, " key_type" );
289292 auto it = param_set.find (key_type);
290293 if (it == param_set.end ()) {
291294 return http_json_reply (MetaServiceCode::INVALID_ARGUMENT,
@@ -330,17 +333,20 @@ HttpResponse process_http_get_value(TxnKv* txn_kv, const brpc::URI& uri) {
330333}
331334
332335std::string handle_kv_output (std::string_view key, std::string_view value,
333- std::string_view original_value_json,
336+ std::string_view original_value_json, std::string_view new_value_json,
334337 std::string_view serialized_value_to_save) {
335338 std::stringstream final_output;
336- final_output << " original_value_hex=" << hex (value) << " \n "
337- << " key_hex=" << hex (key) << " \n "
339+ final_output << " key_hex=" << hex (key) << " \n "
338340 << " original_value_json=" << original_value_json << " \n "
339- << " changed_value_hex=" << hex (serialized_value_to_save) << " \n " ;
341+ << " new_value_json=" << new_value_json << " \n "
342+ << " original_value_hex=" << hex (value) << " \n "
343+ << " new_value_hex=" << hex (serialized_value_to_save) << " \n " ;
340344 std::string final_json_str = final_output.str ();
341345 LOG (INFO) << final_json_str;
342346 if (final_json_str.size () > 25000 ) {
343- std::string file_path = fmt::format (" /tmp/{}.txt" , hex (key));
347+ using namespace std ::chrono;
348+ auto ts = duration_cast<milliseconds>(system_clock::now ().time_since_epoch ()).count ();
349+ std::string file_path = fmt::format (" /tmp/{}_{}.txt" , hex (key), ts);
344350 LOG (INFO) << " write to file=" << file_path << " , key=" << hex (key)
345351 << " size=" << final_json_str.size ();
346352 try {
@@ -350,7 +356,7 @@ std::string handle_kv_output(std::string_view key, std::string_view value,
350356 kv_file.close ();
351357 }
352358 } catch (...) {
353- LOG (INFO) << " write tmp file failed. " ;
359+ LOG (INFO) << " write tmp file failed: " << file_path ;
354360 }
355361 }
356362
@@ -363,10 +369,12 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) {
363369 LOG (INFO) << " set value, body=" << body;
364370
365371 std::string key;
372+ std::string key_type;
366373 if (auto hex_key = http_query (uri, " key" ); !hex_key.empty ()) {
367374 key = unhex (hex_key);
375+ key_type = http_query (uri, " key_type" );
368376 } else { // Encode key from params
369- auto st = encode_key (uri, key);
377+ auto st = encode_key (uri, & key, &key_type );
370378 if (st.code () != MetaServiceCode::OK) {
371379 return http_json_reply (st);
372380 }
@@ -378,13 +386,13 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) {
378386 fmt::format (" failed to create txn, err={}" , err));
379387 }
380388
381- std::string_view key_type = http_query (uri, " key_type" );
382389 auto it = param_set.find (key_type);
383390 if (it == param_set.end ()) {
384391 return http_json_reply (MetaServiceCode::INVALID_ARGUMENT,
385392 fmt::format (" key_type not supported: {}" ,
386393 (key_type.empty () ? " (empty)" : key_type)));
387394 }
395+
388396 auto & json_parsing_function = std::get<3 >(it->second );
389397 std::shared_ptr<google::protobuf::Message> pb_to_save = json_parsing_function (body);
390398 if (pb_to_save == nullptr ) {
@@ -458,14 +466,15 @@ HttpResponse process_http_set_value(TxnKv* txn_kv, brpc::Controller* cntl) {
458466 LOG (WARNING) << " set_value saved, key=" << hex (key);
459467
460468 std::string final_json_str =
461- handle_kv_output (key, value.value (), original_value_json, serialized_value_to_save);
469+ handle_kv_output (key, value.value (), original_value_json, proto_to_json (*pb_to_save),
470+ serialized_value_to_save);
462471
463472 return http_text_reply (MetaServiceCode::OK, " " , final_json_str);
464473}
465474
466475HttpResponse process_http_encode_key (const brpc::URI& uri) {
467476 std::string key;
468- auto st = encode_key (uri, key);
477+ auto st = encode_key (uri, & key);
469478 if (st.code () != MetaServiceCode::OK) {
470479 return http_json_reply (st);
471480 }
0 commit comments