client-cpp  0.6.3
MemoryLogStorage.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2014 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 "kaa/KaaDefaults.hpp"
21 
22 #ifdef KAA_USE_LOGGING
23 
24 #include <list>
25 #include <cstdint>
26 
27 #include "kaa/log/ILogStorage.hpp"
30 
31 namespace kaa {
32 
38 class MemoryLogStorage : public ILogStorage, public ILogStorageStatus {
39 private:
40  typedef struct __MemoryLogStorage__LogBlock__ {
41  __MemoryLogStorage__LogBlock__(size_t blockSize)
42  : blockId(0)
43  , actualSize_(0)
44  , blockSize_(blockSize)
45  , finalized_(false)
46  {
47  }
48 
49  std::int32_t blockId;
50  ILogStorage::container_type logs_;
51  std::size_t actualSize_;
52  std::size_t blockSize_;
53  bool finalized_;
54  } LogBlock;
55 
56 public:
57  MemoryLogStorage(std::size_t blockSize) : blockSize_(blockSize), occupiedSize_(0) {
58  LogBlock initialBlock(blockSize);
59  initialBlock.actualSize_ = 0;
60  initialBlock.finalized_ = false;
61  logBlocks_.push_back(initialBlock);
62  }
63  ~MemoryLogStorage() {}
64 
68  void addLogRecord(const LogRecord & record);
69  container_type getRecordBlock(std::size_t blockSize, std::int32_t blockId);
70  void removeRecordBlock(std::int32_t blockId);
71  void notifyUploadFailed(std::int32_t blockId);
72  void removeOldestRecords(std::size_t allowedVolume);
73 
77  std::size_t getConsumedVolume() const;
78  std::size_t getRecordsCount() const;
79 
80 private:
81  void resize(std::size_t blockSize);
82 
83 private:
84  std::size_t blockSize_;
85  std::size_t occupiedSize_;
86  std::list<LogBlock> logBlocks_;
87 };
88 
89 } // namespace kaa
90 
91 #endif
92 
93 #endif /* MEMORYLOGSTORAGE_HPP_ */