2 * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 "DatabaseManager.h"
18 #include "UserStore.h"
19 #include "SystemStore.h"
20 #include "StoreManager.h"
26 StoreManager::StoreManager()
30 StoreManager::~StoreManager()
36 Store* StoreManager::__findStore(std::list<Store*>& stores, const std::string& uri)
38 for (auto& store : stores) {
39 if (store->metadata.uri != uri)
49 template<typename StoreType>
50 Store* StoreManager::__createStore(Database& database, std::list<Store*>& stores, const std::string& uri)
53 "SELECT columns, retention, \"limit\", readPrivileges, writePrivileges"
54 " FROM ContextStoreSchema"
55 " WHERE uri='" + uri + "'";
57 std::vector<Tuple*> tuples;
58 if (!database.execute(query, COL_STRING COL_INT64 COL_INT64 COL_STRING COL_STRING, NULL, &tuples)) {
59 _E("DB search failed");
68 int64_t retention = 0;
70 std::string readPrivil;
71 std::string writePrivil;
73 tuples[0]->getAt(0, &columns);
74 tuples[0]->getAt(1, &retention);
75 tuples[0]->getAt(2, &limit);
76 tuples[0]->getAt(3, &readPrivil);
77 tuples[0]->getAt(4, &writePrivil);
81 _D("URI: %s, Retention: %u, Limit: %u", uri.c_str(), static_cast<unsigned int>(retention), static_cast<unsigned int>(limit));
82 _D("Columns: %s", columns.c_str());
83 _D("Read: %s", readPrivil.c_str());
84 _D("Write: %s", writePrivil.c_str());
86 StoreType* store = new(std::nothrow) StoreType();
87 IF_FAIL_RETURN_TAG(store, NULL, _E, "Memory allocation failed");
89 store->metadata.uri = uri;
90 store->metadata.columns = columns;
91 store->metadata.retention= static_cast<unsigned int>(retention);
92 store->metadata.limit = static_cast<unsigned int>(limit);
93 store->metadata.readPrivileges = util::tokenizeString(readPrivil, PRIVILEGE_DELIM);
94 store->metadata.writePrivileges = util::tokenizeString(writePrivil, PRIVILEGE_DELIM);
96 stores.push_front(store);
97 if (stores.size() > CACHE_SIZE) {
105 void StoreManager::flushUserCache()
107 for (auto& store : __userStores) {
110 __userStores.clear();
113 void StoreManager::flushSystemCache()
115 for (auto& store : __systemStores) {
118 __systemStores.clear();
121 Store* StoreManager::getUserStore(const std::string& uri)
123 _D("Checking the user DB");
124 Store* store = __findStore(__userStores, uri);
128 store = __createStore<UserStore>(DatabaseManager::getUser(), __userStores, uri);
132 return getSystemStore(uri);
135 Store* StoreManager::getSystemStore(const std::string& uri)
137 _D("Checking the system DB");
138 Store* store = __findStore(__systemStores, uri);
142 return __createStore<SystemStore>(DatabaseManager::getSystem(), __systemStores, uri);