sensor: re-sample pressure data and perform lazy insertions to reduce DB overhead
[platform/core/context/context-provider.git] / src / sensor / SensorLogger.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 <SensorRecorderTypes.h>
19 #include "TypesInternal.h"
20 #include "TimeUtil.h"
21 #include "SensorLogger.h"
22
23 using namespace ctx;
24
25 SensorLogger::SensorLogger() :
26         __lastRemovalTime(0)
27 {
28 }
29
30 SensorLogger::~SensorLogger()
31 {
32 }
33
34 void SensorLogger::flushCache(bool force)
35 {
36 }
37
38 bool SensorLogger::executeQuery(const char *query)
39 {
40         return __dbMgr.execute(0, query, NULL);
41 }
42
43 void SensorLogger::removeExpired(const char *subject, const char *tableName, const char *timeKey)
44 {
45         uint64_t timestamp = TimeUtil::getTime();
46
47         if (timestamp - __lastRemovalTime < SEC_TO_MS(SECONDS_PER_HOUR))
48                 return;
49
50         char *query = sqlite3_mprintf(
51                         "DELETE FROM %s WHERE %s < " \
52                         "%llu - (SELECT MAX(" KEY_RETENTION ") * 1000 FROM " CLIENT_INFO \
53                         " WHERE " KEY_SUBJECT "='%s')",
54                         tableName, timeKey, timestamp, subject);
55
56         __dbMgr.execute(0, query, NULL);
57         sqlite3_free(query);
58
59         __lastRemovalTime = timestamp;
60 }