911988469fed60a858ed706211cb9695541dbbf3
[platform/core/context/context-store.git] / src / server / StoreManager.cpp
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd.
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 "DatabaseManager.h"
18 #include "UserStore.h"
19 #include "SystemStore.h"
20 #include "StoreManager.h"
21
22 #define CACHE_SIZE      3
23
24 using namespace ctx;
25
26 StoreManager::StoreManager()
27 {
28 }
29
30 StoreManager::~StoreManager()
31 {
32         flushUserCache();
33         flushSystemCache();
34 }
35
36 Store* StoreManager::__findStore(std::list<Store*>& stores, const std::string& uri)
37 {
38         for (auto& store : stores) {
39                 if (store->metadata.uri != uri)
40                         continue;
41
42                 _D("Cache hit!");
43                 return store;
44         }
45
46         return NULL;
47 }
48
49 template<typename StoreType>
50 Store* StoreManager::__createStore(Database& database, std::list<Store*>& stores, const std::string& uri)
51 {
52         std::string query =
53                 "SELECT columns, retention, \"limit\", readPrivileges, writePrivileges"
54                 " FROM ContextStoreSchema"
55                 " WHERE uri='" + uri + "'";
56
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");
60                 return NULL;
61         }
62         if (tuples.empty()) {
63                 _W("Not found");
64                 return NULL;
65         }
66
67         std::string columns;
68         int64_t retention = 0;
69         int64_t limit = 0;
70         std::string readPrivil;
71         std::string writePrivil;
72
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);
78
79         delete tuples[0];
80
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());
85
86         StoreType* store = new(std::nothrow) StoreType();
87         IF_FAIL_RETURN_TAG(store, NULL, _E, "Memory allocation failed");
88
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);
95
96         stores.push_front(store);
97         if (stores.size() > CACHE_SIZE) {
98                 delete stores.back();
99                 stores.pop_back();
100         }
101
102         return store;
103 }
104
105 void StoreManager::flushUserCache()
106 {
107         for (auto& store : __userStores) {
108                 delete store;
109         }
110         __userStores.clear();
111 }
112
113 void StoreManager::flushSystemCache()
114 {
115         for (auto& store : __systemStores) {
116                 delete store;
117         }
118         __systemStores.clear();
119 }
120
121 Store* StoreManager::getUserStore(const std::string& uri)
122 {
123         _D("Checking the user DB");
124         Store* store = __findStore(__userStores, uri);
125         if (store)
126                 return store;
127
128         store = __createStore<UserStore>(DatabaseManager::getUser(), __userStores, uri);
129         if (store)
130                 return store;
131
132         return getSystemStore(uri);
133 }
134
135 Store* StoreManager::getSystemStore(const std::string& uri)
136 {
137         _D("Checking the system DB");
138         Store* store = __findStore(__systemStores, uri);
139         if (store)
140                 return store;
141
142         return __createStore<SystemStore>(DatabaseManager::getSystem(), __systemStores, uri);
143 }