Skip to content

Commit 1668dc4

Browse files
Merge branch 'master' of https://github.com/divyagayathri-hcl/sonic-swss-common into zmq_new
2 parents 510132f + b58a501 commit 1668dc4

File tree

7 files changed

+214
-12
lines changed

7 files changed

+214
-12
lines changed

common/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ common_libswsscommon_la_SOURCES = \
7979
common/c-api/zmqserver.cpp \
8080
common/c-api/zmqconsumerstatetable.cpp \
8181
common/c-api/zmqproducerstatetable.cpp \
82+
common/c-api/table.cpp \
8283
common/performancetimer.cpp
8384

8485
common_libswsscommon_la_CXXFLAGS = $(DBGFLAGS) $(AM_CFLAGS) $(CFLAGS_COMMON) $(LIBNL_CFLAGS) $(CODE_COVERAGE_CXXFLAGS)

common/c-api/table.cpp

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#include <string>
2+
#include <vector>
3+
4+
#include "../dbconnector.h"
5+
#include "../table.h"
6+
#include "table.h"
7+
#include "util.h"
8+
9+
using namespace swss;
10+
using namespace std;
11+
12+
SWSSTable SWSSTable_new(SWSSDBConnector db, const char *tableName) {
13+
SWSSTry(return (SWSSTable) new Table((DBConnector *)db, string(tableName)));
14+
}
15+
16+
void SWSSTable_free(SWSSTable tbl) {
17+
SWSSTry(delete (Table *)tbl);
18+
}
19+
20+
int8_t SWSSTable_get(SWSSTable tbl, const char *key, SWSSFieldValueArray *outValues) {
21+
SWSSTry({
22+
vector<FieldValueTuple> fvs;
23+
bool exists = ((Table *)tbl)->get(string(key), fvs);
24+
if (exists) {
25+
*outValues = makeFieldValueArray(fvs);
26+
return 1;
27+
} else {
28+
return 0;
29+
}
30+
});
31+
}
32+
33+
int8_t SWSSTable_hget(SWSSTable tbl, const char *key, const char *field, SWSSString *outValue) {
34+
SWSSTry({
35+
string s;
36+
bool exists = ((Table *)tbl)->hget(string(key), string(field), s);
37+
if (exists) {
38+
*outValue = makeString(move(s));
39+
return 1;
40+
} else {
41+
return 0;
42+
}
43+
});
44+
}
45+
46+
void SWSSTable_set(SWSSTable tbl, const char *key, SWSSFieldValueArray values) {
47+
SWSSTry({
48+
vector<FieldValueTuple> fvs = takeFieldValueArray(values);
49+
((Table *)tbl)->set(string(key), fvs);
50+
});
51+
}
52+
53+
void SWSSTable_hset(SWSSTable tbl, const char *key, const char *field, SWSSStrRef value) {
54+
SWSSTry({ ((Table *)tbl)->hset(string(key), string(field), takeStrRef(value)); });
55+
}
56+
57+
void SWSSTable_del(SWSSTable tbl, const char *key) {
58+
SWSSTry({ ((Table *)tbl)->del(string(key)); });
59+
}
60+
61+
void SWSSTable_hdel(SWSSTable tbl, const char *key, const char *field) {
62+
SWSSTry({ ((Table *)tbl)->hdel(string(key), string(field)); });
63+
}
64+
65+
SWSSStringArray SWSSTable_getKeys(SWSSTable tbl) {
66+
SWSSTry({
67+
vector<string> keys;
68+
((Table *)tbl)->getKeys(keys);
69+
return makeStringArray(move(keys));
70+
})
71+
}

