Implement StoreManager & store handle classes in the server 09/118909/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 14 Mar 2017 11:55:52 +0000 (20:55 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 14 Mar 2017 13:09:57 +0000 (06:09 -0700)
Change-Id: Idcb901e3d7fa4b6bfdb753d428b7ad06c0668c9f
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/Store.cpp [new file with mode: 0644]
src/server/Store.h [new file with mode: 0644]
src/server/StoreManager.cpp [new file with mode: 0644]
src/server/StoreManager.h [new file with mode: 0644]
src/server/SystemStore.cpp [new file with mode: 0644]
src/server/SystemStore.h [new file with mode: 0644]
src/server/UserStore.cpp [new file with mode: 0644]
src/server/UserStore.h [new file with mode: 0644]

diff --git a/src/server/Store.cpp b/src/server/Store.cpp
new file mode 100644 (file)
index 0000000..1bd089b
--- /dev/null
@@ -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<Tuple*>& 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<Tuple*>* 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 (file)
index 0000000..c51778a
--- /dev/null
@@ -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 <vector>
+#include <string>
+#include <Tuple.h>
+#include <Database.h>
+#include <ContextStoreTypesPrivate.h>
+
+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<std::string> readPrivileges;
+                       std::vector<std::string> writePrivileges;
+               };
+
+               virtual ~Store();
+
+               bool permitted(ContextStoreClient& client);
+
+               const std::string& getPath();
+
+               int insert(ContextStoreClient& client, const std::string& columns, std::vector<Tuple*>& tuples);
+
+               int retrieve(ContextStoreClient& client,
+                               const std::string& projection, const std::string& selection,
+                               const std::string& sortOrder, unsigned int limit,
+                               std::vector<Tuple*>* 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 (file)
index 0000000..9c36f95
--- /dev/null
@@ -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<Store*>& stores, const std::string& uri)
+{
+       for (auto& store : stores) {
+               if (store->metadata.uri != uri)
+                       continue;
+
+               _D("Cache hit!");
+               return store;
+       }
+
+       return NULL;
+}
+
+std::vector<std::string> __tokenize(std::string& in, const char* delim)
+{
+       std::vector<std::string> 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<typename StoreType>
+Store* StoreManager::__createStore(Database& database, std::list<Store*>& stores, const std::string& uri)
+{
+       std::string query =
+               "SELECT retention, \"limit\", readPrivileges, writePrivileges"
+               " FROM ContextStoreSchema"
+               " WHERE uri='" + uri + "'";
+
+       std::vector<Tuple*> 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<unsigned int>(retention), static_cast<unsigned int>(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<unsigned int>(retention);
+       store->metadata.limit = static_cast<unsigned int>(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<UserStore>(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<SystemStore>(DatabaseManager::getSystem(), __systemStores, uri);
+}
diff --git a/src/server/StoreManager.h b/src/server/StoreManager.h
new file mode 100644 (file)
index 0000000..f8d8e63
--- /dev/null
@@ -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 <list>
+#include <ContextStoreTypesPrivate.h>
+
+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<Store*>& stores, const std::string& uri);
+
+               template<typename StoreType>
+               Store* __createStore(Database& database, std::list<Store*>& stores, const std::string& uri);
+
+               std::list<Store*> __userStores;
+               std::list<Store*> __systemStores;
+       };
+
+}
+
+#endif /* __CONTEXT_STORE_STORE_MANAGER_H__ */
diff --git a/src/server/SystemStore.cpp b/src/server/SystemStore.cpp
new file mode 100644 (file)
index 0000000..9bf5fb7
--- /dev/null
@@ -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 (file)
index 0000000..12b6865
--- /dev/null
@@ -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 (file)
index 0000000..e636190
--- /dev/null
@@ -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 (file)
index 0000000..0ff8896
--- /dev/null
@@ -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__ */