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)
268 MADARA_GUARD_TYPE guard (
mutex_, std::adopt_lock),
269 guard2 (other.
mutex_, std::adopt_lock);
279 InternalMap::iterator i =
map_.begin ();
280 while (i !=
map_.end ())
286 InternalMap::iterator other_found = other.
map_.find (i->first);
288 if (other_found != other.
map_.end ())
296 other.
map_ [i->first] = other_found->second;
300 leftover.erase (i->first);
304 std::stringstream buffer;
305 buffer << other.
name_;
312 other.
map_[i->first] = ref;
318 std::stringstream buffer2;
319 buffer2 << this->
name_;
324 this->
map_.erase (i++);
329 this->
context_->
set (i->second, zero, this->settings_);
337 for (i = leftover.begin (); i != leftover.end (); ++i)
339 if (
map_.find (i->first) ==
map_.end ())
341 std::stringstream buffer;
347 this->
map_[i->first] = ref;
357 std::stringstream buffer2;
358 buffer2 << other.
name_;
363 other.
map_.erase (i->first);
381 MADARA_GUARD_TYPE guard (
mutex_);
383 std::vector <std::string>
keys;
386 for (
size_t i = 0; i < keys.size (); ++i)
387 this->
erase (keys[i]);
391 MADARA_GUARD_TYPE guard (
mutex_);
404 InternalMap::iterator found =
map_.find (key);
407 if (found !=
map_.end ())
424 MADARA_GUARD_TYPE guard (
mutex_);
445 MADARA_GUARD_TYPE guard (
mutex_);
485 InternalMap::const_iterator found =
map_.find (key);
490 MADARA_GUARD_TYPE guard (
mutex_);
504 InternalMap::const_iterator found =
map_.lower_bound (prefix);
506 if (found !=
map_.end ())
517 std::vector <std::string> & curkeys)
const 519 MADARA_GUARD_TYPE guard (
mutex_);
520 curkeys.resize (
map_.size ());
523 for (std::map <std::string, VariableReference>::const_iterator i =
525 i !=
map_.end (); ++i, ++j)
527 curkeys[j] = i->first;
543 MADARA_GUARD_TYPE guard (
mutex_);
545 std::stringstream buffer;
551 std::map <std::string, VariableReference>::iterator entry =
552 map_.find (final_key);
554 if (entry ==
map_.end ())
579 MADARA_GUARD_TYPE guard (
mutex_);
581 std::stringstream buffer;
587 std::map <std::string, VariableReference>::iterator entry =
588 map_.find (final_key);
590 if (entry ==
map_.end ())
613 MADARA_GUARD_TYPE guard (
mutex_);
614 std::stringstream buffer;
620 std::map <std::string, VariableReference>::iterator entry =
621 map_.find (final_key);
623 if (entry ==
map_.end ())
648 MADARA_GUARD_TYPE guard (
mutex_);
650 std::stringstream buffer;
656 std::map <std::string, VariableReference>::iterator entry =
657 map_.find (final_key);
659 if (entry ==
map_.end ())
667 result =
context_->
set (entry->second, value, settings);
685 MADARA_GUARD_TYPE guard (
mutex_);
687 std::stringstream buffer;
693 std::map <std::string, VariableReference>::iterator entry =
694 map_.find (final_key);
696 if (entry ==
map_.end ())
723 MADARA_GUARD_TYPE guard (
mutex_);
725 std::stringstream buffer;
731 std::map <std::string, VariableReference>::iterator entry =
732 map_.find (final_key);
734 if (entry ==
map_.end ())
760 MADARA_GUARD_TYPE guard (
mutex_);
762 std::stringstream buffer;
768 std::map <std::string, VariableReference>::iterator entry =
769 map_.find (final_key);
771 if (entry ==
map_.end ())
797 MADARA_GUARD_TYPE guard (
mutex_);
799 std::stringstream buffer;
805 std::map <std::string, VariableReference>::iterator entry =
806 map_.find (final_key);
808 if (entry ==
map_.end ())
812 result =
context_->
set (ref, value, size, settings);
816 result =
context_->
set (entry->second, value, size, settings);
826 const std::vector <KnowledgeRecord::Integer> & value)
833 MADARA_GUARD_TYPE guard (
mutex_);
835 std::stringstream buffer;
841 std::map <std::string, VariableReference>::iterator entry =
842 map_.find (final_key);
844 if (entry ==
map_.end ())
862 const std::vector <KnowledgeRecord::Integer> & value,
870 MADARA_GUARD_TYPE guard (
mutex_);
872 std::stringstream buffer;
878 std::map <std::string, VariableReference>::iterator entry =
879 map_.find (final_key);
881 if (entry ==
map_.end ())
889 result =
context_->
set (entry->second, value, settings);
906 MADARA_GUARD_TYPE guard (
mutex_);
908 std::stringstream buffer;
914 std::map <std::string, VariableReference>::iterator entry =
915 map_.find (final_key);
917 if (entry ==
map_.end ())
942 MADARA_GUARD_TYPE guard (
mutex_);
944 std::stringstream buffer;
950 std::map <std::string, VariableReference>::iterator entry =
951 map_.find (final_key);
953 if (entry ==
map_.end ())
961 result =
context_->
set (entry->second, value, settings);
979 MADARA_GUARD_TYPE guard (
mutex_);
981 std::stringstream buffer;
987 std::map <std::string, VariableReference>::iterator entry =
988 map_.find (final_key);
990 if (entry ==
map_.end ())
1016 MADARA_GUARD_TYPE guard (
mutex_);
1018 std::stringstream buffer;
1024 std::map <std::string, VariableReference>::iterator entry =
1025 map_.find (final_key);
1027 if (entry ==
map_.end ())
1044 const double * value,
1052 MADARA_GUARD_TYPE guard (
mutex_);
1054 std::stringstream buffer;
1060 std::map <std::string, VariableReference>::iterator entry =
1061 map_.find (final_key);
1063 if (entry ==
map_.end ())
1080 const double * value,
1089 MADARA_GUARD_TYPE guard (
mutex_);
1091 std::stringstream buffer;
1097 std::map <std::string, VariableReference>::iterator entry =
1098 map_.find (final_key);
1100 if (entry ==
map_.end ())
1104 result =
context_->
set (ref, value, size, settings);
1107 result =
context_->
set (entry->second, value, size, settings);
1115 const std::vector <double> & value)
1122 MADARA_GUARD_TYPE guard (
mutex_);
1124 std::stringstream buffer;
1130 std::map <std::string, VariableReference>::iterator entry =
1131 map_.find (final_key);
1133 if (entry ==
map_.end ())
1150 const std::vector <double> & value,
1158 MADARA_GUARD_TYPE guard (
mutex_);
1160 std::stringstream buffer;
1166 std::map <std::string, VariableReference>::iterator entry =
1167 map_.find (final_key);
1169 if (entry ==
map_.end ())
1177 result =
context_->
set (entry->second, value, settings);
1193 MADARA_GUARD_TYPE guard (
mutex_);
1195 std::stringstream buffer;
1201 std::map <std::string, VariableReference>::iterator entry =
1202 map_.find (final_key);
1204 if (entry ==
map_.end ())
1229 MADARA_GUARD_TYPE guard (
mutex_);
1231 std::stringstream buffer;
1237 std::map <std::string, VariableReference>::iterator entry =
1238 map_.find (final_key);
1240 if (entry ==
map_.end ())
1248 result =
context_->
set (entry->second, value, settings);
1257 const unsigned char * value,
size_t size)
1264 MADARA_GUARD_TYPE guard (
mutex_);
1266 std::stringstream buffer;
1272 std::map <std::string, VariableReference>::iterator entry =
1273 map_.find (final_key);
1275 if (entry ==
map_.end ())
1292 const unsigned char * value,
size_t size,
1300 MADARA_GUARD_TYPE guard (
mutex_);
1302 std::stringstream buffer;
1308 std::map <std::string, VariableReference>::iterator entry =
1309 map_.find (final_key);
1311 if (entry ==
map_.end ())
1326 const unsigned char * value,
size_t size)
1333 MADARA_GUARD_TYPE guard (
mutex_);
1335 std::stringstream buffer;
1341 std::map <std::string, VariableReference>::iterator entry =
1342 map_.find (final_key);
1344 if (entry ==
map_.end ())
1361 const unsigned char * value,
size_t size,
1369 MADARA_GUARD_TYPE guard (
mutex_);
1371 std::stringstream buffer;
1377 std::map <std::string, VariableReference>::iterator entry =
1378 map_.find (final_key);
1380 if (entry ==
map_.end ())
1403 MADARA_GUARD_TYPE guard (
mutex_);
1405 std::stringstream buffer;
1417 bool result (
false);
1420 "Map::is_true: checking for truth in all elements\n");
1425 MADARA_GUARD_TYPE guard (
mutex_);
1429 for (InternalMap::const_iterator index =
map_.begin ();
1430 index !=
map_.end (); ++index)
1433 "Map::is_true: checking index, is_false of %d. \n",
1439 "Map::is_true: result is false, breaking\n");
1446 if (
map_.size () == 0)
1451 "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
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.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
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.
Settings for applying knowledge updates.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
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.