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 (util::getActiveUid() != 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)
+static Store* __getStore(ContextStoreService* hostService, const std::string& uri, bool isSystem)
{
- StoreTask* task = static_cast<StoreTask*>(data);
- task->run();
- delete task;
- return G_SOURCE_REMOVE;
-}
+ StoreManager& manager = *(hostService->getStoreManager());
-static bool __pushTask(ContextStoreService* hostService, StoreTask* task)
-{
- GMainContext* mainContext = hostService->getMainContext();
- IF_FAIL_RETURN_TAG(mainContext, false, _E, "Service not ready");
+ if (isSystem)
+ return manager.getSystemStore(uri);
+ else if (util::getActiveUid() != ROOT_UID)
+ return manager.getUserStore(uri);
- 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;
+ _W("No active user");
+ return NULL;
}
-
ContextStoreService* ContextStore::__hostService = NULL;
ContextStore::ContextStore(const std::string& uri, bool isSystem) :
{
_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;
+ Store* store = __getStore(__hostService, __uri, __isSystem);
+ IF_FAIL_RETURN_TAG(store, E_PARAM, _E, "Failed to find the store");
- delete task;
- return E_FAILED;
+ return store->insert(columns, tuples);
}
ContextStore ContextStore::getUserStore(const std::string& uri)