MADARA  3.2.3
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  std::lock(*context_, *other.context_, mutex_, other.mutex_);
265 
266  ContextGuard context_guard (*context_, std::adopt_lock);
267  ContextGuard other_context_guard (*other.context_, std::adopt_lock);
268  MADARA_GUARD_TYPE guard (mutex_, std::adopt_lock),
269  guard2 (other.mutex_, std::adopt_lock);
270 
271 
272  if (refresh_keys)
273  {
274  other.sync_keys ();
275  this->sync_keys ();
276  }
277 
278  InternalMap leftover (other.map_);
279  InternalMap::iterator i = map_.begin ();
280  while (i != map_.end ())
281  {
282  // temp = this[i->first]
283  knowledge::KnowledgeRecord temp = this->context_->get (i->second, this->settings_);
284 
285  // check if the other map has the key
286  InternalMap::iterator other_found = other.map_.find (i->first);
287 
288  if (other_found != other.map_.end ())
289  {
290  // this[i->first] = other[i->first]
291  this->context_->set (i->second,
292  other.context_->get (other_found->second, other.settings_),
293  this->settings_);
294 
295  // other[i->first] = temp
296  other.map_ [i->first] = other_found->second;
297  other.context_->set (other_found->second, temp, other.settings_);
298 
299  // remove item from the items leftover
300  leftover.erase (i->first);
301  }
302  else
303  {
304  std::stringstream buffer;
305  buffer << other.name_;
306  buffer << delimiter_;
307  buffer << i->first;
308 
309  // other[i->first] = temp
310  VariableReference ref = other.context_->get_ref (
311  buffer.str (), other.settings_);
312  other.map_[i->first] = ref;
313  other.context_->set (ref, temp, other.settings_);
314 
315  // this[i->first] = zero
316  if (delete_keys)
317  {
318  std::stringstream buffer2;
319  buffer2 << this->name_;
320  buffer2 << delimiter_;
321  buffer2 << i->first;
322 
323  this->context_->delete_variable (buffer2.str (), this->settings_);
324  this->map_.erase (i++);
325  }
326  else
327  {
329  this->context_->set (i->second, zero, this->settings_);
330  ++i;
331  }
332  }
333  }
334 
335  // iterate over the elements in the other.map_ that are not in the current map
336 
337  for (i = leftover.begin (); i != leftover.end (); ++i)
338  {
339  if (map_.find (i->first) == map_.end ())
340  {
341  std::stringstream buffer;
342  buffer << name_;
343  buffer << delimiter_;
344  buffer << i->first;
345 
346  VariableReference ref = context_->get_ref (buffer.str (), settings_);
347  this->map_[i->first] = ref;
348 
349  // this[i->first] = other[i->first]
350  this->context_->set (ref,
351  other.context_->get (i->second, other.settings_),
352  this->settings_);
353 
354  // other[i->first] = zero;
355  if (delete_keys)
356  {
357  std::stringstream buffer2;
358  buffer2 << other.name_;
359  buffer2 << delimiter_;
360  buffer2 << i->first;
361 
362  other.context_->delete_variable (buffer2.str (), settings_);
363  other.map_.erase (i->first);
364  }
365  else
366  {
368  other.context_->set (i->second, zero, other.settings_);
369  }
370  }
371  }
372  }
373 }
374 
375 void
377 {
378  if (clear_knowledge)
379  {
380  ContextGuard context_guard (*context_);
381  MADARA_GUARD_TYPE guard (mutex_);
382 
383  std::vector <std::string> keys;
384  this->keys (keys);
385 
386  for (size_t i = 0; i < keys.size (); ++i)
387  this->erase (keys[i]);
388  }
389  else
390  {
391  MADARA_GUARD_TYPE guard (mutex_);
392  map_.clear ();
393  }
394 }
395 
396 void
398 {
399  if (context_)
400  {
401  ContextGuard context_guard (*context_);
402 
403  // find the key in the internal map
404  InternalMap::iterator found = map_.find (key);
405 
406  // delete the variable if it has been found
407  if (found != map_.end ())
408  {
409  context_->delete_variable (found->second.get_name ());
410  }
411  }
412 }
413 
414 void
416  const std::string & var_name,
417  KnowledgeBase & knowledge, bool sync)
418 {
419  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
420  {
421  context_ = &(knowledge.get_context ());
422 
423  ContextGuard context_guard (*context_);
424  MADARA_GUARD_TYPE guard (mutex_);
425 
426  name_ = var_name;
427 
428  // the old map will no longer be appropriate
429  clear (false);
430  if (sync)
431  sync_keys ();
432  }
433 }
434 
435 void
437  const std::string & var_name,
438  Variables & knowledge, bool sync)
439 {
440  if (context_ != knowledge.get_context () || name_ != var_name)
441  {
442  context_ = knowledge.get_context ();
443 
444  ContextGuard context_guard (*context_);
445  MADARA_GUARD_TYPE guard (mutex_);
446 
447  name_ = var_name;
448 
449  // the old map will no longer be appropriate
450  clear (false);
451  if (sync)
452  sync_keys ();
453  }
454 }
455 
456 void
458  const std::string & delimiter,
459  bool sync)
460 {
461  delimiter_ = delimiter;
462 
463  if (context_)
464  {
465  // the old map will no longer be appropriate
466  clear (false);
467  if (sync)
468  sync_keys ();
469  }
470 }
471 
472 
475 {
476  return delimiter_;
477 }
478 
479 bool
481  const std::string & key) const
482 {
483  bool result (false);
484 
485  InternalMap::const_iterator found = map_.find (key);
486 
487  if (context_ && found != map_.end ())
488  {
489  ContextGuard context_guard (*context_);
490  MADARA_GUARD_TYPE guard (mutex_);
491 
492  result = context_->exists (found->second);
493  }
494 
495  return result;
496 }
497 
498 bool
500 const std::string & prefix) const
501 {
502  bool result (false);
503 
504  InternalMap::const_iterator found = map_.lower_bound (prefix);
505 
506  if (found != map_.end ())
507  {
508  if (madara::utility::begins_with (found->first, prefix))
509  result = true;
510  }
511 
512  return result;
513 }
514 
515 void
517  std::vector <std::string> & curkeys) const
518 {
519  MADARA_GUARD_TYPE guard (mutex_);
520  curkeys.resize (map_.size ());
521  unsigned int j = 0;
522 
523  for (std::map <std::string, VariableReference>::const_iterator i =
524  map_.begin ();
525  i != map_.end (); ++i, ++j)
526  {
527  curkeys[j] = i->first;
528  }
529 }
530 
531 
532 
533 int
535  const std::string & key,
536  const std::string & filename)
537 {
538  int result = -1;
539 
540  if (context_ && key != "")
541  {
542  ContextGuard context_guard (*context_);
543  MADARA_GUARD_TYPE guard (mutex_);
544 
545  std::stringstream buffer;
546  buffer << name_;
547  buffer << delimiter_;
548  buffer << key;
549 
550  std::string final_key = buffer.str ();
551  std::map <std::string, VariableReference>::iterator entry =
552  map_.find (final_key);
553 
554  if (entry == map_.end ())
555  {
556  VariableReference ref = context_->get_ref (final_key, settings_);
557  map_[key] = ref;
558  result = context_->read_file (ref, filename, settings_);
559  }
560  else
561  {
562  result = context_->read_file (entry->second, filename, settings_);
563  }
564  }
565  return result;
566 }
567 
568 int
570  const std::string & key,
571  const std::string & filename,
572  const KnowledgeUpdateSettings & settings)
573 {
574  int result = -1;
575 
576  if (context_ && key != "")
577  {
578  ContextGuard context_guard (*context_);
579  MADARA_GUARD_TYPE guard (mutex_);
580 
581  std::stringstream buffer;
582  buffer << name_;
583  buffer << delimiter_;
584  buffer << key;
585 
586  std::string final_key = buffer.str ();
587  std::map <std::string, VariableReference>::iterator entry =
588  map_.find (final_key);
589 
590  if (entry == map_.end ())
591  {
592  VariableReference ref = context_->get_ref (final_key, settings);
593  map_[key] = ref;
594  result = context_->read_file (ref, filename, settings);
595  }
596  else
597  {
598  result = context_->read_file (entry->second, filename, settings);
599  }
600  }
601  return result;
602 }
603 
604 int
607 {
608  int result = -1;
609 
610  if (context_ && key != "")
611  {
612  ContextGuard context_guard (*context_);
613  MADARA_GUARD_TYPE guard (mutex_);
614  std::stringstream buffer;
615  buffer << name_;
616  buffer << delimiter_;
617  buffer << key;
618 
619  std::string final_key = buffer.str ();
620  std::map <std::string, VariableReference>::iterator entry =
621  map_.find (final_key);
622 
623  if (entry == map_.end ())
624  {
625  VariableReference ref = context_->get_ref (final_key, settings_);
626  map_[key] = ref;
627  result = context_->set (ref, value, settings_);
628  }
629  else
630  {
631  result = context_->set (entry->second, value, settings_);
632  }
633  }
634 
635  return result;
636 }
637 
638 int
641  const KnowledgeUpdateSettings & settings)
642 {
643  int result = -1;
644 
645  if (context_ && key != "")
646  {
647  ContextGuard context_guard (*context_);
648  MADARA_GUARD_TYPE guard (mutex_);
649 
650  std::stringstream buffer;
651  buffer << name_;
652  buffer << delimiter_;
653  buffer << key;
654 
655  std::string final_key = buffer.str ();
656  std::map <std::string, VariableReference>::iterator entry =
657  map_.find (final_key);
658 
659  if (entry == map_.end ())
660  {
661  VariableReference ref = context_->get_ref (final_key, settings);
662  map_[key] = ref;
663  result = context_->set (ref, value, settings);
664  }
665  else
666  {
667  result = context_->set (entry->second, value, settings);
668  }
669  }
670 
671  return result;
672 }
673 
674 int
676  const std::string & key,
677  size_t index,
679 {
680  int result = -1;
681 
682  if (context_ && key != "")
683  {
684  ContextGuard context_guard (*context_);
685  MADARA_GUARD_TYPE guard (mutex_);
686 
687  std::stringstream buffer;
688  buffer << name_;
689  buffer << delimiter_;
690  buffer << key;
691 
692  std::string final_key = buffer.str ();
693  std::map <std::string, VariableReference>::iterator entry =
694  map_.find (final_key);
695 
696  if (entry == map_.end ())
697  {
698  VariableReference ref = context_->get_ref (final_key, settings_);
699  map_[key] = ref;
700  result = context_->set_index (ref, index, value, settings_);
701  }
702  else
703  {
704  result = context_->set_index (entry->second, index, value, settings_);
705  }
706  }
707 
708  return result;
709 }
710 
711 int
713  const std::string & key,
714  size_t index,
716  const KnowledgeUpdateSettings & settings)
717 {
718  int result = -1;
719 
720  if (context_ && key != "")
721  {
722  ContextGuard context_guard (*context_);
723  MADARA_GUARD_TYPE guard (mutex_);
724 
725  std::stringstream buffer;
726  buffer << name_;
727  buffer << delimiter_;
728  buffer << key;
729 
730  std::string final_key = buffer.str ();
731  std::map <std::string, VariableReference>::iterator entry =
732  map_.find (final_key);
733 
734  if (entry == map_.end ())
735  {
736  VariableReference ref = context_->get_ref (final_key, settings);
737  map_[key] = ref;
738  result = context_->set_index (ref, index, value, settings);
739  }
740  else
741  {
742  result = context_->set_index (entry->second, index, value, settings);
743  }
744  }
745 
746  return result;
747 }
748 
749 
750 int
753  uint32_t size)
754 {
755  int result = -1;
756 
757  if (context_ && key != "")
758  {
759  ContextGuard context_guard (*context_);
760  MADARA_GUARD_TYPE guard (mutex_);
761 
762  std::stringstream buffer;
763  buffer << name_;
764  buffer << delimiter_;
765  buffer << key;
766 
767  std::string final_key = buffer.str ();
768  std::map <std::string, VariableReference>::iterator entry =
769  map_.find (final_key);
770 
771  if (entry == map_.end ())
772  {
773  VariableReference ref = context_->get_ref (final_key, settings_);
774  map_[key] = ref;
775  result = context_->set (ref, value, size, settings_);
776  }
777  else
778  {
779  result = context_->set (entry->second, value, size, settings_);
780  }
781  }
782 
783  return result;
784 }
785 
786 int
789  uint32_t size,
790  const KnowledgeUpdateSettings & settings)
791 {
792  int result = -1;
793 
794  if (context_ && key != "")
795  {
796  ContextGuard context_guard (*context_);
797  MADARA_GUARD_TYPE guard (mutex_);
798 
799  std::stringstream buffer;
800  buffer << name_;
801  buffer << delimiter_;
802  buffer << key;
803 
804  std::string final_key = buffer.str ();
805  std::map <std::string, VariableReference>::iterator entry =
806  map_.find (final_key);
807 
808  if (entry == map_.end ())
809  {
810  VariableReference ref = context_->get_ref (final_key, settings);
811  map_[key] = ref;
812  result = context_->set (ref, value, size, settings);
813  }
814  else
815  {
816  result = context_->set (entry->second, value, size, settings);
817  }
818  }
819 
820  return result;
821 }
822 
823 int
825 const std::string & key,
826  const std::vector <KnowledgeRecord::Integer> & value)
827 {
828  int result = -1;
829 
830  if (context_ && key != "")
831  {
832  ContextGuard context_guard (*context_);
833  MADARA_GUARD_TYPE guard (mutex_);
834 
835  std::stringstream buffer;
836  buffer << name_;
837  buffer << delimiter_;
838  buffer << key;
839 
840  std::string final_key = buffer.str ();
841  std::map <std::string, VariableReference>::iterator entry =
842  map_.find (final_key);
843 
844  if (entry == map_.end ())
845  {
846  VariableReference ref = context_->get_ref (final_key, settings_);
847  map_[key] = ref;
848  result = context_->set (ref, value, settings_);
849  }
850  else
851  {
852  result = context_->set (entry->second, value, settings_);
853  }
854  }
855 
856  return result;
857 }
858 
859 int
861  const std::string & key,
862  const std::vector <KnowledgeRecord::Integer> & value,
863  const KnowledgeUpdateSettings & settings)
864 {
865  int result = -1;
866 
867  if (context_ && key != "")
868  {
869  ContextGuard context_guard (*context_);
870  MADARA_GUARD_TYPE guard (mutex_);
871 
872  std::stringstream buffer;
873  buffer << name_;
874  buffer << delimiter_;
875  buffer << key;
876 
877  std::string final_key = buffer.str ();
878  std::map <std::string, VariableReference>::iterator entry =
879  map_.find (final_key);
880 
881  if (entry == map_.end ())
882  {
883  VariableReference ref = context_->get_ref (final_key, settings);
884  map_[key] = ref;
885  return context_->set (ref, value, settings);
886  }
887  else
888  {
889  result = context_->set (entry->second, value, settings);
890  }
891  }
892 
893  return result;
894 }
895 
896 int
898  const std::string & key,
899  double value)
900 {
901  int result = -1;
902 
903  if (context_ && key != "")
904  {
905  ContextGuard context_guard (*context_);
906  MADARA_GUARD_TYPE guard (mutex_);
907 
908  std::stringstream buffer;
909  buffer << name_;
910  buffer << delimiter_;
911  buffer << key;
912 
913  std::string final_key = buffer.str ();
914  std::map <std::string, VariableReference>::iterator entry =
915  map_.find (final_key);
916 
917  if (entry == map_.end ())
918  {
919  VariableReference ref = context_->get_ref (final_key, settings_);
920  map_[key] = ref;
921  result = context_->set (ref, value, settings_);
922  }
923  else
924  {
925  result = context_->set (entry->second, value, settings_);
926  }
927  }
928 
929  return result;
930 }
931 
932 int
934  double value,
935  const KnowledgeUpdateSettings & settings)
936 {
937  int result = -1;
938 
939  if (context_ && key != "")
940  {
941  ContextGuard context_guard (*context_);
942  MADARA_GUARD_TYPE guard (mutex_);
943 
944  std::stringstream buffer;
945  buffer << name_;
946  buffer << delimiter_;
947  buffer << key;
948 
949  std::string final_key = buffer.str ();
950  std::map <std::string, VariableReference>::iterator entry =
951  map_.find (final_key);
952 
953  if (entry == map_.end ())
954  {
955  VariableReference ref = context_->get_ref (final_key, settings);
956  map_[key] = ref;
957  result = context_->set (ref, value, settings);
958  }
959  else
960  {
961  result = context_->set (entry->second, value, settings);
962  }
963  }
964 
965  return result;
966 }
967 
968 int
970  const std::string & key,
971  size_t index,
972  double value)
973 {
974  int result = -1;
975 
976  if (context_ && key != "")
977  {
978  ContextGuard context_guard (*context_);
979  MADARA_GUARD_TYPE guard (mutex_);
980 
981  std::stringstream buffer;
982  buffer << name_;
983  buffer << delimiter_;
984  buffer << key;
985 
986  std::string final_key = buffer.str ();
987  std::map <std::string, VariableReference>::iterator entry =
988  map_.find (final_key);
989 
990  if (entry == map_.end ())
991  {
992  VariableReference ref = context_->get_ref (final_key, settings_);
993  map_[key] = ref;
994  result = context_->set_index (ref, index, value, settings_);
995  }
996  else
997  {
998  result = context_->set_index (entry->second, index, value, settings_);
999  }
1000  }
1001 
1002  return result;
1003 }
1004 
1005 int
1007  size_t index,
1008  double value,
1009  const KnowledgeUpdateSettings & settings)
1010 {
1011  int result = -1;
1012 
1013  if (context_ && key != "")
1014  {
1015  ContextGuard context_guard (*context_);
1016  MADARA_GUARD_TYPE guard (mutex_);
1017 
1018  std::stringstream buffer;
1019  buffer << name_;
1020  buffer << delimiter_;
1021  buffer << key;
1022 
1023  std::string final_key = buffer.str ();
1024  std::map <std::string, VariableReference>::iterator entry =
1025  map_.find (final_key);
1026 
1027  if (entry == map_.end ())
1028  {
1029  VariableReference ref = context_->get_ref (final_key, settings);
1030  map_[key] = ref;
1031  result = context_->set_index (ref, index, value, settings);
1032  }
1033  else
1034  {
1035  result = context_->set_index (entry->second, index, value, settings);
1036  }
1037  }
1038 
1039  return result;
1040 }
1041 
1042 int
1044  const double * value,
1045  uint32_t size)
1046 {
1047  int result = -1;
1048 
1049  if (context_ && key != "")
1050  {
1051  ContextGuard context_guard (*context_);
1052  MADARA_GUARD_TYPE guard (mutex_);
1053 
1054  std::stringstream buffer;
1055  buffer << name_;
1056  buffer << delimiter_;
1057  buffer << key;
1058 
1059  std::string final_key = buffer.str ();
1060  std::map <std::string, VariableReference>::iterator entry =
1061  map_.find (final_key);
1062 
1063  if (entry == map_.end ())
1064  {
1065  VariableReference ref = context_->get_ref (final_key, settings_);
1066  map_[key] = ref;
1067  result = context_->set (ref, value, size, settings_);
1068  }
1069  else
1070  {
1071  result = context_->set (entry->second, value, size, settings_);
1072  }
1073  }
1074 
1075  return result;
1076 }
1077 
1078 int
1080  const double * value,
1081  uint32_t size,
1082  const KnowledgeUpdateSettings & settings)
1083 {
1084  int result = -1;
1085 
1086  if (context_ && key != "")
1087  {
1088  ContextGuard context_guard (*context_);
1089  MADARA_GUARD_TYPE guard (mutex_);
1090 
1091  std::stringstream buffer;
1092  buffer << name_;
1093  buffer << delimiter_;
1094  buffer << key;
1095 
1096  std::string final_key = buffer.str ();
1097  std::map <std::string, VariableReference>::iterator entry =
1098  map_.find (final_key);
1099 
1100  if (entry == map_.end ())
1101  {
1102  VariableReference ref = context_->get_ref (final_key, settings);
1103  map_[key] = ref;
1104  result = context_->set (ref, value, size, settings);
1105  }
1106 
1107  result = context_->set (entry->second, value, size, settings);
1108  }
1109 
1110  return result;
1111 }
1112 
1113 int
1115  const std::vector <double> & value)
1116 {
1117  int result = -1;
1118 
1119  if (context_ && key != "")
1120  {
1121  ContextGuard context_guard (*context_);
1122  MADARA_GUARD_TYPE guard (mutex_);
1123 
1124  std::stringstream buffer;
1125  buffer << name_;
1126  buffer << delimiter_;
1127  buffer << key;
1128 
1129  std::string final_key = buffer.str ();
1130  std::map <std::string, VariableReference>::iterator entry =
1131  map_.find (final_key);
1132 
1133  if (entry == map_.end ())
1134  {
1135  VariableReference ref = context_->get_ref (final_key, settings_);
1136  map_[key] = ref;
1137  result = context_->set (ref, value, settings_);
1138  }
1139  else
1140  {
1141  result = context_->set (entry->second, value, settings_);
1142  }
1143  }
1144 
1145  return result;
1146 }
1147 
1148 int
1150  const std::vector <double> & value,
1151  const KnowledgeUpdateSettings & settings)
1152 {
1153  int result = -1;
1154 
1155  if (context_ && key != "")
1156  {
1157  ContextGuard context_guard (*context_);
1158  MADARA_GUARD_TYPE guard (mutex_);
1159 
1160  std::stringstream buffer;
1161  buffer << name_;
1162  buffer << delimiter_;
1163  buffer << key;
1164 
1165  std::string final_key = buffer.str ();
1166  std::map <std::string, VariableReference>::iterator entry =
1167  map_.find (final_key);
1168 
1169  if (entry == map_.end ())
1170  {
1171  VariableReference ref = context_->get_ref (final_key, settings);
1172  map_[key] = ref;
1173  result = context_->set (ref, value, settings);
1174  }
1175  else
1176  {
1177  result = context_->set (entry->second, value, settings);
1178  }
1179  }
1180 
1181  return result;
1182 }
1183 
1184 int
1186  const std::string & value)
1187 {
1188  int result = -1;
1189 
1190  if (context_ && key != "")
1191  {
1192  ContextGuard context_guard (*context_);
1193  MADARA_GUARD_TYPE guard (mutex_);
1194 
1195  std::stringstream buffer;
1196  buffer << name_;
1197  buffer << delimiter_;
1198  buffer << key;
1199 
1200  std::string final_key = buffer.str ();
1201  std::map <std::string, VariableReference>::iterator entry =
1202  map_.find (final_key);
1203 
1204  if (entry == map_.end ())
1205  {
1206  VariableReference ref = context_->get_ref (final_key, settings_);
1207  map_[key] = ref;
1208  result = context_->set (ref, value, settings_);
1209  }
1210  else
1211  {
1212  result = context_->set (entry->second, value, settings_);
1213  }
1214  }
1215 
1216  return result;
1217 }
1218 
1219 int
1221  const std::string & value,
1222  const KnowledgeUpdateSettings & settings)
1223 {
1224  int result = -1;
1225 
1226  if (context_ && key != "")
1227  {
1228  ContextGuard context_guard (*context_);
1229  MADARA_GUARD_TYPE guard (mutex_);
1230 
1231  std::stringstream buffer;
1232  buffer << name_;
1233  buffer << delimiter_;
1234  buffer << key;
1235 
1236  std::string final_key = buffer.str ();
1237  std::map <std::string, VariableReference>::iterator entry =
1238  map_.find (final_key);
1239 
1240  if (entry == map_.end ())
1241  {
1242  VariableReference ref = context_->get_ref (final_key, settings);
1243  map_[key] = ref;
1244  result = context_->set (ref, value, settings);
1245  }
1246  else
1247  {
1248  result = context_->set (entry->second, value, settings);
1249  }
1250  }
1251 
1252  return result;
1253 }
1254 
1255 int
1257  const unsigned char * value, size_t size)
1258 {
1259  int result = -1;
1260 
1261  if (context_ && key != "")
1262  {
1263  ContextGuard context_guard (*context_);
1264  MADARA_GUARD_TYPE guard (mutex_);
1265 
1266  std::stringstream buffer;
1267  buffer << name_;
1268  buffer << delimiter_;
1269  buffer << key;
1270 
1271  std::string final_key = buffer.str ();
1272  std::map <std::string, VariableReference>::iterator entry =
1273  map_.find (final_key);
1274 
1275  if (entry == map_.end ())
1276  {
1277  VariableReference ref = context_->get_ref (final_key, settings_);
1278  map_[key] = ref;
1279  result = context_->set_file (ref, value, size, settings_);
1280  }
1281  else
1282  {
1283  result = context_->set_file (entry->second, value, size, settings_);
1284  }
1285  }
1286 
1287  return result;
1288 }
1289 
1290 int
1292  const unsigned char * value, size_t size,
1293  const KnowledgeUpdateSettings & settings)
1294 {
1295  int result = -1;
1296 
1297  if (context_ && key != "")
1298  {
1299  ContextGuard context_guard (*context_);
1300  MADARA_GUARD_TYPE guard (mutex_);
1301 
1302  std::stringstream buffer;
1303  buffer << name_;
1304  buffer << delimiter_;
1305  buffer << key;
1306 
1307  std::string final_key = buffer.str ();
1308  std::map <std::string, VariableReference>::iterator entry =
1309  map_.find (final_key);
1310 
1311  if (entry == map_.end ())
1312  {
1313  VariableReference ref = context_->get_ref (final_key, settings);
1314  map_[key] = ref;
1315  result = context_->set_file (ref, value, size, settings);
1316  }
1317 
1318  result = context_->set_file (entry->second, value, size, settings);
1319  }
1320 
1321  return result;
1322 }
1323 
1324 int
1326  const unsigned char * value, size_t size)
1327 {
1328  int result = -1;
1329 
1330  if (context_ && key != "")
1331  {
1332  ContextGuard context_guard (*context_);
1333  MADARA_GUARD_TYPE guard (mutex_);
1334 
1335  std::stringstream buffer;
1336  buffer << name_;
1337  buffer << delimiter_;
1338  buffer << key;
1339 
1340  std::string final_key = buffer.str ();
1341  std::map <std::string, VariableReference>::iterator entry =
1342  map_.find (final_key);
1343 
1344  if (entry == map_.end ())
1345  {
1346  VariableReference ref = context_->get_ref (final_key, settings_);
1347  map_[key] = ref;
1348  result = context_->set_jpeg (ref, value, size, settings_);
1349  }
1350  else
1351  {
1352  result = context_->set_jpeg (entry->second, value, size, settings_);
1353  }
1354  }
1355 
1356  return result;
1357 }
1358 
1359 int
1361  const unsigned char * value, size_t size,
1362  const KnowledgeUpdateSettings & settings)
1363 {
1364  int result = -1;
1365 
1366  if (context_ && key != "")
1367  {
1368  ContextGuard context_guard (*context_);
1369  MADARA_GUARD_TYPE guard (mutex_);
1370 
1371  std::stringstream buffer;
1372  buffer << name_;
1373  buffer << delimiter_;
1374  buffer << key;
1375 
1376  std::string final_key = buffer.str ();
1377  std::map <std::string, VariableReference>::iterator entry =
1378  map_.find (final_key);
1379 
1380  if (entry == map_.end ())
1381  {
1382  VariableReference ref = context_->get_ref (final_key, settings);
1383  map_[key] = ref;
1384  result = context_->set_jpeg (ref, value, size, settings);
1385  }
1386  else
1387  {
1388  result = context_->set_jpeg (entry->second, value, size, settings);
1389  }
1390  }
1391 
1392  return result;
1393 }
1394 
1396  const std::string & key,
1397  uint32_t quality,
1398  const KnowledgeReferenceSettings & settings)
1399 {
1400  if (context_)
1401  {
1402  ContextGuard context_guard (*context_);
1403  MADARA_GUARD_TYPE guard (mutex_);
1404 
1405  std::stringstream buffer;
1406  buffer << name_;
1407  buffer << delimiter_;
1408  buffer << key;
1409 
1410  context_->set_quality (buffer.str (), quality, true, settings);
1411  }
1412 }
1413 
1414 bool
1416 {
1417  bool result (false);
1418 
1420  "Map::is_true: checking for truth in all elements\n");
1421 
1422  if (context_ && name_ != "")
1423  {
1424  ContextGuard context_guard (*context_);
1425  MADARA_GUARD_TYPE guard (mutex_);
1426 
1427  result = true;
1428 
1429  for (InternalMap::const_iterator index = map_.begin ();
1430  index != map_.end (); ++index)
1431  {
1433  "Map::is_true: checking index, is_false of %d. \n",
1434  (int)context_->get (index->second).is_false ());
1435 
1436  if (context_->get (index->second).is_false ())
1437  {
1439  "Map::is_true: result is false, breaking\n");
1440 
1441  result = false;
1442  break;
1443  }
1444  }
1445 
1446  if (map_.size () == 0)
1447  result = false;
1448  }
1449 
1451  "Map::is_true: final result is %d\n", (int)result);
1452 
1453  return result;
1454 }
1455 
1456 bool
1458 {
1459  return !is_true ();
1460 }
1461 
1462 
1463 bool
1465 {
1466  return is_true ();
1467 }
1468 
1469 bool
1471 {
1472  return is_false ();
1473 }
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
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:474
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:516
bool is_false(void) const
Determines if the value of the map is false.
Definition: Map.cpp:1457
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:1325
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:1415
bool exists(const std::string &key) const
Checks for the existence of a key.
Definition: Map.cpp:480
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:1395
void erase(const std::string &key)
Erases a variable from the map.
Definition: Map.cpp:397
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:499
void set_delimiter(const std::string &delimiter, bool sync=true)
Sets the delimiter for adding and detecting subvariables.
Definition: Map.cpp:457
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:675
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:605
static struct madara::knowledge::tags::string_t string
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:45
void set_name(const std::string &var_name, KnowledgeBase &knowledge, bool sync=true)
Sets the variable name that this refers to.
Definition: Map.cpp:415
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
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
int read_file(const std::string &key, const std::string &filename)
Read a file into a location in the map.
Definition: Map.cpp:534
void clear(bool clear_knowledge=true)
Clears the map.
Definition: Map.cpp:376
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
Settings for applying knowledge updates.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
uint32_t set_quality(const std::string &key, uint32_t quality, bool force_update, const KnowledgeReferenceSettings &settings)
Atomically sets quality of this process for a variable.
logger::Logger & get_logger(void) const
Gets the logger used for information printing.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
Definition: Map.cpp:1464
MADARA_EXPORT bool begins_with(const std::string &input, const std::string &prefix)
Check if input contains prefix at the beginning.
Definition: Utility.inl:317
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:1470
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:1256
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