Hybrid ICN (hICN) plugin  v21.06-rc0-4-g18fa668
suffix_strategy.h
1 /*
2  * Copyright (c) 2017-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #pragma once
17 
18 #include <core/manifest_format.h>
19 
20 namespace utils {
21 
22 using transport::core::NextSegmentCalculationStrategy;
23 
25  public:
26  static constexpr uint32_t INVALID_SUFFIX =
27  std::numeric_limits<uint32_t>::max();
28 
29  SuffixStrategy(NextSegmentCalculationStrategy strategy)
30  : suffix_stragegy_(strategy),
31  total_count_(0),
32  final_suffix_(INVALID_SUFFIX) {}
33 
34  virtual ~SuffixStrategy() = default;
35 
36  virtual uint32_t checkNextSuffix() = 0;
37 
38  virtual uint32_t getNextSuffix() = 0;
39 
40  virtual uint32_t getFinalSuffix() { return final_suffix_; }
41 
42  virtual void setFinalSuffix(std::uint32_t final_suffix) {
43  if (final_suffix != INVALID_SUFFIX) {
44  final_suffix_ = final_suffix;
45  }
46  }
47 
48  virtual uint32_t checkNextManifestSuffix() = 0;
49 
50  virtual uint32_t getNextManifestSuffix() = 0;
51 
52  virtual uint32_t checkNextContentSuffix() = 0;
53 
54  virtual uint32_t getNextContentSuffix() = 0;
55 
56  virtual void reset(uint32_t offset = 0) = 0;
57 
58  virtual uint32_t getManifestCapacity() = 0;
59 
60  virtual void setManifestCapacity(uint32_t capacity) = 0;
61 
62  virtual uint32_t getTotalCount() { return total_count_; };
63 
64  NextSegmentCalculationStrategy getSuffixStrategy() {
65  return suffix_stragegy_;
66  }
67 
68  protected:
69  inline void incrementTotalCount() { total_count_++; };
70 
71  protected:
72  NextSegmentCalculationStrategy suffix_stragegy_;
73  std::uint32_t total_count_;
74  std::uint32_t final_suffix_;
75 };
76 
78  public:
79  IncrementalSuffixStrategy(std::uint32_t start_offset)
80  : SuffixStrategy(NextSegmentCalculationStrategy::INCREMENTAL),
81  next_suffix_(start_offset) {}
82 
83  TRANSPORT_ALWAYS_INLINE std::uint32_t checkNextSuffix() override {
84  return next_suffix_;
85  }
86 
87  TRANSPORT_ALWAYS_INLINE std::uint32_t getNextSuffix() override {
88  incrementTotalCount();
89  return next_suffix_++;
90  }
91 
92  TRANSPORT_ALWAYS_INLINE std::uint32_t checkNextContentSuffix() override {
93  return checkNextSuffix();
94  }
95 
96  TRANSPORT_ALWAYS_INLINE std::uint32_t getNextContentSuffix() override {
97  return getNextSuffix();
98  }
99 
100  TRANSPORT_ALWAYS_INLINE std::uint32_t checkNextManifestSuffix() override {
101  return checkNextSuffix();
102  }
103 
104  TRANSPORT_ALWAYS_INLINE std::uint32_t getNextManifestSuffix() override {
105  return getNextSuffix();
106  }
107 
108  uint32_t getManifestCapacity() override {
110  "No manifest capacity in IncrementalSuffixStrategy.");
111  }
112 
113  void setManifestCapacity(uint32_t capacity) override {
115  "No manifest capacity in IncrementalSuffixStrategy.");
116  }
117 
118  void reset(std::uint32_t offset = 0) override { next_suffix_ = offset; }
119 
120  protected:
121  std::uint32_t next_suffix_;
122 };
123 
125  public:
126  CapacityBasedSuffixStrategy(std::uint32_t start_offset,
127  std::uint32_t manifest_capacity)
128  : SuffixStrategy(NextSegmentCalculationStrategy::INCREMENTAL),
129  next_suffix_(start_offset),
130  segments_in_manifest_(manifest_capacity),
131  current_manifest_iteration_(0) {}
132 
133  TRANSPORT_ALWAYS_INLINE std::uint32_t checkNextSuffix() override {
134  return next_suffix_;
135  }
136 
137  TRANSPORT_ALWAYS_INLINE std::uint32_t getNextSuffix() override {
138  incrementTotalCount();
139  return next_suffix_++;
140  }
141 
142  TRANSPORT_ALWAYS_INLINE std::uint32_t checkNextContentSuffix() override {
143  return next_suffix_ % segments_in_manifest_ == 0 ? next_suffix_
144  : (next_suffix_ + 1);
145  }
146 
147  TRANSPORT_ALWAYS_INLINE std::uint32_t getNextContentSuffix() override {
148  incrementTotalCount();
149  return next_suffix_ % segments_in_manifest_ == 0 ? next_suffix_++
150  : ++next_suffix_;
151  }
152 
153  TRANSPORT_ALWAYS_INLINE std::uint32_t checkNextManifestSuffix() override {
154  return (current_manifest_iteration_ + 1) * (segments_in_manifest_ + 1);
155  }
156 
157  TRANSPORT_ALWAYS_INLINE std::uint32_t getNextManifestSuffix() override {
158  incrementTotalCount();
159  return (current_manifest_iteration_++) * (segments_in_manifest_ + 1);
160  }
161 
162  TRANSPORT_ALWAYS_INLINE uint32_t getManifestCapacity() override {
163  return segments_in_manifest_;
164  }
165 
166  TRANSPORT_ALWAYS_INLINE void setManifestCapacity(uint32_t capacity) override {
167  segments_in_manifest_ = capacity;
168  }
169 
170  void reset(std::uint32_t offset = 0) override { next_suffix_ = offset; }
171 
172  protected:
173  std::uint32_t next_suffix_;
174  std::uint32_t segments_in_manifest_;
175  std::uint32_t current_manifest_iteration_;
176 };
177 
179  public:
180  static std::unique_ptr<SuffixStrategy> getSuffixStrategy(
181  NextSegmentCalculationStrategy strategy, uint32_t start_offset,
182  uint32_t manifest_capacity = 0) {
183  switch (strategy) {
184  case NextSegmentCalculationStrategy::INCREMENTAL:
185  return std::make_unique<IncrementalSuffixStrategy>(start_offset);
186  case NextSegmentCalculationStrategy::MANIFEST_CAPACITY_BASED:
187  return std::make_unique<CapacityBasedSuffixStrategy>(start_offset,
188  manifest_capacity);
189  default:
191  "No valid NextSegmentCalculationStrategy specified.");
192  }
193  }
194 };
195 
196 } // namespace utils
utils::IncrementalSuffixStrategy
Definition: suffix_strategy.h:77
utils::CapacityBasedSuffixStrategy
Definition: suffix_strategy.h:124
utils::SuffixStrategyFactory
Definition: suffix_strategy.h:178
utils::SuffixStrategy
Definition: suffix_strategy.h:24
errors::RuntimeException
Definition: runtime_exception.h:25