* 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>
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;
*/
#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;
}
--- /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 <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;
+}
*/
#include <ContextStoreTypesPrivate.h>
-#include <ContextStoreSideGate.h>
+#include <ContextStore.h>
#include <ContextStoreService.h>
#include "ContextStoreClient.h"
#include "StoreManager.h"
ServiceBase(conn, CTX_CONTEXT_STORE, CTX_CONTEXT_STORE_SPEC),
__storeManager(NULL)
{
- ContextStoreSideGate::__hostService = this;
+ ContextStore::__hostService = this;
}
ContextStoreService::~ContextStoreService()
+++ /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 <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;
-}
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)
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,