client-cpp  0.7.4
SQLiteDBLogStorage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2015 CyberVision, Inc.
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 #ifndef SQLITEDBLOGSTORAGE_HPP_
18 #define SQLITEDBLOGSTORAGE_HPP_
19 
20 #include <memory>
21 #include <list>
22 #include <cstdint>
23 #include <string>
24 #include <unordered_map>
25 
26 #include <sqlite3.h>
27 
28 #include "kaa/KaaThread.hpp"
29 #include "kaa/log/ILogStorage.hpp"
31 
32 #define KAA_DEFAULT_LOG_DB_STORAGE "logs.db"
33 
34 namespace kaa {
35 
37 {
39 
44 
49 };
50 
52 public:
53  SQLiteDBLogStorage(const std::string& dbName = KAA_DEFAULT_LOG_DB_STORAGE
54  , int optimizationMask = (int)SQLiteOptimizationOptions::SQLITE_NO_OPTIMIZATIONS);
56 
57  virtual void addLogRecord(LogRecordPtr record);
58 
59  virtual ILogStorageStatus& getStatus() {return *this; }
60 
61  virtual RecordPack getRecordBlock(std::size_t blockSize, std::size_t recordsBlockCount);
62  virtual void removeRecordBlock(RecordBlockId id);
63  virtual void notifyUploadFailed(RecordBlockId id);
64 
65  virtual std::size_t getRecordsCount();
66  virtual std::size_t getConsumedVolume();
67 
68 private:
69  void openDBConnection();
70  void closeDBConnection();
71 
72  void initLogTable();
73  void applyOptimization(int mask);
74 
75  void resetBucketID();
76 
77  void updateBucketIDForRecords(std::int32_t id, std::list<int>& idList);
78  void removeRecordById(sqlite3_int64 id);
79 
80 private:
81  const std::string dbName_;
82  sqlite3 *db_;
83 
84  std::size_t unmarkedRecordCount_;
85  std::size_t totalRecordCount_;
86 
87  std::size_t consumedMemory_;
88  std::unordered_map<std::int32_t/*Bucket id*/, std::size_t /*Bucket size*/> consumedMemoryStorage_;
89 
90  KAA_MUTEX_DECLARE(sqliteLogStorageGuard_);
91 };
92 
93 } /* namespace kaa */
94 
95 #endif /* SQLITEDBLOGSTORAGE_HPP_ */
#define KAA_DEFAULT_LOG_DB_STORAGE
virtual void notifyUploadFailed(RecordBlockId id)
Notifies of the delivery of the log block marked by the specified id has been failed.
virtual RecordPack getRecordBlock(std::size_t blockSize, std::size_t recordsBlockCount)
Returns the block of log records which total size is less or equal to the specified block size...
std::pair< RecordBlockId, RecordBlock > RecordPack
The alias for the log block marked by the unique identifier.
Definition: ILogStorage.hpp:61
virtual std::size_t getRecordsCount()
Returns the number of collected logs.
virtual void removeRecordBlock(RecordBlockId id)
Removes the log block marked by the specified id.
std::int32_t RecordBlockId
The alias for the unique identifier of the requested log block.
Definition: ILogStorage.hpp:51
virtual std::size_t getConsumedVolume()
Returns amount of bytes collected logs are consumed.
virtual ILogStorageStatus & getStatus()
Returns the current log storage status.
std::shared_ptr< LogRecord > LogRecordPtr
Definition: ILogStorage.hpp:36
SQLiteOptimizationOptions
The public interface to access to the log storage.
Definition: ILogStorage.hpp:43
virtual void addLogRecord(LogRecordPtr record)
Adds the log record to the storage.
The public interface to represent the current log storage state.
SQLiteDBLogStorage(const std::string &dbName=KAA_DEFAULT_LOG_DB_STORAGE, int optimizationMask=(int) SQLiteOptimizationOptions::SQLITE_NO_OPTIMIZATIONS)