blob: d5cbb7c83b9c57a32c90bda79915e890f1ff14a2 [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();
22 string_properties_.clear();
Arman Uguray631c7e42013-07-30 16:41:12 -070023 stringmap_properties_.clear();
24 strings_properties_.clear();
Paul Stewartdef189e2012-08-02 20:12:09 -070025 uint_properties_.clear();
26}
27
Paul Stewartbaf87072013-10-04 17:03:37 -070028bool 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 Stewartd2e1c362013-03-03 19:06:07 -080038void KeyValueStore::CopyFrom(const KeyValueStore &b) {
39 bool_properties_ = b.bool_properties_;
40 int_properties_ = b.int_properties_;
41 string_properties_ = b.string_properties_;
Arman Uguray631c7e42013-07-30 16:41:12 -070042 stringmap_properties_ = b.stringmap_properties_;
43 strings_properties_ = b.strings_properties_;
Paul Stewartd2e1c362013-03-03 19:06:07 -080044 uint_properties_ = b.uint_properties_;
45}
46
Prathmesh Prabhu3903c702013-11-22 17:31:17 -080047bool 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 agrawal7a4e4002011-09-06 11:26:05 -070065bool KeyValueStore::ContainsBool(const string &name) const {
66 return ContainsKey(bool_properties_, name);
67}
68
Paul Stewart1062d9d2012-04-27 10:42:27 -070069bool KeyValueStore::ContainsInt(const string &name) const {
70 return ContainsKey(int_properties_, name);
71}
72
mukesh agrawal7a4e4002011-09-06 11:26:05 -070073bool KeyValueStore::ContainsString(const string &name) const {
74 return ContainsKey(string_properties_, name);
75}
76
Arman Uguray631c7e42013-07-30 16:41:12 -070077bool KeyValueStore::ContainsStringmap(const std::string &name) const {
78 return ContainsKey(stringmap_properties_, name);
79}
80
Paul Stewart3c5e2b32013-06-13 07:49:49 -070081bool KeyValueStore::ContainsStrings(const string &name) const {
82 return ContainsKey(strings_properties_, name);
83}
84
Darin Petkov63138a92012-02-06 14:09:15 +010085bool KeyValueStore::ContainsUint(const string &name) const {
86 return ContainsKey(uint_properties_, name);
87}
88
mukesh agrawal7a4e4002011-09-06 11:26:05 -070089bool KeyValueStore::GetBool(const string &name) const {
Paul Stewart68e59562013-12-12 08:15:05 -080090 const auto it(bool_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -070091 CHECK(it != bool_properties_.end()) << "for bool property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -070092 return it->second;
93}
94
Paul Stewart1062d9d2012-04-27 10:42:27 -070095int32 KeyValueStore::GetInt(const string &name) const {
Paul Stewart68e59562013-12-12 08:15:05 -080096 const auto it(int_properties_.find(name));
Paul Stewart1062d9d2012-04-27 10:42:27 -070097 CHECK(it != int_properties_.end()) << "for int property " << name;
98 return it->second;
99}
100
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700101const string &KeyValueStore::GetString(const string &name) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800102 const auto it(string_properties_.find(name));
mukesh agrawal1a056262011-10-05 14:36:54 -0700103 CHECK(it != string_properties_.end()) << "for string property " << name;
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700104 return it->second;
105}
106
Arman Uguray631c7e42013-07-30 16:41:12 -0700107const 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 Stewart3c5e2b32013-06-13 07:49:49 -0700115const 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 Petkov63138a92012-02-06 14:09:15 +0100121uint32 KeyValueStore::GetUint(const string &name) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800122 const auto it(uint_properties_.find(name));
Darin Petkov63138a92012-02-06 14:09:15 +0100123 CHECK(it != uint_properties_.end()) << "for uint property " << name;
124 return it->second;
125}
126
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700127void KeyValueStore::SetBool(const string &name, bool value) {
128 bool_properties_[name] = value;
129}
130
Paul Stewart1062d9d2012-04-27 10:42:27 -0700131void KeyValueStore::SetInt(const string &name, int32 value) {
132 int_properties_[name] = value;
133}
134
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700135void KeyValueStore::SetString(const string &name, const string &value) {
136 string_properties_[name] = value;
137}
138
Arman Uguray631c7e42013-07-30 16:41:12 -0700139void KeyValueStore::SetStringmap(const string &name,
140 const map<string, string> &value) {
141 stringmap_properties_[name] = value;
142}
143
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700144void KeyValueStore::SetStrings(const string &name,
145 const vector<string> &value) {
146 strings_properties_[name] = value;
147}
148
Darin Petkov63138a92012-02-06 14:09:15 +0100149void KeyValueStore::SetUint(const string &name, uint32 value) {
150 uint_properties_[name] = value;
151}
152
Paul Stewart88125fb2012-03-26 07:13:51 -0700153void KeyValueStore::RemoveString(const string &name) {
154 string_properties_.erase(name);
155}
156
Arman Uguray631c7e42013-07-30 16:41:12 -0700157void KeyValueStore::RemoveStringmap(const string &name) {
158 stringmap_properties_.erase(name);
159}
160
Paul Stewart3c5e2b32013-06-13 07:49:49 -0700161void KeyValueStore::RemoveStrings(const string &name) {
162 strings_properties_.erase(name);
163}
164
Paul Stewart1062d9d2012-04-27 10:42:27 -0700165void KeyValueStore::RemoveInt(const string &name) {
166 int_properties_.erase(name);
167}
168
Darin Petkovb536a742012-04-26 11:31:28 +0200169bool KeyValueStore::LookupBool(const string &name, bool default_value) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800170 const auto it(bool_properties_.find(name));
Darin Petkovb536a742012-04-26 11:31:28 +0200171 return it == bool_properties_.end() ? default_value : it->second;
172}
173
Paul Stewart68e59562013-12-12 08:15:05 -0800174int 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 Petkov7f060332012-03-14 11:46:47 +0100179string KeyValueStore::LookupString(const string &name,
180 const string &default_value) const {
Paul Stewart68e59562013-12-12 08:15:05 -0800181 const auto it(string_properties_.find(name));
Darin Petkovb536a742012-04-26 11:31:28 +0200182 return it == string_properties_.end() ? default_value : it->second;
Darin Petkov7f060332012-03-14 11:46:47 +0100183}
184
mukesh agrawal7a4e4002011-09-06 11:26:05 -0700185} // namespace shill