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.
17 #include "private/abort.hpp"
18 #include "private/namespaceconfigurationsimpl.hpp"
22 using namespace shareddatalayer;
26 const NamespaceConfiguration getDefaultNamespaceConfiguration()
28 return {"", true, false, "<default>"};
32 NamespaceConfigurationsImpl::NamespaceConfigurationsImpl():
33 namespaceConfigurations({getDefaultNamespaceConfiguration()}),
34 //Current implementation assumes that this index is zero. If changing the index, do needed modifications to implementation.
35 defaultConfigurationIndex(0),
36 namespaceConfigurationsLookupTable()
40 NamespaceConfigurationsImpl::~NamespaceConfigurationsImpl()
44 void NamespaceConfigurationsImpl::addNamespaceConfiguration(const NamespaceConfiguration& namespaceConfiguration)
46 if (!namespaceConfigurationsLookupTable.empty())
47 SHAREDDATALAYER_ABORT("Cannot add namespace configurations after lookup table is initialized");
49 namespaceConfigurations.push_back(namespaceConfiguration);
52 std::string NamespaceConfigurationsImpl::getDescription(const std::string& ns) const
54 NamespaceConfiguration namespaceConfiguration = findConfigurationForNamespace(ns);
55 std::string sourceInfo = namespaceConfiguration.sourceName;
57 if (!namespaceConfiguration.namespacePrefix.empty())
58 sourceInfo += (" prefix: " + namespaceConfiguration.namespacePrefix);
60 std::ostringstream os;
62 os << sourceInfo << ", ";
63 os << "useDbBackend: " << namespaceConfiguration.dbBackendIsUsed << ", ";
64 os << "enableNotifications: " << namespaceConfiguration.notificationsAreEnabled;
68 bool NamespaceConfigurationsImpl::isDbBackendUseEnabled(const std::string& ns) const
70 return findConfigurationForNamespace(ns).dbBackendIsUsed;
73 bool NamespaceConfigurationsImpl::areNotificationsEnabled(const std::string& ns) const
75 return findConfigurationForNamespace(ns).notificationsAreEnabled;
78 const NamespaceConfiguration& NamespaceConfigurationsImpl::findConfigurationForNamespace(const std::string& ns) const
80 if (namespaceConfigurationsLookupTable.count(ns) > 0)
81 return namespaceConfigurations.at(namespaceConfigurationsLookupTable.at(ns));
83 size_t longestMatchingPrefixLen(0);
84 std::vector<int>::size_type foundIndex(0);
85 bool configurationFound(false);
87 for(std::vector<int>::size_type i = (defaultConfigurationIndex + 1); i < namespaceConfigurations.size(); i++)
89 const auto prefixLen(namespaceConfigurations[i].namespacePrefix.length());
91 if (ns.compare(0, prefixLen, namespaceConfigurations[i].namespacePrefix) == 0)
93 if (prefixLen >= longestMatchingPrefixLen)
95 configurationFound = true;
97 longestMatchingPrefixLen = prefixLen;
102 if (!configurationFound)
103 foundIndex = defaultConfigurationIndex;
105 namespaceConfigurationsLookupTable[ns] = foundIndex;
106 return namespaceConfigurations[foundIndex];
109 bool NamespaceConfigurationsImpl::isEmpty() const
111 /* Empty when contains only default configuration */
112 return namespaceConfigurations.size() == 1;
115 bool NamespaceConfigurationsImpl::isNamespaceInLookupTable(const std::string& ns) const
117 return namespaceConfigurationsLookupTable.count(ns) > 0;