46 if (fFile.isOpened()) {
47 EOUT(
"Cannot change base path when current file is opened");
57 if (!CloseFile())
return false;
59 std::string path = fBasePath;
60 if ((path.length()>0) && (path[path.length()-1]!=
'/')) path.append(
"/");
72 if (!fIO->mkdir(path.c_str())) {
73 EOUT(
"Cannot create path %s for hierarchy storage", path.c_str());
77 DOUT0(
"HierarchyStore:: CREATE %s", path.c_str());
84 if (!fFile.OpenWriting(path.c_str())) {
85 EOUT(
"Cannot open file %s for hierarchy storage", path.c_str());
89 return WriteDiff(buf);
94 if (!fFile.isWriting())
return false;
100 if (!fFile.WriteBufHeader(fullsz, buf.
GetTypeId())) {
101 EOUT(
"Cannot write buffer header");
107 EOUT(
"Cannot write buffer segment");
116 if (!fFile.isOpened())
return true;
126 if (fDoStore || fDoFlush)
return true;
133 if (fLastStoreTm.null()) {
139 if (now - fLastStoreTm > store_period) {
142 if (now - fLastFlushTm >= flush_period) {
148 return fDoStore || fDoFlush;
158 DOUT0(
"HierarchyStore:: EXTRACT prev %u now %u chlds %u size %u",
159 (
unsigned) fLastVersion, (
unsigned) h.
GetVersion(),
160 (
unsigned) h()->GetChildsVersion(), (
unsigned) fStoreBuf.GetTotalSize());
171 if (fDoFlush || fDoStore) {
174 h.
SetField(
"storetm", fLastStoreTm);
184 if (!fStoreBuf.null()) WriteDiff(fStoreBuf);
190 if (!fFlushBuf.null()) StartFile(fFlushBuf);
219 if ((fBasePath.length()>0) && (fBasePath[fBasePath.length()-1] !=
'/')) fBasePath.append(
"/");
224 std::string res = fpath;
225 if ((res.length()>0) && (res[res.length()-1] !=
'/')) res.append(
"/");
230 res +=
dabc::format(
"%s/%s.dabc", strdate.c_str(), strtime.c_str());
237 std::string mask = dirname +
"*.dabc";
239 Reference files = fIO->fmatch(mask.c_str(),
true);
241 for (
unsigned n=0;n<files.
NumChilds();n++) {
243 fname.erase(0, dirname.length());
245 size_t pos = fname.find(
".dabc");
246 if (pos == std::string::npos) {
247 EOUT(
"Wrong time file name %s", fname.c_str());
254 EOUT(
"Wrong time file name %s", fname.c_str());
267 if (h.
null() || dirname.empty())
return false;
269 DOUT0(
"SCAN %s", dirname.c_str());
271 std::string mask = dirname +
"*";
273 Reference dirs = fIO->fmatch(mask.c_str(),
false);
275 bool isanysubdir(
false), isanydatedir(
false);
277 std::vector<uint64_t> files;
279 for (
unsigned n=0;n<dirs.
NumChilds();n++) {
282 std::string itemname = fullsubname;
283 itemname.erase(0, dirname.length());
287 DOUT0(
"FOUND %s PART %s", fullsubname.c_str(), itemname.c_str());
291 if (!ScanFiles(fullsubname +
"/", dt, files))
return false;
295 if (!ScanTreeDir(subh, fullsubname +
"/"))
return false;
301 if (isanysubdir && isanydatedir) {
302 EOUT(
"DIR %s Cannot combine subdirs with date dirs!!!", dirname.c_str());
306 if (isanydatedir && (files.size()>0)) {
308 std::sort(files.begin(), files.end());
312 DOUT0(
"DIR: %s mintm: %s maxtm: %s files %u", dirname.c_str(), mindt.AsJSString().c_str(), maxdt.
AsJSString().c_str(), files.size());
330 return ScanTreeDir(fTree, fBasePath);
338 if (f.
eof())
return buf;
340 uint64_t size(0), typ(0);
344 if (buf.
null())
return buf;
356 if (tree.
null())
return false;
358 DOUT0(
"Produce structure for item %s", tree.
ItemName().c_str());
361 for (
unsigned n=0;n<tree.
NumChilds();n++) {
366 if (entry.find(tree_chld.
ItemName())!=0)
continue;
369 if (!ProduceStructure(tree_chld, from_date, till_date, entry, tgt_chld))
return false;
374 std::string fpath = tree.
Field(
"dabc:path").
AsStr();
379 if ((files==0) || (nfiles<=0))
return false;
383 if (!from_date.
null()) {
384 for (
int n=0;n<nfiles;n++)
385 if (files[n] <= from_date.
AsJSDate())
389 std::string fname = MakeFileName(fpath, dt);
391 DOUT0(
"Opening file %s", fname.c_str());
397 if (buf.
null())
return false;
401 if (!entry.empty()) {
406 EOUT(
"Cannot locate entry %s in the storage", entry.c_str());
431 if (!ScanTree())
return false;
434 if (fIO==0)
return false;
439 return ProduceStructure(fTree, dt, 0,
"", tgt);
447 if (!ScanTree())
return res;
450 if (fIO==0)
return res;
453 if (!ProduceStructure(fTree, from, till, entry, h))
return res;
457 EOUT(
"Cannot locate entry %s in the storage", entry.c_str());
void SetIO(FileInterface *_io, bool _ioowner=false)
Generic file storage for DABC buffers.
bool ReadBufPayload(void *ptr, uint64_t sz)
bool OpenReading(const char *fname)
bool ReadBufHeader(uint64_t *size, uint64_t *typ=0)
Reference on memory from memory pool.
unsigned NumSegments() const
Returns number of segment in buffer.
unsigned SegmentSize(unsigned n=0) const
Returns size on the segment, no any boundary checks.
void SetTotalSize(BufferSize_t len)
Set total length of the buffer to specified value Size cannot be bigger than original size of the buf...
unsigned GetTypeId() const
BufferSize_t GetTotalSize() const
Return total size of all buffer segments.
static Buffer CreateBuffer(BufferSize_t sz)
This static method create independent buffer for any other memory pools Therefore it can be used in s...
void SetTypeId(unsigned tid)
void * SegmentPtr(unsigned n=0) const
Returns pointer on the segment, no any boundary checks.
Class for holding GMT time with precision of nanoseconds.
bool SetOnlyTime(const char *sbuf)
Set only time part of DateTime.
std::string OnlyTimeAsString(const char *separ=nullptr, bool localtime=false) const
Fills only time as string.
std::string AsJSString(int ndecimal=3) const
convert string into sec.frac format, can be interpret directly in JavaScript ISO 8601 standard is use...
void SetJSDate(uint64_t jsdate)
Set value in form of JS date - milliseconds since 1.1.1970.
uint64_t AsJSDate() const
Return date and time in JS format - number of millisecond since 1.1.1970.
bool SetOnlyDate(const char *sbuf)
Set only date part from the string.
std::string OnlyDateAsString(const char *separ=nullptr, bool localtime=false) const
Fills only date as string.
Defines and implements basic POSIX file interface.
std::string MakeFileName(const std::string &fpath, const DateTime &dt)
bool ScanTreeDir(dabc::Hierarchy &h, const std::string &dirname)
! scanned files tree
void SetBasePath(const std::string &path)
Set top directory for all recorded data.
Hierarchy GetSerie(const std::string &entry, const DateTime &from, const DateTime &till)
Get entry with history for specified time interval.
bool ScanFiles(const std::string &dirname, const DateTime &onlydate, std::vector< uint64_t > &vect)
virtual ~HierarchyReading()
bool GetStrucutre(Hierarchy &h, const DateTime &dt=0)
Get full structure at given point of time.
bool ProduceStructure(Hierarchy &tree, const DateTime &from, const DateTime &till, const std::string &entry, Hierarchy &tgt)
bool ScanTree()
Scan only directories, do not open any files.
dabc::Buffer ReadBuffer(dabc::BinaryFile &f)
virtual ~HierarchyStore()
bool WriteDiff(dabc::Buffer buf)
bool CheckForNextStore(DateTime &now, double store_period, double flush_period)
Returns true if any new store action should be performed.
bool WriteExtractedData()
Write extracted data to files, performed outside hierarchy mutex.
bool SetBasePath(const std::string &path)
Set base path for data storage, can only be changed when all files are closed.
bool StartFile(dabc::Buffer buf)
bool ExtractData(dabc::Hierarchy &h)
Extract data which is can be stored, must be called under hierarchy mutex.
Represents objects hierarchy of remote (or local) DABC process.
void DisableReading(bool withchlds=true)
Mark all elements that non of data will be read.
Hierarchy GetTop() const
Returns reference on the top element of the hierarchy.
void MarkChangedItems(uint64_t tm=0)
If any field was modified, item will be marked with new version.
uint64_t GetVersion() const
Returns actual version of hierarchy entry.
Hierarchy FindChild(const char *name)
Return child element from hierarchy.
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...
std::string ItemName() const
Name which is used as item name in hierarchy.
bool DettachFromParent()
Detach from parent object.
const RecordField & Field(const std::string &name) const
dabc::Buffer SaveToBuffer(unsigned kind=stream_Full, uint64_t version=0, unsigned hlimit=0)
Save hierarchy in binary form, relative to specified version.
void EnableReading(const Hierarchy &upto=nullptr)
Enable element and all its parents to read data.
void Create(const std::string &name, bool withmutex=false)
Create top-level object with specified name.
bool UpdateFromBuffer(const dabc::Buffer &buf, HierarchyStreamKind kind=stream_Full)
Apply modification to hierarchy, using stored binary data
bool ReadFromBuffer(const dabc::Buffer &buf)
Read hierarchy from buffer.
void EnableHistory(unsigned length=100, bool withchilds=false)
Activate history production for selected element and its childs.
int64_t GetArraySize() const
std::string AsStr(const std::string &dflt="") const
uint64_t * GetUIntArr() const
bool HasField(const std::string &name) const
bool SetField(const std::string &name, const RecordField &v)
Reference on the arbitrary object
void Release()
Releases reference on the object.
const char * GetName() const
Return name of referenced object, if object not assigned, returns "---".
unsigned NumChilds() const
Return number of childs in referenced object.
bool null() const
Returns true if reference contains nullptr.
Reference GetChild(unsigned n) const
Return reference on child n.
std::string format(const char *fmt,...)