6370be11294b29c0a30738ccb4a8bc48e60ba504
[ric-plt/sdl.git] / src / redis / contentsbuilder.cpp
1 /*
2    Copyright (c) 2018-2019 Nokia.
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 */
16
17 #include "private/redis/contentsbuilder.hpp"
18 #include "private/redis/contents.hpp"
19
20 using namespace shareddatalayer;
21 using namespace shareddatalayer::redis;
22
23 ContentsBuilder::ContentsBuilder(const char nsKeySeparator):
24     nsKeySeparator(nsKeySeparator)
25 {
26 }
27
28 ContentsBuilder::~ContentsBuilder()
29 {
30 }
31
32 Contents ContentsBuilder::build(const std::string& string) const
33 {
34     Contents contents;
35     addString(contents, string);
36     return contents;
37 }
38
39 Contents ContentsBuilder::build(const std::string& string,
40                                 const std::string& string2) const
41 {
42     Contents contents;
43     addString(contents, string);
44     addString(contents, string2);
45     return contents;
46 }
47
48 Contents ContentsBuilder::build(const std::string& string,
49                                 const std::string& string2,
50                                 const std::string& string3) const
51 {
52     Contents contents;
53     addString(contents, string);
54     addString(contents, string2);
55     addString(contents, string3);
56     return contents;
57 }
58
59 Contents ContentsBuilder::build(const std::string& string,
60                                 const AsyncConnection::Namespace& ns,
61                                 const AsyncConnection::DataMap& dataMap) const
62 {
63     Contents contents;
64     addString(contents, string);
65     addDataMap(contents, ns, dataMap);
66     return contents;
67 }
68
69 Contents ContentsBuilder::build(const std::string& string,
70                                 const AsyncConnection::Namespace& ns,
71                                 const AsyncConnection::DataMap& dataMap,
72                                 const std::string& string2,
73                                 const std::string& string3) const
74 {
75     Contents contents;
76     addString(contents, string);
77     addDataMap(contents, ns, dataMap);
78     addString(contents, string2);
79     addString(contents, string3);
80     return contents;
81 }
82
83 Contents ContentsBuilder::build(const std::string& string,
84                                 const AsyncConnection::Namespace& ns,
85                                 const AsyncConnection::Key& key,
86                                 const AsyncConnection::Data& data) const
87 {
88     Contents contents;
89     addString(contents, string);
90     addKey(contents, ns, key);
91     addData(contents, data);
92     return contents;
93 }
94
95 Contents ContentsBuilder::build(const std::string& string,
96                                 const AsyncConnection::Namespace& ns,
97                                 const AsyncConnection::Key& key,
98                                 const AsyncConnection::Data& data,
99                                 const std::string& string2,
100                                 const std::string& string3) const
101 {
102     Contents contents;
103     addString(contents, string);
104     addKey(contents, ns, key);
105     addData(contents, data);
106     addString(contents, string2);
107     addString(contents, string3);
108     return contents;
109 }
110
111 Contents ContentsBuilder::build(const std::string& string,
112                                 const AsyncConnection::Namespace& ns,
113                                 const AsyncConnection::Key& key,
114                                 const AsyncConnection::Data& data,
115                                 const AsyncConnection::Data& data2) const
116 {
117     Contents contents;
118     addString(contents, string);
119     addKey(contents, ns, key);
120     addData(contents, data);
121     addData(contents, data2);
122     return contents;
123 }
124
125 Contents ContentsBuilder::build(const std::string& string,
126                                 const AsyncConnection::Namespace& ns,
127                                 const AsyncConnection::Key& key,
128                                 const AsyncConnection::Data& data,
129                                 const AsyncConnection::Data& data2,
130                                 const std::string& string2,
131                                 const std::string& string3) const
132 {
133     Contents contents;
134     addString(contents, string);
135     addKey(contents, ns, key);
136     addData(contents, data);
137     addData(contents, data2);
138     addString(contents, string2);
139     addString(contents, string3);
140     return contents;
141 }
142
143 Contents ContentsBuilder::build(const std::string& string,
144                                 const AsyncConnection::Namespace& ns,
145                                 const AsyncConnection::Keys& keys) const
146 {
147     Contents contents;
148     addString(contents, string);
149     addKeys(contents, ns, keys);
150     return contents;
151 }
152
153 Contents ContentsBuilder::build(const std::string& string,
154                                 const AsyncConnection::Namespace& ns,
155                                 const AsyncConnection::Keys& keys,
156                                 const std::string& string2,
157                                 const std::string& string3) const
158 {
159     Contents contents;
160     addString(contents, string);
161     addKeys(contents, ns, keys);
162     addString(contents, string2);
163     addString(contents, string3);
164     return contents;
165 }
166
167 void ContentsBuilder::addString(Contents& contents,
168                                 const std::string& string) const
169 {
170     contents.stack.push_back(string);
171     contents.sizes.push_back(string.size());
172 }
173
174 void ContentsBuilder::addDataMap(Contents& contents,
175                                  const AsyncConnection::Namespace& ns,
176                                  const AsyncConnection::DataMap& dataMap) const
177 {
178     for (const auto& i : dataMap)
179     {
180         addKey(contents, ns, i.first);
181         addData(contents, i.second);
182     }
183 }
184
185 void ContentsBuilder::addKey(Contents& contents,
186                              const AsyncConnection::Namespace& ns,
187                              const AsyncConnection::Key& key) const
188 {
189     auto content('{' + ns + '}' + nsKeySeparator + key);
190     contents.stack.push_back(content);
191     contents.sizes.push_back((content).size());
192 }
193
194 void ContentsBuilder::addData(Contents& contents,
195                               const AsyncConnection::Data& data) const
196 {
197     contents.stack.push_back(std::string(reinterpret_cast<const char*>(data.data()),
198                                          static_cast<size_t>(data.size())));
199     contents.sizes.push_back(data.size());
200 }
201
202 void ContentsBuilder::addKeys(Contents& contents,
203                               const AsyncConnection::Namespace& ns,
204                               const AsyncConnection::Keys& keys) const
205 {
206     for (const auto& i : keys)
207         addKey(contents, ns, i);
208 }