1 // persist.h --
2 // This is part of Metakit, see http://www.equi4.com/metakit.html
3 
4 /** @file
5  * Definition of the core file management classes
6  */
7 
8 #pragma once
9 
10 /////////////////////////////////////////////////////////////////////////////
11 // Declarations in this file
12 
13 class c4_SaveContext; // wraps file commits
14 class c4_Persist; // persistent table storage
15 
16 class c4_Allocator; // not defined here
17 class c4_Column; // not defined here
18 class c4_Differ; // not defined here
19 class c4_FileMark; // not defined here
20 class c4_Strategy; // not defined here
21 class c4_HandlerSeq; // not defined here
22 
23 /////////////////////////////////////////////////////////////////////////////
24 
25 class c4_SaveContext
26 {
27     c4_Strategy &_strategy;
28     c4_Column *_walk;
29     c4_Differ *_differ;
30 
31     c4_Allocator *_space;
32     c4_Allocator *_cleanup;
33     c4_Allocator *_nextSpace;
34 
35     bool _preflight;
36     bool _fullScan;
37     int _mode;
38 
39     c4_DWordArray _newPositions;
40     int _nextPosIndex;
41 
42     t4_byte *_bufPtr;
43     t4_byte *_curr;
44     t4_byte *_limit;
45     t4_byte _buffer[512];
46 
47 public:
48     c4_SaveContext(c4_Strategy &strategy_, bool fullScan_, int mode_, c4_Differ
49                    *differ_, c4_Allocator *space_);
50     ~c4_SaveContext();
51 
52     void SaveIt(c4_HandlerSeq &root_, c4_Allocator **spacePtr_, c4_Bytes
53                 &rootWalk_);
54 
55     void StoreValue(t4_i32 v_);
56     bool CommitColumn(c4_Column &col_);
57     void CommitSequence(c4_HandlerSeq &seq_, bool selfDesc_);
58 
59     c4_Column *SetWalkBuffer(c4_Column *walk_);
60     bool IsFlipped() const;
61 
62     bool Serializing() const;
63     void AllocDump(const char *, bool = false);
64 
65 private:
66     void FlushBuffer();
67     void Write(const void *buf_, int len_);
68 };
69 
70 /////////////////////////////////////////////////////////////////////////////
71 
72 class c4_Persist
73 {
74     c4_Allocator *_space;
75     c4_Strategy &_strategy;
76     c4_HandlerSeq *_root;
77     c4_Differ *_differ;
78     c4_Bytes _rootWalk;
79     bool (c4_Persist:: *_fCommit)(bool);
80     int _mode;
81     bool _owned;
82 
83     // used for on-the-fly conversion of old-format datafiles
84     t4_byte *_oldBuf;
85     const t4_byte *_oldCurr;
86     const t4_byte *_oldLimit;
87     t4_i32 _oldSeek;
88 
89     int OldRead(t4_byte *buf_, int len_);
90 
91 public:
92     c4_Persist(c4_Strategy &, bool owned_, int mode_);
93     ~c4_Persist();
94 
95     c4_HandlerSeq &Root() const;
96     void SetRoot(c4_HandlerSeq *root_);
97     c4_Strategy &Strategy() const;
98 
99     bool AutoCommit(bool = true);
100     void DoAutoCommit();
101 
102     bool SetAside(c4_Storage &aside_);
103     c4_Storage *GetAside() const;
104 
105     bool Commit(bool full_);
106     bool Rollback(bool full_);
107 
108     bool LoadIt(c4_Column &walk_);
109     void LoadAll();
110 
111     t4_i32 LookupAside(int id_);
112     void ApplyAside(int id_, c4_Column &col_);
113 
114     void OccupySpace(t4_i32 pos_, t4_i32 len_);
115 
116     t4_i32 FetchOldValue();
117     void FetchOldLocation(c4_Column &col_);
118 
119     t4_i32 FreeBytes(t4_i32 *bytes_ = nullptr);
120 
121     static c4_HandlerSeq *Load(c4_Stream *);
122     static void Save(c4_Stream *, c4_HandlerSeq &root_);
123 };
124 
125 /////////////////////////////////////////////////////////////////////////////
126 
127