EPICS ARCHIVER V4
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups
RTree.h
1 // -*- c++ -*-
2 
3 #ifndef __RTREE_H__
4 #define __RTREE_H__
5 
6 // tools
7 // #include <"tools/AVLTree.h"
8 
9 // storage
10 #include "storage/FileAllocator.h"
11 #include "storage/Interval.h"
12 
13 // When using the ArchiveDataTool to convert about 500 channels,
14 // 230MB of data, 102K directory file into an index, these were the
15 // results:
16 // Via NFS:
17 // M=3
18 // real 0m30.871s
19 // user 0m0.790s
20 // sys 0m2.030s
21 //
22 // M=10
23 // real 0m23.944s
24 // user 0m0.830s
25 // sys 0m1.690s
26 //
27 // Local disk:
28 // No profiling, M=10
29 // real 0m17.148s
30 // user 0m0.700s
31 // sys 0m0.990s
32 //
33 // No profiling, M=50
34 // real 0m3.402s (!)
35 // user 0m1.290s
36 // sys 0m0.770s
37 //
38 // --> NFS is bad, small RTreeM values are bad.
39 
54 class RTree
55 {
56 public:
62  class Datablock
63  {
64  public:
65 
67  Datablock();
68 
70  virtual ~Datablock();
71 
73  virtual bool isValid() const = 0;
74 
76  const stdString &getDataFilename() const
77  {
78  LOG_ASSERT(isValid());
79  return data_filename;
80  }
81 
84  {
85  LOG_ASSERT(isValid());
86  return data_offset;
87  }
88 
90  virtual const Interval& getInterval() const = 0;
91 
114  virtual bool getNextChainedBlock() = 0;
115 
124  virtual bool getNextDatablock() = 0;
125 
129  virtual bool getPrevDatablock() = 0;
130 
131  protected:
132 
134  PROHIBIT_DEFAULT_COPY(Datablock);
135 
137  FileOffset data_offset;
138 
140  stdString data_filename;
141  };
142 
144  static const size_t anchor_size = 8;
145 
154  RTree(FileAllocator &fa, FileOffset anchor);
155 
159  void init(int M);
160 
164  void reattach();
165 
167  int getM() const
168  { return M; }
169 
174 
189  Datablock *search(const epicsTime &start) const;
190 
195  Datablock *getFirstDatablock() const;
196 
198  Datablock *getLastDatablock() const;
199 
201  class Node chooseLeaf(const Interval &range);
202 
218  bool insertDatablock(const Interval &range,
219  FileOffset data_offset,
220  const stdString &data_filename);
221 
226  size_t removeDatablock(const Interval &range,
227  FileOffset data_offset,
228  const stdString &data_filename);
229 
240  bool updateLastDatablock(const Interval &range,
241  FileOffset data_offset, stdString data_filename);
242 
244  void makeDot(const char *filename);
245 
253  bool selfTest(unsigned long &nodes, unsigned long &records);
254 
255 private:
256  PROHIBIT_DEFAULT_COPY(RTree);
257 
259  FileAllocator &fa;
260 
267  const FileOffset anchor;
268 
269  // FileOffset to the root = content of what's at anchor
270  FileOffset root_offset;
271 
272  int M;
273 
274  // mutable AVLTree<Node> node_cache;
275  mutable size_t cache_misses, cache_hits;
276 
277  void make_node_dot(FILE *dot, FILE *f, FileOffset node_offset);
278 
283  bool getFirstRecord(class Node &node, int &record_index) const;
284 
286  bool getLastRecord(class Node &node, int &record_index) const;
287 
292  void split_record(Node &node, int idx, const epicsTime &cut);
293 
300  void adjust_tree(Node &node, Node *new_node);
301 
306  size_t removeDatablock(Node &node, const Interval &range,
307  FileOffset data_offset, const stdString &data_filename);
308 
310  size_t removeDatablock(Node &node, int record_index, const Interval &range,
311  FileOffset data_offset, const stdString &data_filename);
312 
316  void remove_record(Node &node, int i);
317 
322  void condense_tree(Node &node);
323 
336  bool updateLast(const epicsTime &start,
337  const epicsTime &end, FileOffset ID);
338 };
339 
340 #endif
341 
Interval getInterval()
Return range covered by this RTree on read error.
virtual bool getPrevDatablock()=0
Absolutely no clue what this one could do.
static const size_t anchor_size
Definition: RTree.h:144
const stdString & getDataFilename() const
The file name referenced by this entry.
Definition: RTree.h:76
RTree(FileAllocator &fa, FileOffset anchor)
Attach RTree to FileAllocator.
bool selfTest(unsigned long &nodes, unsigned long &records)
Returns true if tree passes self test, otherwise prints errors.
bool insertDatablock(const Interval &range, FileOffset data_offset, const stdString &data_filename)
Create and insert a new Datablock.
Each RTree leaf points to Datablocks; they describe where the actual data that the index references r...
Definition: RTree.h:62
bool updateLastDatablock(const Interval &range, FileOffset data_offset, stdString data_filename)
Tries to update existing datablock.
size_t removeDatablock(const Interval &range, FileOffset data_offset, const stdString &data_filename)
Remove reference to given data block.
A time interval.
Definition: Interval.h:10
Datablock()
Constructor.
uint32_t FileOffset
FileOffset is used as a system independent type for, well, offsets into files.
Definition: StorageTypes.h:13
int getM() const
The &#39;M&#39; value, i.e.
Definition: RTree.h:167
Datablock * getFirstDatablock() const
void init(int M)
Initialize empty tree.
Implements a file-based RTree.
Definition: RTree.h:54
Datablock * getLastDatablock() const
FileOffset getDataOffset() const
The file offset of this entry.
Definition: RTree.h:83
Maintains memory blocks within a file.
Definition: FileAllocator.h:22
virtual const Interval & getInterval() const =0
Returns interval.
virtual bool getNextDatablock()=0
Get the data block for the next time interval from the RTree.
Datablock * search(const epicsTime &start) const
Locate data after start time.
virtual bool getNextChainedBlock()=0
Get a sub-block that&#39;s under the current block.
class Node chooseLeaf(const Interval &range)
void makeDot(const char *filename)
Create a graphviz &#39;dot&#39; file.
virtual ~Datablock()
Destructor.
void reattach()
Re-attach to an existing tree.
virtual bool isValid() const =0