Remove expired logs while creating Store objects 07/127907/1
authorMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 2 May 2017 09:04:32 +0000 (18:04 +0900)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Tue, 2 May 2017 09:04:32 +0000 (18:04 +0900)
Change-Id: I334abe0155bcbf6a8bc91c046c9639606b9753a2
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/Store.cpp
src/server/Store.h
src/server/StoreManager.cpp

index d05d3c8..80f529b 100644 (file)
@@ -45,7 +45,6 @@ const std::string& Store::getColumns()
 int Store::insert(ContextStoreClient& client, const std::string& columns, std::vector<std::shared_ptr<Tuple>>& tuples)
 {
        //TODO: handling owner's ID
-       //TODO: delete expired data
        if (!__writable(client))
                return E_ACCESS;
 
@@ -105,3 +104,30 @@ int Store::remove(ContextStoreClient& client, const std::string selection)
 
        return E_NONE;
 }
+
+void Store::removeExpired()
+{
+       std::string timePruningQuery;
+       std::string countPruningQuery;
+
+       // DELETE FROM <table> WHERE __timestamp < datetime('now', '-<retention> hours');
+       if (metadata.retention != 0) {
+               timePruningQuery =
+                       "DELETE FROM [" + metadata.uri + "] WHERE " + COL_TIMESTAMP +
+                       " < datetime('now', '-" + std::to_string(metadata.retention) + " hours');";
+       }
+
+       // DELETE FROM <table> WHERE rowid < (
+       //     SELECT MIN(rowid) FROM (
+       //         SELECT rowid FROM <table> ORDER BY rowid DESC LIMIT <limit>
+       //     )
+       // );
+       if (metadata.limit != 0) {
+               countPruningQuery =
+                       "DELETE FROM [" + metadata.uri + "] WHERE rowid <" \
+                       " (SELECT MIN(rowid) FROM (SELECT rowid FROM [" + metadata.uri + "]" \
+                       " ORDER BY rowid DESC LIMIT " + std::to_string(metadata.limit) + "));";
+       }
+
+       __getDatabase().execute(timePruningQuery + countPruningQuery, NULL);
+}
index 42304f4..543d66c 100644 (file)
@@ -58,6 +58,8 @@ namespace ctx {
 
                int remove(ContextStoreClient& client, const std::string selection);
 
+               void removeExpired();
+
        protected:
                Store();
 
index 3ee0012..1ff2b4c 100644 (file)
@@ -95,6 +95,8 @@ Store* StoreManager::__createStore(Database& database, std::list<Store*>& stores
                stores.pop_back();
        }
 
+       store->removeExpired();
+
        return store;
 }