client-cpp  0.7.0
MemoryLogStorage.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 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"
26 
27 namespace kaa {
28 
36 public:
41 
55  MemoryLogStorage(size_t maxOccupiedSize, float percentToDelete);
56 
57  virtual void addLogRecord(LogRecordPtr serializedRecord);
58  virtual ILogStorageStatus& getStatus() { return *this; }
59 
60  virtual RecordPack getRecordBlock(std::size_t blockSize);
61  virtual void removeRecordBlock(RecordBlockId blockId);
62  virtual void notifyUploadFailed(RecordBlockId blockId);
63 
64  virtual std::size_t getConsumedVolume();
65  virtual std::size_t getRecordsCount();
66 
67 private:
68  void shrinkToSize(std::size_t allowedVolume);
69 
70 private:
71  struct LogRecordWrapper {
72  LogRecordWrapper(LogRecordPtr record, RecordBlockId id = NO_OWNER)
73  : record_(record), blockId_(id) {}
74 
75  LogRecordPtr record_;
76  RecordBlockId blockId_;
77  };
78 
79  typedef RequestId BlockId;
80 
81 private:
82  size_t totalOccupiedSize_ = 0;
83  size_t occupiedSizeOfUnmarkedRecords_ = 0;
84 
85  size_t unmarkedRecordCount_ = 0;
86 
87  size_t maxOccupiedSize_ = 0;
88  size_t shrinkedSize_ = 0;
89 
90  std::list<LogRecordWrapper> logs_;
91  KAA_MUTEX_DECLARE(logsGuard_);
92 
93  BlockId recordBlockId_;
94  static const BlockId NO_OWNER;
95 };
96 
97 } // namespace kaa
98 
99 #endif /* MEMORYLOGSTORAGE_HPP_ */
virtual ILogStorageStatus & getStatus()
Returns the current log storage status.
std::pair< RecordBlockId, RecordBlock > RecordPack
The alias for the log block marked by the unique identifier.
Definition: ILogStorage.hpp:61
The default ILogStorage implementation.
virtual std::size_t getRecordsCount()
Returns the number of collected logs.
std::int32_t RecordBlockId
The alias for the unique identifier of the requested log block.
Definition: ILogStorage.hpp:51
std::int32_t RequestId
Definition: KaaThread.hpp:82
virtual void notifyUploadFailed(RecordBlockId blockId)
Notifies of the delivery of the log block marked by the specified id has been failed.
std::shared_ptr< LogRecord > LogRecordPtr
Definition: ILogStorage.hpp:36
virtual void removeRecordBlock(RecordBlockId blockId)
Removes the log block marked by the specified id.
virtual RecordPack getRecordBlock(std::size_t blockSize)
Returns the block of log records which total size is less or equal to the specified block size...
The public interface to access to the log storage.
Definition: ILogStorage.hpp:43
virtual void addLogRecord(LogRecordPtr serializedRecord)
Adds the log record to the storage.
virtual std::size_t getConsumedVolume()
Returns amount of bytes collected logs are consumed.
MemoryLogStorage()
Creates the size-unlimited log storage.
The public interface to represent the current log storage state.