summaryrefslogtreecommitdiffstats
path: root/akregator/src/mk4storage/metakit/src/persist.h
blob: 61ef261ee25bf11de0a3f32cf9f641d30c930d13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// persist.h --
// $Id$
// This is part of Metakit, see http://www.equi4.com/metakit/

/** @file
 * Definition of the core file management classes
 */

#ifndef __PERSIST_H__
#define __PERSIST_H__

/////////////////////////////////////////////////////////////////////////////
// Declarations in this file

  class c4_SaveContext;       // wraps file commits
  class c4_Persist;         // persistent table storage

  class c4_Allocator;         // not defined here
  class c4_Column;          // not defined here
  class c4_Differ;          // not defined here
  class c4_FileMark;          // not defined here
  class c4_Strategy;          // not defined here
  class c4_HandlerSeq;        // not defined here

/////////////////////////////////////////////////////////////////////////////

class c4_SaveContext
{ 
  c4_Strategy& _strategy;
  c4_Column* _walk;
  c4_Differ* _differ;

  c4_Allocator* _space;
  c4_Allocator* _cleanup;
  c4_Allocator* _nextSpace;

  bool _preflight;
  bool _fullScan;
  int _mode;
  
  c4_DWordArray _newPositions;
  int _nextPosIndex;

  t4_byte* _bufPtr;
  t4_byte* _curr;
  t4_byte* _limit;
  t4_byte _buffer [512];

public:           
  c4_SaveContext (c4_Strategy& strategy_, bool fullScan_, int mode_,
            c4_Differ* differ_, c4_Allocator* space_);
  ~c4_SaveContext ();
  
  void SaveIt(c4_HandlerSeq& root_, c4_Allocator** spacePtr_,
            c4_Bytes& rootWalk_);

  void StoreValue(t4_i32 v_);
  bool CommitColumn(c4_Column& col_);
  void CommitSequence(c4_HandlerSeq& seq_, bool selfDesc_);

  c4_Column* SetWalkBuffer(c4_Column* walk_);
  bool IsFlipped() const;

  bool Serializing() const;
  void AllocDump(const char*, bool =false);
  
private:
  void FlushBuffer();
  void Write(const void* buf_, int len_);
};

/////////////////////////////////////////////////////////////////////////////

class c4_Persist
{
  c4_Allocator* _space;
  c4_Strategy& _strategy;
  c4_HandlerSeq* _root;
  c4_Differ* _differ;
  c4_Bytes _rootWalk;
  bool (c4_Persist::*_fCommit) (bool);
  int _mode;
  bool _owned;

  // used for on-the-fly conversion of old-format datafiles
  t4_byte* _oldBuf;
  const t4_byte* _oldCurr;
  const t4_byte* _oldLimit;
  t4_i32 _oldSeek;

  int OldRead(t4_byte* buf_, int len_);

public:
  c4_Persist (c4_Strategy&, bool owned_, int mode_);
  ~c4_Persist ();
  
  c4_HandlerSeq& Root() const;
  void SetRoot(c4_HandlerSeq* root_);
  c4_Strategy& Strategy() const;

  bool AutoCommit(bool =true);
  void DoAutoCommit();

  bool SetAside(c4_Storage& aside_);
  c4_Storage* GetAside() const;

  bool Commit(bool full_);
  bool Rollback(bool full_);

  bool LoadIt(c4_Column& walk_);
  void LoadAll();

  t4_i32 LookupAside(int id_);
  void ApplyAside(int id_, c4_Column& col_);

  void OccupySpace(t4_i32 pos_, t4_i32 len_);

  t4_i32 FetchOldValue();
  void FetchOldLocation(c4_Column& col_);
  
  static c4_HandlerSeq* Load(c4_Stream*);
  static void Save(c4_Stream*, c4_HandlerSeq& root_);
};

/////////////////////////////////////////////////////////////////////////////

#endif