1 #ifndef _MADARA_THREADSAFECONTEXT_INL_ 2 #define _MADARA_THREADSAFECONTEXT_INL_ 24 return read_file (variable, filename, settings);
57 KnowledgeMap::iterator found;
59 MADARA_GUARD_TYPE guard (
mutex_);
64 found =
map_.find (cur_key);
68 found =
map_.find (key);
71 if (found !=
map_.end ())
73 return &found->second;
85 MADARA_GUARD_TYPE guard (
mutex_);
95 KnowledgeMap::const_iterator found;
97 MADARA_GUARD_TYPE guard (
mutex_);
102 found =
map_.find (cur_key);
106 found =
map_.find (key);
109 if (found !=
map_.end ())
111 return &found->second;
123 MADARA_GUARD_TYPE guard (
mutex_);
134 MADARA_GUARD_TYPE guard (
mutex_);
137 return record && record->
exists ();
147 MADARA_GUARD_TYPE guard (
mutex_);
174 return set (variable, std::forward<T>(value), settings);
184 MADARA_GUARD_TYPE guard (
mutex_);
187 return set_unsafe(variable, std::forward<T>(value), settings);
200 return set (variable, value, size, settings);
212 MADARA_GUARD_TYPE guard (
mutex_);
224 template<
typename... Args>
235 record->write_quality < record->quality)
240 record->set_value(std::forward<Args>(args)...);
241 record->quality = record->write_quality;
282 const char * value,
size_t size,
286 return set_xml (variable, value, size, settings);
292 const char * value,
size_t size,
296 return set_text (variable, value, size, settings);
302 const unsigned char * value,
size_t size,
306 return set_jpeg (variable, value, size, settings);
312 const unsigned char * value,
size_t size,
316 return set_file (variable, value, size, settings);
323 size_t index, T&& value,
327 return set_index (variable, index, std::forward<T>(value), settings);
337 MADARA_GUARD_TYPE guard (
mutex_);
353 record->write_quality < record->quality)
358 record->set_index (index, std::forward<T>(value));
359 record->quality = record->write_quality;
371 std::forward<T>(value), settings);
385 return inc (variable, settings);
394 return dec (variable, settings);
402 MADARA_GUARD_TYPE guard (
mutex_);
408 record->write_quality >= record->quality)
411 record->
quality = record->write_quality;
421 #ifndef _MADARA_NO_KARL_ 428 MADARA_GUARD_TYPE guard (
mutex_);
433 #endif // _MADARA_NO_KARL_ 444 MADARA_GUARD_TYPE guard (
mutex_);
449 key_ptr = &key_actual;
455 KnowledgeMap::iterator record =
map_.find (*key_ptr);
456 found = record !=
map_.end ();
460 record->second.clear_value ();
477 MADARA_GUARD_TYPE guard (
mutex_);
482 key_ptr = &key_actual;
492 result =
map_.erase (*key_ptr) == 1;
504 MADARA_GUARD_TYPE guard (
mutex_);
511 return map_.erase (var.
entry_->first.c_str ()) == 1;
516 KnowledgeMap::iterator end,
519 for (
auto cur = begin; cur != end; ++cur) {
523 map_.erase (begin, end);
535 MADARA_GUARD_TYPE guard (
mutex_);
540 key_ptr = &key_actual;
549 KnowledgeMap::const_iterator found =
map_.find (*key_ptr);
552 if (found !=
map_.end ())
568 MADARA_GUARD_TYPE guard (
mutex_);
574 record->write_quality >= record->quality)
577 record->
quality = record->write_quality;
594 MADARA_GUARD_TYPE guard (
mutex_);
614 MADARA_GUARD_TYPE guard (
mutex_);
619 key_ptr = &key_actual;
632 if (record.
clock < clock)
634 record.
clock = clock;
653 MADARA_GUARD_TYPE guard (
mutex_);
658 key_ptr = &key_actual;
678 MADARA_GUARD_TYPE guard (
mutex_);
687 MADARA_GUARD_TYPE guard (
mutex_);
694 MADARA_GUARD_TYPE guard (
mutex_);
702 MADARA_GUARD_TYPE guard (
mutex_);
715 MADARA_GUARD_TYPE guard (
mutex_);
720 key_ptr = &key_actual;
730 KnowledgeMap::const_iterator found =
map_.find (*key_ptr);
733 if (found !=
map_.end ())
735 return found->second.clock;
753 return mutex_.try_lock ();
767 const std::string & statement,
unsigned int level)
const 778 MADARA_GUARD_TYPE guard (
mutex_);
789 for (KnowledgeMap::iterator i =
map_.begin ();
790 i !=
map_.end (); ++i)
792 i->second.reset_value ();
796 changed_.MADARA_CONDITION_NOTIFY_ONE ();
807 MADARA_GUARD_TYPE guard (
mutex_);
814 mutex_.MADARA_LOCK_UNLOCK ();
839 MADARA_GUARD_TYPE guard (
mutex_);
875 MADARA_GUARD_TYPE guard (
mutex_);
915 changed_.MADARA_CONDITION_NOTIFY_ALL ();
935 MADARA_GUARD_TYPE guard (
mutex_);
942 MADARA_GUARD_TYPE guard (
mutex_);
943 std::stringstream result;
945 result <<
changed_map_.size () <<
" modifications ready to send:\n";
956 result <<
"Double: ";
960 result <<
"Double array: ";
964 result <<
"Integer: ";
968 result <<
"Integer array: ";
972 result <<
"String: ";
976 result <<
"Unknown: ";
979 result << entry.first <<
" = " << record.
to_string () <<
"\n";
982 return result.str ();
990 MADARA_GUARD_TYPE guard (
mutex_);
998 MADARA_GUARD_TYPE guard (
mutex_);
1005 "ThreadSafeContext::save_modifieds:" \
1006 " changed_map.size=%d, snapshot.size=%d\n",
1012 "ThreadSafeContext::save_modifieds:" \
1013 " snapshot[%d].name=%s\n",
1016 snapshot.emplace_back (entry.second);
1026 MADARA_GUARD_TYPE guard (
mutex_);
1028 for (
auto &entry : modifieds)
1030 changed_map_.insert (std::make_pair(entry.get_name (), entry));
1038 MADARA_GUARD_TYPE guard (
mutex_);
1047 MADARA_GUARD_TYPE guard (
mutex_);
1057 MADARA_GUARD_TYPE guard (
mutex_);
1064 for (KnowledgeMap::iterator i =
map_.begin ();
1067 for (
auto &entry :
map_)
1069 if (entry.first.size () > 0 && entry.first[0] !=
'.')
1090 MADARA_GUARD_TYPE guard (
mutex_);
1098 MADARA_GUARD_TYPE guard (
mutex_);
1110 MADARA_GUARD_TYPE guard (
mutex_);
1111 changed_.MADARA_CONDITION_NOTIFY_ONE ();
1114 changed_.MADARA_CONDITION_NOTIFY_ONE ();
1138 #endif // _MADARA_THREADSAFECONTEXT_INL_ This class encapsulates an entry in a KnowledgeBase.
bool expand_variables
Toggle for always attempting to expand variables (true) or never expanding variables (false) ...
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
void delete_variables(KnowledgeMap::iterator begin, KnowledgeMap::iterator end, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes variables.
madara::knowledge::KnowledgeMap map_
Hash table containing variable names and values.
int set_index_unsafe(const VariableReference &variable, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
NON-Atomically sets the value of an array index to a value.
int32_t type(void) const
returns the size of the value
uint64_t set_clock(uint64_t clock)
Atomically sets the lamport clock.
int set_index_unsafe_impl(const VariableReference &variable, size_t index, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
uint32_t quality
priority of the update
MADARA_CONDITION_TYPE changed_
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.
madara::knowledge::KnowledgeRecord KnowledgeRecord
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.
void mark_and_signal(VariableReference ref, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
method for marking a record modified and signaling changes
KnowledgeRecord * get_record_unsafe(void) const
Returns a pointer to the variable's KnowledgeRecord Do not use this pointer unless you've locked the ...
void lock(void) const
Locks the mutex on this context.
bool signal_changes
Toggle whether to signal changes have happened.
bool delete_expression(const std::string &expression)
Deletes the expression from the interpreter cache.
int set_unsafe(const VariableReference &variable, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
NON-Atomically sets the value of a variable to the specific value.
void set_level(int level)
Sets the maximum logging detail level.
bool is_binary_file_type(void) const
returns true if the knowledge record has a binary file type
void signal(bool lock=true) const
Signals that this thread is done with the context.
const VariableReferenceMap & get_modifieds(void) const
Retrieves a list of modified variables.
void print(unsigned int level) const
Atomically prints all variables and values in the context.
VariableReferences save_modifieds(void) const
Saves the list of modified records to use later for resending.
bool exists(void) const
Checks if record exists (i.e., is not uncreated)
void attach_logger(logger::Logger &logger) const
Attaches a logger to be used for printing.
Provides knowledge logging services to files and terminals.
void mark_to_checkpoint(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable reference to modified at current clock for the purposes of checkpointing (even if it...
int get_log_level(void)
Gets the log level.
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.
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
A multi-threaded logger for logging to one or more destinations.
void apply_modified(void)
Changes all global variables to modified at current clock.
int get_level(void)
Gets the maximum logging detail level.
int status(void) const
returns the status of the record.
VariableReferenceMap changed_map_
madara::knowledge::KnowledgeRecord dec(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically decrements the value of the variable.
int set_xml(const std::string &key, const char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an XML string.
uint64_t clock_increment
Default clock increment.
void add_logger(const std::string &filename)
Adds a file to the logger.
bool try_lock(void) const
Locks the mutex on this context.
#define madara_logger_ptr_log(logger, level,...)
Fast version of the madara::logger::log method for Logger pointers.
void unlock(void) const
Unlocks the mutex on this context.
int set_unsafe_impl(const VariableReference &variable, const KnowledgeUpdateSettings &settings, Args &&...args)
const VariableReferenceMap & get_local_modified(void) const
Retrieves a list of modified local variables.
void add_file(const std::string &filename)
Adds a file to the logger.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
void mark_to_send_unsafe(VariableReference ref, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable to modified at current clock, and queues it to send, even if it is a local that woul...
void set_modified(void)
sets the status to modified
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
uint64_t inc_clock(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the Lamport clock and returns the new clock time (intended for sending knowledg...
std::map< const char *, VariableReference, VariableReferenceMapCompare > VariableReferenceMap
a map of variable references
bool always_overwrite
Toggle for always overwriting records, regardless of quality, clock values, etc.
std::vector< VariableReference > VariableReferences
a vector of variable references
int set_text(const std::string &key, const char *value, size_t size, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to an XML string.
std::string expand_statement(const std::string &statement) const
Expands a string with variable expansion.
void mark_to_checkpoint_unsafe(VariableReference ref, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable to modified at current clock for the purposes of checkpointing.
bool delete_variable(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Deletes the key.
logger::Logger * logger_
Logger for printing.
madara::knowledge::KnowledgeRecord * with(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
const char * get_name(void) const
Returns the name of the variable.
Provides functions and classes for the distributed knowledge base.
uint64_t get_clock(void) const
Atomically gets the Lamport clock.
void mark_to_send(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Changes variable reference to modified at current clock, and queues it to send, even if it is a local...
void reset_checkpoint(void) const
Reset all checkpoint variables in the modified lists.
void add_modifieds(const VariableReferences &modifieds) const
Adds a list of VariableReferences to the current modified list.
bool clear(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Clears a variable.
Settings for applying knowledge updates.
bool treat_globals_as_locals
Toggle whether updates to global variables are treated as local variables and not marked as modified ...
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
Copyright (c) 2015 Carnegie Mellon University.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
KnowledgeRecord retrieve_index(size_t index) const
retrieves the value at an array index.
std::string debug_modifieds(void) const
Retrieves a stringified list of all modified variables that are ready to send over transport on next ...
KnowledgeRecord retrieve_index(const std::string &key, size_t index, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Retrieves a value at a specified index within a knowledge array.
bool track_local_changes
Toggle for checkpointing support.
bool delete_expression(const std::string &expression)
Attempts to delete an expression from cache.
Settings for applying knowledge updates.
bool is_string_type(void) const
returns true if the record is a string type (STRING, XML, TEXT_FILE)
std::string to_string(const std::string &delimiter=", ") const
converts the value to a string.
madara::expression::Interpreter * interpreter_
KaRL interpreter.
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a 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...
bool treat_locals_as_globals
Toggle whether updates to local variables are treated as global variables that should be sent over th...
uint64_t clock
last modification time
VariableReferenceMap local_changed_map_
void set_log_level(int level)
Sets the log level.
madara::knowledge::KnowledgeRecord inc(const std::string &key, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically increments the value of the variable.
void reset_modified(void)
Reset all variables to be unmodified.