17 #ifndef SQLITEDBLOGSTORAGE_HPP_
18 #define SQLITEDBLOGSTORAGE_HPP_
24 #include <unordered_map>
35 #define KAA_DEFAULT_LOG_DB_STORAGE "logs.db"
39 class IKaaClientContext;
63 const std::string& dbName,
81 void init(
int optimizationMask);
83 void openDBConnection();
84 void closeDBConnection();
87 void applyDBOptimization(
int mask);
89 bool checkBucketOverflow(
const LogRecord& record) {
90 return (currentBucketSize_ + record.
getSize() > maxBucketSize_) ||
91 (currentBucketRecordCount_ + 1 > maxBucketRecordCount_);
95 void markBucketAsInUse(std::int32_t
id);
96 void markBucketsAsFree();
97 bool retrieveLastBucketInfo();
99 void retrieveConsumedSizeAndVolume();
100 bool truncateIfBucketSizeIncompatible();
102 std::string storageStatisticsToStr() {
103 return (boost::format(
"Storage: total_logs %d, unmarked_logs %d, total_size: %d B")
104 % totalRecordCount_ % unmarkedRecordCount_ % consumedMemory_).str();
107 std::string bucketStatisticsToStr() {
108 return (boost::format(
"Bucket: id %d, logs %d, size %d B")
109 % currentBucketId_ % currentBucketRecordCount_ % currentBucketSize_).str();
113 struct InnerBucketInfo {
114 InnerBucketInfo(std::size_t sizeInBytes, std::size_t sizeInLogs)
115 : sizeInBytes_(sizeInBytes), sizeInLogs_(sizeInLogs) {}
117 std::size_t sizeInBytes_ = 0;
118 std::size_t sizeInLogs_ = 0;
123 const std::string dbName_;
124 sqlite3 *db_ =
nullptr;
126 const std::size_t maxBucketSize_;
127 const std::size_t maxBucketRecordCount_;
129 std::int32_t currentBucketId_ = 0;
130 std::size_t currentBucketSize_ = 0;
131 std::size_t currentBucketRecordCount_ = 0;
133 std::size_t unmarkedRecordCount_ = 0;
134 std::size_t totalRecordCount_ = 0;
136 std::size_t consumedMemory_ = 0;
137 std::unordered_map<std::int32_t, InnerBucketInfo> consumedMemoryStorage_;
139 KAA_MUTEX_DECLARE(sqliteLogStorageGuard_);
141 IKaaClientContext &context_;
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.
virtual std::size_t getRecordsCount()
Returns the number of collected logs.
std::size_t getSize() const
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.
SQLiteOptimizationOptions
Interface of a log storage.
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.