summaryrefslogtreecommitdiffstats
path: root/db/mork/src/morkRowSpace.h
blob: 0ef331bb8039266b3f43db5612e6b69bfd2a7e62 (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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-  */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef _MORKROWSPACE_
#define _MORKROWSPACE_ 1

#ifndef _MORK_
#include "mork.h"
#endif

#ifndef _MORKNODE_
#include "morkNode.h"
#endif

#ifndef _MORKSPACE_
#include "morkSpace.h"
#endif

#ifndef _MORKNODEMAP_
#include "morkNodeMap.h"
#endif

#ifndef _MORKROWMAP_
#include "morkRowMap.h"
#endif

#ifndef _MORKTABLE_
#include "morkTable.h"
#endif

#ifndef _MORKARRAY_
#include "morkArray.h"
#endif

#ifndef _MORKDEQUE_
#include "morkDeque.h"
#endif

//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

#define morkDerived_kRowSpace  /*i*/ 0x7253 /* ascii 'rS' */

#define morkRowSpace_kStartRowMapSlotCount 11

#define morkRowSpace_kMaxIndexCount 8 /* no more indexes than this */
#define morkRowSpace_kPrimeCacheSize 17 /* should be prime number */

class morkAtomRowMap;

/*| morkRowSpace:
|*/
class morkRowSpace : public morkSpace { // 

// public: // slots inherited from morkSpace (meant to inform only)
  // nsIMdbHeap*    mNode_Heap;

  // mork_base      mNode_Base;     // must equal morkBase_kNode
  // mork_derived   mNode_Derived;  // depends on specific node subclass
  
  // mork_access    mNode_Access;   // kOpen, kClosing, kShut, or kDead
  // mork_usage     mNode_Usage;    // kHeap, kStack, kMember, kGlobal, kNone
  // mork_able      mNode_Mutable;  // can this node be modified?
  // mork_load      mNode_Load;     // is this node clean or dirty?
  
  // mork_uses      mNode_Uses;     // refcount for strong refs
  // mork_refs      mNode_Refs;     // refcount for strong refs + weak refs
  
  // morkStore*  mSpace_Store; // weak ref to containing store
  
  // mork_bool   mSpace_DoAutoIDs;    // whether db should assign member IDs
  // mork_bool   mSpace_HaveDoneAutoIDs; // whether actually auto assigned IDs
  // mork_u1     mSpace_Pad[ 2 ];    // pad to u4 alignment

public: // state is public because the entire Mork system is private

  nsIMdbHeap*  mRowSpace_SlotHeap;

#ifdef MORK_ENABLE_PROBE_MAPS
  morkRowProbeMap   mRowSpace_Rows;   // hash table of morkRow instances
#else /*MORK_ENABLE_PROBE_MAPS*/
  morkRowMap   mRowSpace_Rows;   // hash table of morkRow instances
#endif /*MORK_ENABLE_PROBE_MAPS*/
  morkTableMap mRowSpace_Tables; // all the tables in this row scope

  mork_tid     mRowSpace_NextTableId;  // for auto-assigning table IDs
  mork_rid     mRowSpace_NextRowId;    // for auto-assigning row IDs
  
  mork_count   mRowSpace_IndexCount; // if nonzero, row indexes exist
    
  // every nonzero slot in IndexCache is a strong ref to a morkAtomRowMap:
  morkAtomRowMap* mRowSpace_IndexCache[ morkRowSpace_kPrimeCacheSize ];

  morkDeque    mRowSpace_TablesByPriority[ morkPriority_kCount ];

public: // more specific dirty methods for row space:
  void SetRowSpaceDirty() { this->SetNodeDirty(); }
  void SetRowSpaceClean() { this->SetNodeClean(); }
  
  mork_bool IsRowSpaceClean() const { return this->IsNodeClean(); }
  mork_bool IsRowSpaceDirty() const { return this->IsNodeDirty(); }

// { ===== begin morkNode interface =====
public: // morkNode virtual methods
  virtual void CloseMorkNode(morkEnv* ev) override; // CloseRowSpace() only if open
  virtual ~morkRowSpace(); // assert that CloseRowSpace() executed earlier
  
public: // morkMap construction & destruction
  morkRowSpace(morkEnv* ev, const morkUsage& inUsage, mork_scope inScope,
    morkStore* ioStore, nsIMdbHeap* ioNodeHeap, nsIMdbHeap* ioSlotHeap);
  void CloseRowSpace(morkEnv* ev); // called by CloseMorkNode();

public: // dynamic type identification
  mork_bool IsRowSpace() const
  { return IsNode() && mNode_Derived == morkDerived_kRowSpace; }
// } ===== end morkNode methods =====

public: // typing
  static void NonRowSpaceTypeError(morkEnv* ev);
  static void ZeroScopeError(morkEnv* ev);
  static void ZeroKindError(morkEnv* ev);
  static void ZeroTidError(morkEnv* ev);
  static void MinusOneRidError(morkEnv* ev);

  //static void ExpectAutoIdOnlyError(morkEnv* ev);
  //static void ExpectAutoIdNeverError(morkEnv* ev);

public: // other space methods

  mork_num CutAllRows(morkEnv* ev, morkPool* ioPool);
  // CutAllRows() puts all rows and cells back into the pool.
  
  morkTable* NewTable(morkEnv* ev, mork_kind inTableKind,
    mdb_bool inMustBeUnique, const mdbOid* inOptionalMetaRowOid);
  
  morkTable* NewTableWithTid(morkEnv* ev, mork_tid inTid,
    mork_kind inTableKind, const mdbOid* inOptionalMetaRowOid);
  
  morkTable* FindTableByKind(morkEnv* ev, mork_kind inTableKind);
  morkTable* FindTableByTid(morkEnv* ev, mork_tid inTid)
  { return mRowSpace_Tables.GetTable(ev, inTid); }

  mork_tid MakeNewTableId(morkEnv* ev);
  mork_rid MakeNewRowId(morkEnv* ev);

  // morkRow* FindRowByRid(morkEnv* ev, mork_rid inRid)
  // { return (morkRow*) mRowSpace_Rows.GetRow(ev, inRid); }

  morkRow* NewRowWithOid(morkEnv* ev, const mdbOid* inOid);
  morkRow* NewRow(morkEnv* ev);

  morkRow* FindRow(morkEnv* ev, mork_column inColumn, const mdbYarn* inYarn);

  morkAtomRowMap* ForceMap(morkEnv* ev, mork_column inColumn);
  morkAtomRowMap* FindMap(morkEnv* ev, mork_column inColumn);

protected: // internal utilities
  morkAtomRowMap* make_index(morkEnv* ev, mork_column inColumn);

public: // typesafe refcounting inlines calling inherited morkNode methods
  static void SlotWeakRowSpace(morkRowSpace* me,
    morkEnv* ev, morkRowSpace** ioSlot)
  { morkNode::SlotWeakNode((morkNode*) me, ev, (morkNode**) ioSlot); }
  
  static void SlotStrongRowSpace(morkRowSpace* me,
    morkEnv* ev, morkRowSpace** ioSlot)
  { morkNode::SlotStrongNode((morkNode*) me, ev, (morkNode**) ioSlot); }
};

//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

#define morkDerived_kRowSpaceMap  /*i*/ 0x725A /* ascii 'rZ' */

/*| morkRowSpaceMap: maps mork_scope -> morkRowSpace
|*/
class morkRowSpaceMap : public morkNodeMap { // for mapping tokens to tables

public:

  virtual ~morkRowSpaceMap();
  morkRowSpaceMap(morkEnv* ev, const morkUsage& inUsage,
    nsIMdbHeap* ioHeap, nsIMdbHeap* ioSlotHeap);

public: // other map methods

  mork_bool  AddRowSpace(morkEnv* ev, morkRowSpace* ioRowSpace)
  { return this->AddNode(ev, ioRowSpace->SpaceScope(), ioRowSpace); }
  // the AddRowSpace() boolean return equals ev->Good().

  mork_bool  CutRowSpace(morkEnv* ev, mork_scope inScope)
  { return this->CutNode(ev, inScope); }
  // The CutRowSpace() boolean return indicates whether removal happened. 
  
  morkRowSpace*  GetRowSpace(morkEnv* ev, mork_scope inScope)
  { return (morkRowSpace*) this->GetNode(ev, inScope); }
  // Note the returned space does NOT have an increase in refcount for this.

  mork_num CutAllRowSpaces(morkEnv* ev)
  { return this->CutAllNodes(ev); }
  // CutAllRowSpaces() releases all the referenced table values.
};

class morkRowSpaceMapIter: public morkMapIter{ // typesafe wrapper class

public:
  morkRowSpaceMapIter(morkEnv* ev, morkRowSpaceMap* ioMap)
  : morkMapIter(ev, ioMap) { }
 
  morkRowSpaceMapIter( ) : morkMapIter()  { }
  void InitRowSpaceMapIter(morkEnv* ev, morkRowSpaceMap* ioMap)
  { this->InitMapIter(ev, ioMap); }
   
  mork_change*
  FirstRowSpace(morkEnv* ev, mork_scope* outScope, morkRowSpace** outRowSpace)
  { return this->First(ev, outScope, outRowSpace); }
  
  mork_change*
  NextRowSpace(morkEnv* ev, mork_scope* outScope, morkRowSpace** outRowSpace)
  { return this->Next(ev, outScope, outRowSpace); }
  
  mork_change*
  HereRowSpace(morkEnv* ev, mork_scope* outScope, morkRowSpace** outRowSpace)
  { return this->Here(ev, outScope, outRowSpace); }
  
  mork_change*
  CutHereRowSpace(morkEnv* ev, mork_scope* outScope, morkRowSpace** outRowSpace)
  { return this->CutHere(ev, outScope, outRowSpace); }
};

//3456789_123456789_123456789_123456789_123456789_123456789_123456789_123456789

#endif /* _MORKROWSPACE_ */