-
Notifications
You must be signed in to change notification settings - Fork 274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add swss::Table to c api #964
Open
erer1243
wants to merge
3
commits into
sonic-net:master
Choose a base branch
from
erer1243:c-api-table
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+211
−12
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <string> | ||
#include <vector> | ||
|
||
#include "../dbconnector.h" | ||
#include "../table.h" | ||
#include "table.h" | ||
#include "util.h" | ||
|
||
using namespace swss; | ||
using namespace std; | ||
|
||
SWSSTable SWSSTable_new(SWSSDBConnector db, const char *tableName) { | ||
SWSSTry(return (SWSSTable) new Table((DBConnector *)db, string(tableName))); | ||
} | ||
|
||
void SWSSTable_free(SWSSTable tbl) { | ||
SWSSTry(delete (Table *)tbl); | ||
} | ||
|
||
int8_t SWSSTable_get(SWSSTable tbl, const char *key, SWSSFieldValueArray *outValues) { | ||
SWSSTry({ | ||
vector<FieldValueTuple> fvs; | ||
bool exists = ((Table *)tbl)->get(string(key), fvs); | ||
if (exists) { | ||
*outValues = makeFieldValueArray(fvs); | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} | ||
|
||
int8_t SWSSTable_hget(SWSSTable tbl, const char *key, const char *field, SWSSString *outValue) { | ||
SWSSTry({ | ||
string s; | ||
bool exists = ((Table *)tbl)->hget(string(key), string(field), s); | ||
if (exists) { | ||
*outValue = makeString(move(s)); | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
}); | ||
} | ||
|
||
void SWSSTable_set(SWSSTable tbl, const char *key, SWSSFieldValueArray values) { | ||
SWSSTry({ | ||
vector<FieldValueTuple> fvs = takeFieldValueArray(values); | ||
((Table *)tbl)->set(string(key), fvs); | ||
}); | ||
} | ||
|
||
void SWSSTable_hset(SWSSTable tbl, const char *key, const char *field, SWSSStrRef value) { | ||
SWSSTry({ ((Table *)tbl)->hset(string(key), string(field), takeStrRef(value)); }); | ||
} | ||
|
||
void SWSSTable_del(SWSSTable tbl, const char *key) { | ||
SWSSTry({ ((Table *)tbl)->del(string(key)); }); | ||
} | ||
|
||
void SWSSTable_hdel(SWSSTable tbl, const char *key, const char *field) { | ||
SWSSTry({ ((Table *)tbl)->hdel(string(key), string(field)); }); | ||
} | ||
|
||
SWSSStringArray SWSSTable_getKeys(SWSSTable tbl) { | ||
SWSSTry({ | ||
vector<string> keys; | ||
((Table *)tbl)->getKeys(keys); | ||
return makeStringArray(move(keys)); | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#ifndef SWSS_COMMON_C_API_TABLE_H | ||
#define SWSS_COMMON_C_API_TABLE_H | ||
|
||
#include "dbconnector.h" | ||
#include "util.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#include <stdint.h> | ||
|
||
typedef struct SWSSTableOpaque *SWSSTable; | ||
|
||
SWSSTable SWSSTable_new(SWSSDBConnector db, const char *tableName); | ||
|
||
void SWSSTable_free(SWSSTable tbl); | ||
|
||
// If the key exists, populates outValues with the table's values and returns 1. | ||
// If the key doesn't exist, returns 0. | ||
int8_t SWSSTable_get(SWSSTable tbl, const char *key, SWSSFieldValueArray *outValues); | ||
|
||
// If the key and field exist, populates outValue with the field's value and returns 1. | ||
// If the key doesn't exist, returns 0. | ||
int8_t SWSSTable_hget(SWSSTable tbl, const char *key, const char *field, SWSSString *outValue); | ||
|
||
void SWSSTable_set(SWSSTable tbl, const char *key, SWSSFieldValueArray values); | ||
|
||
void SWSSTable_hset(SWSSTable tbl, const char *key, const char *field, SWSSStrRef value); | ||
|
||
void SWSSTable_del(SWSSTable tbl, const char *key); | ||
|
||
void SWSSTable_hdel(SWSSTable tbl, const char *key, const char *field); | ||
|
||
SWSSStringArray SWSSTable_getKeys(SWSSTable tbl); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a "const char **data;", do you need delete all string pointer in this array first?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The c api free functions don't do that because the data is sometimes user-owned. The behavior is expressed in the header file comments.