1// persist.h --
2// $Id: persist.h 1230 2007-03-09 15:58:53Z jcw $
3// This is part of Metakit, see http://www.equi4.com/metakit.html
4
5/** @file
6 * Definition of the core file management classes
7 */
8
9#ifndef __PERSIST_H__
10#define __PERSIST_H__
11
12/////////////////////////////////////////////////////////////////////////////
13// Declarations in this file
14
15class c4_SaveContext; // wraps file commits
16class c4_Persist; // persistent table storage
17
18class c4_Allocator; // not defined here
19class c4_Column; // not defined here
20class c4_Differ; // not defined here
21class c4_FileMark; // not defined here
22class c4_Strategy; // not defined here
23class c4_HandlerSeq; // not defined here
24
25/////////////////////////////////////////////////////////////////////////////
26
27class c4_SaveContext {
28    c4_Strategy &_strategy;
29    c4_Column *_walk;
30    c4_Differ *_differ;
31
32    c4_Allocator *_space;
33    c4_Allocator *_cleanup;
34    c4_Allocator *_nextSpace;
35
36    bool _preflight;
37    bool _fullScan;
38    int _mode;
39
40    c4_DWordArray _newPositions;
41    int _nextPosIndex;
42
43    t4_byte *_bufPtr;
44    t4_byte *_curr;
45    t4_byte *_limit;
46    t4_byte _buffer[512];
47
48  public:
49    c4_SaveContext(c4_Strategy &strategy_, bool fullScan_, int mode_, c4_Differ
50      *differ_, c4_Allocator *space_);
51    ~c4_SaveContext();
52
53    void SaveIt(c4_HandlerSeq &root_, c4_Allocator **spacePtr_, c4_Bytes
54      &rootWalk_);
55
56    void StoreValue(t4_i32 v_);
57    bool CommitColumn(c4_Column &col_);
58    void CommitSequence(c4_HandlerSeq &seq_, bool selfDesc_);
59
60    c4_Column *SetWalkBuffer(c4_Column *walk_);
61    bool IsFlipped()const;
62
63    bool Serializing()const;
64    void AllocDump(const char *, bool = false);
65
66  private:
67    void FlushBuffer();
68    void Write(const void *buf_, int len_);
69};
70
71/////////////////////////////////////////////////////////////////////////////
72
73class c4_Persist {
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_ = 0);
120
121    static c4_HandlerSeq *Load(c4_Stream*);
122    static void Save(c4_Stream *, c4_HandlerSeq &root_);
123};
124
125/////////////////////////////////////////////////////////////////////////////
126
127#endif
128