Debian Codegraph @

components/keyrings/common/memstore/iterator.h

raw
1 /* Copyright (c) 2021, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #ifndef ITERATOR_INCLUDED
25 #define ITERATOR_INCLUDED
26
27 #include "cache.h" /* Datacache */
28
29 namespace keyring_common {
30
31 namespace iterator {
32 template <typename Data_extension>
33 class Iterator {
34 public:
35 /** Constructor */
36 Iterator() : it_(), end_(), version_(0), valid_(false), cached_(false) {}
37 Iterator(const cache::Datacache<Data_extension> &datacache,
38 const meta::Metadata &metadata)
39 : it_(datacache.at(metadata)),
40 end_(datacache.end()),
41 version_(datacache.version()),
42 valid_(it_ != end_),
43 cached_(false) {}
44
45 Iterator(const cache::Datacache<Data_extension> &datacache, bool cached)
46 : it_(datacache.begin()),
47 end_(datacache.end()),
48 version_(datacache.version()),
49 valid_(true),
50 cached_(cached) {
51 if (cached_) {
52 for (std::pair<meta::Metadata, Data_extension> element : datacache)
53 metadata_.store(element.first, element.second);
54
55 it_ = metadata_.begin();
56 end_ = metadata_.end();
57 }
58 }
59
60 /** Destructor */
61 ~Iterator() { metadata_.clear(); }
62
63 /** Get iterator */
64 typename cache::Cache<Data_extension>::const_iterator get_iterator() const {
65 return it_;
66 }
67
68 /** Move iterator forward */
69 bool next(size_t version) {
70 if (iterator_valid(version) == false) {
71 it_ = end_;
72 valid_ = false;
73 return false;
74 }
75 it_++;
76 return true;
77 }
78
79 bool metadata(size_t version, meta::Metadata &metadata) {
80 if (iterator_valid(version) == false) {
81 it_ = end_;
82 valid_ = false;
83 return false;
84 }
85 metadata = it_->first;
86 return true;
87 }
88
89 bool data(size_t version, Data_extension &data) {
90 if (iterator_valid(version) == false) {
91 it_ = end_;
92 valid_ = false;
93 return false;
94 }
95 data = it_->second;
96 return true;
97 }
98
99 bool valid(size_t version) {
100 valid_ = iterator_valid(version);
101 return valid_;
102 }
103
104 private:
105 /** Internal validity checker */
106 inline bool iterator_valid(size_t version) {
107 if (cached_) {
108 return valid_ && (it_ != end_);
109 }
110 return valid_ && (version == version_) && (it_ != end_);
111 }
112
113 private:
114 /** Const Iterator */
115 typename cache::Cache<Data_extension>::const_iterator it_;
116 /** End */
117 typename cache::Cache<Data_extension>::const_iterator end_;
118 /** Iterator version */
119 size_t version_;
120 /** validity of the iterator */
121 bool valid_;
122 /** Iterator type */
123 bool cached_;
124 /** Local copy */
125 typename cache::Datacache<Data_extension> metadata_;
126 };
127
128 } // namespace iterator
129
130 } // namespace keyring_common
131
132 #endif // !ITERATOR_INCLUDED