2 Copyright (c) 2018-2019 Nokia.
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
8 http://www.apache.org/licenses/LICENSE-2.0
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.
18 * This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 * platform project (RICP).
23 #include "private/abort.hpp"
24 #include "private/tst/redisreplybuilder.hpp"
26 using namespace shareddatalayer;
27 using namespace shareddatalayer::redis;
28 using namespace shareddatalayer::tst;
30 RedisReplyBuilder::RedisReplyBuilder():
31 builtRedisReplies({}),
32 defaultStringValue({ 'a', 'b', 'c' }),
33 errorMessage(nullptr),
34 requiredRedisModuleCommands(getRequiredRedisModuleCommands()),
36 commandListQueryElements({})
38 initArrayReplyContent();
39 initCommandListQueryReplyContent();
42 RedisReplyBuilder::~RedisReplyBuilder()
44 delete[] errorMessage;
46 for (auto& redisReplyPtr : commandListQueryElements)
48 delete *(redisReplyPtr->element);
52 for (auto& redisReplyPtr : builtRedisReplies)
58 void RedisReplyBuilder::initArrayReplyContent()
60 if (!arrayReplyElements.empty())
61 SHAREDDATALAYER_ABORT("ArrayReplyContent initialization should done only once");
63 arrayReplyElement1.type = REDIS_REPLY_STRING;
64 arrayReplyElement1.integer = 0;
65 arrayReplyElement1.str = const_cast<char*>(&defaultStringValue[0]);
66 arrayReplyElement1.len = static_cast<int>(defaultStringValue.size());
67 arrayReplyElements.push_back(&arrayReplyElement1);
68 arrayReplyElement2.type = REDIS_REPLY_NIL;
69 arrayReplyElement2.integer = 0;
70 arrayReplyElement2.str = nullptr;
71 arrayReplyElement2.len = 0;
72 arrayReplyElements.push_back(&arrayReplyElement2);
75 void RedisReplyBuilder::initCommandListQueryReplyContent()
77 if (!commandItems.empty() || !commandListQueryElements.empty())
78 SHAREDDATALAYER_ABORT("CommandListQueryReplyContent initialization should done only once");
80 redisReply * commandItem = nullptr;
81 redisReply * arrayItem = nullptr;
83 for (auto& requiredRedisModuleCommand : requiredRedisModuleCommands)
85 commandItem = new redisReply();
86 commandItem->type = REDIS_REPLY_STRING;
87 commandItem->integer = 0;
88 commandItem->str = const_cast<char*>(requiredRedisModuleCommand.c_str());
89 commandItem->len = static_cast<int>(requiredRedisModuleCommand.size());
90 commandItems.push_back(commandItem);
93 for (auto& commandItem : commandItems)
95 arrayItem = new redisReply();
96 arrayItem->type = REDIS_REPLY_ARRAY;
97 arrayItem->elements = 1;
98 arrayItem->element = &commandItem;
100 commandListQueryElements.push_back(arrayItem);
104 redisReply& RedisReplyBuilder::buildNilReply()
106 auto rr = new redisReply();
107 rr->type = REDIS_REPLY_NIL;
108 builtRedisReplies.push_back(rr);
109 return std::ref(*rr);
112 redisReply& RedisReplyBuilder::buildIntegerReply()
114 auto rr = new redisReply();
115 rr->type = REDIS_REPLY_INTEGER;
117 builtRedisReplies.push_back(rr);
118 return std::ref(*rr);
121 redisReply& RedisReplyBuilder::buildStatusReply()
123 auto rr = new redisReply();
124 rr->type = REDIS_REPLY_STATUS;
125 rr->str = const_cast<char*>(&defaultStringValue[0]);
126 rr->len = static_cast<int>(defaultStringValue.size());
127 builtRedisReplies.push_back(rr);
128 return std::ref(*rr);
131 redisReply& RedisReplyBuilder::buildStringReply()
133 auto rr = new redisReply();
134 rr->type = REDIS_REPLY_STRING;
135 rr->str = const_cast<char*>(&defaultStringValue[0]);
136 rr->len = static_cast<int>(defaultStringValue.size());
137 builtRedisReplies.push_back(rr);
138 return std::ref(*rr);
141 redisReply& RedisReplyBuilder::buildArrayReply()
143 auto rr = new redisReply();
144 rr->type = REDIS_REPLY_ARRAY;
146 rr->element = &arrayReplyElements[0];
147 builtRedisReplies.push_back(rr);
148 return std::ref(*rr);
151 redisReply& RedisReplyBuilder::buildCommandListQueryReply()
153 if (commandListQueryElements.empty())
154 SHAREDDATALAYER_ABORT("Cannot built command list query reply");
156 auto rr = new redisReply();
157 rr->type = REDIS_REPLY_ARRAY;
158 rr->elements = commandListQueryElements.size();
159 rr->element = &commandListQueryElements[0];
160 builtRedisReplies.push_back(rr);
161 return std::ref(*rr);
164 redisReply& RedisReplyBuilder::buildIncompleteCommandListQueryReply()
166 if (commandListQueryElements.empty())
167 SHAREDDATALAYER_ABORT("Cannot built incomplete command list query reply");
169 auto rr = new redisReply();
170 rr->type = REDIS_REPLY_ARRAY;
171 rr->elements = commandListQueryElements.size() - 1;
172 rr->element = &commandListQueryElements[0];
173 builtRedisReplies.push_back(rr);
174 return std::ref(*rr);
177 redisReply& RedisReplyBuilder::buildErrorReply(const std::string& msg)
179 auto rr = new redisReply();
180 auto len(msg.size());
181 delete[] errorMessage;
182 errorMessage = new char[len];
183 std::memcpy(errorMessage, msg.c_str(), len);
184 rr->type = REDIS_REPLY_ERROR;
185 rr->str = errorMessage;
186 rr->len = static_cast<int>(len);
187 builtRedisReplies.push_back(rr);
188 return std::ref(*rr);