Add Redis Sentinel based database discovery
[ric-plt/sdl.git] / src / redis / asynccommanddispatcher.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/asynccommanddispatcher.hpp"
18 #include <cstdlib>
19 #include "config.h"
20 #include "private/redis/databaseinfo.hpp"
21 #if HAVE_HIREDIS_VIP
22 #include "private/redis/asynchiredisclustercommanddispatcher.hpp"
23 #include "private/redis/asynchirediscommanddispatcher.hpp"
24 #elif HAVE_HIREDIS
25 #include "private/redis/asynchirediscommanddispatcher.hpp"
26 #endif
27 #include "private/abort.hpp"
28 #include "private/engine.hpp"
29
30 using namespace shareddatalayer::redis;
31
32 std::shared_ptr<AsyncCommandDispatcher> AsyncCommandDispatcher::create(Engine& engine,
33                                                                        const DatabaseInfo& databaseInfo,
34                                                                        std::shared_ptr<ContentsBuilder> contentsBuilder,
35                                                                        bool usePermanentCommandCallbacks,
36                                                                        std::shared_ptr<Logger> logger,
37                                                                        bool usedForSentinel)
38 {
39 #if HAVE_HIREDIS_VIP
40     static_cast<void>(usedForSentinel);
41     if (databaseInfo.type == DatabaseInfo::Type::CLUSTER)
42     {
43         return std::make_shared<AsyncHiredisClusterCommandDispatcher>(engine,
44                                                                       databaseInfo.ns,
45                                                                       databaseInfo.hosts,
46                                                                       contentsBuilder,
47                                                                       usePermanentCommandCallbacks,
48                                                                       logger);
49     }
50     else
51         return std::make_shared<AsyncHiredisCommandDispatcher>(engine,
52                                                                databaseInfo.hosts.at(0).getHost(),
53                                                                databaseInfo.hosts.at(0).getPort(),
54                                                                contentsBuilder,
55                                                                usePermanentCommandCallbacks,
56                                                                logger);
57 #elif HAVE_HIREDIS
58     if (databaseInfo.type == DatabaseInfo::Type::CLUSTER)
59         SHAREDDATALAYER_ABORT("Not implemented.");
60     return std::make_shared<AsyncHiredisCommandDispatcher>(engine,
61                                                            databaseInfo.hosts.at(0).getHost(),
62                                                            databaseInfo.hosts.at(0).getPort(),
63                                                            contentsBuilder,
64                                                            usePermanentCommandCallbacks,
65                                                            logger,
66                                                            usedForSentinel);
67 #else
68     SHAREDDATALAYER_ABORT("Not implemented.");
69 #endif
70 }