client-cpp  0.6.1
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  : actualSize_(0)
43  , blockSize_(blockSize)
44  , finalized_(false)
45  {
46  }
47 
48  std::string blockId;
49  ILogStorage::container_type logs_;
50  std::size_t actualSize_;
51  std::size_t blockSize_;
52  bool finalized_;
53  } LogBlock;
54 
55 public:
56  MemoryLogStorage(std::size_t blockSize) : blockSize_(blockSize), occupiedSize_(0) {
57  LogBlock initialBlock(blockSize);
58  initialBlock.actualSize_ = 0;
59  initialBlock.finalized_ = false;
60  logBlocks_.push_back(initialBlock);
61  }
62  ~MemoryLogStorage() {}
63 
67  void addLogRecord(const LogRecord & record);
68  container_type getRecordBlock(std::size_t blockSize, const std::string& blockId);
69  void removeRecordBlock(const std::string& blockId);
70  void notifyUploadFailed(const std::string& blockId);
71  void removeOldestRecords(std::size_t allowedVolume);
72 
76  std::size_t getConsumedVolume() const;
77  std::size_t getRecordsCount() const;
78 
79 private:
80  void resize(std::size_t blockSize);
81 
82 private:
83  std::size_t blockSize_;
84  std::size_t occupiedSize_;
85  std::list<LogBlock> logBlocks_;
86 };
87 
88 } // namespace kaa
89 
90 #endif
91 
92 #endif /* MEMORYLOGSTORAGE_HPP_ */