common/c-api/table.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef SWSS_COMMON_C_API_TABLE_H
2+
#define SWSS_COMMON_C_API_TABLE_H
3+
4+
#include "dbconnector.h"
5+
#include "util.h"
6+
7+
#ifdef __cplusplus
8+
extern "C" {
9+
#endif
10+
11+
#include <stdint.h>
12+
13+
typedef struct SWSSTableOpaque *SWSSTable;
14+
15+
SWSSTable SWSSTable_new(SWSSDBConnector db, const char *tableName);
16+
17+
void SWSSTable_free(SWSSTable tbl);
18+
19+
// If the key exists, populates outValues with the table's values and returns 1.
20+
// If the key doesn't exist, returns 0.
21+
int8_t SWSSTable_get(SWSSTable tbl, const char *key, SWSSFieldValueArray *outValues);
22+
23+
// If the key and field exist, populates outValue with the field's value and returns 1.
24+
// If the key doesn't exist, returns 0.
25+
int8_t SWSSTable_hget(SWSSTable tbl, const char *key, const char *field, SWSSString *outValue);
26+
27+
void SWSSTable_set(SWSSTable tbl, const char *key, SWSSFieldValueArray values);
28+
29+
void SWSSTable_hset(SWSSTable tbl, const char *key, const char *field, SWSSStrRef value);
30+
31+
void SWSSTable_del(SWSSTable tbl, const char *key);
32+
33+
void SWSSTable_hdel(SWSSTable tbl, const char *key, const char *field);
34+
35+
SWSSStringArray SWSSTable_getKeys(SWSSTable tbl);
36+
37+
#ifdef __cplusplus
38+
}
39+
#endif
40+
41+
#endif

common/c-api/util.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ void SWSSFieldValueArray_free(SWSSFieldValueArray arr) {
3131
void SWSSKeyOpFieldValuesArray_free(SWSSKeyOpFieldValuesArray kfvs) {
3232
SWSSTry(delete[] kfvs.data);
3333
}
34+
35+
void SWSSStringArray_free(SWSSStringArray arr) {
36+
SWSSTry(delete[] arr.data);
37+
}

common/c-api/util.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ typedef struct SWSSStringOpaque *SWSSString;
1919
typedef struct SWSSStrRefOpaque *SWSSStrRef;
2020

2121
// FFI version of swss::FieldValueTuple
22+
// field should be freed with libc's free()
2223
typedef struct {
2324
const char *field;
2425
SWSSString value;
@@ -59,6 +60,13 @@ typedef enum {
5960
SWSSSelectResult_SIGNAL = 2,
6061
} SWSSSelectResult;
6162

63+
// FFI version of std::vector<std::string>
64+
// data strings should be freed with libc's free()
65+
typedef struct {
66+
uint64_t len;
67+
const char **data;
68+
} SWSSStringArray;
69+
6270
// data should not include a null terminator
6371
SWSSString SWSSString_new(const char *data, uint64_t length);
6472

@@ -85,6 +93,10 @@ void SWSSFieldValueArray_free(SWSSFieldValueArray arr);
8593
// grained control of ownership).
8694
void SWSSKeyOpFieldValuesArray_free(SWSSKeyOpFieldValuesArray kfvs);
8795

96+
// arr.data may be null. This is not recursive - elements must be freed separately (for finer
97+
// grained control of ownership).
98+
void SWSSStringArray_free(SWSSStringArray arr);
99+
88100
#ifdef __cplusplus
89101
}
90102
#endif
@@ -214,6 +226,19 @@ template <class T> static inline SWSSKeyOpFieldValuesArray makeKeyOpFieldValuesA
214226
return out;
215227
}
216228

229+
static inline SWSSStringArray makeStringArray(std::vector<std::string> &&in) {
230+
const char **data = new const char*[in.size()];
231+
232+
size_t i = 0;
233+
for (std::string &s : in)
234+
data[i++] = strdup(s.c_str());
235+
236+
SWSSStringArray out;
237+
out.len = (uint64_t)in.size();
238+
out.data = data;
239+
return out;
240+
}
241+
217242
static inline std::string takeString(SWSSString s) {
218243
return std::string(std::move(*((std::string *)s)));
219244
}

