19 std::map<std::string, knowledge::KnowledgeRecord> contents;
24 if (contents.size() > 0)
26 for (std::map<std::string, knowledge::KnowledgeRecord>::iterator i =
28 i != contents.end(); ++i)
30 map_[i->first.substr(common.size())] =
43 std::map<std::string, knowledge::KnowledgeRecord> contents;
48 if (contents.size() > 0)
50 for (std::map<std::string, knowledge::KnowledgeRecord>::iterator i =
52 i != contents.end(); ++i)
54 map_[i->first.substr(common.size())] =
64 delimiter_(rhs.delimiter_)
75 MADARA_GUARD_TYPE guard(mutex_);
77 for (InternalMap::const_iterator index =
map_.begin(); index !=
map_.end();
80 context_->mark_modified(index->second);
87 std::stringstream result;
94 MADARA_GUARD_TYPE guard(mutex_);
96 result << this->name_;
97 result <<
" [" <<
map_.size() <<
"]";
102 for (InternalMap::const_iterator index =
map_.begin(); index !=
map_.end();
114 result << index->first <<
" => "
115 <<
context_->get(index->second).to_string();
131 return get_debug_info();
137 return new Map(*
this);
153 MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.
mutex_);
156 this->name_ = rhs.
name_;
170 MADARA_GUARD_TYPE guard(mutex_);
171 std::stringstream buffer;
173 buffer << delimiter_;
178 std::map<std::string, VariableReference>::const_iterator entry =
179 map_.find(final_key);
181 if (entry ==
map_.end())
185 return context_->get(ref, keep_local);
188 result =
context_->get(entry->second, keep_local);
197 return to_record(key);
202 MADARA_GUARD_TYPE guard(mutex_);
208 std::vector<std::string> additions;
213 MADARA_GUARD_TYPE guard(mutex_);
217 std::map<std::string, knowledge::KnowledgeRecord> contents;
222 for (std::map<std::string, knowledge::KnowledgeRecord>::iterator i =
224 i != contents.end(); ++i)
230 additions.push_back(key);
240 Map& other,
bool refresh_keys,
bool delete_keys)
248 MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
249 guard2(other.
mutex_, std::adopt_lock);
258 InternalMap::iterator i =
map_.begin();
259 while (i !=
map_.end())
263 this->
context_->get(i->second, this->settings_);
266 InternalMap::iterator other_found = other.
map_.find(i->first);
268 if (other_found != other.
map_.end())
276 other.
map_[i->first] = other_found->second;
280 leftover.erase(i->first);
284 std::stringstream buffer;
285 buffer << other.
name_;
286 buffer << delimiter_;
292 other.
map_[i->first] = ref;
298 std::stringstream buffer2;
299 buffer2 << this->name_;
300 buffer2 << delimiter_;
303 this->
context_->delete_variable(buffer2.str(), this->settings_);
304 this->
map_.erase(i++);
309 this->
context_->set(i->second, zero, this->settings_);
318 for (i = leftover.begin(); i != leftover.end(); ++i)
320 if (
map_.find(i->first) ==
map_.end())
322 std::stringstream buffer;
324 buffer << delimiter_;
328 this->
map_[i->first] = ref;
337 std::stringstream buffer2;
338 buffer2 << other.
name_;
339 buffer2 << delimiter_;
343 other.
map_.erase(i->first);
360 MADARA_GUARD_TYPE guard(mutex_);
362 std::vector<std::string> keys;
365 for (
size_t i = 0; i < keys.size(); ++i)
366 this->erase(keys[i]);
373 MADARA_GUARD_TYPE guard(mutex_);
383 MADARA_GUARD_TYPE guard(mutex_);
385 for (
auto entry :
map_)
397 MADARA_GUARD_TYPE guard(mutex_);
400 InternalMap::iterator found =
map_.find(key);
403 if (found !=
map_.end())
405 context_->delete_variable(found->second.get_name());
419 MADARA_GUARD_TYPE guard(mutex_);
438 MADARA_GUARD_TYPE guard(mutex_);
452 delimiter_ = delimiter;
472 InternalMap::const_iterator found =
map_.find(key);
477 MADARA_GUARD_TYPE guard(mutex_);
479 result =
context_->exists(found->second);
490 InternalMap::const_iterator found =
map_.lower_bound(prefix);
492 if (found !=
map_.end())
502 std::vector<std::string>& curkeys)
const
504 MADARA_GUARD_TYPE guard(mutex_);
505 curkeys.resize(
map_.size());
508 for (std::map<std::string, VariableReference>::const_iterator
510 i !=
map_.end(); ++i, ++j)
512 curkeys[j] = i->first;
524 MADARA_GUARD_TYPE guard(mutex_);
526 std::stringstream buffer;
528 buffer << delimiter_;
532 std::map<std::string, VariableReference>::iterator entry =
533 map_.find(final_key);
535 if (entry ==
map_.end())
539 result =
context_->read_file(ref, filename, settings_);
543 result =
context_->read_file(entry->second, filename, settings_);
557 MADARA_GUARD_TYPE guard(mutex_);
559 std::stringstream buffer;
561 buffer << delimiter_;
565 std::map<std::string, VariableReference>::iterator entry =
566 map_.find(final_key);
568 if (entry ==
map_.end())
572 result =
context_->read_file(ref, filename, settings);
576 result =
context_->read_file(entry->second, filename, settings);
590 MADARA_GUARD_TYPE guard(mutex_);
591 std::stringstream buffer;
593 buffer << delimiter_;
597 std::map<std::string, VariableReference>::iterator entry =
598 map_.find(final_key);
600 if (entry ==
map_.end())
604 result =
context_->set(ref, value, settings_);
608 result =
context_->set(entry->second, value, settings_);
624 MADARA_GUARD_TYPE guard(mutex_);
626 std::stringstream buffer;
628 buffer << delimiter_;
632 std::map<std::string, VariableReference>::iterator entry =
633 map_.find(final_key);
635 if (entry ==
map_.end())
639 result =
context_->set(ref, value, settings);
643 result =
context_->set(entry->second, value, settings);
658 MADARA_GUARD_TYPE guard(mutex_);
660 std::stringstream buffer;
662 buffer << delimiter_;
666 std::map<std::string, VariableReference>::iterator entry =
667 map_.find(final_key);
669 if (entry ==
map_.end())
673 result =
context_->set(ref, value, settings);
677 result =
context_->set(entry->second, value, settings);
692 MADARA_GUARD_TYPE guard(mutex_);
694 std::stringstream buffer;
696 buffer << delimiter_;
700 std::map<std::string, VariableReference>::iterator entry =
701 map_.find(final_key);
703 if (entry ==
map_.end())
707 result =
context_->set_index(ref, index, value, settings_);
711 result =
context_->set_index(entry->second, index, value, settings_);
727 MADARA_GUARD_TYPE guard(mutex_);
729 std::stringstream buffer;
731 buffer << delimiter_;
735 std::map<std::string, VariableReference>::iterator entry =
736 map_.find(final_key);
738 if (entry ==
map_.end())
742 result =
context_->set_index(ref, index, value, settings);
746 result =
context_->set_index(entry->second, index, value, settings);
761 MADARA_GUARD_TYPE guard(mutex_);
763 std::stringstream buffer;
765 buffer << delimiter_;
769 std::map<std::string, VariableReference>::iterator entry =
770 map_.find(final_key);
772 if (entry ==
map_.end())
776 result =
context_->set(ref, value, size, settings_);
780 result =
context_->set(entry->second, value, size, settings_);
796 MADARA_GUARD_TYPE guard(mutex_);
798 std::stringstream buffer;
800 buffer << delimiter_;
804 std::map<std::string, VariableReference>::iterator entry =
805 map_.find(final_key);
807 if (entry ==
map_.end())
811 result =
context_->set(ref, value, size, settings);
815 result =
context_->set(entry->second, value, size, settings);
823 const std::string& key,
const std::vector<KnowledgeRecord::Integer>& value)
830 MADARA_GUARD_TYPE guard(mutex_);
832 std::stringstream buffer;
834 buffer << delimiter_;
838 std::map<std::string, VariableReference>::iterator entry =
839 map_.find(final_key);
841 if (entry ==
map_.end())
845 result =
context_->set(ref, value, settings_);
849 result =
context_->set(entry->second, value, settings_);
857 const std::vector<KnowledgeRecord::Integer>& value,
865 MADARA_GUARD_TYPE guard(mutex_);
867 std::stringstream buffer;
869 buffer << delimiter_;
873 std::map<std::string, VariableReference>::iterator entry =
874 map_.find(final_key);
876 if (entry ==
map_.end())
880 return context_->set(ref, value, settings);
884 result =
context_->set(entry->second, value, settings);
899 MADARA_GUARD_TYPE guard(mutex_);
901 std::stringstream buffer;
903 buffer << delimiter_;
907 std::map<std::string, VariableReference>::iterator entry =
908 map_.find(final_key);
910 if (entry ==
map_.end())
914 result =
context_->set(ref, value, settings_);
918 result =
context_->set(entry->second, value, settings_);
933 MADARA_GUARD_TYPE guard(mutex_);
935 std::stringstream buffer;
937 buffer << delimiter_;
941 std::map<std::string, VariableReference>::iterator entry =
942 map_.find(final_key);
944 if (entry ==
map_.end())
948 result =
context_->set(ref, value, settings);
952 result =
context_->set(entry->second, value, settings);
960 const std::string& key,
size_t index,
double value)
967 MADARA_GUARD_TYPE guard(mutex_);
969 std::stringstream buffer;
971 buffer << delimiter_;
975 std::map<std::string, VariableReference>::iterator entry =
976 map_.find(final_key);
978 if (entry ==
map_.end())
982 result =
context_->set_index(ref, index, value, settings_);
986 result =
context_->set_index(entry->second, index, value, settings_);
1001 MADARA_GUARD_TYPE guard(mutex_);
1003 std::stringstream buffer;
1005 buffer << delimiter_;
1009 std::map<std::string, VariableReference>::iterator entry =
1010 map_.find(final_key);
1012 if (entry ==
map_.end())
1016 result =
context_->set_index(ref, index, value, settings);
1020 result =
context_->set_index(entry->second, index, value, settings);
1028 const std::string& key,
const double* value, uint32_t size)
1035 MADARA_GUARD_TYPE guard(mutex_);
1037 std::stringstream buffer;
1039 buffer << delimiter_;
1043 std::map<std::string, VariableReference>::iterator entry =
1044 map_.find(final_key);
1046 if (entry ==
map_.end())
1050 result =
context_->set(ref, value, size, settings_);
1054 result =
context_->set(entry->second, value, size, settings_);
1069 MADARA_GUARD_TYPE guard(mutex_);
1071 std::stringstream buffer;
1073 buffer << delimiter_;
1077 std::map<std::string, VariableReference>::iterator entry =
1078 map_.find(final_key);
1080 if (entry ==
map_.end())
1084 result =
context_->set(ref, value, size, settings);
1087 result =
context_->set(entry->second, value, size, settings);
1094 const std::string& key,
const std::vector<double>& value)
1101 MADARA_GUARD_TYPE guard(mutex_);
1103 std::stringstream buffer;
1105 buffer << delimiter_;
1109 std::map<std::string, VariableReference>::iterator entry =
1110 map_.find(final_key);
1112 if (entry ==
map_.end())
1116 result =
context_->set(ref, value, settings_);
1120 result =
context_->set(entry->second, value, settings_);
1135 MADARA_GUARD_TYPE guard(mutex_);
1137 std::stringstream buffer;
1139 buffer << delimiter_;
1143 std::map<std::string, VariableReference>::iterator entry =
1144 map_.find(final_key);
1146 if (entry ==
map_.end())
1150 result =
context_->set(ref, value, settings);
1154 result =
context_->set(entry->second, value, settings);
1169 MADARA_GUARD_TYPE guard(mutex_);
1171 std::stringstream buffer;
1173 buffer << delimiter_;
1177 std::map<std::string, VariableReference>::iterator entry =
1178 map_.find(final_key);
1180 if (entry ==
map_.end())
1184 result =
context_->set(ref, value, settings_);
1188 result =
context_->set(entry->second, value, settings_);
1203 MADARA_GUARD_TYPE guard(mutex_);
1205 std::stringstream buffer;
1207 buffer << delimiter_;
1211 std::map<std::string, VariableReference>::iterator entry =
1212 map_.find(final_key);
1214 if (entry ==
map_.end())
1218 result =
context_->set(ref, value, settings);
1222 result =
context_->set(entry->second, value, settings);
1230 const std::string& key,
const unsigned char* value,
size_t size)
1237 MADARA_GUARD_TYPE guard(mutex_);
1239 std::stringstream buffer;
1241 buffer << delimiter_;
1245 std::map<std::string, VariableReference>::iterator entry =
1246 map_.find(final_key);
1248 if (entry ==
map_.end())
1252 result =
context_->set_file(ref, value, size, settings_);
1256 result =
context_->set_file(entry->second, value, size, settings_);
1264 const unsigned char* value,
size_t size,
1272 MADARA_GUARD_TYPE guard(mutex_);
1274 std::stringstream buffer;
1276 buffer << delimiter_;
1280 std::map<std::string, VariableReference>::iterator entry =
1281 map_.find(final_key);
1283 if (entry ==
map_.end())
1287 result =
context_->set_file(ref, value, size, settings);
1290 result =
context_->set_file(entry->second, value, size, settings);
1297 const std::string& key,
const unsigned char* value,
size_t size)
1304 MADARA_GUARD_TYPE guard(mutex_);
1306 std::stringstream buffer;
1308 buffer << delimiter_;
1312 std::map<std::string, VariableReference>::iterator entry =
1313 map_.find(final_key);
1315 if (entry ==
map_.end())
1319 result =
context_->set_jpeg(ref, value, size, settings_);
1323 result =
context_->set_jpeg(entry->second, value, size, settings_);
1331 const unsigned char* value,
size_t size,
1339 MADARA_GUARD_TYPE guard(mutex_);
1341 std::stringstream buffer;
1343 buffer << delimiter_;
1347 std::map<std::string, VariableReference>::iterator entry =
1348 map_.find(final_key);
1350 if (entry ==
map_.end())
1354 result =
context_->set_jpeg(ref, value, size, settings);
1358 result =
context_->set_jpeg(entry->second, value, size, settings);
1371 MADARA_GUARD_TYPE guard(mutex_);
1373 std::stringstream buffer;
1375 buffer << delimiter_;
1378 context_->set_quality(buffer.str(), quality,
true, settings);
1387 "Map::is_true: checking for truth in all elements\n");
1392 MADARA_GUARD_TYPE guard(mutex_);
1396 for (InternalMap::const_iterator index =
map_.begin(); index !=
map_.end();
1400 "Map::is_true: checking index, is_false of %d. \n",
1401 (
int)
context_->get(index->second).is_false());
1403 if (
context_->get(index->second).is_false())
1406 "Map::is_true: result is false, breaking\n");
1413 if (
map_.size() == 0)
1418 "Map::is_true: final result is %d\n", (
int)result);
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
VariableReferenceMap map_
const ThreadSafeContext * context_
A thread-safe guard for a context or knowledge base.
This class provides a distributed knowledge base to users.
This class encapsulates an entry in a KnowledgeBase.
Settings for applying knowledge updates.
Settings for applying knowledge updates.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the current value of a variable.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
size_t to_map(const std::string &subject, std::map< std::string, knowledge::KnowledgeRecord > &target)
Fills a variable map with Knowledge Records that match an expression.
Optimized reference to a variable within the knowledge base.
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
KnowledgeUpdateSettings settings_
Settings for modifications.
std::string name_
Prefix of variable.
MADARA_LOCK_TYPE mutex_
guard for access and changes
This class stores a map of strings to KaRL variables.
int set_file(const std::string &key, const unsigned char *value, size_t size)
Atomically sets the value of an index to an arbitrary string.
void set_delimiter(const std::string &delimiter, bool sync=true)
Sets the delimiter for adding and detecting subvariables.
ThreadSafeContext * context_
Variable context that we are modifying.
size_t size(void) const
Returns the size of the map.
bool is_false(void) const
Determines if the value of the map is false.
void erase(const std::string &key)
Erases a variable from the map.
int set_jpeg(const std::string &key, const unsigned char *value, size_t size)
Atomically sets the value of an index to a JPEG image.
std::map< std::string, VariableReference > InternalMap
internal map of variable references
std::string delimiter_
Delimiter for the prefix to subvars.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
bool is_true(void) const
Determines if all values in the map are true.
virtual ~Map()
Destructor.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
bool exists(const std::string &key) const
Checks for the existence of a key.
void operator=(const Map &rhs)
Assignment operator.
int set(const std::string &key, madara::knowledge::KnowledgeRecord::Integer value=madara::knowledge::KnowledgeRecord::MODIFIED)
Sets a location within the map to the specified value.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
void exchange(Map &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the map at this location with the map at another location.
knowledge::KnowledgeRecord to_record(const std::string &key)
Retrieves a copy of the record from the map.
knowledge::KnowledgeRecord operator[](const std::string &key)
Retrieves a copy of the record from the map.
virtual BaseContainer * clone(void) const
Clones this container.
Map(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
void modify(void)
Mark the vector as modified.
std::vector< std::string > sync_keys(void)
Syncs the keys from the knowledge base.
InternalMap map_
Map of variables to values.
bool has_prefix(const std::string &prefix) const
Checks for the existence of a prefix in the keys.
void reset(void)
Resets the map.
void keys(std::vector< std::string > &curkeys) const
Returns the keys within the map.
void set_quality(const std::string &key, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
int read_file(const std::string &key, const std::string &filename)
Read a file into a location in the map.
void clear(bool clear_knowledge=true)
Clears the map.
int set_index(const std::string &key, size_t index, madara::knowledge::KnowledgeRecord::Integer value)
Sets an index within an array to a specified value.
virtual bool is_false_(void) const
Polymorphic is false method which can be used to determine if at least one value in the container is ...
Provides functions and classes for the distributed knowledge base.
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.