--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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__ */
--- /dev/null
+/*
+ * 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);
+}
--- /dev/null
+/*
+ * 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__ */
--- /dev/null
+/*
+ * 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();
+}
--- /dev/null
+/*
+ * 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__ */
--- /dev/null
+/*
+ * 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();
+}
--- /dev/null
+/*
+ * 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__ */