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