Skip to content

Commit

Permalink
Use json array in consumer-producer to save fields order
Browse files Browse the repository at this point in the history
  • Loading branch information
kcudnik committed Oct 27, 2016
1 parent f54b581 commit efd9b91
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 24 deletions.
7 changes: 4 additions & 3 deletions common/consumertable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ void ConsumerTable::pop(KeyOpFieldsValuesTuple &kco)
"local ret = {key, op}\n"

"local jj = cjson.decode(value)\n"
"local size = #jj\n"

"for k,v in pairs(jj) do\n"
" table.insert(ret, k)\n"
" table.insert(ret, v)\n"
"for idx=1,size,2 do\n"
" table.insert(ret, jj[idx])\n"
" table.insert(ret, jj[idx+1])\n"
"end\n"

"if op == 'get' or op == 'getresponse' then\n"
Expand Down
14 changes: 7 additions & 7 deletions common/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ namespace swss {

string JSon::buildJson(const vector<FieldValueTuple> &fv)
{
nlohmann::json j;
nlohmann::json j = nlohmann::json::array();

// we use array to save order
for (auto &i : fv)
{
j[fvField(i)] = fvValue(i);
j.push_back(fvField(i));
j.push_back(fvValue(i));
}

return j.dump();
Expand All @@ -26,12 +28,10 @@ void JSon::readJson(const string &jsonstr, vector<FieldValueTuple> &fv)

FieldValueTuple e;

std::map<std::string, nlohmann::json> m = j;

for (auto&& kv : m)
for (size_t i = 0; i < j.size(); i+=2)
{
fvField(e) = kv.first;
fvValue(e) = kv.second;
fvField(e) = j[i];
fvValue(e) = j[i+1];
fv.push_back(e);
}
}
Expand Down
15 changes: 1 addition & 14 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,7 @@ void validateFields(const string& key, const vector<FieldValueTuple>& f)
int i = 0;
EXPECT_EQ(maxNumOfFields, f.size());

// since we are using cjson in lua script
// retrived fields order is different than producer
// since value is passed as json, order does not matter
// but we need to compensate it here

auto copy = f;

std::sort(copy.begin(), copy.end(),
[](const FieldValueTuple & a, const FieldValueTuple & b) -> bool
{
return readNumberAtEOL(fvField(a)) < readNumberAtEOL(fvField(b));
});

for (auto fv : copy)
for (auto fv : f)
{
EXPECT_EQ(i, readNumberAtEOL(fvField(fv)));
EXPECT_EQ(i, readNumberAtEOL(fvValue(fv)));
Expand Down

0 comments on commit efd9b91

Please sign in to comment.