client-cpp  0.10.0
MemoryLogStorage.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 MEMORYLOGSTORAGE_HPP_
18 #define MEMORYLOGSTORAGE_HPP_
19 
20 #include <list>
21 #include <cstdint>
22 
23 #include "kaa/KaaThread.hpp"
24 #include "kaa/log/ILogStorage.hpp"
27 
28 namespace kaa {
29 
30 class IKaaClientContext;
31 
39 public:
47  std::size_t bucketRecordCount = LogStorageConstants::DEFAULT_MAX_BUCKET_RECORD_COUNT);
48 
65  std::size_t maxOccupiedSize,
66  float percentToDelete,
67  std::size_t bucketSize = LogStorageConstants::DEFAULT_MAX_BUCKET_SIZE,
68  std::size_t bucketRecordCount = LogStorageConstants::DEFAULT_MAX_BUCKET_RECORD_COUNT);
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 shrinkToSize(std::size_t allowedVolume);
82 
83  void addNewBucket() {
84  buckets_.emplace_back(++currentBucketId_);
85  }
86 
87  bool checkBucketOverflow(const LogRecord& record) {
88  const auto& currentBucket = buckets_.back();
89  return (currentBucket.occupiedSize_ + record.getSize() > maxBucketSize_) ||
90  (currentBucket.logs_.size() + 1 > maxBucketRecordCount_);
91  }
92 
93  void internalAddLogRecord(LogRecord&& record);
94 
95 private:
96  enum class BucketState {
97  FREE,
98  IN_USE
99  };
100 
101  struct InternalBucket {
102  InternalBucket(std::int32_t bucketId)
103  : bucketId_(bucketId) {}
104 
105  BucketState state_ = BucketState::FREE;
106  std::int32_t bucketId_ = 0;
107  std::size_t occupiedSize_ = 0;
108  std::list<LogRecord> logs_;
109  };
110 
111 private:
112  const std::size_t maxBucketSize_;
113  const std::size_t maxBucketRecordCount_;
114 
115  std::int32_t currentBucketId_ = 0;
116 
117  std::size_t occupiedSizeOfUnmarkedRecords_ = 0;
118  std::size_t unmarkedRecordCount_ = 0;
119 
120  std::size_t totalOccupiedSize_ = 0;
121  std::size_t maxOccupiedSize_ = 0;
122  std::size_t shrinkedSize_ = 0;
123 
124  std::list<InternalBucket> buckets_;
125  KAA_MUTEX_DECLARE(memoryLogStorageGuard_);
126  IKaaClientContext &context_;
127 };
128 
129 } // namespace kaa
130 
131 #endif /* MEMORYLOGSTORAGE_HPP_ */
virtual BucketInfo addLogRecord(LogRecord &&record)
Persists a log record.
MemoryLogStorage(IKaaClientContext &context, std::size_t bucketSize=LogStorageConstants::DEFAULT_MAX_BUCKET_SIZE, std::size_t bucketRecordCount=LogStorageConstants::DEFAULT_MAX_BUCKET_RECORD_COUNT)
Creates the size-unlimited log storage.
virtual ILogStorageStatus & getStatus()
Returns a log storage status.
The helper class which is used to transfer logs from LogStorage to LogCollector.
Definition: LogBucket.hpp:33
The default ILogStorage implementation.
virtual std::size_t getRecordsCount()
Returns the number of collected logs.
virtual void removeBucket(std::int32_t bucketId)
Tells a log storage to remove a log bucket.
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
Interface of a log storage.
Definition: ILogStorage.hpp:40
virtual std::size_t getConsumedVolume()
Returns amount of bytes collected logs are consumed.
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...
virtual LogBucket getNextBucket()
Returns a new log bucket.
The public interface to represent the current log storage state.