sensor: re-sample pressure data and perform lazy insertions to reduce DB overhead
[platform/core/context/context-provider.git] / src / sensor / ClientInfo.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <sqlite3.h>
18 #include <Types.h>
19 #include <SensorRecorderTypes.h>
20 #include "TypesInternal.h"
21 #include "SensorProvider.h"
22 #include "ClientInfo.h"
23
24 using namespace ctx;
25
26 unsigned int ClientInfo::__refCnt = 0;
27 DatabaseManager *ClientInfo::__dbMgr = NULL;
28 UninstallMonitor *ClientInfo::__uninstallMonitor = NULL;
29
30 ClientInfo::ClientInfo()
31 {
32         if (++__refCnt != 1)
33                 return;
34
35         __uninstallMonitor = new(std::nothrow) UninstallMonitor();
36         IF_FAIL_VOID_TAG(__uninstallMonitor, _E, "Memory allocation failed");
37
38         __dbMgr = new(std::nothrow) DatabaseManager();
39         IF_FAIL_VOID_TAG(__dbMgr, _E, "Memory allocation failed");
40
41         bool ret = __dbMgr->executeSync(
42                         "CREATE TABLE IF NOT EXISTS " CLIENT_INFO " (" \
43                                 KEY_SUBJECT " TEXT NOT NULL," \
44                                 KEY_PKG_ID " TEXT NOT NULL," \
45                                 KEY_OPTION " TEXT NOT NULL," \
46                                 KEY_RETENTION " INTEGER NOT NULL," \
47                                 "PRIMARY KEY (" KEY_SUBJECT "," KEY_PKG_ID ")" \
48                         ")", NULL);
49
50         IF_FAIL_VOID_TAG(ret, _E, "Table creation failed");
51 }
52
53 ClientInfo::~ClientInfo()
54 {
55         if (--__refCnt != 0)
56                 return;
57
58         delete __dbMgr;
59         __dbMgr = NULL;
60
61         delete __uninstallMonitor;
62         __uninstallMonitor = NULL;
63 }
64
65 int ClientInfo::get(std::string subject, std::string pkgId, Json& option)
66 {
67         IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
68
69         bool ret;
70         std::string optStr;
71         std::vector<Json> records;
72         char *query = sqlite3_mprintf(
73                         "SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
74                         KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
75                         subject.c_str(), pkgId.c_str());
76
77         ret = __dbMgr->executeSync(query, &records);
78         sqlite3_free(query);
79
80         IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
81         IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
82         IF_FAIL_RETURN(records[0].get(NULL, KEY_OPTION, &optStr), ERR_OPERATION_FAILED);
83
84         option = optStr;
85
86         return ERR_NONE;
87 }
88
89 int ClientInfo::get(std::string subject, std::vector<Json>& options)
90 {
91         IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
92
93         bool ret;
94         std::string optStr;
95         std::vector<Json> records;
96         char *query = sqlite3_mprintf(
97                         "SELECT " KEY_OPTION " FROM " CLIENT_INFO " WHERE " \
98                         KEY_SUBJECT "='%q'",
99                         subject.c_str());
100
101         ret = __dbMgr->executeSync(query, &records);
102         sqlite3_free(query);
103
104         IF_FAIL_RETURN(ret, ERR_OPERATION_FAILED);
105         IF_FAIL_RETURN(!records.empty(), ERR_NO_DATA);
106
107         for (Json& jObj : records) {
108                 if (!jObj.get(NULL, KEY_OPTION, &optStr))
109                         continue;
110                 options.push_back(Json(optStr));
111         }
112
113         return ERR_NONE;
114 }
115
116 bool ClientInfo::exist(std::string subject)
117 {
118         IF_FAIL_RETURN_TAG(__dbMgr, ERR_OPERATION_FAILED, _W, "DB not initialized");
119
120         bool ret;
121         std::vector<Json> records;
122         char *query = sqlite3_mprintf(
123                         "SELECT " KEY_PKG_ID " FROM " CLIENT_INFO " WHERE " \
124                         KEY_SUBJECT "='%q' LIMIT 1",
125                         subject.c_str());
126
127         ret = __dbMgr->executeSync(query, &records);
128         sqlite3_free(query);
129
130         IF_FAIL_RETURN(ret, false);
131         IF_FAIL_RETURN(!records.empty(), false);
132
133         return true;
134 }
135
136 bool ClientInfo::set(std::string subject, std::string pkgId, Json option, int retentionPeriod)
137 {
138         IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
139
140         bool ret;
141         char *query = sqlite3_mprintf(
142                         "INSERT INTO " CLIENT_INFO " VALUES ('%q', '%q', '%q', %d)",
143                         subject.c_str(), pkgId.c_str(), option.str().c_str(), retentionPeriod);
144
145         ret = __dbMgr->executeSync(query, NULL);
146         sqlite3_free(query);
147
148         return ret;
149 }
150
151 bool ClientInfo::remove(std::string subject, std::string pkgId)
152 {
153         IF_FAIL_RETURN_TAG(__dbMgr, false, _W, "DB not initialized");
154
155         bool ret;
156         char *query = sqlite3_mprintf(
157                         "DELETE FROM " CLIENT_INFO " WHERE " \
158                         KEY_SUBJECT "='%q' AND " KEY_PKG_ID "='%q'",
159                         subject.c_str(), pkgId.c_str());
160
161         ret = __dbMgr->executeSync(query, NULL);
162         sqlite3_free(query);
163
164         return ret;
165 }
166
167 void ClientInfo::purgeClient(std::string pkgId)
168 {
169         IF_FAIL_VOID_TAG(__dbMgr, _W, "DB not initialized");
170
171         bool ret;
172         std::string subject;
173         std::vector<Json> records;
174
175         char *query = sqlite3_mprintf(
176                         "SELECT " KEY_SUBJECT " FROM " CLIENT_INFO " WHERE " KEY_PKG_ID "='%q'",
177                         pkgId.c_str());
178
179         ret = __dbMgr->executeSync(query, &records);
180         sqlite3_free(query);
181         IF_FAIL_VOID(ret);
182
183         for (Json& jObj : records) {
184                 if (!jObj.get(NULL, KEY_SUBJECT, &subject))
185                         continue;
186                 _I("Stop recording '%s' for '%s'", subject.c_str(), pkgId.c_str());
187                 SensorProvider::removeClient(subject, pkgId);
188         }
189 }