DABC (Data Acquisition Backbone Core)  2.9.9
Hierarchy.h
Go to the documentation of this file.
1 // $Id: Hierarchy.h 4471 2020-04-15 13:19:26Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Hierarchy
17 #define DABC_Hierarchy
18 
19 #ifndef DABC_Record
20 #include "dabc/Record.h"
21 #endif
22 
23 #ifndef DABC_Queue
24 #include "dabc/Queue.h"
25 #endif
26 
27 #include <cstdint>
28 
29 namespace dabc {
30 
31  extern const char* prop_kind;
32  extern const char* prop_version; // version number of hierarchy item
33  extern const char* prop_realname; // real name property specified, when dabc item can not have such name (including HTTP syntax)
34  extern const char* prop_masteritem; // relative name of master item, which should be loaded before item itself can be used
35  extern const char* prop_producer; // identifies item, which can deliver binary data for all its
36  extern const char* prop_error; // indicates any kind of error - typically in text form
37  extern const char* prop_hash; // content hash, which should describe if object is changed
38  extern const char* prop_auth; // when specified, enabled (true) or disabled (false) authentication for the elements
39  extern const char* prop_history; // indicates that history for that element is kept
40  extern const char* prop_time; // time property, supplied when history is created
41  extern const char* prop_more; // indicate that item can provide more hierarchy if requested
42  extern const char* prop_view; // property indicate preferable view for the item
43 
44 
45  class Hierarchy;
46 
47  struct HistoryItem {
48  public:
49  uint64_t version{0};
50  RecordFieldsMap* fields{nullptr};
51  HistoryItem() = default;
53  void reset() { version = 0; delete fields; fields = nullptr; }
54  RecordFieldsMap* take() { auto res = fields; fields = nullptr; return res; }
55  };
56 
57 
58  class HistoryContainer : public Object {
59  public:
60 
61  bool fEnabled{false};
62  bool fChildsEnabled{false};
63  RecordFieldsMap* fPrev{nullptr};
65  uint64_t fRemoteReqVersion{0};
66  uint64_t fLocalReqVersion{0};
67  bool fCrossBoundary{false};
68 
70  Object(0,"cont", flAutoDestroy | flIsOwner),
71  fEnabled(false),
72  fChildsEnabled(false),
73  fPrev(nullptr),
74  fArr(),
77  fCrossBoundary(false)
78  {}
79 
81  {
82  delete fPrev; fPrev = nullptr;
83  }
84 
85  uint64_t StoreSize(uint64_t version, int hist_limit = -1);
86 
87  bool Stream(iostream &s, uint64_t version, int hist_limit = -1);
88 
90  {
91  if (fArr.Size()==0) return nullptr;
92  RecordFieldsMap *next = fArr.Front().take();
93  fArr.PopOnly();
94  return next;
95  }
96  };
97 
98 
99  class History : public Reference {
101 
102  void Allocate(unsigned sz = 0)
103  {
105  if (sz>0) GetObject()->fArr.Allocate(sz);
106  }
107 
108  bool DoHistory() const { return null() ? false : GetObject()->fEnabled && (GetObject()->fArr.Capacity()>0); }
109 
110  unsigned Capacity() const { return null() ? 0 : GetObject()->fArr.Capacity(); }
111 
112  unsigned Size() const { return null() ? 0 : GetObject()->fArr.Size(); }
113 
114  bool SaveTo(HStore& res);
115  };
116 
117  // =======================================================
118 
119  class HistoryIterContainer;
120 
121  class HistoryIter : public Record {
122 
124 
125  bool next();
126  };
127 
128  // =================================================================
129 
130 
132  stream_NamesList, // store only names list
133  stream_Value, // store only selected entry without childs (plus history, if specified)
134  stream_Full, // store full hierarchy (plus history, if specified)
135  stream_NoDelete // when reading, no any element will be deleted
136  };
137 
139 
140  friend class Hierarchy;
141  friend class HistoryIter;
142  friend class HistoryIterContainer;
143 
144  protected:
145 
146  enum {
150  maskHistory = 8
151  };
152 
155  uint64_t fNodeVersion;
156 
161  uint64_t fNamesVersion;
162 
165  uint64_t fChildsVersion;
166 
167  bool fAutoTime;
168 
169  bool fPermanent;
170 
174 
178 
180 
182 
184 
186 
187  std::string ItemName();
188 
193  HierarchyContainer* CreateChildAt(const std::string &name, int indx);
194 
199 
204 
206  void SetModified(bool node, bool hierarchy, bool recursive = false);
207 
209  void AddHistory(RecordFieldsMap* diff);
210 
212  bool ExtractHistoryStep(RecordFieldsMap* fields, unsigned step);
213 
215  void ClearHistoryEntries();
216 
219  bool CheckIfDoingHistory();
220 
223  unsigned MarkVersionIfChanged(uint64_t ver, uint64_t& tm, bool withchilds);
224 
226  void MarkReading(bool withchilds, bool readvalues, bool readchilds);
227 
233  void MarkChangedItems(uint64_t tm = 0);
234 
236  void EnableTimeRecording(bool withchilds = true);
237 
238  virtual Object *CreateInstance(const std::string &name) { return new HierarchyContainer(name); }
239 
240  virtual void _ChildsChanged() { fNamesChanged = true; fChildsChanged = true; fNodeChanged = true; }
241 
242  uint64_t GetNextVersion() const;
243 
244  void SetVersion(uint64_t v) { fNodeVersion = v; }
245 
246  void CreateHMutex();
247 
249  virtual bool SaveTo(HStore& res, bool create_node = true);
250 
251  public:
252  HierarchyContainer(const std::string &name);
253  virtual ~HierarchyContainer();
254 
255  virtual const char* ClassName() const { return "Hierarchy"; }
256 
257  uint64_t StoreSize(unsigned kind = stream_Full, uint64_t v = 0, unsigned hist_limit = 0);
258 
259  bool Stream(iostream& s, unsigned kind = stream_Full, uint64_t v = 0, unsigned hist_limit = 0);
260 
263  bool IsNodeChanged(bool withchilds = true);
264 
265  uint64_t GetVersion() const { return fNodeVersion; }
266 
267  uint64_t GetChildsVersion() const { return fChildsVersion; }
268 
269  void BuildObjectsHierarchy(const Reference& top);
270 
271  Buffer& bindata() { return fBinData; }
272  };
273 
274 
275  // ______________________________________________________________
276 
285  class Hierarchy : public Record {
286 
288 
289 
290  void Create(const std::string &name, bool withmutex = false);
291 
292  Mutex* GetHMutex() const { return null() ? 0 : GetObject()->fHierarchyMutex; }
293 
296  Hierarchy FindMaster() const;
297 
301  std::string FindBinaryProducer(std::string& request_name, bool topmost = true);
302 
303  //RecordField& Field(const std::string &name) { return GetObject()->Fields().Field(name); }
304  const RecordField& Field(const std::string &name) const { return GetObject()->Fields().Field(name); }
305 
306  bool IsAnyFieldChanged() const { return GetObject() ? GetObject()->Fields().WasChanged() : false; }
307 
309  void BuildNew(Reference top);
310 
312  void SetPermanent(bool on = true) { if (GetObject()) GetObject()->fPermanent = on; }
313 
316  bool Update(Hierarchy& src);
317 
320  bool Duplicate(const Hierarchy& src);
321 
323  void EnableHistory(unsigned length = 100, bool withchilds = false);
324 
326  void EnableTimeRecording(bool withchilds = true)
327  { if (GetObject()) GetObject()->EnableTimeRecording(withchilds); }
328 
330  void MarkChangedItems(uint64_t tm = 0)
331  { if (GetObject()) GetObject()->MarkChangedItems(tm); }
332 
334  bool HasLocalHistory() const;
335 
337  bool HasActualRemoteHistory() const;
338 
340  dabc::Buffer SaveToBuffer(unsigned kind = stream_Full, uint64_t version = 0, unsigned hlimit = 0);
341 
343  bool ReadFromBuffer(const dabc::Buffer& buf);
344 
347 
349  uint64_t GetVersion() const { return GetObject() ? GetObject()->GetVersion() : 0; }
350 
352  void SetVersion(uint64_t v) { if (GetObject()) GetObject()->SetVersion(v); }
353 
356  bool IsBinItemChanged(const std::string &itemname, uint64_t hash, uint64_t last_version = 0);
357 
359  bool FillBinHeader(const std::string &itemname, dabc::Command& cmd, uint64_t mhash = 0, const std::string &dflt_master_name = "");
360 
362  Hierarchy FindChild(const char* name) { return Record::FindChild(name); }
363 
365  bool RemoveEmptyFolders(const std::string &path);
366 
368  std::string ItemName() const;
369 
371  Hierarchy GetTop() const;
372 
374  bool DettachFromParent();
375 
377  void DisableReading(bool withchlds = true);
378 
380  void DisableReadingAsChild();
381 
383  void EnableReading(const Hierarchy &upto = nullptr);
384 
388  Hierarchy GetHChild(const std::string &name, bool allowslahes = false, bool force = false, bool sortorder = false);
389 
392  Hierarchy CreateHChild(const std::string &name, bool allowslahes = false, bool sortorder = false)
393  { return GetHChild(name, allowslahes, true, sortorder); }
394 
396  bool RemoveHChild(const std::string &name, bool allowslahes = false);
397 
399  Hierarchy CreateFolder(const std::string &name) { return GetHChild(name, true); }
400 
403  };
404 
405 }
406 
407 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Reference on memory from memory pool.
Definition: Buffer.h:135
Represents command with its arguments.
Definition: Command.h:99
class, used for direct store of records in JSON/XML form
Definition: Record.h:179
bool fChildsChanged
indicate if something was changed in the hierarchy
Definition: Hierarchy.h:173
HierarchyContainer(const std::string &name)
Definition: Hierarchy.cxx:158
uint64_t fChildsVersion
Version of hierarchy structure Childs version is changed when any childs is changed or inserted/remov...
Definition: Hierarchy.h:165
uint64_t GetNextVersion() const
Definition: Hierarchy.cxx:748
unsigned MarkVersionIfChanged(uint64_t ver, uint64_t &tm, bool withchilds)
If item changed, marked with version, time stamp applied, history recording returns mask with changes...
Definition: Hierarchy.cxx:690
uint64_t fNamesVersion
Version used in DNS requests.
Definition: Hierarchy.h:161
void AddHistory(RecordFieldsMap *diff)
Add new entry to history.
Definition: Hierarchy.cxx:603
bool fPermanent
indicate that item is permanent and should be excluded from update
Definition: Hierarchy.h:169
bool CheckIfDoingHistory()
Return true if history activated for the node If necessary, history object will be initialized.
Definition: Hierarchy.cxx:649
uint64_t GetChildsVersion() const
Definition: Hierarchy.h:267
std::string ItemName()
Definition: Hierarchy.cxx:573
History fHist
special object with history data
Definition: Hierarchy.h:181
void ClearHistoryEntries()
Clear all entries in history, but not history object itself.
Definition: Hierarchy.cxx:624
HierarchyContainer * TopParent()
Definition: Hierarchy.cxx:202
bool ExtractHistoryStep(RecordFieldsMap *fields, unsigned step)
Extract values of specified history step.
Definition: Hierarchy.cxx:612
HierarchyContainer * CreateChildAt(const std::string &name, int indx)
Create child with specified name.
Definition: Hierarchy.cxx:555
bool fAutoTime
when enabled, by node change (not hierarchy) time attribute will be set
Definition: Hierarchy.h:167
bool fDisableChildsReading
when true, non of childs should be read
Definition: Hierarchy.h:176
bool fNodeChanged
indicate if something was changed in the node during update
Definition: Hierarchy.h:171
Mutex * fHierarchyMutex
mutex, which should be use for access to hierarchy and all its childs
Definition: Hierarchy.h:183
void SetModified(bool node, bool hierarchy, bool recursive=false)
Switch on node or hierarchy modified flags.
Definition: Hierarchy.cxx:441
uint64_t GetVersion() const
Definition: Hierarchy.h:265
bool DuplicateHierarchyFrom(HierarchyContainer *cont)
Duplicate hierarchy from provided container.
Definition: Hierarchy.cxx:453
void MarkReading(bool withchilds, bool readvalues, bool readchilds)
Mark reading flags.
Definition: Hierarchy.cxx:677
void EnableTimeRecording(bool withchilds=true)
Enable time recording for hierarchy element every time when item is changed.
Definition: Hierarchy.cxx:782
bool fDisableDataReading
when true, non of data (fields and history) need to be read in streamer
Definition: Hierarchy.h:175
void SetVersion(uint64_t v)
Definition: Hierarchy.h:244
virtual Object * CreateInstance(const std::string &name)
Method used to create new item to be placed as child of the object.
Definition: Hierarchy.h:238
bool UpdateHierarchyFrom(HierarchyContainer *cont)
Update hierarchy from provided container.
Definition: Hierarchy.cxx:476
bool Stream(iostream &s, unsigned kind=stream_Full, uint64_t v=0, unsigned hist_limit=0)
Definition: Hierarchy.cxx:224
bool fDisableReadingAsChild
when true, object will not be updated when provided as child
Definition: Hierarchy.h:177
uint64_t fNodeVersion
Version number of the node Any changes in the node will cause changes of the version.
Definition: Hierarchy.h:155
virtual void _ChildsChanged()
Method called when new childs are add or old are removed.
Definition: Hierarchy.h:240
virtual bool SaveTo(HStore &res, bool create_node=true)
Save hierarchy in json/xml form.
Definition: Hierarchy.cxx:403
bool fNamesChanged
indicate if DNS structure was changed (either childs or relevant dabc fields)
Definition: Hierarchy.h:172
void BuildObjectsHierarchy(const Reference &top)
Definition: Hierarchy.cxx:581
void MarkChangedItems(uint64_t tm=0)
Central method, which analyzes all possible changes in node (and its childs) If any changes found,...
Definition: Hierarchy.cxx:761
Buffer fBinData
binary data, assigned with element
Definition: Hierarchy.h:179
bool IsNodeChanged(bool withchilds=true)
Returns true if any node field was changed or removed/inserted If specified, all childs will be check...
Definition: Hierarchy.cxx:631
virtual const char * ClassName() const
Returns class name of the object instance.
Definition: Hierarchy.h:255
uint64_t StoreSize(unsigned kind=stream_Full, uint64_t v=0, unsigned hist_limit=0)
Definition: Hierarchy.cxx:216
Represents objects hierarchy of remote (or local) DABC process.
Definition: Hierarchy.h:285
bool IsBinItemChanged(const std::string &itemname, uint64_t hash, uint64_t last_version=0)
Return true if one could suppose that binary item is changed and binary data must be regenerated.
Definition: Hierarchy.cxx:1047
void DisableReading(bool withchlds=true)
Mark all elements that non of data will be read.
Definition: Hierarchy.cxx:1157
bool FillBinHeader(const std::string &itemname, dabc::Command &cmd, uint64_t mhash=0, const std::string &dflt_master_name="")
Fill binary header with item and master versions.
Definition: Hierarchy.cxx:1070
Hierarchy GetTop() const
Returns reference on the top element of the hierarchy.
Definition: Hierarchy.cxx:1139
void DisableReadingAsChild()
Disable reading of element when it appears as child in the structure.
Definition: Hierarchy.cxx:1162
Hierarchy FindMaster() const
Find master item It is used in ROOT to specify position of streamer info.
Definition: Hierarchy.cxx:1035
bool HasLocalHistory() const
Returns true if item records history local, no need to request any other sources.
Definition: Hierarchy.cxx:857
void EnableTimeRecording(bool withchilds=true)
Enable time recording for hierarchy element every time when item is changed.
Definition: Hierarchy.h:326
void MarkChangedItems(uint64_t tm=0)
If any field was modified, item will be marked with new version.
Definition: Hierarchy.h:330
uint64_t GetVersion() const
Returns actual version of hierarchy entry.
Definition: Hierarchy.h:349
bool Duplicate(const Hierarchy &src)
Duplicate hierarchy from the source.
Definition: Hierarchy.cxx:821
Hierarchy FindChild(const char *name)
Return child element from hierarchy.
Definition: Hierarchy.h:362
bool RemoveHChild(const std::string &name, bool allowslahes=false)
Delete H item, including all empty parent folders.
Definition: Hierarchy.cxx:1018
bool IsAnyFieldChanged() const
Definition: Hierarchy.h:306
Hierarchy CreateHChild(const std::string &name, bool allowslahes=false, bool sortorder=false)
Create child item in hierarchy with specified name If allowslahes enabled, instead of subfolders item...
Definition: Hierarchy.h:392
std::string ItemName() const
Name which is used as item name in hierarchy.
Definition: Hierarchy.cxx:1133
bool DettachFromParent()
Detach from parent object.
Definition: Hierarchy.cxx:1150
const RecordField & Field(const std::string &name) const
Definition: Hierarchy.h:304
Hierarchy GetHChild(const std::string &name, bool allowslahes=false, bool force=false, bool sortorder=false)
Return child, if necessary creates with full subfolder If force specified, missing childs and folders...
Definition: Hierarchy.cxx:944
HistoryIter MakeHistoryIter()
Produce history iterator.
Definition: Hierarchy.cxx:1236
Hierarchy CreateFolder(const std::string &name)
Create folder in hierarchy, one could use it to add new childs to it.
Definition: Hierarchy.h:399
bool RemoveEmptyFolders(const std::string &path)
Removes folder and its parents as long as no other childs are present.
Definition: Hierarchy.cxx:1117
void EnableReading(const Hierarchy &upto=nullptr)
Enable element and all its parents to read data.
Definition: Hierarchy.cxx:1167
void Create(const std::string &name, bool withmutex=false)
Create top-level object with specified name.
Definition: Hierarchy.cxx:934
void SetPermanent(bool on=true)
Mark item as permanent - it will not be touched when update from other places will be done.
Definition: Hierarchy.h:312
Mutex * GetHMutex() const
Definition: Hierarchy.h:292
std::string FindBinaryProducer(std::string &request_name, bool topmost=true)
Search for parent element, where binary_producer property is specified Returns name of binary produce...
Definition: Hierarchy.cxx:1098
bool UpdateFromBuffer(const dabc::Buffer &buf, HierarchyStreamKind kind=stream_Full)
Apply modification to hierarchy, using stored binary data
Definition: Hierarchy.cxx:912
bool HasActualRemoteHistory() const
Returns true if remote history is recorded and it is up-to-date.
Definition: Hierarchy.cxx:863
bool ReadFromBuffer(const dabc::Buffer &buf)
Read hierarchy from buffer.
Definition: Hierarchy.cxx:896
bool Update(Hierarchy &src)
Reconstruct complete hierarchy, setting node/structure modifications fields correctly.
Definition: Hierarchy.cxx:802
void BuildNew(Reference top)
Build objects hierarchy, referenced by top.
Definition: Hierarchy.cxx:795
void SetVersion(uint64_t v)
Change version of the item, only for advanced usage.
Definition: Hierarchy.h:352
void EnableHistory(unsigned length=100, bool withchilds=false)
Activate history production for selected element and its childs.
Definition: Hierarchy.cxx:837
uint64_t fRemoteReqVersion
last version, which was taken from remote
Definition: Hierarchy.h:65
RecordFieldsMap * TakeNext()
Definition: Hierarchy.h:89
bool fChildsEnabled
true if history recording also was enabled for childs
Definition: Hierarchy.h:62
bool Stream(iostream &s, uint64_t version, int hist_limit=-1)
Definition: Hierarchy.cxx:49
bool fEnabled
indicates if history recording is enabled
Definition: Hierarchy.h:61
bool fCrossBoundary
flag set when recover from binary, indicates that history is complete for specified version
Definition: Hierarchy.h:67
RecordFieldsMap * fPrev
map with previous set of attributes
Definition: Hierarchy.h:63
uint64_t StoreSize(uint64_t version, int hist_limit=-1)
Definition: Hierarchy.cxx:42
virtual ~HistoryContainer()
Definition: Hierarchy.h:80
uint64_t fLocalReqVersion
local version, when request was done
Definition: Hierarchy.h:66
RecordsQueue< HistoryItem > fArr
container with history items
Definition: Hierarchy.h:64
unsigned Size() const
Definition: Hierarchy.h:112
void Allocate(unsigned sz=0)
Definition: Hierarchy.h:102
bool DoHistory() const
Definition: Hierarchy.h:108
bool SaveTo(HStore &res)
Definition: Hierarchy.cxx:113
unsigned Capacity() const
Definition: Hierarchy.h:110
posix pthread mutex
Definition: threads.h:61
Base class for most of the DABC classes.
Definition: Object.h:116
@ flAutoDestroy
object will be automatically destroyed when no references exists, normally set in constructor,...
Definition: Object.h:167
@ flIsOwner
flag indicates default ownership for child objects
Definition: Object.h:164
Container for records fields.
Definition: Record.h:440
dabc::Buffer SaveToBuffer()
Definition: Record.cxx:1712
Template of queue with complex objects.
Definition: Queue.h:402
Reference on the arbitrary object
Definition: Reference.h:73
void SetObject(Object *obj, bool withmutex=true)
Direct set of object to reference.
Definition: Reference.cxx:37
Object * GetObject() const
Return pointer on the object.
Definition: Reference.h:129
Reference FindChild(const char *name) const
Searches for child in referenced object.
Definition: Reference.cxx:210
class to stream binary data
Definition: Record.h:41
Event manipulation API.
Definition: api.h:23
const char * prop_time
Definition: Hierarchy.cxx:37
const char * prop_realname
Definition: Hierarchy.cxx:30
const char * prop_error
Definition: Hierarchy.cxx:33
const char * prop_hash
Definition: Hierarchy.cxx:35
const char * prop_view
Definition: Hierarchy.cxx:39
const char * prop_masteritem
Definition: Hierarchy.cxx:31
const char * prop_kind
Definition: Hierarchy.cxx:29
const char * prop_more
Definition: Hierarchy.cxx:38
const char * prop_auth
Definition: Hierarchy.cxx:34
HierarchyStreamKind
Definition: Hierarchy.h:131
@ stream_NoDelete
Definition: Hierarchy.h:135
@ stream_Full
Definition: Hierarchy.h:134
@ stream_NamesList
Definition: Hierarchy.h:132
@ stream_Value
Definition: Hierarchy.h:133
const char * prop_producer
Definition: Hierarchy.cxx:32
const char * prop_history
Definition: Hierarchy.cxx:36
const char * prop_version
Definition: Hierarchy.cxx:28
uint64_t version
version number
Definition: Hierarchy.h:49
RecordFieldsMap * take()
Definition: Hierarchy.h:54
RecordFieldsMap * fields
all fields, which are preserved
Definition: Hierarchy.h:50
HistoryItem()=default