Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "shill/key_value_store.h" |
| 6 | |
Eric Shienbrood | 3e20a23 | 2012-02-16 11:35:56 -0500 | [diff] [blame] | 7 | #include <base/stl_util.h> |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 8 | |
Christopher Wiley | b691efd | 2012-08-09 13:51:51 -0700 | [diff] [blame] | 9 | #include "shill/logging.h" |
| 10 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 11 | using std::map; |
| 12 | using std::string; |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 13 | using std::vector; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 14 | |
| 15 | namespace shill { |
| 16 | |
| 17 | KeyValueStore::KeyValueStore() {} |
| 18 | |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 19 | void KeyValueStore::Clear() { |
| 20 | bool_properties_.clear(); |
| 21 | int_properties_.clear(); |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 22 | key_value_store_properties_.clear(); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 23 | string_properties_.clear(); |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 24 | stringmap_properties_.clear(); |
| 25 | strings_properties_.clear(); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 26 | uint_properties_.clear(); |
| 27 | } |
| 28 | |
Paul Stewart | baf8707 | 2013-10-04 17:03:37 -0700 | [diff] [blame] | 29 | bool KeyValueStore::IsEmpty() { |
| 30 | return |
| 31 | bool_properties_.empty() && |
| 32 | int_properties_.empty() && |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 33 | key_value_store_properties_.empty() && |
Paul Stewart | baf8707 | 2013-10-04 17:03:37 -0700 | [diff] [blame] | 34 | string_properties_.empty() && |
| 35 | stringmap_properties_.empty() && |
| 36 | strings_properties_.empty() && |
| 37 | uint_properties_.empty(); |
| 38 | } |
| 39 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 40 | void KeyValueStore::CopyFrom(const KeyValueStore& b) { |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 41 | bool_properties_ = b.bool_properties_; |
| 42 | int_properties_ = b.int_properties_; |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 43 | key_value_store_properties_ = b.key_value_store_properties_; |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 44 | string_properties_ = b.string_properties_; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 45 | stringmap_properties_ = b.stringmap_properties_; |
| 46 | strings_properties_ = b.strings_properties_; |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 47 | uint_properties_ = b.uint_properties_; |
| 48 | } |
| 49 | |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 50 | bool KeyValueStore::KeyValueStorePropertiesAreEqual( |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 51 | const KeyValueStore& other) const { |
| 52 | for (const auto& entry : key_value_store_properties_) { |
| 53 | const auto& key = entry.first; |
| 54 | const auto& value = entry.second; |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 55 | if (!other.ContainsKeyValueStore(key) || |
| 56 | !value.Equals(other.GetKeyValueStore(key))) { |
| 57 | return false; |
| 58 | } |
| 59 | } |
| 60 | return true; |
| 61 | } |
| 62 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 63 | bool KeyValueStore::Equals(const KeyValueStore& other) const { |
Prathmesh Prabhu | 3903c70 | 2013-11-22 17:31:17 -0800 | [diff] [blame] | 64 | // First compare sizes of all maps to detect unequal stores faster. |
| 65 | if (bool_properties_.size() != other.bool_properties_.size() || |
| 66 | int_properties_.size() != other.int_properties_.size() || |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 67 | key_value_store_properties_.size() != |
| 68 | other.key_value_store_properties_.size() || |
Prathmesh Prabhu | 3903c70 | 2013-11-22 17:31:17 -0800 | [diff] [blame] | 69 | string_properties_.size() != other.string_properties_.size() || |
| 70 | stringmap_properties_.size() != other.stringmap_properties_.size() || |
| 71 | strings_properties_.size()!= other.strings_properties_.size() || |
| 72 | uint_properties_.size() != other.uint_properties_.size()) |
| 73 | return false; |
| 74 | |
| 75 | return bool_properties_ == other.bool_properties_ && |
| 76 | int_properties_ == other.int_properties_ && |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 77 | KeyValueStorePropertiesAreEqual(other) && |
Prathmesh Prabhu | 3903c70 | 2013-11-22 17:31:17 -0800 | [diff] [blame] | 78 | string_properties_ == other.string_properties_ && |
| 79 | stringmap_properties_ == other.stringmap_properties_ && |
| 80 | strings_properties_ == other.strings_properties_ && |
| 81 | uint_properties_ == other.uint_properties_; |
| 82 | } |
| 83 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 84 | bool KeyValueStore::ContainsBool(const string& name) const { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 85 | return ContainsKey(bool_properties_, name); |
| 86 | } |
| 87 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 88 | bool KeyValueStore::ContainsInt(const string& name) const { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 89 | return ContainsKey(int_properties_, name); |
| 90 | } |
| 91 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 92 | bool KeyValueStore::ContainsKeyValueStore(const string& name) const { |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 93 | return ContainsKey(key_value_store_properties_, name); |
| 94 | } |
| 95 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 96 | bool KeyValueStore::ContainsString(const string& name) const { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 97 | return ContainsKey(string_properties_, name); |
| 98 | } |
| 99 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 100 | bool KeyValueStore::ContainsStringmap(const std::string& name) const { |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 101 | return ContainsKey(stringmap_properties_, name); |
| 102 | } |
| 103 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 104 | bool KeyValueStore::ContainsStrings(const string& name) const { |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 105 | return ContainsKey(strings_properties_, name); |
| 106 | } |
| 107 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 108 | bool KeyValueStore::ContainsUint(const string& name) const { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 109 | return ContainsKey(uint_properties_, name); |
| 110 | } |
| 111 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 112 | bool KeyValueStore::GetBool(const string& name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 113 | const auto it(bool_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 114 | CHECK(it != bool_properties_.end()) << "for bool property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 115 | return it->second; |
| 116 | } |
| 117 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 118 | int32_t KeyValueStore::GetInt(const string& name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 119 | const auto it(int_properties_.find(name)); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 120 | CHECK(it != int_properties_.end()) << "for int property " << name; |
| 121 | return it->second; |
| 122 | } |
| 123 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 124 | const KeyValueStore& KeyValueStore::GetKeyValueStore(const string& name) const { |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 125 | const auto it(key_value_store_properties_.find(name)); |
| 126 | CHECK(it != key_value_store_properties_.end()) |
| 127 | << "for key value store property " << name; |
| 128 | return it->second; |
| 129 | } |
| 130 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 131 | const string& KeyValueStore::GetString(const string& name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 132 | const auto it(string_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 133 | CHECK(it != string_properties_.end()) << "for string property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 134 | return it->second; |
| 135 | } |
| 136 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 137 | const map<string, string>& KeyValueStore::GetStringmap( |
| 138 | const string& name) const { |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 139 | const auto it(stringmap_properties_.find(name)); |
| 140 | CHECK(it != stringmap_properties_.end()) << "for stringmap property " |
| 141 | << name; |
| 142 | return it->second; |
| 143 | } |
| 144 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 145 | const vector<string>& KeyValueStore::GetStrings(const string& name) const { |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 146 | const auto it(strings_properties_.find(name)); |
| 147 | CHECK(it != strings_properties_.end()) << "for strings property " << name; |
| 148 | return it->second; |
| 149 | } |
| 150 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 151 | uint32_t KeyValueStore::GetUint(const string& name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 152 | const auto it(uint_properties_.find(name)); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 153 | CHECK(it != uint_properties_.end()) << "for uint property " << name; |
| 154 | return it->second; |
| 155 | } |
| 156 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 157 | void KeyValueStore::SetBool(const string& name, bool value) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 158 | bool_properties_[name] = value; |
| 159 | } |
| 160 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 161 | void KeyValueStore::SetInt(const string& name, int32_t value) { |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 162 | int_properties_[name] = value; |
| 163 | } |
| 164 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 165 | void KeyValueStore::SetKeyValueStore(const string& name, |
| 166 | const KeyValueStore& value) { |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 167 | key_value_store_properties_[name].Clear(); |
| 168 | key_value_store_properties_[name].CopyFrom(value); |
| 169 | } |
| 170 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 171 | void KeyValueStore::SetString(const string& name, const string& value) { |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 172 | string_properties_[name] = value; |
| 173 | } |
| 174 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 175 | void KeyValueStore::SetStringmap(const string& name, |
| 176 | const map<string, string>& value) { |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 177 | stringmap_properties_[name] = value; |
| 178 | } |
| 179 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 180 | void KeyValueStore::SetStrings(const string& name, |
| 181 | const vector<string>& value) { |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 182 | strings_properties_[name] = value; |
| 183 | } |
| 184 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 185 | void KeyValueStore::SetUint(const string& name, uint32_t value) { |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 186 | uint_properties_[name] = value; |
| 187 | } |
| 188 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 189 | void KeyValueStore::RemoveInt(const string& name) { |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 190 | int_properties_.erase(name); |
| 191 | } |
| 192 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 193 | void KeyValueStore::RemoveKeyValueStore(const string& name) { |
Paul Stewart | bb83356 | 2015-01-21 23:30:46 -0800 | [diff] [blame] | 194 | key_value_store_properties_.erase(name); |
| 195 | } |
| 196 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 197 | void KeyValueStore::RemoveString(const string& name) { |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 198 | string_properties_.erase(name); |
| 199 | } |
| 200 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 201 | void KeyValueStore::RemoveStringmap(const string& name) { |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 202 | stringmap_properties_.erase(name); |
| 203 | } |
| 204 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 205 | void KeyValueStore::RemoveStrings(const string& name) { |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 206 | strings_properties_.erase(name); |
| 207 | } |
| 208 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 209 | bool KeyValueStore::LookupBool(const string& name, bool default_value) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 210 | const auto it(bool_properties_.find(name)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 211 | return it == bool_properties_.end() ? default_value : it->second; |
| 212 | } |
| 213 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 214 | int KeyValueStore::LookupInt(const string& name, int default_value) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 215 | const auto it(int_properties_.find(name)); |
| 216 | return it == int_properties_.end() ? default_value : it->second; |
| 217 | } |
| 218 | |
Paul Stewart | 8ae1874 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 219 | string KeyValueStore::LookupString(const string& name, |
| 220 | const string& default_value) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 221 | const auto it(string_properties_.find(name)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 222 | return it == string_properties_.end() ? default_value : it->second; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 223 | } |
| 224 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 225 | } // namespace shill |