1 /* Copyright (C) 2014 InfiniDB, Inc.
2 
3    This program is free software; you can redistribute it and/or
4    modify it under the terms of the GNU General Public License
5    as published by the Free Software Foundation; version 2 of
6    the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16    MA 02110-1301, USA. */
17 
18 /******************************************************************************************
19 * $Id: we_indextree.h 4450 2013-01-21 14:13:24Z rdempsey $
20 *
21 ******************************************************************************************/
22 /** @file */
23 
24 #ifndef _WE_INDEXTREE_H_
25 #define _WE_INDEXTREE_H_
26 
27 #include <stdlib.h>
28 
29 #include <we_dbfileop.h>
30 #include <we_index.h>
31 #include <we_freemgr.h>
32 #include <we_indexlist.h>
33 
34 #if defined(_MSC_VER) && defined(WRITEENGINEINDEXTREE_DLLEXPORT)
35 #define EXPORT __declspec(dllexport)
36 #else
37 #define EXPORT
38 #endif
39 
40 /** Namespace WriteEngine */
41 namespace WriteEngine
42 {
43 
44 /** Class ColumnOp */
45 class IndexTree : public DbFileOp
46 {
47 public:
48     /**
49      * @brief Constructor
50      */
51     EXPORT IndexTree();
52 
53     /**
54      * @brief Default Destructor
55      */
56     EXPORT ~IndexTree();
57 
58     /**
59      * @brief A wrapper for the call to free manager
60      */
61     EXPORT const int      assignSegment(  int segmentType, IdxEmptyListEntry* assignPtr, int no );
62 
63     /**
64      * @brief Build empty index tree part
65      */
66     EXPORT const int      buildEmptyTreePart( const uint64_t key, const int width, const RID rid, const int startBitTestNo, const int offset = 0 );
67 
68     /**
69      * @brief Build empty index branch
70      */
71     EXPORT const int      buildEmptyIndexTreeBranch( const uint64_t key, const int width, const RID rid, const int rootTestbitVal );
72 
73     /**
74      * @brief Build exist index branch
75      */
76     EXPORT const int      buildExistIndexTreeBranch( const uint64_t key, const int width, const RID rid, const int rootTestbitVal, IdxBitmapPointerEntry bitmapEntry );
77 
78     /**
79      * @brief Calculate bit test array
80      */
81     EXPORT const int      calculateBittestArray();
82 
83     /**
84      * @brief Close index
85      */
86     EXPORT void           closeIndex();
87 
88     /**
89      * @brief Clear index tree member variables
90      */
clear()91     void           clear()
92     {
93         clearBlock( &m_rootBlock );
94     }
95 
96     /**
97      * @brief Create index related files
98      */
99     EXPORT const int      createIndex( const FID treeFid, const FID listFid, const bool useFreeMgrFlag = true  );
100 
101     /**
102      * @brief Delete a value from an index
103      */
104     EXPORT const int      deleteIndex( const uint64_t key, const int width, const RID rid );
105 
106     /**
107      * @brief Drop index related files
108      */
dropIndex(const FID treeFid,const FID listFid)109     const int      dropIndex( const FID treeFid, const FID listFid  )
110 #ifdef BROKEN_BY_MULTIPLE_FILES_PER_OID
111     {
112         m_cbTree.file.pFile = m_fileopTree.openFile(treeFid);
113         m_cbList.file.pFile = m_fileopList.openFile(listFid);
114         closeIndex();
115         m_useMultiCol = false;
116         deleteFile( treeFid );
117         deleteFile( listFid );
118         return NO_ERROR;
119     }
120 #endif
121     {
122         std::string segFile;
123         m_cbTree.file.pFile = m_fileopTree.openFile(
124                                   treeFid, 0, 0, 0, segFile);
125         m_cbList.file.pFile = m_fileopList.openFile(
126                                   listFid, 0, 0, 0, segFile);
127         closeIndex();
128         m_useMultiCol = false;
129         deleteFile( treeFid );
130         deleteFile( listFid );
131         return NO_ERROR;
132     }
133 
134     /**
135      * @brief Get the test bit value
136      */
137     EXPORT const bool     getTestbitValue( const uint64_t key, const int width, const int curTestNo, int* bittestVal );
138 
139     /**
140      * @brief Get the match entry in the tree
141      */
142     EXPORT const bool     getTreeMatchEntry( DataBlock* block, const uint64_t sbid, const uint64_t entry, const int width,
143             const int allocCount, const bool* entryMap, int* matchEntry, IdxBitTestEntry* checkEntry );
144 
145     /**
146      * @brief Get the tree node summary information
147      */
148     EXPORT const int      getTreeNodeInfo( DataBlock* block, const uint64_t sbid, const uint64_t entry, const int width,
149                                            const IdxTreeGroupType group, int* allocCount, int* realCount, bool* entryMap );
150 
151     /**
152      * @brief Check index address pointer is empty or not
153      */
154     EXPORT const bool     isAddrPtrEmpty( void* pStruct, const IdxTreeEntryType entryType ) const;
155 
156     /**
157      * @brief Check whether the tree is empty
158      */
159     EXPORT const bool     isTreeEmpty();
160 
161     /**
162      * @brief Init index
163      */
164     EXPORT const int      initIndex( const FID treeFid, const FID listFid );
165 
166     /**
167      * @brief Move tree entries
168      */
169     EXPORT const int      moveEntry( const uint64_t oldFbo, const uint64_t oldSbid, const uint64_t oldEntry, const int width,  const uint64_t newFbo,
170                                      const uint64_t newSbid, const uint64_t newEntry, const int newGroup, const int allocCount, bool* entryMap, int* moveCount, const int newAllocCount = 0 );
171 
172     /**
173      * @brief Open index related files
174      */
175     EXPORT const int      openIndex( const FID treeFid, const FID listFid  );
176 
177     /**
178      * @brief Process index, including delete and search
179      */
180     EXPORT const int      processIndex( const uint64_t key, const int width, const RID rid, IdxEmptyListEntry& listHdrAddr, const bool bDelete = true );
181 
182     /**
183      * @brief A wrapper for the call to free manager
184      */
185     EXPORT const int      releaseSegment( int segmentType, IdxEmptyListEntry* myPtr );
186 
187     /**
188      * @brief Clean up index file
189      */
190     EXPORT const int      resetIndexFile( const FID treeFid, const FID listFid  );
191 
192     /**
193      * @brief Setup bitset by column
194      */
195     EXPORT const int      setBitsetColumn( void* val, const int pos, const int width, const ColType colType );
196 
197     /**
198      * @brief Set bit test entry
199      */
200     EXPORT void           setBittestEntry( IdxBitTestEntry* bittestEntry, const uint64_t testbitVal, const uint64_t group, const uint64_t fbo, const uint64_t sbid, const uint64_t entry, constu int64_t entryType = BIT_TEST ) const;
201 
202     /**
203      * @brief Set blank entry
204      */
setBlankEntry(void * pStruct)205     void           setBlankEntry( void* pStruct ) const
206     {
207         memset( pStruct, 0, 8 );
208     }
209 
210     /**
211      * @brief Set empty list ptr entry
212      */
213     EXPORT void           setEmptyListEntry( IdxEmptyListEntry* myEntry, const uint64_t group, const uint64_t fbo, const uint64_t sbid, const uint64_t entry ) const;
214 
215     /**
216      * @brief Set transaction Id
217      */
setTransId(const TxnID txnId)218     EXPORT void           setTransId( const TxnID txnId )
219     {
220         FileOp::setTransId( txnId );
221         m_cbTree.session.txnid = m_cbList.session.txnid = txnId;
222         m_freeMgr.setTransId(txnId);
223         m_listMgr.setTransId(txnId);
224     }
225 
226     /**
227      * @brief Set use brm flag
228      */
229 
230     /**
231      * @brief Update a value in an index
232      */
233     EXPORT const int      updateIndex( const uint64_t key, const int width, const RID rid );
234 
235     EXPORT const int      updateListFile( const uint64_t key, const int width, const RID rid, const int curLevel, const uint64_t group, const int allocCount, const int useCount, const int offset, const bool addFlag = false );
236 
237 
238     // internal use functions
239     EXPORT void           clearTree( IdxTree* myTree );
240     EXPORT void           clearTreeNode( IdxTreeNode* myNode );
getTree()241     const IdxTree& getTree()
242     {
243         return m_tree;
244     }
getRootBlock()245     const DataBlock& getRootBlock()
246     {
247         return m_rootBlock;
248     }
getUseMultiRid()249     const bool     getUseMultiRid()
250     {
251         return m_useMultiRid;
252     }
253 
254     EXPORT void           setTreeHeader( IdxTree* myTree, const uint64_t key, const RID rid, const int width,
255                                          const int testbitVal, const IdxBitmapPointerEntry bitmapEntry );
256     EXPORT void           setTreeNode( IdxTreeNode* myNode, const int level, const int allocCount, const int useCount,
257                                        const int offset, const IdxBitTestEntry nextEntry, const IdxBitTestEntry curEntry  );
258 
259     EXPORT const int      updateIndexList( const uint64_t key, const int width, const RID rid, IdxEmptyListEntry* myEntry, const int no, const bool addFlag = false);
260 
261     EXPORT void           printMemSubBlock( DataBlock* curBlock, const int sbid, const bool bNoZero = false );
262     EXPORT void           printSubBlock( const int fbo, const int sbid, const bool bNoZero = false );
263 
setAssignFbo(const int fbo)264     void           setAssignFbo( const int fbo )
265     {
266         m_assignFbo = fbo;    // internal testing purpose only
267     }
setUseFreeMgr(const bool val)268     void           setUseFreeMgr( const bool val )
269     {
270         m_useFreeMgr = val;
271     }
setUseListMgr(const bool val)272     void           setUseListMgr( const bool val )
273     {
274         m_useListMgr = val;
275     }
setUseMultiCol(const bool val)276     void           setUseMultiCol( const bool val )
277     {
278         m_useMultiCol = val;
279     }
setUseMultiRid(const bool val)280     void           setUseMultiRid( const bool val )
281     {
282         m_useMultiRid = val;
283     }
284 
setCbTree(CommBlock & cb)285     void           setCbTree(CommBlock& cb)
286     {
287         memcpy(&m_cbTree.session, &cb.session,
288                sizeof(cb.session));
289         memcpy(&m_cbTree.file, &cb.file,
290                sizeof(cb.file));
291     }
getCbTree()292     const CommBlock& getCbTree() const
293     {
294         return m_cbTree;
295     }
296 
setCbList(CommBlock & cb)297     void           setCbList(CommBlock& cb)
298     {
299         memcpy(&m_cbList.session, &cb.session,
300                sizeof(cb.session));
301         memcpy(&m_cbList.file, &cb.file,
302                sizeof(cb.file));
303     }
getCbList()304     const CommBlock& getCbList() const
305     {
306         return m_cbList;
307     }
308 
309     DataBlock      m_rootBlock;                     /** @brief This block contains sub block 0 and sub block 1 */
310     CommBlock      m_cbTree;                        /** @brief index tree */
311     CommBlock      m_cbList;                        /** @brief index list */
312 
313     IdxTree        m_tree;                          /** @brief index tree node */
314     FreeMgr        m_freeMgr;                       /** @brief index free mgr */
315     IndexList      m_listMgr;                       /** @brief index list mgr */
316 
317     IdxMultiColKey m_multiColKey;                   /** @brief index multi-column key */
318     IdxMultiRid    m_multiRid;                      /** @brief index multi rids */
319     FileOp         m_fileopTree;
320     FileOp         m_fileopList;
321 private:
322     // internal testing flags
323     bool           m_useFreeMgr;
324     bool           m_useListMgr;
325     bool           m_useMultiCol;
326     bool           m_useMultiRid;
327     int            m_assignFbo;
328 };
329 
330 } //end of namespace
331 
332 #undef EXPORT
333 
334 #endif // _WE_INDEXTREE_H_
335