1 #ifndef _CEGOBTREEMANAGER_H_INCLUDED_
2 #define _CEGOBTREEMANAGER_H_INCLUDED_
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 // CegoBTreeManager.h
6 // ------------------
7 // Cego BTree+ manager class definition
8 //
9 // Design and Implementation by Bjoern Lemke
10 //
11 // (C)opyright 2000-2019 Bjoern Lemke
12 //
13 // INTERFACE MODULE
14 //
15 // Class: CegoBTreeManager
16 //
17 // Description: This class implements the btree algorithm to be used for table index objects.
18 //
19 // Status: CLEAN
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22 
23 // LFC INCLUDES
24 #include <lfcbase/Chain.h>
25 #include <lfcbase/ListT.h>
26 #include <lfcbase/AVLTreeT.h>
27 
28 // CEGO INCLUDES
29 #include "CegoDefs.h"
30 #include "CegoDataType.h"
31 #include "CegoField.h"
32 #include "CegoDataPointer.h"
33 #include "CegoSystemObject.h"
34 #include "CegoObjectManager.h"
35 #include "CegoBTreeNode.h"
36 #include "CegoBTreeValue.h"
37 
38 class CegoBTreeManager {
39 
40 public:
41 
42     CegoBTreeManager(CegoObjectManager* pObjMng, CegoBTreeObject *pBTO);
43     ~CegoBTreeManager();
44 
45     void createCache();
46 
47     void commit(CegoDataPointer& sysEntry);
48     void rollback();
49 
50     void insertBTreeWithCommit(const CegoDataPointer& dp,
51 		     const CegoBTreeValue &iv,
52 		     unsigned long long tid);
53 
54     void deleteBTree(const CegoBTreeValue &iv,
55 		     const CegoDataPointer& dp,
56 		     unsigned long long tid);
57 
58     void insertBTree(const CegoDataPointer& dp,
59 		      const CegoBTreeValue &iv,
60 		      unsigned long long tid);
61 
62     void deleteBTree(const CegoDataPointer& sysEntry,
63 		     const CegoBTreeValue &iv,
64 		     const CegoDataPointer& dp,
65 		     unsigned long long tid);
66 
67     bool verifyBTree();
68 
69     void dumpBTree();
70 
71     int getNumPages();
72     int freeBTree();
73 
74     int traceBTree();
75 
76 private:
77 
78     class BTreeCache {
79 
80     public:
81 
82 	BTreeCache();
83 	~BTreeCache();
84 
85 	CegoBufferPage* newCachePage(CegoBufferPage& bp, const CegoBufferPage::PageType pageType, bool copyPage);
86 	CegoBufferPage* getCachePage(PageIdType pageId);
87 
88 	CegoBufferPage* getFirst();
89 	CegoBufferPage* getNext();
90 
91     private:
92 
93 	class CacheEntry {
94 
95 	public:
96 
CacheEntry()97 	    CacheEntry()
98 	    {
99 	    }
100 
101 
CacheEntry(PageIdType pageId)102 	    CacheEntry(PageIdType pageId)
103 	    {
104 		_pageId = pageId;
105 	    }
106 
CacheEntry(CegoBufferPage * pBP)107 	    CacheEntry(CegoBufferPage* pBP)
108 	    {
109 		_pBP = pBP;
110 		_pageId = pBP->getPageId();
111 	    }
112 
~CacheEntry()113 	    ~CacheEntry()
114 	    {
115 	    }
116 
getPage()117 	    CegoBufferPage* getPage() const
118 	    {
119 		return _pBP;
120 	    }
121 
setPage(CegoBufferPage * pBP)122 	    void setPage(CegoBufferPage* pBP)
123 	    {
124 		_pBP = pBP;
125 		_pageId = pBP->getPageId();
126 	    }
127 
128 	    CacheEntry& operator = ( const CacheEntry& e)
129 	    {
130 		_pBP = e._pBP;
131 		_pageId = e._pageId;
132 		return (*this);
133 	    }
134 
135 	    bool operator == ( const CacheEntry& e)
136 	    {
137 		if ( _pageId == e._pageId )
138 		    return true;
139 		return false;
140 	    }
141 
142 	    bool operator < ( const CacheEntry& e)
143 	    {
144 		if ( _pageId < e._pageId )
145 		    return true;
146 		return false;
147 	    }
148 
149 	    bool operator > ( const CacheEntry& e)
150 	    {
151 		if ( _pageId > e._pageId )
152 		    return true;
153 		return false;
154 	    }
155 
156 	    friend ostream& operator << (ostream& s, const CacheEntry& e)
157 	    {
158 		s << "(" << e._pageId << ")";
159 		return s;
160 	    }
161 
162 	private:
163 
164 	    PageIdType _pageId;
165 	    CegoBufferPage* _pBP;
166 
167 	};
168 
169 	AVLTreeT<CacheEntry> _cache;
170     };
171 
172 
173     bool checkDuplicate(const CegoBTreeValue &iv, const CegoBTreeNode &leaf, unsigned long long tid);
174 
175 
176     CegoBufferPage* allocPage(CegoBufferPage::PageType type);
177     CegoBufferPage* getPage(PageIdType pageId);
178     void putPage(CegoBufferPage* pBP);
179 
180     bool verifyNode(PageIdType pageId);
181     void dumpNode(int level, PageIdType pageId);
182 
183     int countNodePages(PageIdType pageId, PageIdType& firstLeafPageId, bool& isFirst);
184     int countLeafPages(PageIdType firstLeafPageId);
185 
186     int freeNodePages(PageIdType pageId, PageIdType& firstLeafPageId, bool& isFirst);
187     int freeLeafPages(PageIdType firstLeafPageId);
188 
189     int traceNodePages(PageIdType pageId, PageIdType& firstLeafPageId, bool& isFirst);
190     int traceLeafPages(PageIdType firstLeafPageId);
191 
192     CegoBTreeObject* _pBTO;
193     CegoObjectManager* _pObjMng;
194     CegoDatabaseManager* _pDBMng;
195 
196     CegoObject::ObjectType _btreeType;
197     ListT<CegoField> _btreeSchema;
198     Chain _btreeName;
199     int _keyLen;
200     int _tabSetId;
201 
202     // CegoBufferPage _rootBP;
203     // bool _rootFixed;
204 
205     BTreeCache* _pCache;
206 
207     unsigned long _modId;
208 };
209 
210 #endif
211