forked from sonic-net/sonic-swss-common
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproducertable.cpp
More file actions
106 lines (89 loc) · 2.4 KB
/
producertable.cpp
File metadata and controls
106 lines (89 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "common/redisreply.h"
#include "common/producertable.h"
#include "common/json.h"
#include "common/json.hpp"
#include <stdlib.h>
#include <tuple>
using namespace std;
using json = nlohmann::json;
namespace swss {
ProducerTable::ProducerTable(DBConnector *db, string tableName) :
Table(db, tableName)
{
}
ProducerTable::ProducerTable(DBConnector *db, string tableName, string dumpFile) :
Table(db, tableName)
{
m_dumpFile.open(dumpFile, fstream::out | fstream::trunc);
m_dumpFile << "[" << endl;
}
ProducerTable::~ProducerTable() {
if (m_dumpFile.is_open())
{
m_dumpFile << endl << "]" << endl;
m_dumpFile.close();
}
}
void ProducerTable::enqueueDbChange(string key, string value, string op)
{
string lpush_value;
string lpush_key("LPUSH ");
string lpush_op = lpush_key;
char *temp;
int len;
lpush_key += getKeyQueueTableName();
lpush_key += " ";
lpush_key += key;
enqueue(lpush_key, REDIS_REPLY_INTEGER);
len = redisFormatCommand(&temp, "LPUSH %s %s", getValueQueueTableName().c_str(), value.c_str());
lpush_value = string(temp, len);
free(temp);
enqueue(lpush_value, REDIS_REPLY_INTEGER, true);
lpush_op += getOpQueueTableName();
lpush_op += " ";
lpush_op += op;
enqueue(lpush_op, REDIS_REPLY_INTEGER);
string publish("PUBLISH ");
publish += getChannelTableName();
publish += " G";
enqueue(publish, REDIS_REPLY_INTEGER);
}
void ProducerTable::set(string key, vector<FieldValueTuple> &values, string op)
{
if (m_dumpFile.is_open())
{
if (!m_firstItem)
m_dumpFile << "," << endl;
else
m_firstItem = false;
json j;
string json_key = getKeyName(key);
j[json_key] = json::object();
for (auto it : values)
j[json_key][fvField(it)] = fvValue(it);
j["OP"] = op;
m_dumpFile << j.dump(4);
}
multi();
enqueueDbChange(key, JSon::buildJson(values), "S" + op);
exec();
}
void ProducerTable::del(string key, string op)
{
if (m_dumpFile.is_open())
{
if (!m_firstItem)
m_dumpFile << "," << endl;
else
m_firstItem = false;
json j;
string json_key = getKeyName(key);
j[json_key] = json::object();
j["OP"] = op;
m_dumpFile << j.dump(4);
}
multi();
enqueueDbChange(key, "{}", "D" + op);
exec();
}
}