client-cpp  0.10.0
SQLiteDBLogStorage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014-2016 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"
32 
33 
34 
35 #define KAA_DEFAULT_LOG_DB_STORAGE "logs.db"
36 
37 namespace kaa {
38 
39 class IKaaClientContext;
40 
42 {
44 
49 
54 };
55 
57 public:
59  std::size_t bucketSize = LogStorageConstants::DEFAULT_MAX_BUCKET_SIZE,
60  std::size_t bucketRecordCount = LogStorageConstants::DEFAULT_MAX_BUCKET_RECORD_COUNT);
61 
63  const std::string& dbName,
64  int optimizationMask = (int)SQLiteOptimizationOptions::SQLITE_NO_OPTIMIZATIONS,
65  std::size_t bucketSize = LogStorageConstants::DEFAULT_MAX_BUCKET_SIZE,
66  std::size_t bucketRecordCount = LogStorageConstants::DEFAULT_MAX_BUCKET_RECORD_COUNT);
67 
69 
70  virtual BucketInfo addLogRecord(LogRecord&& record);
71  virtual ILogStorageStatus& getStatus() { return *this; }
72 
73  virtual LogBucket getNextBucket();
74  virtual void removeBucket(std::int32_t bucketId);
75  virtual void rollbackBucket(std::int32_t bucketId);
76 
77  virtual std::size_t getConsumedVolume();
78  virtual std::size_t getRecordsCount();
79 
80 private:
81  void init(int optimizationMask);
82 
83  void openDBConnection();
84  void closeDBConnection();
85 
86  void initDBTables();
87  void applyDBOptimization(int mask);
88 
89  bool checkBucketOverflow(const LogRecord& record) {
90  return (currentBucketSize_ + record.getSize() > maxBucketSize_) ||
91  (currentBucketRecordCount_ + 1 > maxBucketRecordCount_);
92  }
93 
94  void addNextBucket();
95  void markBucketAsInUse(std::int32_t id);
96  void markBucketsAsFree();
97  bool retrieveLastBucketInfo();
98 
99  void retrieveConsumedSizeAndVolume();
100  bool truncateIfBucketSizeIncompatible();
101 
102  std::string storageStatisticsToStr() {
103  return (boost::format("Storage: total_logs %d, unmarked_logs %d, total_size: %d B")
104  % totalRecordCount_ % unmarkedRecordCount_ % consumedMemory_).str();
105  }
106 
107  std::string bucketStatisticsToStr() {
108  return (boost::format("Bucket: id %d, logs %d, size %d B")
109  % currentBucketId_ % currentBucketRecordCount_ % currentBucketSize_).str();
110  }
111 
112 private:
113  struct InnerBucketInfo {
114  InnerBucketInfo(std::size_t sizeInBytes, std::size_t sizeInLogs)
115  : sizeInBytes_(sizeInBytes), sizeInLogs_(sizeInLogs) {}
116 
117  std::size_t sizeInBytes_ = 0;
118  std::size_t sizeInLogs_ = 0;
119  };
120 
121 private:
122 
123  const std::string dbName_;
124  sqlite3 *db_ = nullptr;
125 
126  const std::size_t maxBucketSize_;
127  const std::size_t maxBucketRecordCount_;
128 
129  std::int32_t currentBucketId_ = 0;
130  std::size_t currentBucketSize_ = 0;
131  std::size_t currentBucketRecordCount_ = 0;
132 
133  std::size_t unmarkedRecordCount_ = 0;
134  std::size_t totalRecordCount_ = 0;
135 
136  std::size_t consumedMemory_ = 0;
137  std::unordered_map<std::int32_t/*Bucket id*/, InnerBucketInfo> consumedMemoryStorage_;
138 
139  KAA_MUTEX_DECLARE(sqliteLogStorageGuard_);
140 
141  IKaaClientContext &context_;
142 };
143 
144 } /* namespace kaa */
145 
146 #endif /* SQLITEDBLOGSTORAGE_HPP_ */
SQLiteDBLogStorage(IKaaClientContext &context, std::size_t bucketSize=LogStorageConstants::DEFAULT_MAX_BUCKET_SIZE, std::size_t bucketRecordCount=LogStorageConstants::DEFAULT_MAX_BUCKET_RECORD_COUNT)
The helper class which is used to transfer logs from LogStorage to LogCollector.
Definition: LogBucket.hpp:33
virtual std::size_t getRecordsCount()
Returns the number of collected logs.
std::size_t getSize() const
Definition: LogRecord.hpp:50
virtual LogBucket getNextBucket()
Returns a new log bucket.
virtual void removeBucket(std::int32_t bucketId)
Tells a log storage to remove a log bucket.
virtual BucketInfo addLogRecord(LogRecord &&record)
Persists a log record.
virtual std::size_t getConsumedVolume()
Returns amount of bytes collected logs are consumed.
virtual ILogStorageStatus & getStatus()
Returns a log storage status.
static const std::size_t DEFAULT_MAX_BUCKET_RECORD_COUNT
static const std::size_t DEFAULT_MAX_BUCKET_SIZE
Describes a unique log bucket.
Definition: BucketInfo.hpp:31
SQLiteOptimizationOptions
Interface of a log storage.
Definition: ILogStorage.hpp:40
virtual void rollbackBucket(std::int32_t bucketId)
Tells a log storage to consider a log bucket as unused, i.e. a log bucket will be accessible again vi...
The public interface to represent the current log storage state.