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(); |
| 22 | string_properties_.clear(); |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 23 | stringmap_properties_.clear(); |
| 24 | strings_properties_.clear(); |
Paul Stewart | def189e | 2012-08-02 20:12:09 -0700 | [diff] [blame] | 25 | uint_properties_.clear(); |
| 26 | } |
| 27 | |
Paul Stewart | baf8707 | 2013-10-04 17:03:37 -0700 | [diff] [blame] | 28 | bool KeyValueStore::IsEmpty() { |
| 29 | return |
| 30 | bool_properties_.empty() && |
| 31 | int_properties_.empty() && |
| 32 | string_properties_.empty() && |
| 33 | stringmap_properties_.empty() && |
| 34 | strings_properties_.empty() && |
| 35 | uint_properties_.empty(); |
| 36 | } |
| 37 | |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 38 | void KeyValueStore::CopyFrom(const KeyValueStore &b) { |
| 39 | bool_properties_ = b.bool_properties_; |
| 40 | int_properties_ = b.int_properties_; |
| 41 | string_properties_ = b.string_properties_; |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 42 | stringmap_properties_ = b.stringmap_properties_; |
| 43 | strings_properties_ = b.strings_properties_; |
Paul Stewart | d2e1c36 | 2013-03-03 19:06:07 -0800 | [diff] [blame] | 44 | uint_properties_ = b.uint_properties_; |
| 45 | } |
| 46 | |
Prathmesh Prabhu | 3903c70 | 2013-11-22 17:31:17 -0800 | [diff] [blame] | 47 | bool KeyValueStore::Equals(const KeyValueStore &other) const { |
| 48 | // First compare sizes of all maps to detect unequal stores faster. |
| 49 | if (bool_properties_.size() != other.bool_properties_.size() || |
| 50 | int_properties_.size() != other.int_properties_.size() || |
| 51 | string_properties_.size() != other.string_properties_.size() || |
| 52 | stringmap_properties_.size() != other.stringmap_properties_.size() || |
| 53 | strings_properties_.size()!= other.strings_properties_.size() || |
| 54 | uint_properties_.size() != other.uint_properties_.size()) |
| 55 | return false; |
| 56 | |
| 57 | return bool_properties_ == other.bool_properties_ && |
| 58 | int_properties_ == other.int_properties_ && |
| 59 | string_properties_ == other.string_properties_ && |
| 60 | stringmap_properties_ == other.stringmap_properties_ && |
| 61 | strings_properties_ == other.strings_properties_ && |
| 62 | uint_properties_ == other.uint_properties_; |
| 63 | } |
| 64 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 65 | bool KeyValueStore::ContainsBool(const string &name) const { |
| 66 | return ContainsKey(bool_properties_, name); |
| 67 | } |
| 68 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 69 | bool KeyValueStore::ContainsInt(const string &name) const { |
| 70 | return ContainsKey(int_properties_, name); |
| 71 | } |
| 72 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 73 | bool KeyValueStore::ContainsString(const string &name) const { |
| 74 | return ContainsKey(string_properties_, name); |
| 75 | } |
| 76 | |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 77 | bool KeyValueStore::ContainsStringmap(const std::string &name) const { |
| 78 | return ContainsKey(stringmap_properties_, name); |
| 79 | } |
| 80 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 81 | bool KeyValueStore::ContainsStrings(const string &name) const { |
| 82 | return ContainsKey(strings_properties_, name); |
| 83 | } |
| 84 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 85 | bool KeyValueStore::ContainsUint(const string &name) const { |
| 86 | return ContainsKey(uint_properties_, name); |
| 87 | } |
| 88 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 89 | bool KeyValueStore::GetBool(const string &name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 90 | const auto it(bool_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 91 | CHECK(it != bool_properties_.end()) << "for bool property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 92 | return it->second; |
| 93 | } |
| 94 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 95 | int32 KeyValueStore::GetInt(const string &name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 96 | const auto it(int_properties_.find(name)); |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 97 | CHECK(it != int_properties_.end()) << "for int property " << name; |
| 98 | return it->second; |
| 99 | } |
| 100 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 101 | const string &KeyValueStore::GetString(const string &name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 102 | const auto it(string_properties_.find(name)); |
mukesh agrawal | 1a05626 | 2011-10-05 14:36:54 -0700 | [diff] [blame] | 103 | CHECK(it != string_properties_.end()) << "for string property " << name; |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 104 | return it->second; |
| 105 | } |
| 106 | |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 107 | const map<string, string> &KeyValueStore::GetStringmap( |
| 108 | const string &name) const { |
| 109 | const auto it(stringmap_properties_.find(name)); |
| 110 | CHECK(it != stringmap_properties_.end()) << "for stringmap property " |
| 111 | << name; |
| 112 | return it->second; |
| 113 | } |
| 114 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 115 | const vector<string> &KeyValueStore::GetStrings(const string &name) const { |
| 116 | const auto it(strings_properties_.find(name)); |
| 117 | CHECK(it != strings_properties_.end()) << "for strings property " << name; |
| 118 | return it->second; |
| 119 | } |
| 120 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 121 | uint32 KeyValueStore::GetUint(const string &name) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 122 | const auto it(uint_properties_.find(name)); |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 123 | CHECK(it != uint_properties_.end()) << "for uint property " << name; |
| 124 | return it->second; |
| 125 | } |
| 126 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 127 | void KeyValueStore::SetBool(const string &name, bool value) { |
| 128 | bool_properties_[name] = value; |
| 129 | } |
| 130 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 131 | void KeyValueStore::SetInt(const string &name, int32 value) { |
| 132 | int_properties_[name] = value; |
| 133 | } |
| 134 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 135 | void KeyValueStore::SetString(const string &name, const string &value) { |
| 136 | string_properties_[name] = value; |
| 137 | } |
| 138 | |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 139 | void KeyValueStore::SetStringmap(const string &name, |
| 140 | const map<string, string> &value) { |
| 141 | stringmap_properties_[name] = value; |
| 142 | } |
| 143 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 144 | void KeyValueStore::SetStrings(const string &name, |
| 145 | const vector<string> &value) { |
| 146 | strings_properties_[name] = value; |
| 147 | } |
| 148 | |
Darin Petkov | 63138a9 | 2012-02-06 14:09:15 +0100 | [diff] [blame] | 149 | void KeyValueStore::SetUint(const string &name, uint32 value) { |
| 150 | uint_properties_[name] = value; |
| 151 | } |
| 152 | |
Paul Stewart | 88125fb | 2012-03-26 07:13:51 -0700 | [diff] [blame] | 153 | void KeyValueStore::RemoveString(const string &name) { |
| 154 | string_properties_.erase(name); |
| 155 | } |
| 156 | |
Arman Uguray | 631c7e4 | 2013-07-30 16:41:12 -0700 | [diff] [blame] | 157 | void KeyValueStore::RemoveStringmap(const string &name) { |
| 158 | stringmap_properties_.erase(name); |
| 159 | } |
| 160 | |
Paul Stewart | 3c5e2b3 | 2013-06-13 07:49:49 -0700 | [diff] [blame] | 161 | void KeyValueStore::RemoveStrings(const string &name) { |
| 162 | strings_properties_.erase(name); |
| 163 | } |
| 164 | |
Paul Stewart | 1062d9d | 2012-04-27 10:42:27 -0700 | [diff] [blame] | 165 | void KeyValueStore::RemoveInt(const string &name) { |
| 166 | int_properties_.erase(name); |
| 167 | } |
| 168 | |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 169 | bool KeyValueStore::LookupBool(const string &name, bool default_value) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 170 | const auto it(bool_properties_.find(name)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 171 | return it == bool_properties_.end() ? default_value : it->second; |
| 172 | } |
| 173 | |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 174 | int KeyValueStore::LookupInt(const string &name, int default_value) const { |
| 175 | const auto it(int_properties_.find(name)); |
| 176 | return it == int_properties_.end() ? default_value : it->second; |
| 177 | } |
| 178 | |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 179 | string KeyValueStore::LookupString(const string &name, |
| 180 | const string &default_value) const { |
Paul Stewart | 68e5956 | 2013-12-12 08:15:05 -0800 | [diff] [blame] | 181 | const auto it(string_properties_.find(name)); |
Darin Petkov | b536a74 | 2012-04-26 11:31:28 +0200 | [diff] [blame] | 182 | return it == string_properties_.end() ? default_value : it->second; |
Darin Petkov | 7f06033 | 2012-03-14 11:46:47 +0100 | [diff] [blame] | 183 | } |
| 184 | |
mukesh agrawal | 7a4e400 | 2011-09-06 11:26:05 -0700 | [diff] [blame] | 185 | } // namespace shill |