From: Mu-Woong Lee Date: Tue, 14 Mar 2017 11:55:52 +0000 (+0900) Subject: Implement StoreManager & store handle classes in the server X-Git-Tag: accepted/tizen/unified/20170414.163522^2~17 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=48d1ecc062c7f6c413a0f2abbed0465f3e6d0cad;p=platform%2Fcore%2Fcontext%2Fcontext-store.git Implement StoreManager & store handle classes in the server Change-Id: Idcb901e3d7fa4b6bfdb753d428b7ad06c0668c9f Signed-off-by: Mu-Woong Lee --- diff --git a/src/server/Store.cpp b/src/server/Store.cpp new file mode 100644 index 0000000..1bd089b --- /dev/null +++ b/src/server/Store.cpp @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Store.h" + +using namespace ctx; + +Store::Metadata::Metadata() : + retention(0), + limit(0) +{ +} + +Store::Store() +{ +} + +Store::~Store() +{ +} + +bool Store::permitted(ContextStoreClient& client) +{ + return __readable(client) || __writable(client); +} + +const std::string& Store::getPath() +{ + return metadata.path; +} + +int Store::insert(ContextStoreClient& client, const std::string& columns, std::vector& tuples) +{ + //TODO: handling owner's ID + //TODO: delete expired data + if (!__writable(client)) + return E_ACCESS; + + _D("Inserting %u tuples of (%s) to %s", tuples.size(), columns.c_str(), metadata.uri.c_str()); + + int error = __getDatabase().insert(metadata.uri, columns, tuples) ? E_NONE : E_PARAM; + return error; +} + +int Store::retrieve(ContextStoreClient& client, + const std::string& projection, const std::string& selection, + const std::string& sortOrder, unsigned int limit, std::vector* tuples) +{ + if (!__readable(client)) + return E_ACCESS; + + _D("Retrieving (%s) from %s", projection.c_str(), metadata.uri.c_str()); + + std::string query = "SELECT " + projection + " FROM [" + metadata.uri + "]"; + if (!selection.empty()) + query = query + " WHERE " + selection; + if (!sortOrder.empty()) + query = query + " ORDER BY " + sortOrder; + if (limit > 0) + query = query + " LIMIT " + std::to_string(limit); + + if (!__getDatabase().execute(query, tuples)) + return E_PARAM; + + return E_NONE; +} + +int Store::remove(ContextStoreClient& client, const std::string selection) +{ + //TODO: handling owner's ID + if (!__writable(client)) + return E_ACCESS; + + _D("Removing from %s", metadata.uri.c_str()); + + std::string query = "DELETE FROM [" + metadata.uri + "]"; + if (!selection.empty()) + query = query + " WHERE " + selection; + + if (!__getDatabase().execute(query, NULL)) + return E_PARAM; + + return E_NONE; +} diff --git a/src/server/Store.h b/src/server/Store.h new file mode 100644 index 0000000..c51778a --- /dev/null +++ b/src/server/Store.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_STORE_STORE_H__ +#define __CONTEXT_STORE_STORE_H__ + +#include +#include +#include +#include +#include + +namespace ctx { + + class ContextStoreClient; + + class Store { + public: + class Metadata { + public: + Metadata(); + + std::string path; + std::string uri; + unsigned int retention; + unsigned int limit; + std::vector readPrivileges; + std::vector writePrivileges; + }; + + virtual ~Store(); + + bool permitted(ContextStoreClient& client); + + const std::string& getPath(); + + int insert(ContextStoreClient& client, const std::string& columns, std::vector& tuples); + + int retrieve(ContextStoreClient& client, + const std::string& projection, const std::string& selection, + const std::string& sortOrder, unsigned int limit, + std::vector* tuples); + + int remove(ContextStoreClient& client, const std::string selection); + + protected: + Store(); + + Metadata metadata; + + private: + virtual bool __readable(ContextStoreClient& client) = 0; + virtual bool __writable(ContextStoreClient& client) = 0; + virtual Database& __getDatabase() = 0; + + friend class StoreManager; + }; + +} + +#endif /* __CONTEXT_STORE_STORE_H__ */ diff --git a/src/server/StoreManager.cpp b/src/server/StoreManager.cpp new file mode 100644 index 0000000..9c36f95 --- /dev/null +++ b/src/server/StoreManager.cpp @@ -0,0 +1,152 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "DatabaseManager.h" +#include "UserStore.h" +#include "SystemStore.h" +#include "StoreManager.h" + +#define CACHE_SIZE 3 + +using namespace ctx; + +StoreManager::StoreManager() +{ +} + +StoreManager::~StoreManager() +{ +} + +Store* StoreManager::__findStore(std::list& stores, const std::string& uri) +{ + for (auto& store : stores) { + if (store->metadata.uri != uri) + continue; + + _D("Cache hit!"); + return store; + } + + return NULL; +} + +std::vector __tokenize(std::string& in, const char* delim) +{ + std::vector tokens; + std::size_t begin = 0; + std::size_t end = in.find(delim, 0); + + while (end != std::string::npos) { + tokens.push_back(in.substr(begin, end - begin)); + begin = end + 1; + end = in.find(delim, begin); + } + + return tokens; +} + +template +Store* StoreManager::__createStore(Database& database, std::list& stores, const std::string& uri) +{ + std::string query = + "SELECT retention, \"limit\", readPrivileges, writePrivileges" + " FROM ContextStoreSchema" + " WHERE uri='" + uri + "'"; + + std::vector tuples; + if (!database.execute(query, COL_INT64 COL_INT64 COL_STRING COL_STRING, NULL, &tuples)) { + _E("DB search failed"); + return NULL; + } + if (tuples.empty()) { + _W("Not found"); + return NULL; + } + + int64_t retention = 0; + int64_t limit = 0; + std::string readPrivil; + std::string writePrivil; + + tuples[0]->getAt(0, &retention); + tuples[0]->getAt(1, &limit); + tuples[0]->getAt(2, &readPrivil); + tuples[0]->getAt(3, &writePrivil); + + delete tuples[0]; + + _D("URI: %s, Retention: %u, Limit: %u", uri.c_str(), static_cast(retention), static_cast(limit)); + _D("Read: %s", readPrivil.c_str()); + _D("Write: %s", writePrivil.c_str()); + + StoreType* store = new(std::nothrow) StoreType(); + IF_FAIL_RETURN_TAG(store, NULL, _E, "Memory allocation failed"); + + store->metadata.uri = uri; + store->metadata.retention= static_cast(retention); + store->metadata.limit = static_cast(limit); + store->metadata.readPrivileges = __tokenize(readPrivil, PRIVILEGE_DELIM); + store->metadata.writePrivileges = __tokenize(writePrivil, PRIVILEGE_DELIM); + + stores.push_front(store); + if (stores.size() > CACHE_SIZE) { + delete stores.back(); + stores.pop_back(); + } + + return store; +} + +void StoreManager::flushUserCache() +{ + for (auto& store : __userStores) { + delete store; + } + __userStores.clear(); +} + +void StoreManager::flushSystemCache() +{ + for (auto& store : __systemStores) { + delete store; + } + __systemStores.clear(); +} + +Store* StoreManager::getUserStore(const std::string& uri) +{ + _D("Checking the user DB"); + Store* store = __findStore(__userStores, uri); + if (store) + return store; + + store = __createStore(DatabaseManager::getUser(), __userStores, uri); + if (store) + return store; + + return getSystemStore(uri); +} + +Store* StoreManager::getSystemStore(const std::string& uri) +{ + _D("Checking the system DB"); + Store* store = __findStore(__systemStores, uri); + if (store) + return store; + + return __createStore(DatabaseManager::getSystem(), __systemStores, uri); +} diff --git a/src/server/StoreManager.h b/src/server/StoreManager.h new file mode 100644 index 0000000..f8d8e63 --- /dev/null +++ b/src/server/StoreManager.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_STORE_STORE_MANAGER_H__ +#define __CONTEXT_STORE_STORE_MANAGER_H__ + +#include +#include + +namespace ctx { + + class Store; + class Database; + + class StoreManager { + public: + StoreManager(); + ~StoreManager(); + + Store* getUserStore(const std::string& uri); + Store* getSystemStore(const std::string& uri); + + void flushUserCache(); + void flushSystemCache(); + + private: + Store* __findStore(std::list& stores, const std::string& uri); + + template + Store* __createStore(Database& database, std::list& stores, const std::string& uri); + + std::list __userStores; + std::list __systemStores; + }; + +} + +#endif /* __CONTEXT_STORE_STORE_MANAGER_H__ */ diff --git a/src/server/SystemStore.cpp b/src/server/SystemStore.cpp new file mode 100644 index 0000000..9bf5fb7 --- /dev/null +++ b/src/server/SystemStore.cpp @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ContextStoreClient.h" +#include "DatabaseManager.h" +#include "SystemStore.h" + +using namespace ctx; + +SystemStore::SystemStore() +{ +} + +SystemStore::~SystemStore() +{ +} + +bool SystemStore::__readable(ContextStoreClient& client) +{ + if (!client.hasPrivileges(metadata.readPrivileges)) + return false; + + return true; +} + +bool SystemStore::__writable(ContextStoreClient& client) +{ + if (!client.isSystem()) + return false; + + if (!client.hasPrivileges(metadata.writePrivileges)) + return false; + + return true; +} + +Database& SystemStore::__getDatabase() +{ + return DatabaseManager::getSystem(); +} diff --git a/src/server/SystemStore.h b/src/server/SystemStore.h new file mode 100644 index 0000000..12b6865 --- /dev/null +++ b/src/server/SystemStore.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_STORE_SYSTEM_STORE_H__ +#define __CONTEXT_STORE_SYSTEM_STORE_H__ + +#include "Store.h" + +namespace ctx { + + class SystemStore : public Store { + public: + ~SystemStore(); + + private: + SystemStore(); + + bool __readable(ContextStoreClient& client); + bool __writable(ContextStoreClient& client); + Database& __getDatabase(); + + friend class StoreManager; + }; + +} + +#endif /* __CONTEXT_STORE_SYSTEM_STORE_H__ */ diff --git a/src/server/UserStore.cpp b/src/server/UserStore.cpp new file mode 100644 index 0000000..e636190 --- /dev/null +++ b/src/server/UserStore.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ContextStoreClient.h" +#include "DatabaseManager.h" +#include "UserStore.h" + +using namespace ctx; + +UserStore::UserStore() +{ +} + +UserStore::~UserStore() +{ +} + +bool UserStore::__readable(ContextStoreClient& client) +{ + if (client.isSystem()) + return false; + + if (!client.hasPrivileges(metadata.readPrivileges)) + return false; + + return true; +} + +bool UserStore::__writable(ContextStoreClient& client) +{ + if (client.isSystem()) + return false; + + if (!client.hasPrivileges(metadata.writePrivileges)) + return false; + + return true; +} + +Database& UserStore::__getDatabase() +{ + return DatabaseManager::getUser(); +} diff --git a/src/server/UserStore.h b/src/server/UserStore.h new file mode 100644 index 0000000..0ff8896 --- /dev/null +++ b/src/server/UserStore.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CONTEXT_STORE_USER_STORE_H__ +#define __CONTEXT_STORE_USER_STORE_H__ + +#include "Store.h" + +namespace ctx { + + class UserStore : public Store { + public: + ~UserStore(); + + private: + UserStore(); + + bool __readable(ContextStoreClient& client); + bool __writable(ContextStoreClient& client); + Database& __getDatabase(); + + friend class StoreManager; + }; + +} + +#endif /* __CONTEXT_STORE_USER_STORE_H__ */