Add first version
[ric-plt/sdl.git] / src / redis / asyncdatabasediscovery.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/asyncdatabasediscovery.hpp"
18 #include "private/databaseconfiguration.hpp"
19 #include "private/logger.hpp"
20 #include <cstdlib>
21 #include "config.h"
22 #if HAVE_HIREDIS
23 #include "private/redis/asynchiredisdatabasediscovery.hpp"
24 #endif
25 #include "private/abort.hpp"
26
27 using namespace shareddatalayer::redis;
28
29 std::shared_ptr<AsyncDatabaseDiscovery> AsyncDatabaseDiscovery::create(std::shared_ptr<Engine> engine,
30                                                                        const boost::optional<Namespace>& ns,
31                                                                        const DatabaseConfiguration& staticDatabaseConfiguration,
32                                                                        std::shared_ptr<Logger> logger)
33 {
34     auto staticAddresses(staticDatabaseConfiguration.getServerAddresses());
35
36     if (staticAddresses.empty())
37         staticAddresses = staticDatabaseConfiguration.getDefaultServerAddresses();
38
39     auto staticDbType(staticDatabaseConfiguration.getDbType());
40
41     if (staticDbType == DatabaseConfiguration::DbType::REDIS_CLUSTER)
42 #if HAVE_HIREDIS_VIP
43         return std::make_shared<AsyncHiredisDatabaseDiscovery>(engine,
44                                                                ns,
45                                                                DatabaseInfo::Type::CLUSTER,
46                                                                staticAddresses,
47                                                                logger);
48 #else
49         SHAREDDATALAYER_ABORT("No Hiredis vip for Redis cluster configuration");
50 #endif
51     else
52 #if HAVE_HIREDIS
53         return std::make_shared<AsyncHiredisDatabaseDiscovery>(engine,
54                                                                ns,
55                                                                DatabaseInfo::Type::SINGLE,
56                                                                staticAddresses,
57                                                                logger);
58 #else
59         static_cast<void>(logger);
60         SHAREDDATALAYER_ABORT("No Hiredis");
61 #endif
62 }
63