blob: aeb70665a6619ec5e4c30c72ba57dff828d5783b [file] [log] [blame]
Kelvin Zhangd567c8b2021-07-08 14:10:23 -04001//
2// Copyright (C) 2021 The Android Open Source Project
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 UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_MAP_H_
18#define UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_MAP_H_
19
20#include <functional>
21#include <map>
22#include <utility>
23
24#include "update_engine/common/utils.h"
25#include "update_engine/payload_generator/extent_ranges.h"
26#include "update_engine/update_metadata.pb.h"
27
28namespace chromeos_update_engine {
29
30// Data structure for storing a disjoint set of extents.
31// Currently the only usecase is for VABCPartitionWriter to keep track of which
32// block belongs to which merge operation. Therefore this class only contains
33// the minimal set of functions needed.
34template <typename T, typename Comparator = ExtentLess>
35class ExtentMap {
36 public:
37 bool AddExtent(const Extent& extent, T&& value) {
38 if (Get(extent)) {
39 return false;
40 }
41 const auto& [it, inserted] = map_.insert({extent, std::forward<T>(value)});
42 if (inserted) {
43 set_.AddExtent(extent);
44 }
45 return inserted;
46 }
47
48 size_t size() const { return map_.size(); }
49
50 // Return a pointer to entry which is intersecting |extent|. If T is already
51 // a pointer type, return T on success. This function always return
52 // |nullptr| on failure. Therefore you cannot store nullptr as an entry.
53 std::optional<T> Get(const Extent& extent) const {
54 const auto it = map_.find(extent);
55 if (it == map_.end()) {
56 LOG_IF(WARNING, set_.OverlapsWithExtent(extent))
57 << "Looking up a partially intersecting extent isn't supported by "
58 "this data structure.";
59 return {};
60 }
61 return {it->second};
62 }
63
64 // Return a set of extents that are contained in this extent map.
65 // If |extent| is completely covered by this extent map, a vector of itself
66 // will be returned.
67 // If only a subset of |extent| is covered by this extent map, a vector of
68 // parts in this map will be returned.
69 // If |extent| has no intersection with this map, an empty vector will be
70 // returned.
71 // E.g. extent map contains [0,5] and [10,15], GetIntersectingExtents([3, 12])
72 // would return [3,5] and [10,12]
73 std::vector<Extent> GetIntersectingExtents(const Extent& extent) const {
74 return set_.GetIntersectingExtents(extent);
75 }
76
77 // Complement of |GetIntersectingExtents|, return vector of extents which are
78 // part of |extent| but not covered by this map.
79 std::vector<Extent> GetNonIntersectingExtents(const Extent& extent) const {
80 return FilterExtentRanges({extent}, set_);
81 }
82
83 private:
84 // Get a range of exents that potentially intersect with parameter |extent|
85 std::map<Extent, T, Comparator> map_;
86 ExtentRanges set_;
87};
88} // namespace chromeos_update_engine
89
90#endif // UPDATE_ENGINE_PAYLOAD_CONSUMER_EXTENT_MAP_H_