9 :
BaseContainer (
"", settings), context_ (0), delimiter_ (delimiter)
21 std::map <std::string, knowledge::KnowledgeRecord> contents;
26 if (contents.size () > 0)
28 for (std::map <std::string, knowledge::KnowledgeRecord>::iterator i =
29 contents.begin (); i != contents.end (); ++i)
31 map_[i->first.substr (common.size ())] =
32 knowledge.
get_ref (i->first, keep_local);
45 std::map <std::string, knowledge::KnowledgeRecord> contents;
50 if (contents.size () > 0)
52 for (std::map <std::string, knowledge::KnowledgeRecord>::iterator i =
53 contents.begin (); i != contents.end (); ++i)
55 map_[i->first.substr (common.size ())] =
56 knowledge.
get_ref (i->first, keep_local);
81 MADARA_GUARD_TYPE guard (
mutex_);
83 for (InternalMap::const_iterator index =
map_.begin ();
84 index !=
map_.end (); ++index)
94 std::stringstream result;
101 MADARA_GUARD_TYPE guard (
mutex_);
103 result << this->
name_;
104 result <<
" [" <<
map_.size () <<
"]";
109 for (InternalMap::const_iterator index =
map_.begin ();
110 index !=
map_.end (); ++index)
121 result << index->first <<
" => " <<
128 return result.str ();
147 return new Map (*
this);
185 MADARA_GUARD_TYPE guard (
mutex_);
186 std::stringstream buffer;
193 std::map <std::string, VariableReference>::const_iterator entry =
194 map_.find (final_key);
196 if (entry ==
map_.end ())
203 result =
context_->
get (entry->second, keep_local);
219 MADARA_GUARD_TYPE guard (
mutex_);
224 std::vector <std::string>
227 std::vector <std::string> additions;
232 MADARA_GUARD_TYPE guard (
mutex_);
236 std::map <std::string, knowledge::KnowledgeRecord> contents;
241 for (std::map <std::string, knowledge::KnowledgeRecord>::iterator i =
242 contents.begin (); i != contents.end (); ++i)
244 std::string key = i->first.substr (common.size ());
248 additions.push_back (key);
260 Map & other,
bool refresh_keys,
bool delete_keys)
266 MADARA_GUARD_TYPE guard (
mutex_), guard2 (other.
mutex_);
275 InternalMap::iterator i =
map_.begin ();
276 while (i !=
map_.end ())
282 InternalMap::iterator other_found = other.
map_.find (i->first);
284 if (other_found != other.
map_.end ())
292 other.
map_ [i->first] = other_found->second;
296 leftover.erase (i->first);
300 std::stringstream buffer;
301 buffer << other.
name_;
308 other.
map_[i->first] = ref;
314 std::stringstream buffer2;
315 buffer2 << this->
name_;
320 this->
map_.erase (i++);
325 this->
context_->
set (i->second, zero, this->settings_);
333 for (i = leftover.begin (); i != leftover.end (); ++i)
335 if (
map_.find (i->first) ==
map_.end ())
337 std::stringstream buffer;
343 this->
map_[i->first] = ref;
353 std::stringstream buffer2;
354 buffer2 << other.
name_;
359 other.
map_.erase (i->first);
377 MADARA_GUARD_TYPE guard (
mutex_);
379 std::vector <std::string>
keys;
382 for (
size_t i = 0; i < keys.size (); ++i)
383 this->
erase (keys[i]);
387 MADARA_GUARD_TYPE guard (
mutex_);
400 InternalMap::iterator found =
map_.find (key);
403 if (found !=
map_.end ())
420 MADARA_GUARD_TYPE guard (
mutex_);
441 MADARA_GUARD_TYPE guard (
mutex_);
481 InternalMap::const_iterator found =
map_.find (key);
486 MADARA_GUARD_TYPE guard (
mutex_);
500 InternalMap::const_iterator found =
map_.lower_bound (prefix);
502 if (found !=
map_.end ())
513 std::vector <std::string> & curkeys)
const 515 MADARA_GUARD_TYPE guard (
mutex_);
516 curkeys.resize (
map_.size ());
519 for (std::map <std::string, VariableReference>::const_iterator i =
521 i !=
map_.end (); ++i, ++j)
523 curkeys[j] = i->first;
539 MADARA_GUARD_TYPE guard (
mutex_);
541 std::stringstream buffer;
547 std::map <std::string, VariableReference>::iterator entry =
548 map_.find (final_key);
550 if (entry ==
map_.end ())
575 MADARA_GUARD_TYPE guard (
mutex_);
577 std::stringstream buffer;
583 std::map <std::string, VariableReference>::iterator entry =
584 map_.find (final_key);
586 if (entry ==
map_.end ())
609 MADARA_GUARD_TYPE guard (
mutex_);
610 std::stringstream buffer;
616 std::map <std::string, VariableReference>::iterator entry =
617 map_.find (final_key);
619 if (entry ==
map_.end ())
644 MADARA_GUARD_TYPE guard (
mutex_);
646 std::stringstream buffer;
652 std::map <std::string, VariableReference>::iterator entry =
653 map_.find (final_key);
655 if (entry ==
map_.end ())
663 result =
context_->
set (entry->second, value, settings);
681 MADARA_GUARD_TYPE guard (
mutex_);
683 std::stringstream buffer;
689 std::map <std::string, VariableReference>::iterator entry =
690 map_.find (final_key);
692 if (entry ==
map_.end ())
719 MADARA_GUARD_TYPE guard (
mutex_);
721 std::stringstream buffer;
727 std::map <std::string, VariableReference>::iterator entry =
728 map_.find (final_key);
730 if (entry ==
map_.end ())
756 MADARA_GUARD_TYPE guard (
mutex_);
758 std::stringstream buffer;
764 std::map <std::string, VariableReference>::iterator entry =
765 map_.find (final_key);
767 if (entry ==
map_.end ())
793 MADARA_GUARD_TYPE guard (
mutex_);
795 std::stringstream buffer;
801 std::map <std::string, VariableReference>::iterator entry =
802 map_.find (final_key);
804 if (entry ==
map_.end ())
808 result =
context_->
set (ref, value, size, settings);
812 result =
context_->
set (entry->second, value, size, settings);
822 const std::vector <KnowledgeRecord::Integer> & value)
829 MADARA_GUARD_TYPE guard (
mutex_);
831 std::stringstream buffer;
837 std::map <std::string, VariableReference>::iterator entry =
838 map_.find (final_key);
840 if (entry ==
map_.end ())
858 const std::vector <KnowledgeRecord::Integer> & value,
866 MADARA_GUARD_TYPE guard (
mutex_);
868 std::stringstream buffer;
874 std::map <std::string, VariableReference>::iterator entry =
875 map_.find (final_key);
877 if (entry ==
map_.end ())
885 result =
context_->
set (entry->second, value, settings);
902 MADARA_GUARD_TYPE guard (
mutex_);
904 std::stringstream buffer;
910 std::map <std::string, VariableReference>::iterator entry =
911 map_.find (final_key);
913 if (entry ==
map_.end ())
938 MADARA_GUARD_TYPE guard (
mutex_);
940 std::stringstream buffer;
946 std::map <std::string, VariableReference>::iterator entry =
947 map_.find (final_key);
949 if (entry ==
map_.end ())
957 result =
context_->
set (entry->second, value, settings);
975 MADARA_GUARD_TYPE guard (
mutex_);
977 std::stringstream buffer;
983 std::map <std::string, VariableReference>::iterator entry =
984 map_.find (final_key);
986 if (entry ==
map_.end ())
1012 MADARA_GUARD_TYPE guard (
mutex_);
1014 std::stringstream buffer;
1020 std::map <std::string, VariableReference>::iterator entry =
1021 map_.find (final_key);
1023 if (entry ==
map_.end ())
1040 const double * value,
1048 MADARA_GUARD_TYPE guard (
mutex_);
1050 std::stringstream buffer;
1056 std::map <std::string, VariableReference>::iterator entry =
1057 map_.find (final_key);
1059 if (entry ==
map_.end ())
1076 const double * value,
1085 MADARA_GUARD_TYPE guard (
mutex_);
1087 std::stringstream buffer;
1093 std::map <std::string, VariableReference>::iterator entry =
1094 map_.find (final_key);
1096 if (entry ==
map_.end ())
1100 result =
context_->
set (ref, value, size, settings);
1103 result =
context_->
set (entry->second, value, size, settings);
1111 const std::vector <double> & value)
1118 MADARA_GUARD_TYPE guard (
mutex_);
1120 std::stringstream buffer;
1126 std::map <std::string, VariableReference>::iterator entry =
1127 map_.find (final_key);
1129 if (entry ==
map_.end ())
1146 const std::vector <double> & value,
1154 MADARA_GUARD_TYPE guard (
mutex_);
1156 std::stringstream buffer;
1162 std::map <std::string, VariableReference>::iterator entry =
1163 map_.find (final_key);
1165 if (entry ==
map_.end ())
1173 result =
context_->
set (entry->second, value, settings);
1189 MADARA_GUARD_TYPE guard (
mutex_);
1191 std::stringstream buffer;
1197 std::map <std::string, VariableReference>::iterator entry =
1198 map_.find (final_key);
1200 if (entry ==
map_.end ())
1225 MADARA_GUARD_TYPE guard (
mutex_);
1227 std::stringstream buffer;
1233 std::map <std::string, VariableReference>::iterator entry =
1234 map_.find (final_key);
1236 if (entry ==
map_.end ())
1244 result =
context_->
set (entry->second, value, settings);
1253 const unsigned char * value,
size_t size)
1260 MADARA_GUARD_TYPE guard (
mutex_);
1262 std::stringstream buffer;
1268 std::map <std::string, VariableReference>::iterator entry =
1269 map_.find (final_key);
1271 if (entry ==
map_.end ())
1288 const unsigned char * value,
size_t size,
1296 MADARA_GUARD_TYPE guard (
mutex_);
1298 std::stringstream buffer;
1304 std::map <std::string, VariableReference>::iterator entry =
1305 map_.find (final_key);
1307 if (entry ==
map_.end ())
1322 const unsigned char * value,
size_t size)
1329 MADARA_GUARD_TYPE guard (
mutex_);
1331 std::stringstream buffer;
1337 std::map <std::string, VariableReference>::iterator entry =
1338 map_.find (final_key);
1340 if (entry ==
map_.end ())
1357 const unsigned char * value,
size_t size,
1365 MADARA_GUARD_TYPE guard (
mutex_);
1367 std::stringstream buffer;
1373 std::map <std::string, VariableReference>::iterator entry =
1374 map_.find (final_key);
1376 if (entry ==
map_.end ())
1399 MADARA_GUARD_TYPE guard (
mutex_);
1401 std::stringstream buffer;
1413 bool result (
false);
1416 "Map::is_true: checking for truth in all elements\n");
1421 MADARA_GUARD_TYPE guard (
mutex_);
1425 for (InternalMap::const_iterator index =
map_.begin ();
1426 index !=
map_.end (); ++index)
1429 "Map::is_true: checking index, is_false of %d. \n",
1435 "Map::is_true: result is false, breaking\n");
1442 if (
map_.size () == 0)
1447 "Map::is_true: final result is %d\n", (int)result);
This class encapsulates an entry in a KnowledgeBase.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
ThreadSafeContext * context_
Variable context that we are modifying.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=knowledge::KnowledgeReferenceSettings(false))
Retrieves the value of a variable.
std::map< std::string, VariableReference > InternalMap
internal map of variable references
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a 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.
int set_file(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an arbitrary string.
int set_jpeg(const std::string &key, const unsigned char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to a JPEG image.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
std::string name_
Prefix of variable.
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
void keys(std::vector< std::string > &curkeys) const
Returns the keys within the map.
bool is_false(void) const
Determines if the value of the map is false.
Map(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
virtual BaseContainer * clone(void) const
Clones this container.
std::vector< std::string > sync_keys(void)
Syncs the keys from the knowledge base.
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.
MADARA_LOCK_TYPE mutex_
guard for access and changes
bool is_true(void) const
Determines if all values in the map are true.
bool exists(const std::string &key) const
Checks for the existence of a key.
Optimized reference to a variable within the knowledge base.
int set_index(const std::string &key, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of an array index to a value.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
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.
void erase(const std::string &key)
Erases a variable from the map.
std::string delimiter_
Delimiter for the prefix to subvars.
bool has_prefix(const std::string &prefix) const
Checks for the existence of a prefix in the keys.
void set_delimiter(const std::string &delimiter, bool sync=true)
Sets the delimiter for adding and detecting subvariables.
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.
A thread-safe guard for a context or knowledge base.
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.
void modify(void)
Mark the vector as modified.
virtual void modify_(void)
Polymorphic modify method used by collection containers.
This class stores a map of strings to KaRL variables.
This class provides a distributed knowledge base to users.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
void exchange(Map &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the map at this location with the map at another location.
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.
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
Provides functions and classes for the distributed knowledge base.
KnowledgeUpdateSettings settings_
Settings for modifications.
knowledge::KnowledgeRecord to_record(const std::string &key)
Retrieves a copy of the record from the map.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
uint32_t set_quality(const std::string &key, uint32_t quality, bool force_update, const KnowledgeReferenceSettings &settings)
Atomically sets quality of this process for a variable.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
MADARA_Export bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
bool is_false(void) const
Checks to see if the record is false.
Settings for applying knowledge updates.
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
virtual ~Map()
Destructor.
Provides an interface for external functions into the MADARA KaRL variable settings.
This class is an abstract base class for all containers.
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 ...
InternalMap map_
Map of variables to values.
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Atomically returns a reference to the variable.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
knowledge::KnowledgeRecord operator[](const std::string &key)
Retrieves a copy of the record from the map.
size_t size(void) const
Returns the size of the map.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.
void operator=(const Map &rhs)
Assignment operator.