server: add the record insertion API for other services in the context framework 50/126950/4
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 25 Apr 2017 10:31:01 +0000 (19:31 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Wed, 26 Apr 2017 08:38:15 +0000 (17:38 +0900)
Change-Id: I79ede86b7164e3b14a79ed9573f32eab92094db1
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
include/private/ContextStore.h [moved from include/private/ContextStoreSideGate.h with 60% similarity]
src/server-dummy/ContextStore.cpp [moved from src/server-dummy/ContextStoreSideGate.cpp with 54% similarity]
src/server/ContextStore.cpp [new file with mode: 0644]
src/server/ContextStoreService.cpp
src/server/ContextStoreSideGate.cpp [deleted file]
src/server/Store.cpp
src/server/Store.h

similarity index 60%
rename from include/private/ContextStoreSideGate.h
rename to include/private/ContextStore.h
index 777f6c5..947d675 100644 (file)
@@ -14,8 +14,8 @@
  * limitations under the License.
  */
 
-#ifndef __CONTEXT_STORE_SIDE_GATE_H__
-#define __CONTEXT_STORE_SIDE_GATE_H__
+#ifndef __CONTEXT_STORE_H__
+#define __CONTEXT_STORE_H__
 
 #include <Tuple.h>
 
@@ -23,17 +23,23 @@ namespace ctx {
 
        class ContextStoreService;
 
-       class EXPORT_API ContextStoreSideGate {
+       class EXPORT_API ContextStore {
        public:
-               ~ContextStoreSideGate();
+               ~ContextStore();
+
+               int insert(const std::string& columns, std::shared_ptr<Tuple> tuple);
 
                int insert(const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples);
 
-               static ContextStoreSideGate getUserStore(const std::string& uri);
-               static ContextStoreSideGate getSystemStore(const std::string& uri);
+               // This tries to get the user-level store corresponding to the uri first,
+               // but if no store exists, it goes down to get the system-level store of the given uri.
+               static ContextStore getUserStore(const std::string& uri);
+
+               // This directly goes to the system level and gets the system-level store of the given uri.
+               static ContextStore getSystemStore(const std::string& uri);
 
        private:
-               ContextStoreSideGate(const std::string& uri, bool isSystem);
+               ContextStore(const std::string& uri, bool isSystem);
 
                std::string __uri;
                bool __isSystem;
similarity index 54%
rename from src/server-dummy/ContextStoreSideGate.cpp
rename to src/server-dummy/ContextStore.cpp
index 0240651..6b8e6b7 100644 (file)
  */
 
 #include <ContextStoreService.h>
-#include <ContextStoreSideGate.h>
+#include <ContextStore.h>
 
 using namespace ctx;
 
-ContextStoreService* ContextStoreSideGate::__hostService = NULL;
+ContextStoreService* ContextStore::__hostService = NULL;
 
-ContextStoreSideGate::ContextStoreSideGate(const std::string& uri, bool isSystem) :
+ContextStore::ContextStore(const std::string& uri, bool isSystem) :
        __uri(uri),
        __isSystem(isSystem)
 {
 }
 
-ContextStoreSideGate::~ContextStoreSideGate()
+ContextStore::~ContextStore()
 {
 }
 
-int ContextStoreSideGate::insert(const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples)
+int ContextStore::insert(const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples)
 {
        return E_SUPPORT;
 }
 
-ContextStoreSideGate ContextStoreSideGate::getUserStore(const std::string& uri)
+ContextStore ContextStore::getUserStore(const std::string& uri)
 {
-       ContextStoreSideGate sideGate(uri, false);
-       return sideGate;
+       ContextStore store(uri, false);
+       return store;
 }
 
-ContextStoreSideGate ContextStoreSideGate::getSystemStore(const std::string& uri)
+ContextStore ContextStore::getSystemStore(const std::string& uri)
 {
-       ContextStoreSideGate sideGate(uri, true);
-       return sideGate;
+       ContextStore store(uri, true);
+       return store;
 }
diff --git a/src/server/ContextStore.cpp b/src/server/ContextStore.cpp
new file mode 100644 (file)
index 0000000..fdec8e6
--- /dev/null
@@ -0,0 +1,151 @@
+/*
+ * 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 <ContextStoreService.h>
+#include <ContextStore.h>
+#include "StoreManager.h"
+#include "Store.h"
+
+#define ROOT_UID 0
+
+using namespace ctx;
+
+
+class StoreTask {
+private:
+       ContextStoreService* __hostService;
+       std::string __uri;
+       bool __isSystem;
+
+protected:
+       StoreTask(ContextStoreService* svc, const std::string& uri, bool isSystem) :
+               __hostService(svc),
+               __uri(uri),
+               __isSystem(isSystem)
+       {
+       }
+
+       Store* getStore()
+       {
+               StoreManager& manager = __hostService->getStoreManager();
+
+               if (__isSystem)
+                       return manager.getSystemStore(__uri);
+               else if (__hostService->getActiveUser() != ROOT_UID)
+                       return manager.getUserStore(__uri);
+
+               _W("No active user");
+               return NULL;
+       }
+
+public:
+       virtual ~StoreTask() {}
+       virtual void run() = 0;
+};
+
+
+class InsertionTask : public StoreTask {
+private:
+       std::string __columns;
+       std::vector<std::shared_ptr<Tuple>> __tuples;
+
+public:
+       InsertionTask(ContextStoreService* svc, const std::string& uri, bool isSystem,
+                       const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples) :
+               StoreTask(svc, uri, isSystem),
+               __columns(columns),
+               __tuples(tuples)
+       {
+       }
+
+       void run()
+       {
+               Store* store = getStore();
+               IF_FAIL_VOID_TAG(store, _E, "Failed to find the store");
+
+               int err = store->insert(__columns, __tuples);
+               IF_FAIL_VOID_TAG(err == E_NONE, _E, "Execution failed");
+       }
+};
+
+
+static gboolean __runTask(gpointer data)
+{
+       StoreTask* task = static_cast<StoreTask*>(data);
+       task->run();
+       delete task;
+       return G_SOURCE_REMOVE;
+}
+
+static bool __pushTask(ContextStoreService* hostService, StoreTask* task)
+{
+       GMainContext* mainContext = hostService->getMainContext();
+       IF_FAIL_RETURN_TAG(mainContext, false, _E, "Service not ready");
+
+       GSource* gSrc = g_idle_source_new();
+       IF_FAIL_RETURN_TAG(gSrc, false, _E, E_STR_ALLOC);
+
+       g_source_set_callback(gSrc, __runTask, task, NULL);
+       g_source_attach(gSrc, mainContext);
+       g_source_unref(gSrc);
+
+       return true;
+}
+
+
+ContextStoreService* ContextStore::__hostService = NULL;
+
+ContextStore::ContextStore(const std::string& uri, bool isSystem) :
+       __uri(uri),
+       __isSystem(isSystem)
+{
+}
+
+ContextStore::~ContextStore()
+{
+}
+
+int ContextStore::insert(const std::string& columns, std::shared_ptr<Tuple> tuple)
+{
+       std::vector<std::shared_ptr<Tuple>> tuples;
+       tuples.push_back(tuple);
+       return insert(columns, tuples);
+}
+
+int ContextStore::insert(const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples)
+{
+       _D("[%s] %s, #records = %d", __isSystem ? "system" : "user", __uri.c_str(), tuples.size());
+
+       StoreTask* task = new InsertionTask(__hostService, __uri, __isSystem, columns, tuples);
+
+       if (__pushTask(__hostService, task))
+               return E_NONE;
+
+       delete task;
+       return E_FAILED;
+}
+
+ContextStore ContextStore::getUserStore(const std::string& uri)
+{
+       ContextStore store(uri, false);
+       return store;
+}
+
+ContextStore ContextStore::getSystemStore(const std::string& uri)
+{
+       ContextStore store(uri, true);
+       return store;
+}
index 2c138e8..35c73a0 100644 (file)
@@ -15,7 +15,7 @@
  */
 
 #include <ContextStoreTypesPrivate.h>
-#include <ContextStoreSideGate.h>
+#include <ContextStore.h>
 #include <ContextStoreService.h>
 #include "ContextStoreClient.h"
 #include "StoreManager.h"
@@ -28,7 +28,7 @@ ContextStoreService::ContextStoreService(GDBusConnection* conn) :
        ServiceBase(conn, CTX_CONTEXT_STORE, CTX_CONTEXT_STORE_SPEC),
        __storeManager(NULL)
 {
-       ContextStoreSideGate::__hostService = this;
+       ContextStore::__hostService = this;
 }
 
 ContextStoreService::~ContextStoreService()
diff --git a/src/server/ContextStoreSideGate.cpp b/src/server/ContextStoreSideGate.cpp
deleted file mode 100644 (file)
index 415087b..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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 <ContextStoreService.h>
-#include <ContextStoreSideGate.h>
-
-using namespace ctx;
-
-ContextStoreService* ContextStoreSideGate::__hostService = NULL;
-
-ContextStoreSideGate::ContextStoreSideGate(const std::string& uri, bool isSystem) :
-       __uri(uri),
-       __isSystem(isSystem)
-{
-}
-
-ContextStoreSideGate::~ContextStoreSideGate()
-{
-}
-
-int ContextStoreSideGate::insert(const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples)
-{
-       //TODO: Switch to the thread-default mainloop, get the Store object, and insert.
-       return E_SUPPORT;
-}
-
-ContextStoreSideGate ContextStoreSideGate::getUserStore(const std::string& uri)
-{
-       ContextStoreSideGate sideGate(uri, false);
-       return sideGate;
-}
-
-ContextStoreSideGate ContextStoreSideGate::getSystemStore(const std::string& uri)
-{
-       ContextStoreSideGate sideGate(uri, true);
-       return sideGate;
-}
index a7ff134..d05d3c8 100644 (file)
@@ -56,6 +56,15 @@ int Store::insert(ContextStoreClient& client, const std::string& columns, std::v
        return error;
 }
 
+int Store::insert(const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples)
+{
+       _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;
+       tuples.clear();
+       return error;
+}
+
 int Store::retrieve(ContextStoreClient& client,
                const std::string& projection, const std::string& selection,
                const std::string& sortOrder, unsigned int limit, std::vector<std::shared_ptr<Tuple>>* tuples)
index d2edfc6..42304f4 100644 (file)
@@ -49,6 +49,8 @@ namespace ctx {
 
                int insert(ContextStoreClient& client, const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples);
 
+               int insert(const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples);
+
                int retrieve(ContextStoreClient& client,
                                const std::string& projection, const std::string& selection,
                                const std::string& sortOrder, unsigned int limit,