Skip to content

Commit 620f79b

Browse files
BenHuddlestontrondn
authored andcommitted
MB-30041: Migrate client_connection_map to nlohmann::json
Change-Id: I17beac8275c29c6a6c250465c744735ba95d524e Reviewed-on: http://review.couchbase.org/104254 Tested-by: Build Bot <[email protected]> Reviewed-by: Trond Norbye <[email protected]>
1 parent 135dad6 commit 620f79b

File tree

8 files changed

+55
-86
lines changed

8 files changed

+55
-86
lines changed

protocol/connection/client_connection_map.cc

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
#include "client_connection_map.h"
1818

19+
#include <nlohmann/json.hpp>
20+
#include <utilities/json_utilities.h>
21+
1922
/////////////////////////////////////////////////////////////////////////
2023
// Implementation of the ConnectionMap class
2124
/////////////////////////////////////////////////////////////////////////
@@ -41,63 +44,38 @@ bool ConnectionMap::contains(bool ssl, sa_family_t family) {
4144
}
4245
}
4346

44-
void ConnectionMap::initialize(cJSON* ports) {
47+
void ConnectionMap::initialize(const nlohmann::json& ports) {
4548
invalidate();
46-
cJSON* array = cJSON_GetObjectItem(ports, "ports");
47-
if (array == nullptr) {
48-
std::string msg("ports not found in portnumber file: ");
49-
msg.append(to_string(ports, false));
50-
throw std::runtime_error(msg);
49+
auto array = ports.find("ports");
50+
if (array == ports.end()) {
51+
throw std::runtime_error("ports not found in portnumber file: " +
52+
ports.dump());
5153
}
5254

53-
auto numEntries = cJSON_GetArraySize(array);
5455
sa_family_t family;
55-
for (int ii = 0; ii < numEntries; ++ii) {
56-
auto obj = cJSON_GetArrayItem(array, ii);
57-
auto fam = cJSON_GetObjectItem(obj, "family");
58-
if (strcmp(fam->valuestring, "AF_INET") == 0) {
56+
for (const auto obj : *array) {
57+
// auto fam = port.find("family");
58+
auto fam = cb::jsonGet<std::string>(obj, "family");
59+
if (fam == "AF_INET") {
5960
family = AF_INET;
60-
} else if (strcmp(fam->valuestring, "AF_INET6") == 0) {
61-
family = AF_INET6;
6261
} else {
63-
std::string msg("Unsupported network family: ");
64-
msg.append(to_string(obj, false));
65-
throw std::runtime_error(msg);
66-
}
67-
68-
auto ssl = cJSON_GetObjectItem(obj, "ssl");
69-
if (ssl == nullptr) {
70-
std::string msg("ssl missing for entry: ");
71-
msg.append(to_string(obj, false));
72-
throw std::runtime_error(msg);
73-
}
74-
75-
auto port = cJSON_GetObjectItem(obj, "port");
76-
if (port == nullptr) {
77-
std::string msg("port missing for entry: ");
78-
msg.append(to_string(obj, false));
79-
throw std::runtime_error(msg);
80-
}
81-
82-
auto protocol = cJSON_GetObjectItem(obj, "protocol");
83-
if (protocol == nullptr) {
84-
std::string msg("protocol missing for entry: ");
85-
msg.append(to_string(obj, false));
86-
throw std::runtime_error(msg);
62+
family = AF_INET6;
8763
}
8864

89-
auto portval = static_cast<in_port_t>(port->valueint);
90-
bool useSsl = ssl->type == cJSON_True ? true : false;
65+
auto ssl = cb::jsonGet<bool>(obj, "ssl");
66+
auto port = cb::jsonGet<size_t>(obj, "port");
67+
auto protocol = cb::jsonGet<std::string>(obj, "protocol");
9168

9269
MemcachedConnection* connection;
93-
if (strcmp(protocol->valuestring, "memcached") != 0) {
70+
if (protocol != "memcached") {
9471
throw std::logic_error(
9572
"ConnectionMap::initialize: Invalid value passed for "
9673
"protocol: " +
97-
std::string(protocol->valuestring));
74+
std::string(protocol));
9875
}
9976

100-
connection = new MemcachedConnection("", portval, family, useSsl);
77+
auto portVal = static_cast<in_port_t>(port);
78+
connection = new MemcachedConnection("", portVal, family, ssl);
10179
connections.push_back(std::unique_ptr<MemcachedConnection>{connection});
10280
}
10381
}

protocol/connection/client_connection_map.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
#pragma once
1818

1919
#include "client_connection.h"
20-
#include <cJSON_utils.h>
20+
21+
#include <nlohmann/json_fwd.hpp>
2122

2223
class ConnectionMap {
2324
public:
2425
/**
2526
* Initialize the connection map with connections matching the ports
2627
* opened from Memcached
2728
*/
28-
void initialize(cJSON* ports);
29+
void initialize(const nlohmann::json& ports);
2930

3031
/**
3132
* Invalidate all of the connections

tests/testapp/testapp.cc

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
#include <include/memcached/protocol_binary.h>
2525
#include <nlohmann/json.hpp>
26+
#include <utilities/json_utilities.h>
2627
#include <atomic>
2728
#include <csignal>
2829
#include <thread>
@@ -458,22 +459,6 @@ void write_config_to_file(const std::string& config, const std::string& fname) {
458459
}
459460
}
460461

461-
/**
462-
* Load and parse the content of a file into a cJSON array
463-
*
464-
* @param file the name of the file
465-
* @return the decoded cJSON representation
466-
* @throw a string if something goes wrong
467-
*/
468-
static unique_cJSON_ptr loadJsonFile(const std::string& file) {
469-
unique_cJSON_ptr ret(cJSON_Parse(cb::io::loadFile(file).c_str()));
470-
if (ret.get() == nullptr) {
471-
throw std::logic_error("Failed to parse: " + file);
472-
}
473-
474-
return ret;
475-
}
476-
477462
void TestappTest::verify_server_running() {
478463
if (embedded_memcached_server) {
479464
// we don't monitor this thread...
@@ -551,43 +536,37 @@ void TestappTest::parse_portnumber_file(in_port_t& port_out,
551536
port_out = (in_port_t)-1;
552537
ssl_port_out = (in_port_t)-1;
553538

554-
unique_cJSON_ptr portnumbers;
555-
portnumbers = loadJsonFile(portnumber_file);
556-
ASSERT_NE(nullptr, portnumbers);
557-
connectionMap.initialize(portnumbers.get());
558-
559-
cJSON* array = cJSON_GetObjectItem(portnumbers.get(), "ports");
560-
ASSERT_NE(nullptr, array) << "ports not found in portnumber file";
539+
// We'll throw here if anything goes wrong
540+
const nlohmann::json portnumbers =
541+
nlohmann::json::parse(cb::io::loadFile(portnumber_file));
561542

562-
auto numEntries = cJSON_GetArraySize(array);
563-
for (int ii = 0; ii < numEntries; ++ii) {
564-
auto obj = cJSON_GetArrayItem(array, ii);
543+
connectionMap.initialize(portnumbers);
565544

566-
auto protocol = cJSON_GetObjectItem(obj, "protocol");
567-
if (strcmp(protocol->valuestring, "memcached") != 0) {
545+
auto array = portnumbers["ports"];
546+
for (auto obj : array) {
547+
auto proto = obj.find("protocol");
548+
if (proto == obj.end() || proto->get<std::string>() != "memcached") {
568549
// the new test use the connectionmap
569550
continue;
570551
}
571552

572-
auto family = cJSON_GetObjectItem(obj, "family");
573-
if (strcmp(family->valuestring, "AF_INET") != 0) {
553+
auto fam = obj.find("family");
554+
if (fam == obj.end() || fam->get<std::string>() != "AF_INET") {
574555
// For now we don't test IPv6
575556
continue;
576557
}
577-
auto ssl = cJSON_GetObjectItem(obj, "ssl");
578-
ASSERT_NE(nullptr, ssl);
579-
auto port = cJSON_GetObjectItem(obj, "port");
580-
ASSERT_NE(nullptr, port);
581-
582-
in_port_t* out_port;
583-
if (ssl->type == cJSON_True) {
584-
out_port = &ssl_port_out;
558+
559+
auto ssl = cb::jsonGet<bool>(obj, "ssl");
560+
auto port = cb::jsonGet<size_t>(obj, "port");
561+
562+
in_port_t* outPort;
563+
if (ssl) {
564+
outPort = &ssl_port_out;
585565
} else {
586-
out_port = &port_out;
566+
outPort = &port_out;
587567
}
588-
*out_port = static_cast<in_port_t>(port->valueint);
568+
*outPort = static_cast<in_port_t>(port);
589569
}
590-
591570
EXPECT_EQ(0, remove(portnumber_file.c_str()));
592571
}
593572

tests/testapp/testapp_audit.cc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,14 @@
2121

2222
#include "testapp.h"
2323

24-
#include <platform/dirutils.h>
2524
#include "testapp_client_test.h"
2625
#include "memcached_audit_events.h"
2726
#include "auditd/auditd_audit_events.h"
2827

28+
#include <cJSON.h>
29+
#include <cJSON_utils.h>
30+
#include <platform/dirutils.h>
31+
2932
#include <fstream>
3033
#include <string>
3134
#include <thread>

tests/testapp/testapp_cmd_timers.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "testapp.h"
1919
#include "testapp_client_test.h"
2020

21+
#include <cJSON.h>
22+
#include <cJSON_utils.h>
2123
#include <platform/compress.h>
2224
#include <protocol/connection/client_connection.h>
2325
#include <algorithm>

tests/testapp/testapp_errmap.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "testapp.h"
1919
#include "testapp_client_test.h"
2020

21+
#include <cJSON.h>
22+
#include <cJSON_utils.h>
2123

2224
class ErrmapTest : public TestappClientTest {
2325
public:

tests/testapp/testapp_subdoc.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "testapp_subdoc_common.h"
2525

2626
#include <cJSON.h>
27+
#include <cJSON_utils.h>
2728
#include <memcached/protocol_binary.h>
2829
#include <memcached/util.h> // for memcached_protocol_errcode_2_text()
2930
#include <platform/cb_malloc.h>

tests/testapp/testapp_subdoc_multipath.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121

2222
#include "testapp_subdoc_common.h"
2323

24+
#include <cJSON.h>
25+
#include <cJSON_utils.h>
26+
2427
// Test multi-path lookup command - simple single SUBDOC_GET
2528
TEST_P(SubdocTestappTest, SubdocMultiLookup_GetSingle) {
2629
store_document("dict", "{\"key1\":1,\"key2\":\"two\", \"key3\":3.0}");

0 commit comments

Comments
 (0)