common/schema.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,9 @@ namespace swss {
485485
#define CFG_PAC_GLOBAL_CONFIG_TABLE "PAC_GLOBAL_CONFIG_TABLE"
486486
#define CFG_PAC_HOSTAPD_GLOBAL_CONFIG_TABLE "HOSTAPD_GLOBAL_CONFIG_TABLE"
487487

488+
#define CFG_SRV6_MY_SID_TABLE_NAME "SRV6_MY_SIDS"
489+
#define CFG_SRV6_MY_LOCATOR_TABLE_NAME "SRV6_MY_LOCATORS"
490+
488491
/***** STATE DATABASE *****/
489492

490493
#define STATE_SWITCH_CAPABILITY_TABLE_NAME "SWITCH_CAPABILITY"

tests/c_api_ut.cpp

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include "common/c-api/dbconnector.h"
66
#include "common/c-api/producerstatetable.h"
77
#include "common/c-api/subscriberstatetable.h"
8+
#include "common/c-api/table.h"
89
#include "common/c-api/util.h"
910
#include "common/c-api/zmqclient.h"
1011
#include "common/c-api/zmqconsumerstatetable.h"
@@ -40,14 +41,18 @@ static void sortKfvs(vector<KeyOpFieldsValuesTuple> &kfvs) {
4041

4142
#define free(x) std::free(const_cast<void *>(reinterpret_cast<const void *>(x)));
4243

44+
static void freeFieldValuesArray(SWSSFieldValueArray arr) {
45+
for (uint64_t i = 0; i < arr.len; i++) {
46+
free(arr.data[i].field);
47+
SWSSString_free(arr.data[i].value);
48+
}
49+
SWSSFieldValueArray_free(arr);
50+
}
51+
4352
static void freeKeyOpFieldValuesArray(SWSSKeyOpFieldValuesArray arr) {
4453
for (uint64_t i = 0; i < arr.len; i++) {
4554
free(arr.data[i].key);
46-
for (uint64_t j = 0; j < arr.data[i].fieldValues.len; j++) {
47-
free(arr.data[i].fieldValues.data[j].field);
48-
SWSSString_free(arr.data[i].fieldValues.data[j].value);
49-
}
50-
SWSSFieldValueArray_free(arr.data[i].fieldValues);
55+
freeFieldValuesArray(arr.data[i].fieldValues);
5156
}
5257
SWSSKeyOpFieldValuesArray_free(arr);
5358
}
@@ -105,6 +110,57 @@ TEST(c_api, DBConnector) {
105110
SWSSDBConnector_free(db);
106111
}
107112

113+
TEST(c_api, Table) {
114+
clearDB();
115+
SWSSStringManager sm;
116+
117+
SWSSDBConnector db = SWSSDBConnector_new_named("TEST_DB", 1000, true);
118+
SWSSTable tbl = SWSSTable_new(db, "mytable");
119+
120+
SWSSFieldValueArray fvs;
121+
SWSSString ss;
122+
EXPECT_FALSE(SWSSTable_get(tbl, "mykey", &fvs));
123+
EXPECT_FALSE(SWSSTable_hget(tbl, "mykey", "myfield", &ss));
124+
125+
SWSSStringArray keys = SWSSTable_getKeys(tbl);
126+
EXPECT_EQ(keys.len, 0);
127+
SWSSStringArray_free(keys);
128+
129+
SWSSTable_hset(tbl, "mykey", "myfield", sm.makeStrRef("myvalue"));
130+
keys = SWSSTable_getKeys(tbl);
131+
ASSERT_EQ(keys.len, 1);
132+
EXPECT_STREQ(keys.data[0], "mykey");
133+
free(keys.data[0]);
134+
SWSSStringArray_free(keys);
135+
136+
ASSERT_TRUE(SWSSTable_hget(tbl, "mykey", "myfield", &ss));
137+
EXPECT_STREQ(SWSSStrRef_c_str((SWSSStrRef)ss), "myvalue");
138+
SWSSString_free(ss);
139+
140+
SWSSTable_hdel(tbl, "mykey", "myfield");
141+
EXPECT_FALSE(SWSSTable_hget(tbl, "mykey", "myfield", &ss));
142+
143+
SWSSFieldValueTuple data[2] = {{.field = "myfield1", .value = sm.makeString("myvalue1")},
144+
{.field = "myfield2", .value = sm.makeString("myvalue2")}};
145+
fvs.len = 2;
146+
fvs.data = data;
147+
SWSSTable_set(tbl, "mykey", fvs);
148+
149+
ASSERT_TRUE(SWSSTable_get(tbl, "mykey", &fvs));
150+
EXPECT_EQ(fvs.len, 2);
151+
EXPECT_STREQ(data[0].field, fvs.data[0].field);
152+
EXPECT_STREQ(data[1].field, fvs.data[1].field);
153+
freeFieldValuesArray(fvs);
154+
155+
SWSSTable_del(tbl, "mykey");
156+
keys = SWSSTable_getKeys(tbl);
157+
EXPECT_EQ(keys.len, 0);
158+
SWSSStringArray_free(keys);
159+
160+
SWSSTable_free(tbl);
161+
SWSSDBConnector_free(db);
162+
}
163+
108164
TEST(c_api, ConsumerProducerStateTables) {
109165
clearDB();
110166
SWSSStringManager sm;
@@ -119,9 +175,8 @@ TEST(c_api, ConsumerProducerStateTables) {
119175
ASSERT_EQ(arr.len, 0);
120176
freeKeyOpFieldValuesArray(arr);
121177

122-
SWSSFieldValueTuple data[2] = {
123-
{.field = "myfield1", .value = sm.makeString("myvalue1")},
124-
{.field = "myfield2", .value = sm.makeString("myvalue2")}};
178+
SWSSFieldValueTuple data[2] = {{.field = "myfield1", .value = sm.makeString("myvalue1")},
179+
{.field = "myfield2", .value = sm.makeString("myvalue2")}};
125180
SWSSFieldValueArray values = {
126181
.len = 2,
127182
.data = data,
@@ -154,7 +209,7 @@ TEST(c_api, ConsumerProducerStateTables) {
154209
ASSERT_EQ(fieldValues1.size(), 1);
155210
EXPECT_EQ(fieldValues1[0].first, "myfield3");
156211
EXPECT_EQ(fieldValues1[0].second, "myvalue3");
157-
212+
158213
arr = SWSSConsumerStateTable_pops(cst);
159214
EXPECT_EQ(arr.len, 0);
160215
freeKeyOpFieldValuesArray(arr);
@@ -244,14 +299,16 @@ TEST(c_api, ZmqConsumerProducerStateTable) {
244299
// On flag = 0, we use the ZmqProducerStateTable
245300
// On flag = 1, we use the ZmqClient directly
246301
for (int flag = 0; flag < 2; flag++) {
247-
SWSSFieldValueTuple values_key1_data[2] = {{.field = "myfield1", .value = sm.makeString("myvalue1")},
248-
{.field = "myfield2", .value = sm.makeString("myvalue2")}};
302+
SWSSFieldValueTuple values_key1_data[2] = {
303+
{.field = "myfield1", .value = sm.makeString("myvalue1")},
304+
{.field = "myfield2", .value = sm.makeString("myvalue2")}};
249305
SWSSFieldValueArray values_key1 = {
250306
.len = 2,
251307
.data = values_key1_data,
252308
};
253309

254-
SWSSFieldValueTuple values_key2_data[1] = {{.field = "myfield3", .value = sm.makeString("myvalue3")}};
310+
SWSSFieldValueTuple values_key2_data[1] = {
311+
{.field = "myfield3", .value = sm.makeString("myvalue3")}};
255312
SWSSFieldValueArray values_key2 = {
256313
.len = 1,
257314
.data = values_key2_data,

0 commit comments

Comments
 (0)