-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDb.cc
166 lines (155 loc) · 4.9 KB
/
Db.cc
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdexcept>
#include <sstream>
#include "YangAst.hh"
#include "DbTree.hh"
#include "Db.hh"
// class Db
Db::Db(const std::list<std::string>& schemas) :
_roots(new Key(new Singelton(), 0)),
_schema(schemas)
{
}
Db::~Db()
{
}
Obj&
Db::merge(const std::string& name, const Common::KeyVals& leafs)
{
}
Obj
Db::replace(const std::string& name, const Common::KeyVals& leafs)
{
Yang::Ast::InteriorNode* n = _schema.findInteriorNode(name);
if (n == 0) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << " could not find: " << name
<< " as a list or container in schema";
throw std:: invalid_argument(ss.str());
}
// @todo fix top level list support
const DbVal* key = n->createKey(leafs);
if (key == 0) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << " could not create key for list: " << name;
throw std:: invalid_argument(ss.str());
}
Type* t = _roots->findOrCreate(name, n);
Key* k = t->findOrCreate(key, t);
std::vector<Vals> vals;
n->createLeafs(leafs, vals);
k->replace(vals);
return Obj(k);
}
Obj*
Db::find(const std::string& name, const Common::KeyVals& leafs)
{
const Yang::Ast::Node* node = _schema.findInteriorNode(name);
if (node == 0) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << " could not find: " << name
<< " as a list or container in schema";
throw std:: invalid_argument(ss.str());
}
// @todo add support module level leafs
const DbVal* key = node->createKey(leafs);
if (key == 0) {
std::stringstream ss;
ss << " could not create key: " << name;
Common::Error::err(__PRETTY_FUNCTION__, ss.str());
}
Type* t = _roots->find(name);
Key* k = t->find(key);
if (k == 0) {
std::stringstream ss;
ss << "could not find key: " << *key;
Common::Error::err(__PRETTY_FUNCTION__, ss.str());
}
return new Obj(k);
}
void
Db::printSchema()
{
_schema.print();
}
// class Obj
Obj&
Obj::merge(const std::string& name, const Common::KeyVals& leafs)
{
}
Obj
Obj::replace(const std::string& name, const Common::KeyVals& leafs)
{
Yang::Ast::InteriorNode& schemaObj = _key->getSchemaObj();
Yang::Ast::InteriorNode* n = schemaObj.findInteriorNode(name);
if (n == 0) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << " could not find: " << name
<< " as a list or container in schema";
throw std:: invalid_argument(ss.str());
}
const DbVal* key = n->createKey(leafs);
if (key == 0) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << " could not create key for list: " << name;
throw std:: invalid_argument(ss.str());
}
Type* t = _key->findOrCreate(name, n);
Key* k = t->findOrCreate(key, t);
std::vector<Vals> vals;
n->createLeafs(leafs, vals);
k->replace(vals);
return Obj(k);
}
void
Obj::getLeafs(Common::KeyVals& keyVals) const
{
const std::vector<Vals>& leafs = _key->getLeafs();
const Yang::Ast::InteriorNode& schemaObj = _key->getSchemaObj();
for (int i = 0, size = leafs.size(); i < size; i++) {
const Yang::Ast::LeafBase& leaf = schemaObj.getLeaf(i);
const std::string& leafName = leaf.getString();
const Vals& vals = leafs[i];
if (vals.empty()) {
typedef std::vector<std::string> Defaults;
const Defaults& ds = leaf.getDefaults();
for (int i = 0, size = ds.size(); i < size; i++) {
keyVals.insert(make_pair(leafName, ds[i]));
}
} else {
for (Vals::const_iterator i = vals.begin();
i != vals.end();
++i) {
// @todo stringstreams are slow
std::stringstream ss;
ss << **i;
keyVals.insert(make_pair(leafName, ss.str()));
}
}
}
}
Obj*
Obj::find(const std::string& name, const Common::KeyVals& leafs)
{
Yang::Ast::InteriorNode& schemaObj = _key->getSchemaObj();
Yang::Ast::Node* node = schemaObj.findInteriorNode(name);
if (node == 0) {
std::stringstream ss;
ss << __PRETTY_FUNCTION__ << " could not find: " << name
<< " as a list or container in schema";
throw std:: invalid_argument(ss.str());
}
const DbVal* key = node->createKey(leafs);
if (key == 0) {
std::stringstream ss;
ss << " could not create key: " << name;
Common::Error::err(__PRETTY_FUNCTION__, ss.str());
}
Type* t = _key->find(name);
Key* k = t->find(key);
if (k == 0) {
std::stringstream ss;
ss << "could not find key: " << *key;
Common::Error::err(__PRETTY_FUNCTION__, ss.str());
}
return new Obj(k);
}