MADARA  3.4.1
Map.cpp
Go to the documentation of this file.
1 #include "Map.h"
3 
4 #include <algorithm>
5 
7  const KnowledgeUpdateSettings& settings, const std::string& delimiter)
8  : BaseContainer("", settings), context_(0), delimiter_(delimiter)
9 {
10 }
11 
14  const std::string& delimiter)
15  : BaseContainer(name, settings),
16  context_(&(knowledge.get_context())),
17  delimiter_(delimiter)
18 {
19  std::map<std::string, knowledge::KnowledgeRecord> contents;
20  std::string common = name + delimiter_;
21  context_->to_map(common, contents);
22 
23  KnowledgeUpdateSettings keep_local(true);
24  if (contents.size() > 0)
25  {
26  for (std::map<std::string, knowledge::KnowledgeRecord>::iterator i =
27  contents.begin();
28  i != contents.end(); ++i)
29  {
30  map_[i->first.substr(common.size())] =
31  knowledge.get_ref(i->first, keep_local);
32  }
33  }
34 }
35 
38  const std::string& delimiter)
39  : BaseContainer(name, settings),
40  context_(knowledge.get_context()),
41  delimiter_(delimiter)
42 {
43  std::map<std::string, knowledge::KnowledgeRecord> contents;
44  std::string common = name + delimiter_;
45  context_->to_map(common, contents);
46 
47  KnowledgeUpdateSettings keep_local(true);
48  if (contents.size() > 0)
49  {
50  for (std::map<std::string, knowledge::KnowledgeRecord>::iterator i =
51  contents.begin();
52  i != contents.end(); ++i)
53  {
54  map_[i->first.substr(common.size())] =
55  knowledge.get_ref(i->first, keep_local);
56  }
57  }
58 }
59 
61  : BaseContainer(rhs),
62  context_(rhs.context_),
63  map_(rhs.map_),
64  delimiter_(rhs.delimiter_)
65 {
66 }
67 
69 
71 {
72  if (context_ && name_ != "")
73  {
74  ContextGuard context_guard(*context_);
75  MADARA_GUARD_TYPE guard(mutex_);
76 
77  for (InternalMap::const_iterator index = map_.begin(); index != map_.end();
78  ++index)
79  {
80  context_->mark_modified(index->second);
81  }
82  }
83 }
84 
86 {
87  std::stringstream result;
88 
89  result << "Map: ";
90 
91  if (context_)
92  {
93  ContextGuard context_guard(*context_);
94  MADARA_GUARD_TYPE guard(mutex_);
95 
96  result << this->name_;
97  result << " [" << map_.size() << "]";
98  result << " = [";
99 
100  bool first(true);
101 
102  for (InternalMap::const_iterator index = map_.begin(); index != map_.end();
103  ++index)
104  {
105  if (!first)
106  {
107  result << ", ";
108  }
109  else
110  {
111  first = false;
112  }
113 
114  result << index->first << " => "
115  << context_->get(index->second).to_string();
116  }
117 
118  result << "]";
119  }
120 
121  return result.str();
122 }
123 
125 {
126  modify();
127 }
128 
130 {
131  return get_debug_info();
132 }
133 
136 {
137  return new Map(*this);
138 }
139 
141 {
142  if (context_ && name_ != "" && map_.find(index) != map_.end())
143  {
144  ContextGuard context_guard(*context_);
145  context_->mark_modified(map_[index]);
146  }
147 }
148 
150 {
151  if (this != &rhs)
152  {
153  MADARA_GUARD_TYPE guard(mutex_), guard2(rhs.mutex_);
154 
155  this->context_ = rhs.context_;
156  this->name_ = rhs.name_;
157  this->settings_ = rhs.settings_;
158  this->map_ = rhs.map_;
159  }
160 }
161 
164 {
166 
167  if (context_)
168  {
169  ContextGuard context_guard(*context_);
170  MADARA_GUARD_TYPE guard(mutex_);
171  std::stringstream buffer;
172  buffer << name_;
173  buffer << delimiter_;
174  buffer << key;
175 
176  KnowledgeUpdateSettings keep_local(true);
177  std::string final_key = buffer.str();
178  std::map<std::string, VariableReference>::const_iterator entry =
179  map_.find(final_key);
180 
181  if (entry == map_.end())
182  {
183  VariableReference ref = context_->get_ref(final_key, keep_local);
184  map_[key] = ref;
185  return context_->get(ref, keep_local);
186  }
187 
188  result = context_->get(entry->second, keep_local);
189  }
190 
191  return result;
192 }
193 
195 operator[](const std::string& key)
196 {
197  return to_record(key);
198 }
199 
201 {
202  MADARA_GUARD_TYPE guard(mutex_);
203  return map_.size();
204 }
205 
206 std::vector<std::string> madara::knowledge::containers::Map::sync_keys(void)
207 {
208  std::vector<std::string> additions;
209 
210  if (context_)
211  {
212  ContextGuard context_guard(*context_);
213  MADARA_GUARD_TYPE guard(mutex_);
214 
215  clear(false);
216 
217  std::map<std::string, knowledge::KnowledgeRecord> contents;
218  std::string common = name_ + delimiter_;
219  context_->to_map(common, contents);
220  KnowledgeUpdateSettings keep_local(true);
221 
222  for (std::map<std::string, knowledge::KnowledgeRecord>::iterator i =
223  contents.begin();
224  i != contents.end(); ++i)
225  {
226  std::string key = i->first.substr(common.size());
227 
228  if (map_.find(key) == map_.end())
229  {
230  additions.push_back(key);
231  map_[key] = context_->get_ref(i->first, keep_local);
232  }
233  }
234  }
235 
236  return additions;
237 }
238 
240  Map& other, bool refresh_keys, bool delete_keys)
241 {
242  if (context_ && other.context_)
243  {
244  std::lock(*context_, *other.context_, mutex_, other.mutex_);
245 
246  ContextGuard context_guard(*context_, std::adopt_lock);
247  ContextGuard other_context_guard(*other.context_, std::adopt_lock);
248  MADARA_GUARD_TYPE guard(mutex_, std::adopt_lock),
249  guard2(other.mutex_, std::adopt_lock);
250 
251  if (refresh_keys)
252  {
253  other.sync_keys();
254  this->sync_keys();
255  }
256 
257  InternalMap leftover(other.map_);
258  InternalMap::iterator i = map_.begin();
259  while (i != map_.end())
260  {
261  // temp = this[i->first]
263  this->context_->get(i->second, this->settings_);
264 
265  // check if the other map has the key
266  InternalMap::iterator other_found = other.map_.find(i->first);
267 
268  if (other_found != other.map_.end())
269  {
270  // this[i->first] = other[i->first]
271  this->context_->set(i->second,
272  other.context_->get(other_found->second, other.settings_),
273  this->settings_);
274 
275  // other[i->first] = temp
276  other.map_[i->first] = other_found->second;
277  other.context_->set(other_found->second, temp, other.settings_);
278 
279  // remove item from the items leftover
280  leftover.erase(i->first);
281  }
282  else
283  {
284  std::stringstream buffer;
285  buffer << other.name_;
286  buffer << delimiter_;
287  buffer << i->first;
288 
289  // other[i->first] = temp
290  VariableReference ref =
291  other.context_->get_ref(buffer.str(), other.settings_);
292  other.map_[i->first] = ref;
293  other.context_->set(ref, temp, other.settings_);
294 
295  // this[i->first] = zero
296  if (delete_keys)
297  {
298  std::stringstream buffer2;
299  buffer2 << this->name_;
300  buffer2 << delimiter_;
301  buffer2 << i->first;
302 
303  this->context_->delete_variable(buffer2.str(), this->settings_);
304  this->map_.erase(i++);
305  }
306  else
307  {
309  this->context_->set(i->second, zero, this->settings_);
310  ++i;
311  }
312  }
313  }
314 
315  // iterate over the elements in the other.map_ that are not in the current
316  // map
317 
318  for (i = leftover.begin(); i != leftover.end(); ++i)
319  {
320  if (map_.find(i->first) == map_.end())
321  {
322  std::stringstream buffer;
323  buffer << name_;
324  buffer << delimiter_;
325  buffer << i->first;
326 
327  VariableReference ref = context_->get_ref(buffer.str(), settings_);
328  this->map_[i->first] = ref;
329 
330  // this[i->first] = other[i->first]
331  this->context_->set(ref,
332  other.context_->get(i->second, other.settings_), this->settings_);
333 
334  // other[i->first] = zero;
335  if (delete_keys)
336  {
337  std::stringstream buffer2;
338  buffer2 << other.name_;
339  buffer2 << delimiter_;
340  buffer2 << i->first;
341 
342  other.context_->delete_variable(buffer2.str(), settings_);
343  other.map_.erase(i->first);
344  }
345  else
346  {
348  other.context_->set(i->second, zero, other.settings_);
349  }
350  }
351  }
352  }
353 }
354 
356 {
357  if (context_ && clear_knowledge)
358  {
359  ContextGuard context_guard(*context_);
360  MADARA_GUARD_TYPE guard(mutex_);
361 
362  std::vector<std::string> keys;
363  this->keys(keys);
364 
365  for (size_t i = 0; i < keys.size(); ++i)
366  this->erase(keys[i]);
367 
368  map_.clear();
369  }
370 
371  else if (context_)
372  {
373  MADARA_GUARD_TYPE guard(mutex_);
374  map_.clear();
375  }
376 }
377 
379 {
380  if (context_)
381  {
382  ContextGuard context_guard(*context_);
383  MADARA_GUARD_TYPE guard(mutex_);
384 
385  for (auto entry : map_)
386  context_->clear(entry.first);
387 
388  map_.clear();
389  }
390 }
391 
393 {
394  if (context_)
395  {
396  ContextGuard context_guard(*context_);
397  MADARA_GUARD_TYPE guard(mutex_);
398 
399  // find the key in the internal map
400  InternalMap::iterator found = map_.find(key);
401 
402  // delete the variable if it has been found
403  if (found != map_.end())
404  {
405  context_->delete_variable(found->second.get_name());
406  map_.erase(found);
407  }
408  }
409 }
410 
412  const std::string& var_name, KnowledgeBase& knowledge, bool sync)
413 {
414  if (context_ != &(knowledge.get_context()) || name_ != var_name)
415  {
416  context_ = &(knowledge.get_context());
417 
418  ContextGuard context_guard(*context_);
419  MADARA_GUARD_TYPE guard(mutex_);
420 
421  name_ = var_name;
422 
423  // the old map will no longer be appropriate
424  clear(false);
425  if (sync)
426  sync_keys();
427  }
428 }
429 
431  const std::string& var_name, Variables& knowledge, bool sync)
432 {
433  if (context_ != knowledge.get_context() || name_ != var_name)
434  {
435  context_ = knowledge.get_context();
436 
437  ContextGuard context_guard(*context_);
438  MADARA_GUARD_TYPE guard(mutex_);
439 
440  name_ = var_name;
441 
442  // the old map will no longer be appropriate
443  clear(false);
444  if (sync)
445  sync_keys();
446  }
447 }
448 
450  const std::string& delimiter, bool sync)
451 {
452  delimiter_ = delimiter;
453 
454  if (context_)
455  {
456  // the old map will no longer be appropriate
457  clear(false);
458  if (sync)
459  sync_keys();
460  }
461 }
462 
464 {
465  return delimiter_;
466 }
467 
469 {
470  bool result(false);
471 
472  InternalMap::const_iterator found = map_.find(key);
473 
474  if (context_ && found != map_.end())
475  {
476  ContextGuard context_guard(*context_);
477  MADARA_GUARD_TYPE guard(mutex_);
478 
479  result = context_->exists(found->second);
480  }
481 
482  return result;
483 }
484 
486  const std::string& prefix) const
487 {
488  bool result(false);
489 
490  InternalMap::const_iterator found = map_.lower_bound(prefix);
491 
492  if (found != map_.end())
493  {
494  if (madara::utility::begins_with(found->first, prefix))
495  result = true;
496  }
497 
498  return result;
499 }
500 
502  std::vector<std::string>& curkeys) const
503 {
504  MADARA_GUARD_TYPE guard(mutex_);
505  curkeys.resize(map_.size());
506  unsigned int j = 0;
507 
508  for (std::map<std::string, VariableReference>::const_iterator
509  i = map_.begin();
510  i != map_.end(); ++i, ++j)
511  {
512  curkeys[j] = i->first;
513  }
514 }
515 
517  const std::string& key, const std::string& filename)
518 {
519  int result = -1;
520 
521  if (context_ && key != "")
522  {
523  ContextGuard context_guard(*context_);
524  MADARA_GUARD_TYPE guard(mutex_);
525 
526  std::stringstream buffer;
527  buffer << name_;
528  buffer << delimiter_;
529  buffer << key;
530 
531  std::string final_key = buffer.str();
532  std::map<std::string, VariableReference>::iterator entry =
533  map_.find(final_key);
534 
535  if (entry == map_.end())
536  {
537  VariableReference ref = context_->get_ref(final_key, settings_);
538  map_[key] = ref;
539  result = context_->read_file(ref, filename, settings_);
540  }
541  else
542  {
543  result = context_->read_file(entry->second, filename, settings_);
544  }
545  }
546  return result;
547 }
548 
550  const std::string& filename, const KnowledgeUpdateSettings& settings)
551 {
552  int result = -1;
553 
554  if (context_ && key != "")
555  {
556  ContextGuard context_guard(*context_);
557  MADARA_GUARD_TYPE guard(mutex_);
558 
559  std::stringstream buffer;
560  buffer << name_;
561  buffer << delimiter_;
562  buffer << key;
563 
564  std::string final_key = buffer.str();
565  std::map<std::string, VariableReference>::iterator entry =
566  map_.find(final_key);
567 
568  if (entry == map_.end())
569  {
570  VariableReference ref = context_->get_ref(final_key, settings);
571  map_[key] = ref;
572  result = context_->read_file(ref, filename, settings);
573  }
574  else
575  {
576  result = context_->read_file(entry->second, filename, settings);
577  }
578  }
579  return result;
580 }
581 
584 {
585  int result = -1;
586 
587  if (context_ && key != "")
588  {
589  ContextGuard context_guard(*context_);
590  MADARA_GUARD_TYPE guard(mutex_);
591  std::stringstream buffer;
592  buffer << name_;
593  buffer << delimiter_;
594  buffer << key;
595 
596  std::string final_key = buffer.str();
597  std::map<std::string, VariableReference>::iterator entry =
598  map_.find(final_key);
599 
600  if (entry == map_.end())
601  {
602  VariableReference ref = context_->get_ref(final_key, settings_);
603  map_[key] = ref;
604  result = context_->set(ref, value, settings_);
605  }
606  else
607  {
608  result = context_->set(entry->second, value, settings_);
609  }
610  }
611 
612  return result;
613 }
614 
617  const KnowledgeUpdateSettings& settings)
618 {
619  int result = -1;
620 
621  if (context_ && key != "")
622  {
623  ContextGuard context_guard(*context_);
624  MADARA_GUARD_TYPE guard(mutex_);
625 
626  std::stringstream buffer;
627  buffer << name_;
628  buffer << delimiter_;
629  buffer << key;
630 
631  std::string final_key = buffer.str();
632  std::map<std::string, VariableReference>::iterator entry =
633  map_.find(final_key);
634 
635  if (entry == map_.end())
636  {
637  VariableReference ref = context_->get_ref(final_key, settings);
638  map_[key] = ref;
639  result = context_->set(ref, value, settings);
640  }
641  else
642  {
643  result = context_->set(entry->second, value, settings);
644  }
645  }
646 
647  return result;
648 }
649 
651  const KnowledgeRecord& value, const KnowledgeUpdateSettings& settings)
652 {
653  int result = -1;
654 
655  if (context_ && key != "")
656  {
657  ContextGuard context_guard(*context_);
658  MADARA_GUARD_TYPE guard(mutex_);
659 
660  std::stringstream buffer;
661  buffer << name_;
662  buffer << delimiter_;
663  buffer << key;
664 
665  std::string final_key = buffer.str();
666  std::map<std::string, VariableReference>::iterator entry =
667  map_.find(final_key);
668 
669  if (entry == map_.end())
670  {
671  VariableReference ref = context_->get_ref(final_key, settings);
672  map_[key] = ref;
673  result = context_->set(ref, value, settings);
674  }
675  else
676  {
677  result = context_->set(entry->second, value, settings);
678  }
679  }
680 
681  return result;
682 }
683 
686 {
687  int result = -1;
688 
689  if (context_ && key != "")
690  {
691  ContextGuard context_guard(*context_);
692  MADARA_GUARD_TYPE guard(mutex_);
693 
694  std::stringstream buffer;
695  buffer << name_;
696  buffer << delimiter_;
697  buffer << key;
698 
699  std::string final_key = buffer.str();
700  std::map<std::string, VariableReference>::iterator entry =
701  map_.find(final_key);
702 
703  if (entry == map_.end())
704  {
705  VariableReference ref = context_->get_ref(final_key, settings_);
706  map_[key] = ref;
707  result = context_->set_index(ref, index, value, settings_);
708  }
709  else
710  {
711  result = context_->set_index(entry->second, index, value, settings_);
712  }
713  }
714 
715  return result;
716 }
717 
720  const KnowledgeUpdateSettings& settings)
721 {
722  int result = -1;
723 
724  if (context_ && key != "")
725  {
726  ContextGuard context_guard(*context_);
727  MADARA_GUARD_TYPE guard(mutex_);
728 
729  std::stringstream buffer;
730  buffer << name_;
731  buffer << delimiter_;
732  buffer << key;
733 
734  std::string final_key = buffer.str();
735  std::map<std::string, VariableReference>::iterator entry =
736  map_.find(final_key);
737 
738  if (entry == map_.end())
739  {
740  VariableReference ref = context_->get_ref(final_key, settings);
741  map_[key] = ref;
742  result = context_->set_index(ref, index, value, settings);
743  }
744  else
745  {
746  result = context_->set_index(entry->second, index, value, settings);
747  }
748  }
749 
750  return result;
751 }
752 
754  const madara::knowledge::KnowledgeRecord::Integer* value, uint32_t size)
755 {
756  int result = -1;
757 
758  if (context_ && key != "")
759  {
760  ContextGuard context_guard(*context_);
761  MADARA_GUARD_TYPE guard(mutex_);
762 
763  std::stringstream buffer;
764  buffer << name_;
765  buffer << delimiter_;
766  buffer << key;
767 
768  std::string final_key = buffer.str();
769  std::map<std::string, VariableReference>::iterator entry =
770  map_.find(final_key);
771 
772  if (entry == map_.end())
773  {
774  VariableReference ref = context_->get_ref(final_key, settings_);
775  map_[key] = ref;
776  result = context_->set(ref, value, size, settings_);
777  }
778  else
779  {
780  result = context_->set(entry->second, value, size, settings_);
781  }
782  }
783 
784  return result;
785 }
786 
788  const madara::knowledge::KnowledgeRecord::Integer* value, uint32_t size,
789  const KnowledgeUpdateSettings& settings)
790 {
791  int result = -1;
792 
793  if (context_ && key != "")
794  {
795  ContextGuard context_guard(*context_);
796  MADARA_GUARD_TYPE guard(mutex_);
797 
798  std::stringstream buffer;
799  buffer << name_;
800  buffer << delimiter_;
801  buffer << key;
802 
803  std::string final_key = buffer.str();
804  std::map<std::string, VariableReference>::iterator entry =
805  map_.find(final_key);
806 
807  if (entry == map_.end())
808  {
809  VariableReference ref = context_->get_ref(final_key, settings);
810  map_[key] = ref;
811  result = context_->set(ref, value, size, settings);
812  }
813  else
814  {
815  result = context_->set(entry->second, value, size, settings);
816  }
817  }
818 
819  return result;
820 }
821 
823  const std::string& key, const std::vector<KnowledgeRecord::Integer>& value)
824 {
825  int result = -1;
826 
827  if (context_ && key != "")
828  {
829  ContextGuard context_guard(*context_);
830  MADARA_GUARD_TYPE guard(mutex_);
831 
832  std::stringstream buffer;
833  buffer << name_;
834  buffer << delimiter_;
835  buffer << key;
836 
837  std::string final_key = buffer.str();
838  std::map<std::string, VariableReference>::iterator entry =
839  map_.find(final_key);
840 
841  if (entry == map_.end())
842  {
843  VariableReference ref = context_->get_ref(final_key, settings_);
844  map_[key] = ref;
845  result = context_->set(ref, value, settings_);
846  }
847  else
848  {
849  result = context_->set(entry->second, value, settings_);
850  }
851  }
852 
853  return result;
854 }
855 
857  const std::vector<KnowledgeRecord::Integer>& value,
858  const KnowledgeUpdateSettings& settings)
859 {
860  int result = -1;
861 
862  if (context_ && key != "")
863  {
864  ContextGuard context_guard(*context_);
865  MADARA_GUARD_TYPE guard(mutex_);
866 
867  std::stringstream buffer;
868  buffer << name_;
869  buffer << delimiter_;
870  buffer << key;
871 
872  std::string final_key = buffer.str();
873  std::map<std::string, VariableReference>::iterator entry =
874  map_.find(final_key);
875 
876  if (entry == map_.end())
877  {
878  VariableReference ref = context_->get_ref(final_key, settings);
879  map_[key] = ref;
880  return context_->set(ref, value, settings);
881  }
882  else
883  {
884  result = context_->set(entry->second, value, settings);
885  }
886  }
887 
888  return result;
889 }
890 
892  const std::string& key, double value)
893 {
894  int result = -1;
895 
896  if (context_ && key != "")
897  {
898  ContextGuard context_guard(*context_);
899  MADARA_GUARD_TYPE guard(mutex_);
900 
901  std::stringstream buffer;
902  buffer << name_;
903  buffer << delimiter_;
904  buffer << key;
905 
906  std::string final_key = buffer.str();
907  std::map<std::string, VariableReference>::iterator entry =
908  map_.find(final_key);
909 
910  if (entry == map_.end())
911  {
912  VariableReference ref = context_->get_ref(final_key, settings_);
913  map_[key] = ref;
914  result = context_->set(ref, value, settings_);
915  }
916  else
917  {
918  result = context_->set(entry->second, value, settings_);
919  }
920  }
921 
922  return result;
923 }
924 
926  double value, const KnowledgeUpdateSettings& settings)
927 {
928  int result = -1;
929 
930  if (context_ && key != "")
931  {
932  ContextGuard context_guard(*context_);
933  MADARA_GUARD_TYPE guard(mutex_);
934 
935  std::stringstream buffer;
936  buffer << name_;
937  buffer << delimiter_;
938  buffer << key;
939 
940  std::string final_key = buffer.str();
941  std::map<std::string, VariableReference>::iterator entry =
942  map_.find(final_key);
943 
944  if (entry == map_.end())
945  {
946  VariableReference ref = context_->get_ref(final_key, settings);
947  map_[key] = ref;
948  result = context_->set(ref, value, settings);
949  }
950  else
951  {
952  result = context_->set(entry->second, value, settings);
953  }
954  }
955 
956  return result;
957 }
958 
960  const std::string& key, size_t index, double value)
961 {
962  int result = -1;
963 
964  if (context_ && key != "")
965  {
966  ContextGuard context_guard(*context_);
967  MADARA_GUARD_TYPE guard(mutex_);
968 
969  std::stringstream buffer;
970  buffer << name_;
971  buffer << delimiter_;
972  buffer << key;
973 
974  std::string final_key = buffer.str();
975  std::map<std::string, VariableReference>::iterator entry =
976  map_.find(final_key);
977 
978  if (entry == map_.end())
979  {
980  VariableReference ref = context_->get_ref(final_key, settings_);
981  map_[key] = ref;
982  result = context_->set_index(ref, index, value, settings_);
983  }
984  else
985  {
986  result = context_->set_index(entry->second, index, value, settings_);
987  }
988  }
989 
990  return result;
991 }
992 
994  size_t index, double value, const KnowledgeUpdateSettings& settings)
995 {
996  int result = -1;
997 
998  if (context_ && key != "")
999  {
1000  ContextGuard context_guard(*context_);
1001  MADARA_GUARD_TYPE guard(mutex_);
1002 
1003  std::stringstream buffer;
1004  buffer << name_;
1005  buffer << delimiter_;
1006  buffer << key;
1007 
1008  std::string final_key = buffer.str();
1009  std::map<std::string, VariableReference>::iterator entry =
1010  map_.find(final_key);
1011 
1012  if (entry == map_.end())
1013  {
1014  VariableReference ref = context_->get_ref(final_key, settings);
1015  map_[key] = ref;
1016  result = context_->set_index(ref, index, value, settings);
1017  }
1018  else
1019  {
1020  result = context_->set_index(entry->second, index, value, settings);
1021  }
1022  }
1023 
1024  return result;
1025 }
1026 
1028  const std::string& key, const double* value, uint32_t size)
1029 {
1030  int result = -1;
1031 
1032  if (context_ && key != "")
1033  {
1034  ContextGuard context_guard(*context_);
1035  MADARA_GUARD_TYPE guard(mutex_);
1036 
1037  std::stringstream buffer;
1038  buffer << name_;
1039  buffer << delimiter_;
1040  buffer << key;
1041 
1042  std::string final_key = buffer.str();
1043  std::map<std::string, VariableReference>::iterator entry =
1044  map_.find(final_key);
1045 
1046  if (entry == map_.end())
1047  {
1048  VariableReference ref = context_->get_ref(final_key, settings_);
1049  map_[key] = ref;
1050  result = context_->set(ref, value, size, settings_);
1051  }
1052  else
1053  {
1054  result = context_->set(entry->second, value, size, settings_);
1055  }
1056  }
1057 
1058  return result;
1059 }
1060 
1062  const double* value, uint32_t size, const KnowledgeUpdateSettings& settings)
1063 {
1064  int result = -1;
1065 
1066  if (context_ && key != "")
1067  {
1068  ContextGuard context_guard(*context_);
1069  MADARA_GUARD_TYPE guard(mutex_);
1070 
1071  std::stringstream buffer;
1072  buffer << name_;
1073  buffer << delimiter_;
1074  buffer << key;
1075 
1076  std::string final_key = buffer.str();
1077  std::map<std::string, VariableReference>::iterator entry =
1078  map_.find(final_key);
1079 
1080  if (entry == map_.end())
1081  {
1082  VariableReference ref = context_->get_ref(final_key, settings);
1083  map_[key] = ref;
1084  result = context_->set(ref, value, size, settings);
1085  }
1086 
1087  result = context_->set(entry->second, value, size, settings);
1088  }
1089 
1090  return result;
1091 }
1092 
1094  const std::string& key, const std::vector<double>& value)
1095 {
1096  int result = -1;
1097 
1098  if (context_ && key != "")
1099  {
1100  ContextGuard context_guard(*context_);
1101  MADARA_GUARD_TYPE guard(mutex_);
1102 
1103  std::stringstream buffer;
1104  buffer << name_;
1105  buffer << delimiter_;
1106  buffer << key;
1107 
1108  std::string final_key = buffer.str();
1109  std::map<std::string, VariableReference>::iterator entry =
1110  map_.find(final_key);
1111 
1112  if (entry == map_.end())
1113  {
1114  VariableReference ref = context_->get_ref(final_key, settings_);
1115  map_[key] = ref;
1116  result = context_->set(ref, value, settings_);
1117  }
1118  else
1119  {
1120  result = context_->set(entry->second, value, settings_);
1121  }
1122  }
1123 
1124  return result;
1125 }
1126 
1128  const std::vector<double>& value, const KnowledgeUpdateSettings& settings)
1129 {
1130  int result = -1;
1131 
1132  if (context_ && key != "")
1133  {
1134  ContextGuard context_guard(*context_);
1135  MADARA_GUARD_TYPE guard(mutex_);
1136 
1137  std::stringstream buffer;
1138  buffer << name_;
1139  buffer << delimiter_;
1140  buffer << key;
1141 
1142  std::string final_key = buffer.str();
1143  std::map<std::string, VariableReference>::iterator entry =
1144  map_.find(final_key);
1145 
1146  if (entry == map_.end())
1147  {
1148  VariableReference ref = context_->get_ref(final_key, settings);
1149  map_[key] = ref;
1150  result = context_->set(ref, value, settings);
1151  }
1152  else
1153  {
1154  result = context_->set(entry->second, value, settings);
1155  }
1156  }
1157 
1158  return result;
1159 }
1160 
1162  const std::string& key, const std::string& value)
1163 {
1164  int result = -1;
1165 
1166  if (context_ && key != "")
1167  {
1168  ContextGuard context_guard(*context_);
1169  MADARA_GUARD_TYPE guard(mutex_);
1170 
1171  std::stringstream buffer;
1172  buffer << name_;
1173  buffer << delimiter_;
1174  buffer << key;
1175 
1176  std::string final_key = buffer.str();
1177  std::map<std::string, VariableReference>::iterator entry =
1178  map_.find(final_key);
1179 
1180  if (entry == map_.end())
1181  {
1182  VariableReference ref = context_->get_ref(final_key, settings_);
1183  map_[key] = ref;
1184  result = context_->set(ref, value, settings_);
1185  }
1186  else
1187  {
1188  result = context_->set(entry->second, value, settings_);
1189  }
1190  }
1191 
1192  return result;
1193 }
1194 
1196  const std::string& value, const KnowledgeUpdateSettings& settings)
1197 {
1198  int result = -1;
1199 
1200  if (context_ && key != "")
1201  {
1202  ContextGuard context_guard(*context_);
1203  MADARA_GUARD_TYPE guard(mutex_);
1204 
1205  std::stringstream buffer;
1206  buffer << name_;
1207  buffer << delimiter_;
1208  buffer << key;
1209 
1210  std::string final_key = buffer.str();
1211  std::map<std::string, VariableReference>::iterator entry =
1212  map_.find(final_key);
1213 
1214  if (entry == map_.end())
1215  {
1216  VariableReference ref = context_->get_ref(final_key, settings);
1217  map_[key] = ref;
1218  result = context_->set(ref, value, settings);
1219  }
1220  else
1221  {
1222  result = context_->set(entry->second, value, settings);
1223  }
1224  }
1225 
1226  return result;
1227 }
1228 
1230  const std::string& key, const unsigned char* value, size_t size)
1231 {
1232  int result = -1;
1233 
1234  if (context_ && key != "")
1235  {
1236  ContextGuard context_guard(*context_);
1237  MADARA_GUARD_TYPE guard(mutex_);
1238 
1239  std::stringstream buffer;
1240  buffer << name_;
1241  buffer << delimiter_;
1242  buffer << key;
1243 
1244  std::string final_key = buffer.str();
1245  std::map<std::string, VariableReference>::iterator entry =
1246  map_.find(final_key);
1247 
1248  if (entry == map_.end())
1249  {
1250  VariableReference ref = context_->get_ref(final_key, settings_);
1251  map_[key] = ref;
1252  result = context_->set_file(ref, value, size, settings_);
1253  }
1254  else
1255  {
1256  result = context_->set_file(entry->second, value, size, settings_);
1257  }
1258  }
1259 
1260  return result;
1261 }
1262 
1264  const unsigned char* value, size_t size,
1265  const KnowledgeUpdateSettings& settings)
1266 {
1267  int result = -1;
1268 
1269  if (context_ && key != "")
1270  {
1271  ContextGuard context_guard(*context_);
1272  MADARA_GUARD_TYPE guard(mutex_);
1273 
1274  std::stringstream buffer;
1275  buffer << name_;
1276  buffer << delimiter_;
1277  buffer << key;
1278 
1279  std::string final_key = buffer.str();
1280  std::map<std::string, VariableReference>::iterator entry =
1281  map_.find(final_key);
1282 
1283  if (entry == map_.end())
1284  {
1285  VariableReference ref = context_->get_ref(final_key, settings);
1286  map_[key] = ref;
1287  result = context_->set_file(ref, value, size, settings);
1288  }
1289 
1290  result = context_->set_file(entry->second, value, size, settings);
1291  }
1292 
1293  return result;
1294 }
1295 
1297  const std::string& key, const unsigned char* value, size_t size)
1298 {
1299  int result = -1;
1300 
1301  if (context_ && key != "")
1302  {
1303  ContextGuard context_guard(*context_);
1304  MADARA_GUARD_TYPE guard(mutex_);
1305 
1306  std::stringstream buffer;
1307  buffer << name_;
1308  buffer << delimiter_;
1309  buffer << key;
1310 
1311  std::string final_key = buffer.str();
1312  std::map<std::string, VariableReference>::iterator entry =
1313  map_.find(final_key);
1314 
1315  if (entry == map_.end())
1316  {
1317  VariableReference ref = context_->get_ref(final_key, settings_);
1318  map_[key] = ref;
1319  result = context_->set_jpeg(ref, value, size, settings_);
1320  }
1321  else
1322  {
1323  result = context_->set_jpeg(entry->second, value, size, settings_);
1324  }
1325  }
1326 
1327  return result;
1328 }
1329 
1331  const unsigned char* value, size_t size,
1332  const KnowledgeUpdateSettings& settings)
1333 {
1334  int result = -1;
1335 
1336  if (context_ && key != "")
1337  {
1338  ContextGuard context_guard(*context_);
1339  MADARA_GUARD_TYPE guard(mutex_);
1340 
1341  std::stringstream buffer;
1342  buffer << name_;
1343  buffer << delimiter_;
1344  buffer << key;
1345 
1346  std::string final_key = buffer.str();
1347  std::map<std::string, VariableReference>::iterator entry =
1348  map_.find(final_key);
1349 
1350  if (entry == map_.end())
1351  {
1352  VariableReference ref = context_->get_ref(final_key, settings);
1353  map_[key] = ref;
1354  result = context_->set_jpeg(ref, value, size, settings);
1355  }
1356  else
1357  {
1358  result = context_->set_jpeg(entry->second, value, size, settings);
1359  }
1360  }
1361 
1362  return result;
1363 }
1364 
1366  uint32_t quality, const KnowledgeReferenceSettings& settings)
1367 {
1368  if (context_)
1369  {
1370  ContextGuard context_guard(*context_);
1371  MADARA_GUARD_TYPE guard(mutex_);
1372 
1373  std::stringstream buffer;
1374  buffer << name_;
1375  buffer << delimiter_;
1376  buffer << key;
1377 
1378  context_->set_quality(buffer.str(), quality, true, settings);
1379  }
1380 }
1381 
1383 {
1384  bool result(false);
1385 
1387  "Map::is_true: checking for truth in all elements\n");
1388 
1389  if (context_ && name_ != "")
1390  {
1391  ContextGuard context_guard(*context_);
1392  MADARA_GUARD_TYPE guard(mutex_);
1393 
1394  result = true;
1395 
1396  for (InternalMap::const_iterator index = map_.begin(); index != map_.end();
1397  ++index)
1398  {
1400  "Map::is_true: checking index, is_false of %d. \n",
1401  (int)context_->get(index->second).is_false());
1402 
1403  if (context_->get(index->second).is_false())
1404  {
1406  "Map::is_true: result is false, breaking\n");
1407 
1408  result = false;
1409  break;
1410  }
1411  }
1412 
1413  if (map_.size() == 0)
1414  result = false;
1415  }
1416 
1418  "Map::is_true: final result is %d\n", (int)result);
1419 
1420  return result;
1421 }
1422 
1424 {
1425  return !is_true();
1426 }
1427 
1429 {
1430  return is_true();
1431 }
1432 
1434 {
1435  return is_false();
1436 }
#define madara_logger_log(loggering, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
VariableReferenceMap map_
const ThreadSafeContext * context_
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:24
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
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.
Definition: Variables.h:53
This class is an abstract base class for all containers.
Definition: BaseContainer.h:34
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.
Definition: Map.h:33
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.
Definition: Map.cpp:1229
void set_delimiter(const std::string &delimiter, bool sync=true)
Sets the delimiter for adding and detecting subvariables.
Definition: Map.cpp:449
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Map.h:576
size_t size(void) const
Returns the size of the map.
Definition: Map.cpp:200
bool is_false(void) const
Determines if the value of the map is false.
Definition: Map.cpp:1423
void erase(const std::string &key)
Erases a variable from the map.
Definition: Map.cpp:392
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.
Definition: Map.cpp:1296
std::map< std::string, VariableReference > InternalMap
internal map of variable references
Definition: Map.h:571
std::string delimiter_
Delimiter for the prefix to subvars.
Definition: Map.h:586
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
Definition: Map.cpp:129
bool is_true(void) const
Determines if all values in the map are true.
Definition: Map.cpp:1382
virtual ~Map()
Destructor.
Definition: Map.cpp:68
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
Definition: Map.cpp:411
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
Definition: Map.cpp:85
bool exists(const std::string &key) const
Checks for the existence of a key.
Definition: Map.cpp:468
void operator=(const Map &rhs)
Assignment operator.
Definition: Map.cpp:149
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.
Definition: Map.cpp:582
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
Definition: Map.cpp:463
void exchange(Map &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the map at this location with the map at another location.
Definition: Map.cpp:239
knowledge::KnowledgeRecord to_record(const std::string &key)
Retrieves a copy of the record from the map.
Definition: Map.cpp:163
knowledge::KnowledgeRecord operator[](const std::string &key)
Retrieves a copy of the record from the map.
Definition: Map.cpp:195
virtual BaseContainer * clone(void) const
Clones this container.
Definition: Map.cpp:135
Map(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: Map.cpp:6
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true.
Definition: Map.cpp:1428
virtual void modify_(void)
Polymorphic modify method used by collection containers.
Definition: Map.cpp:124
void modify(void)
Mark the vector as modified.
Definition: Map.cpp:70
std::vector< std::string > sync_keys(void)
Syncs the keys from the knowledge base.
Definition: Map.cpp:206
InternalMap map_
Map of variables to values.
Definition: Map.h:581
bool has_prefix(const std::string &prefix) const
Checks for the existence of a prefix in the keys.
Definition: Map.cpp:485
void reset(void)
Resets the map.
Definition: Map.cpp:378
void keys(std::vector< std::string > &curkeys) const
Returns the keys within the map.
Definition: Map.cpp:501
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.
Definition: Map.cpp:1365
int read_file(const std::string &key, const std::string &filename)
Read a file into a location in the map.
Definition: Map.cpp:516
void clear(bool clear_knowledge=true)
Clears the map.
Definition: Map.cpp:355
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.
Definition: Map.cpp:684
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 ...
Definition: Map.cpp:1433
constexpr string_t string
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.
Definition: Utility.inl:638