MADARA  3.1.8
BufferVector.cpp
Go to the documentation of this file.
1 #include "BufferVector.h"
3 
4 namespace madara { namespace knowledge { namespace containers {
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  int size,
17  bool delete_vars,
18  const KnowledgeUpdateSettings & settings,
19  const std::string & delimiter)
20  : BaseContainer (name, settings), context_ (&(knowledge.get_context ())),
21  delimiter_ (delimiter)
22 {
23  size_ = get_size_ref ();
24  resize (size, delete_vars);
25 }
26 
28  const std::string & name,
30  int size,
31  bool delete_vars,
32  const KnowledgeUpdateSettings & settings,
33  const std::string & delimiter)
34  : BaseContainer (name, settings), context_ (knowledge.get_context ()),
35  delimiter_ (delimiter)
36 {
37  size_ = get_size_ref ();
38  resize (size, delete_vars);
39 }
40 
42 : BaseContainer (rhs), context_ (rhs.context_),
43  vector_ (rhs.vector_),
44  size_ (rhs.size_),
46 {
47 
48 }
49 
50 
52 {
53 
54 }
55 
56 void
58 {
59  if (context_ && name_ != "")
60  {
61  ContextGuard context_guard (*context_);
62  for (size_t index = 0; index < vector_.size (); ++index)
63  context_->mark_modified (vector_[index]);
64 
66  }
67 }
68 
71 {
72  std::stringstream result;
73 
74  result << "Buffer Vector: ";
75 
76  if (context_)
77  {
78  ContextGuard context_guard (*context_);
79  MADARA_GUARD_TYPE guard (mutex_);
80  size_t elements = vector_.size ();
81 
82  result << this->name_;
83  result << " [" << elements << "]";
84  result << " = [";
85 
86  if (elements > 0)
87  {
88  result << context_->get (vector_[0]).to_string ();
89 
90  for (size_t index = 1; index < elements; ++index)
91  {
92  result << ", " << context_->get (vector_[index]).to_string ();
93  }
94  }
95 
96  result << "]";
97  }
98 
99  return result.str ();
100 }
101 
102 
103 void
105 {
106  modify ();
107 }
108 
111 {
112  return get_debug_info ();
113 }
114 
117 {
118  return new BufferVector (*this);
119 }
120 
121 void
122 BufferVector::modify (size_t index)
123 {
124  if (context_ && name_ != "" && index < vector_.size ())
125  {
126  ContextGuard context_guard (*context_);
127  context_->mark_modified (vector_[index]);
128  }
129 }
130 
131 void
133  const BufferVector & rhs)
134 {
135  if (this != &rhs)
136  {
137  MADARA_GUARD_TYPE guard (mutex_), guard2 (rhs.mutex_);
138 
139  this->context_ = rhs.context_;
140  this->name_ = rhs.name_;
141  this->settings_ = rhs.settings_;
142  this->size_ = rhs.size_;
143  this->vector_ = rhs.vector_;
144  this->delimiter_ = rhs.delimiter_;
145  }
146 }
147 
148 void
150  const unsigned char * value, size_t size)
151 {
152  if (context_ && name_ != "")
153  {
154  ContextGuard context_guard (*context_);
155  MADARA_GUARD_TYPE guard (mutex_);
156 
157  if (!size_.is_valid ())
158  {
159  size_ = get_size_ref ();
160  }
161 
162  size_t i = this->size ();
163  resize ((int)i + 1);
164  set_file (i, value, size);
165  }
166 }
167 
168 
171 {
172  VariableReference ref;
173 
174  if (context_ && name_ != "")
175  {
176  KnowledgeUpdateSettings keep_local (true);
177  std::stringstream buffer;
178 
179  ContextGuard context_guard (*context_);
180  MADARA_GUARD_TYPE guard (mutex_);
181 
182  buffer << name_;
183  buffer << delimiter_;
184  buffer << "size";
185 
186  ref = context_->get_ref (buffer.str (), keep_local);
187  }
188 
189  return ref;
190 }
191 
192 void
194  int size, bool delete_vars)
195 {
196  if (context_ && name_ != "")
197  {
198  ContextGuard context_guard (*context_);
199  MADARA_GUARD_TYPE guard (mutex_);
200 
201  if (!size_.is_valid ())
202  {
203  size_ = get_size_ref ();
204  }
205 
206  if (size >= 0)
207  {
208  size_t old_size = vector_.size ();
209 
210  if (old_size != (size_t)size)
211  {
212  vector_.resize (size);
213 
215 
216  if ((size_t)size > old_size)
217  {
218  for (; old_size < (size_t)size; ++old_size)
219  {
220  std::stringstream buffer;
221  buffer << name_;
222  buffer << delimiter_;
223  buffer << old_size;
224  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
225  }
226  }
227  else if (delete_vars)
228  {
229  for (; (size_t)size < old_size; ++size)
230  {
231  std::stringstream buffer;
232  buffer << name_;
233  buffer << delimiter_;
234  buffer << size;
235 
236  context_->delete_variable (buffer.str (), settings_);
237  }
238  }
239  }
240  }
241  else
242  {
243  // dynamically allocate size from the context
244  size_t cur_size =
246 
247  size_t old_size = vector_.size ();
248 
249  if (old_size != cur_size)
250  {
251  vector_.resize (cur_size);
252 
253  if (cur_size > old_size)
254  {
255  for (; old_size < cur_size; ++old_size)
256  {
257  std::stringstream buffer;
258  buffer << name_;
259  buffer << delimiter_;
260  buffer << old_size;
261  vector_[old_size] = context_->get_ref (buffer.str (), settings_);
262  }
263  }
264  else if (delete_vars)
265  {
266  for (; cur_size < old_size; ++cur_size)
267  {
268  std::stringstream buffer;
269  buffer << name_;
270  buffer << delimiter_;
271  buffer << cur_size;
272 
273  context_->delete_variable (buffer.str (), settings_);
274  }
275  }
276  }
277  }
278  }
279 }
280 
281 size_t
282 BufferVector::size (void) const
283 {
284  MADARA_GUARD_TYPE guard (mutex_);
285  return vector_.size();
286 }
287 
288 void
290  const std::string & var_name,
292 {
293  if (context_ != &(knowledge.get_context ()) || name_ != var_name)
294  {
295  context_ = &(knowledge.get_context ());
296 
297  ContextGuard context_guard (*context_);
298  MADARA_GUARD_TYPE guard (mutex_);
299 
300  name_ = var_name;
301 
302  vector_.clear ();
303 
304  size_ = get_size_ref ();
305 
306  resize (size);
307  }
308 }
309 
310 void
312  const std::string & var_name,
313  Variables & knowledge, int size)
314 {
315  if (context_ != knowledge.get_context () || name_ != var_name)
316  {
317  context_ = knowledge.get_context ();
318 
319  ContextGuard context_guard (*context_);
320  MADARA_GUARD_TYPE guard (mutex_);
321 
322  name_ = var_name;
323 
324  vector_.clear ();
325  resize (size);
326  }
327 }
328 
329 void
331  const std::string & var_name,
333 {
334  if (context_ != &knowledge || name_ != var_name)
335  {
336  context_ = &knowledge;
337 
338  ContextGuard context_guard (*context_);
339  MADARA_GUARD_TYPE guard (mutex_);
340 
341  name_ = var_name;
342 
343  vector_.clear ();
344  resize (size);
345  }
346 }
347 
348 void
350 const std::string & delimiter)
351 {
352  delimiter_ = delimiter;
353  if (context_)
354  {
355  ContextGuard context_guard (*context_);
356  MADARA_GUARD_TYPE guard (mutex_);
357 
358  vector_.clear ();
359  resize (-1);
360  }
361 }
362 
363 
366 {
367  return delimiter_;
368 }
369 
370 void
372  BufferVector & other, bool refresh_keys, bool delete_keys)
373 {
374  if (context_ && other.context_)
375  {
376  ContextGuard context_guard (*context_);
377  ContextGuard other_context_guard (*other.context_);
378  MADARA_GUARD_TYPE guard (mutex_), guard2 (other.mutex_);
379 
380  if (refresh_keys)
381  {
382  other.resize ();
383  this->resize ();
384  }
385 
386  size_t other_size = other.vector_.size ();
387  size_t this_size = this->vector_.size ();
388 
389  for (size_t i = 0; i < this_size; ++i)
390  {
391  // temp = this[i];
393 
394  if (i < other_size)
395  {
396  // this[i] = other[i];
397  context_->set (this->vector_[i],
398  context_->get (other.vector_[i], other.settings_),
399  settings_);
400 
401  // other[i] = temp;
402  other.context_->set (other.vector_[i], temp, other.settings_);
403  }
404  else
405  {
406  if (delete_keys)
407  {
408  std::stringstream buffer;
409  buffer << this->name_;
410  buffer << delimiter_;
411  buffer << i;
412  this->context_->delete_variable (buffer.str (), other.settings_);
413  }
414  else
415  {
417  this->context_->set (this->vector_[i], zero, this->settings_);
418  }
419 
420  {
421  std::stringstream buffer;
422  buffer << other.name_;
423  buffer << delimiter_;
424  buffer << i;
425 
426  // other[i] = temp;
427  other.context_->set (buffer.str (), temp, other.settings_);
428  }
429  }
430 
431  }
432 
433  // copy the other vector's elements to this vector's location
434  for (size_t i = this_size; i < other_size; ++i)
435  {
436  std::stringstream buffer;
437  buffer << this->name_;
438  buffer << delimiter_;
439  buffer << i;
440  context_->set (buffer.str (),
441  other.context_->get (other.vector_[i], other.settings_), this->settings_);
442  }
443 
444  // set the size appropriately
445  this->context_->set (this->size_,
446  knowledge::KnowledgeRecord::Integer (other_size), this->settings_);
447  other.context_->set (other.size_,
449 
450  if (refresh_keys)
451  {
452  this->resize (-1, true);
453  other.resize (-1, true);
454  }
455  }
456 }
457 
458 void
460 {
461  if (context_ && other.context_)
462  {
463  ContextGuard context_guard (*context_);
464  ContextGuard other_context_guard (*other.context_);
465  MADARA_GUARD_TYPE guard (mutex_);
466  MADARA_GUARD_TYPE guard2 (other.mutex_);
467 
468  size_t other_size = other.vector_.size ();
469  size_t this_size = this->vector_.size ();
470 
471  size_t size = other_size + this_size;
472  other.resize ((int)size);
473 
474  for (size_t i = 0, j = other_size; i < this_size; ++i, ++j)
475  {
476  other.context_->set (other.vector_[j], (*this)[i], other.settings_);
477  }
478 
479  this->resize (0, true);
480  }
481 }
482 
483 void
485  KnowledgeVector & target) const
486 {
487  if (context_)
488  {
489  ContextGuard context_guard (*context_);
490  MADARA_GUARD_TYPE guard (mutex_);
491 
492  target.clear ();
493  target.reserve (vector_.size());
494 
495  for (const auto &cur : vector_)
496  {
497  if (cur.is_valid()) {
498  target.emplace_back (context_->get (cur));
499  } else {
500  target.emplace_back ();
501  }
502  }
503  }
504 }
505 
508  size_t index) const
509 {
510  return to_record (index);
511 }
512 
515  size_t index) const
516 {
518 
519  if (index < vector_.size () && context_)
520  {
521  ContextGuard context_guard (*context_);
522  MADARA_GUARD_TYPE guard (mutex_);
523  result = context_->get (vector_[index], settings_);
524  }
525 
526  return result;
527 }
528 
529 bool
531  size_t index) const
532 {
533  bool result (false);
534 
535  if (index < vector_.size () && context_)
536  {
537  ContextGuard context_guard (*context_);
538  MADARA_GUARD_TYPE guard (mutex_);
539  result = context_->exists (vector_[index]);
540  }
541 
542  return result;
543 }
544 
545 
546 int
548  size_t index,
549  const std::string & filename)
550 {
551  int result = -1;
552 
553  if (index < vector_.size () && context_)
554  {
555  ContextGuard context_guard (*context_);
556  MADARA_GUARD_TYPE guard (mutex_);
557  result = context_->read_file (vector_[index], filename, settings_);
558  }
559 
560  return result;
561 }
562 
563 
564 int
566  size_t index,
567  const std::string & filename,
568  const KnowledgeUpdateSettings & settings)
569 {
570  int result = -1;
571 
572  if (index < vector_.size () && context_)
573  {
574  ContextGuard context_guard (*context_);
575  MADARA_GUARD_TYPE guard (mutex_);
576  result = context_->read_file (vector_[index], filename, settings);
577  }
578 
579  return result;
580 }
581 
582 
583 int
585  size_t index,
586  const unsigned char * value, size_t size)
587 {
588  int result = -1;
589 
590  if (index < vector_.size () && context_)
591  {
592  ContextGuard context_guard (*context_);
593  MADARA_GUARD_TYPE guard (mutex_);
594  result = context_->set_file (vector_[index], value, size, settings_);
595  }
596 
597  return result;
598 }
599 
600 int
602  size_t index, const knowledge::KnowledgeRecord & value)
603 {
604  int result = -1;
605 
606  if (index < vector_.size () && context_)
607  {
608  ContextGuard context_guard (*context_);
609  MADARA_GUARD_TYPE guard (mutex_);
610 
611  context_->set (vector_[index], value, settings_);
612  }
613 
614  return result;
615 }
616 
617 int
619  size_t index,
620  const unsigned char * value, size_t size,
621  const KnowledgeUpdateSettings & settings)
622 {
623  int result = -1;
624 
625  if (index < vector_.size () && context_)
626  {
627  ContextGuard context_guard (*context_);
628  MADARA_GUARD_TYPE guard (mutex_);
629  result = context_->set_file (vector_[index], value, size, settings);
630  }
631 
632  return result;
633 }
634 
635 
636 int
638  size_t index,
639  const unsigned char * value, size_t size)
640 {
641  int result = -1;
642 
643  if (index < vector_.size () && context_)
644  {
645  ContextGuard context_guard (*context_);
646  MADARA_GUARD_TYPE guard (mutex_);
647  result = context_->set_jpeg (vector_[index], value, size, settings_);
648  }
649 
650  return result;
651 }
652 
653 int
655  size_t index,
656  const unsigned char * value, size_t size,
657  const KnowledgeUpdateSettings & settings)
658 {
659  int result = -1;
660 
661  if (index < vector_.size () && context_)
662  {
663  ContextGuard context_guard (*context_);
664  MADARA_GUARD_TYPE guard (mutex_);
665  result = context_->set_jpeg (vector_[index], value, size, settings);
666  }
667 
668  return result;
669 }
670 
671 void
673  size_t index,
674  uint32_t quality,
675  const KnowledgeReferenceSettings & settings)
676 {
677  if (index < vector_.size () && context_)
678  {
679  ContextGuard context_guard (*context_);
680  MADARA_GUARD_TYPE guard (mutex_);
681 
682  context_->set_quality (vector_[index].get_name (), quality, true,
683  settings);
684  }
685 }
686 
687 bool
689 {
690  bool result (false);
691 
693  "BufferVector::is_true: Checking for truth\n");
694 
695  if (context_)
696  {
697  ContextGuard context_guard (*context_);
698  MADARA_GUARD_TYPE guard (mutex_);
699 
700  result = true;
701 
703  "BufferVector::is_true: context was not null. Result changed to %d\n",
704  (int)result);
705 
706  for (size_t index = 0; index < vector_.size (); ++index)
707  {
708 
710  "BufferVector::is_true: checking index %d, is_false of %d. \n",
711  (int)result, (int)context_->get (vector_[index]).is_false ());
712 
713  if (context_->get (vector_[index]).is_false ())
714  {
716  "BufferVector::is_true: result is false, breaking\n");
717 
718  result = false;
719  break;
720  }
721  }
722 
723  if (vector_.size () == 0)
724  result = false;
725  }
726 
728  "BufferVector::is_true: final result is %d\n", (int)result);
729 
730  return result;
731 }
732 
733 bool
735 {
736  return !is_true ();
737 }
738 
739 
740 bool
742 {
743  return is_true ();
744 }
745 
746 bool
748 {
749  return is_false ();
750 }
751 
752 } } }
This class encapsulates an entry in a KnowledgeBase.
bool is_false(void) const
Determines if the value of the vector is false.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
This class stores a vector of character buffers.
Definition: BufferVector.h:31
VariableReference size_
Reference to the size field of the vector space.
Definition: BufferVector.h:390
std::vector< VariableReference > vector_
Values of the array.
Definition: BufferVector.h:385
int set_jpeg(size_t index, const unsigned char *value, size_t size)
Atomically sets the value of an index to a JPEG image.
void resize(int size=-1, bool delete_vars=true)
Resizes the vector.
std::string get_name(void) const
Returns the name of the container.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
virtual bool is_true_(void) const
Polymorphic is true method which can be used to determine if all values in the container are true...
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.
size_t size(void) const
Returns the size of the local vector.
std::string name_
Prefix of variable.
std::string get_delimiter(void)
Gets the delimiter for adding and detecting subvariables.
void push_back(const unsigned char *value, size_t size)
Pushes the value to the end of the array after incrementing the array size.
void exchange(BufferVector &other, bool refresh_keys=true, bool delete_keys=true)
Exchanges the vector at this location with the vector at another location.
std::string get_debug_info(void)
Returns the type of the container along with name and any other useful information.
This class stores variables and their values for use by any entity needing state information in a thr...
void transfer_to(BufferVector &other)
Transfers elements from this vector to another.
void modify(void)
Mark the vector as modified.
void operator=(const BufferVector &rhs)
Assignment operator.
MADARA_LOCK_TYPE mutex_
guard for access and changes
bool is_true(void) const
Determines if all values in the vector are true.
VariableReference get_size_ref(void) const
Returns a reference to the size field of the current name.
void copy_to(KnowledgeVector &target) const
Copies the vector elements to an STL vector of Knowledge Records.
Optimized reference to a variable within the knowledge base.
#define madara_logger_log(logger, level,...)
Fast version of the madara::logger::log method.
Definition: Logger.h:20
bool is_valid(void) const
Checks to see if the variable reference has been initialized.
bool exists(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically checks to see if a variable already exists.
Provides container classes for fast knowledge base access and mutation.
Definition: Barrier.h:27
::std::vector< KnowledgeRecord > KnowledgeVector
virtual void modify_(void)
Polymorphic modify method used by collection containers.
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
knowledge::KnowledgeRecord operator[](size_t index) const
Retrieves a copy of the record from the map.
bool exists(size_t index) const
Checks to see if the index has ever been assigned a value.
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:44
virtual BaseContainer * clone(void) const
Clones this container.
int set(size_t index, const knowledge::KnowledgeRecord &value)
Atomically sets the value of an index to a provided record.
void set_delimiter(const std::string &delimiter)
Sets the delimiter for adding and detecting subvariables.
std::string delimiter_
Delimiter for the prefix to subvars.
Definition: BufferVector.h:395
Integer to_integer(void) const
converts the value to an integer
static constexpr struct madara::knowledge::tags::string_t string
int read_file(size_t index, const std::string &filename)
Read a file into the index using stored settings.
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.
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: BufferVector.h:380
knowledge::KnowledgeRecord to_record(size_t index) const
Retrieves a copy of the record from the map.
VariableReference get_ref(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings())
Atomically returns a reference to the variable.
Settings for applying knowledge updates.
Copyright (c) 2015 Carnegie Mellon University.
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.
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.
BufferVector(const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings(), const std::string &delimiter=".")
Default constructor.
Definition: BufferVector.cpp:6
virtual std::string get_debug_info_(void)
Returns the type of the container along with name and any other useful information.
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
int read_file(const std::string &key, const std::string &filename, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically reads a file into a variable.
void mark_modified(const VariableReference &variable, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Marks the variable reference as updated for the purposes of sending or checkpointing knowledge (for g...
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
int set_file(size_t index, const unsigned char *value, size_t size)
Atomically sets the value of an index to an arbitrary string using stored settings.
void set_quality(size_t index, uint32_t quality, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings(false))
Sets the quality of writing to a certain variable from this entity.
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 ...
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.