blob: 6ef91d6583ea2893fd925f09dc8a6184a44a0035 [file] [log] [blame]
Darin Petkov63138a92012-02-06 14:09:15 +01001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
mukesh agrawal7a4e4002011-09-06 11:26:05 -07002// 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 Shienbrood3e20a232012-02-16 11:35:56 -05007#include <base/stl_util.h>
mukesh agrawal7a4e4002011-09-06 11:26:05 -07008
Christopher Wileyb691efd2012-08-09 13:51:51 -07009#include "shill/logging.h"
10
mukesh agrawal7a4e4002011-09-06 11:26:05 -070011using std::map;
12using std::string;
Paul Stewart3c5e2b32013-06-13 07:49:49 -070013using std::vector;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070014
15namespace shill {
16
17KeyValueStore::KeyValueStore() {}
18
Paul Stewartdef189e2012-08-02 20:12:09 -070019void KeyValueStore::Clear() {
20 bool_properties_.clear();
21 int_properties_.clear();
Paul Stewartbb833562015-01-21 23:30:46 -080022 key_value_store_properties_.clear();
Paul Stewartdef189e2012-08-02 20:12:09 -070023 string_properties_.clear();
Arman Uguray631c7e42013-07-30 16:41:12 -070024 stringmap_properties_.clear();
25 strings_properties_.clear();
Paul Stewartdef189e2012-08-02 20:12:09 -070026 uint_properties_.clear();
27}
28
Paul Stewartbaf87072013-10-04 17:03:37 -070029bool KeyValueStore::IsEmpty() {
30 return
31 bool_properties_.empty() &&
32 int_properties_.empty() &&
Paul Stewartbb833562015-01-21 23:30:46 -080033 key_value_store_properties_.empty() &&
Paul Stewartbaf87072013-10-04 17:03:37 -070034 string_properties_.empty() &&
35 stringmap_properties_.empty() &&
36 strings_properties_.empty() &&
37 uint_properties_.empty();
38}
39
Paul Stewart8ae18742015-06-16 13:13:10 -070040void KeyValueStore::CopyFrom(const KeyValueStore& b) {
Paul Stewartd2e1c362013-03-03 19:06:07 -080041 bool_properties_ = b.bool_properties_;
42 int_properties_ = b.int_properties_;
Paul Stewartbb833562015-01-21 23:30:46 -080043 key_value_store_properties_ = b.key_value_store_properties_;
Paul Stewartd2e1c362013-03-03 19:06:07 -080044 string_properties_ = b.string_properties_;
Arman Uguray631c7e42013-07-30 16:41:12 -070045 stringmap_properties_ = b.stringmap_properties_;
46 strings_properties_ = b.strings_properties_;
Paul Stewartd2e1c362013-03-03 19:06:07 -080047 uint_properties_ = b.uint_properties_;
48}
49
Paul Stewartbb833562015-01-21 23:30:46 -080050bool KeyValueStore::KeyValueStorePropertiesAreEqual(
Paul Stewart8ae18742015-06-16 13:13:10 -070051 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 Stewartbb833562015-01-21 23:30:46 -080055 if (!other.ContainsKeyValueStore(key) ||
56 !value.Equals(other.GetKeyValueStore(key))) {
57 return false;
58 }
59 }
60 return true;
61}
62
Paul Stewart8ae18742015-06-16 13:13:10 -070063bool KeyValueStore::Equals(const KeyValueStore& other) const {
Prathmesh Prabhu3903c702013-11-22 17:31:17 -080064 // 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 Stewartbb833562015-01-21 23:30:46 -080067 key_value_store_properties_.size() !=
68 other.key_value_store_properties_.size() ||
Prathmesh Prabhu3903c702013-11-22 17:31:17 -080069 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 Stewartbb833562015-01-21 23:30:46 -080077 KeyValueStorePropertiesAreEqual(other) &&
Prathmesh Prabhu3903c702013-11-22 17:31:17 -080078 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 Stewart8ae18742015-06-16 13:13:10 -070084bool KeyValueStore::ContainsBool(const string& name) const {
mukesh agrawal7a4e4002011-09-06 11:26:05 -070085 return ContainsKey(bool_properties_, name);
86}
87
Paul Stewart8ae18742015-06-16 13:13:10 -070088bool KeyValueStore::ContainsInt(const string& name) const {
Paul Stewart1062d9d2012-04-27 10:42:27 -070089 return ContainsKey(int_properties_, name);
90}
91
Paul Stewart8ae18742015-06-16 13:13:10 -070092bool KeyValueStore::ContainsKeyValueStore(const string& name) const {
Paul Stewartbb833562015-01-21 23:30:46 -080093 return ContainsKey(key_value_store_properties_, name);
94}
95
Paul Stewart8ae18742015-06-16 13:13:10 -070096bool KeyValueStore::ContainsString(const string& name) const {
mukesh agrawal7a4e4002011-09-06 11:26:05 -070097 return ContainsKey(string_properties_, name);
98}
99
Paul Stewart8ae18742015-06-16 13:13:10 -0700100bool KeyValueStore::ContainsStringmap(const std::string& name) const {
Arman Uguray631c7e42013-07-30 16:41:12 -0700101 return ContainsKey(stringmap_properties_, name);
102}
103
Paul Stewart8ae18742015-06-16 13:13:10 -0700104bool KeyValueStore::ContainsStrings(const string& name) const {
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700105 return ContainsKey(strings_properties_, name);
106}
107
Paul Stewart8ae18742015-06-16 13:13:10 -0700108bool KeyValueStore::ContainsUint(const string& name) const {
Darin Petkov63138a92012-02-06 14:09:15 +0100109 return ContainsKey(uint_properties_, name);
110}
111
Paul Stewart8ae18742015-06-16 13:13:10 -0700112bool KeyValueStore::GetBool(const string& name) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800113 const auto it(bool_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -0700114 CHECK(it != bool_properties_.end()) << "for bool property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700115 return it->second;
116}
117
Paul Stewart8ae18742015-06-16 13:13:10 -0700118int32_t KeyValueStore::GetInt(const string& name) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800119 const auto it(int_properties_.find(name));
Paul Stewart1062d9d2012-04-27 10:42:27 -0700120 CHECK(it != int_properties_.end()) << "for int property " << name;
121 return it->second;
122}
123
Paul Stewart8ae18742015-06-16 13:13:10 -0700124const KeyValueStore& KeyValueStore::GetKeyValueStore(const string& name) const {
Paul Stewartbb833562015-01-21 23:30:46 -0800125 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 Stewart8ae18742015-06-16 13:13:10 -0700131const string& KeyValueStore::GetString(const string& name) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800132 const auto it(string_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -0700133 CHECK(it != string_properties_.end()) << "for string property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700134 return it->second;
135}
136
Paul Stewart8ae18742015-06-16 13:13:10 -0700137const map<string, string>& KeyValueStore::GetStringmap(
138 const string& name) const {
Arman Uguray631c7e42013-07-30 16:41:12 -0700139 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 Stewart8ae18742015-06-16 13:13:10 -0700145const vector<string>& KeyValueStore::GetStrings(const string& name) const {
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700146 const auto it(strings_properties_.find(name));
147 CHECK(it != strings_properties_.end()) << "for strings property " << name;
148 return it->second;
149}
150
Paul Stewart8ae18742015-06-16 13:13:10 -0700151uint32_t KeyValueStore::GetUint(const string& name) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800152 const auto it(uint_properties_.find(name));
Darin Petkov63138a92012-02-06 14:09:15 +0100153 CHECK(it != uint_properties_.end()) << "for uint property " << name;
154 return it->second;
155}
156
Paul Stewart8ae18742015-06-16 13:13:10 -0700157void KeyValueStore::SetBool(const string& name, bool value) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700158 bool_properties_[name] = value;
159}
160
Paul Stewart8ae18742015-06-16 13:13:10 -0700161void KeyValueStore::SetInt(const string& name, int32_t value) {
Paul Stewart1062d9d2012-04-27 10:42:27 -0700162 int_properties_[name] = value;
163}
164
Paul Stewart8ae18742015-06-16 13:13:10 -0700165void KeyValueStore::SetKeyValueStore(const string& name,
166 const KeyValueStore& value) {
Paul Stewartbb833562015-01-21 23:30:46 -0800167 key_value_store_properties_[name].Clear();
168 key_value_store_properties_[name].CopyFrom(value);
169}
170
Paul Stewart8ae18742015-06-16 13:13:10 -0700171void KeyValueStore::SetString(const string& name, const string& value) {
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700172 string_properties_[name] = value;
173}
174
Paul Stewart8ae18742015-06-16 13:13:10 -0700175void KeyValueStore::SetStringmap(const string& name,
176 const map<string, string>& value) {
Arman Uguray631c7e42013-07-30 16:41:12 -0700177 stringmap_properties_[name] = value;
178}
179
Paul Stewart8ae18742015-06-16 13:13:10 -0700180void KeyValueStore::SetStrings(const string& name,
181 const vector<string>& value) {
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700182 strings_properties_[name] = value;
183}
184
Paul Stewart8ae18742015-06-16 13:13:10 -0700185void KeyValueStore::SetUint(const string& name, uint32_t value) {
Darin Petkov63138a92012-02-06 14:09:15 +0100186 uint_properties_[name] = value;
187}
188
Paul Stewart8ae18742015-06-16 13:13:10 -0700189void KeyValueStore::RemoveInt(const string& name) {
Paul Stewartbb833562015-01-21 23:30:46 -0800190 int_properties_.erase(name);
191}
192
Paul Stewart8ae18742015-06-16 13:13:10 -0700193void KeyValueStore::RemoveKeyValueStore(const string& name) {
Paul Stewartbb833562015-01-21 23:30:46 -0800194 key_value_store_properties_.erase(name);
195}
196
Paul Stewart8ae18742015-06-16 13:13:10 -0700197void KeyValueStore::RemoveString(const string& name) {
Paul Stewart88125fb2012-03-26 07:13:51 -0700198 string_properties_.erase(name);
199}
200
Paul Stewart8ae18742015-06-16 13:13:10 -0700201void KeyValueStore::RemoveStringmap(const string& name) {
Arman Uguray631c7e42013-07-30 16:41:12 -0700202 stringmap_properties_.erase(name);
203}
204
Paul Stewart8ae18742015-06-16 13:13:10 -0700205void KeyValueStore::RemoveStrings(const string& name) {
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700206 strings_properties_.erase(name);
207}
208
Paul Stewart8ae18742015-06-16 13:13:10 -0700209bool KeyValueStore::LookupBool(const string& name, bool default_value) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800210 const auto it(bool_properties_.find(name));
Darin Petkovb536a742012-04-26 11:31:28 +0200211 return it == bool_properties_.end() ? default_value : it->second;
212}
213
Paul Stewart8ae18742015-06-16 13:13:10 -0700214int KeyValueStore::LookupInt(const string& name, int default_value) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800215 const auto it(int_properties_.find(name));
216 return it == int_properties_.end() ? default_value : it->second;
217}
218
Paul Stewart8ae18742015-06-16 13:13:10 -0700219string KeyValueStore::LookupString(const string& name,
220 const string& default_value) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800221 const auto it(string_properties_.find(name));
Darin Petkovb536a742012-04-26 11:31:28 +0200222 return it == string_properties_.end() ? default_value : it->second;
Darin Petkov7f060332012-03-14 11:46:47 +0100223}
224
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700225} // namespace shill