1 #ifndef _CEGOTABLEMANAGER_H_INCLUDED_
2 #define _CEGOTABLEMANAGER_H_INCLUDED_
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 // CegoTableManager.h
6 // ------------------
7 // Cego table 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: CegoTableManager
16 //
17 // Description: Basic table management
18 //
19 // Status: CLEAN
20 //
21 ///////////////////////////////////////////////////////////////////////////////
22 
23 // base includes
24 #include <lfcbase/Chain.h>
25 #include <lfcbase/ListT.h>
26 
27 // xml includes
28 #include <lfcxml/XMLOutStream.h>
29 
30 // cego includes
31 #include "CegoDataType.h"
32 #include "CegoTupleState.h"
33 #include "CegoField.h"
34 #include "CegoPredDesc.h"
35 #include "CegoQueryHelper.h"
36 #include "CegoLockHandler.h"
37 #include "CegoTransactionManager.h"
38 #include "CegoAlterDesc.h"
39 #include "CegoDefs.h"
40 #include "CegoProcedure.h"
41 #include "CegoView.h"
42 #include "CegoSystemObject.h"
43 #include "CegoBTreeValue.h"
44 #include "CegoTrigger.h"
45 
46 class CegoTransactionManager;
47 class CegoDbThreadPool;
48 
49 class CegoTableManager : public CegoSystemObject {
50 
51 public:
52 
53     enum IsolationLevel { READ_COMMITTED, READ_UNCOMMITTED };
54 
55     CegoTableManager(CegoDatabaseManager *pDBMng);
56     ~CegoTableManager();
57 
58     void setThreadId(unsigned long long tid);
59     unsigned long long getThreadId() const;
60 
61     void setAppend(bool doAppend);
62 
63     void createBasicTableSet(const Chain& tableSet);
64     void dropTableSet(const Chain& tableSet);
65     void reorgTableSet(const Chain& tableSet);
66 
67     void stopTableSet(const Chain& tableSet, bool archComplete);
68 
69     void resetTableSet(const Chain& tableSet);
70 
71     void beginBackup(const Chain& tableSet, const Chain& msg);
72     void endBackup(const Chain& tableSet, const Chain&msg, bool keepTicket = true);
73     void resetBUStat(const Chain& tableSet);
74 
75     void syncTableSet(const Chain& tableSet, const Chain& msg, const Chain& escCommand, int timeout);
76 
77     void logTo(const Chain& tableSet, const Chain& secondary);
78 
79     void addDataFile(const Chain& tableSet, const Chain& type, int fileId, const Chain& dataFile, int fileSize);
80 
81     // transaction methods
82 
83     void setIsolationLevel(IsolationLevel level);
84     IsolationLevel getIsolationLevel() const;
85 
86     void setAutoCommit(bool autoCommit);
87     bool getAutoCommit() const;
88 
89     void lockTable(int tabSetId, const Chain& tableName);
90     void unlockTable(int tabSetId, const Chain& tableName);
91 
92     void beginTransaction(int tabSetId, bool doLog);
93     void getTransactionAffectedTables(int tabSetId, SetT<Chain>& tableList);
94 
95     unsigned long long commitTransactionSynced(int tabSetId);
96     unsigned long long commitTransaction(int tabSetId, bool doLog);
97 
98     unsigned long long rollbackTransactionSynced(int tabSetId);
99     unsigned long long rollbackTransaction(int tabSetId, bool doLog);
100 
101     unsigned long long getTID(int tabSetId);
102 
103     void setTAStep(int tabSetId, unsigned long long tastep);
104     unsigned long long getTAStep(int tabSetId);
105 
106     void setTID(int tabSetId, unsigned long long tid);
107 
108     void getTupleInfo(int tabSetId, const CegoDataPointer dp, unsigned long long &tid, unsigned long long &tastep, CegoTupleState &ts);
109     void setTupleInfo(int tabSetId, const CegoDataPointer dp, unsigned long long tid, unsigned long long tastep, CegoTupleState ts);
110 
111     // table methods
112 
113     CegoTableObject createDataTable(int tabSetId, const Chain& tableName, CegoObject::ObjectType type, const ListT<CegoField>& fl, bool useColumnId = false);
114 
115     void alterDataTableSynced(CegoTableObject& oe, const ListT<CegoAlterDesc>& alterList);
116     void alterDataTable(int tabSetId, const Chain& tableName, CegoObject::ObjectType type, const ListT<CegoAlterDesc>& alterList);
117 
118     void renameObject(int tabSetId, const Chain& objName,  CegoObject::ObjectType type, const Chain& newObjName);
119 
120     void reorgObjectSynced(int tabSetId, const Chain& objName, CegoObject::ObjectType type);
121     void reorgTable(int tabSetId, const Chain& tableName);
122 
123     void invalidateIndexForTable(int tabSetId, const Chain& tableName);
124 
125     void insertDataTable(CegoTableObject& oe,
126 			 ListT<CegoField>& fvl,
127 			 CegoDataPointer& dp,
128 			 bool doLogging, bool flushLog);
129 
130 
131     void insertDataTable(CegoTableObject& oe,  ListT<CegoField>& fvl,
132 			 const ListT<CegoTableObject>& idxList,
133 			 const ListT<CegoBTreeObject>& btreeList,
134 			 const ListT<CegoKeyObject>& keyList,
135 			 const ListT<CegoCheckObject>& checkList,
136 			 const CegoDataPointer& sysEntry,
137 			 const Chain& virginIndex,
138 			 CegoDataPointer& dp,
139 			 bool doLogging, bool doAppend, bool increaseStep, bool flushLog);
140 
141     unsigned long long deleteDataTable(CegoTableObject& oe, CegoPredDesc* pPred, CegoProcBlock* pBlock, bool isSynced);
142     unsigned long long deleteDataTable(CegoTableObject& oe,
143 				       const ListT<CegoTableObject>& idxList,
144 				       const ListT<CegoBTreeObject>& btreeList,
145 				       const ListT<CegoKeyObject>& keyList,
146 				       CegoPredDesc* pPred, CegoProcBlock* pBlock, bool isSynced);
147 
148     void deleteDataTableEntrySynced(int tabSetId,
149 				    const Chain& tableName,
150 				    CegoObject::ObjectType type,
151 				    const CegoDataPointer& dp,
152 				    const ListT<CegoField>& fvl,
153 				    const ListT<CegoTableObject>& idxList,
154 				    const ListT<CegoBTreeObject>& btreeList,
155 				    const ListT<CegoKeyObject>& keyList,
156 				    bool doCheckKey,
157 				    bool doIgnoreIndexError,
158 				    bool isSynced);
159 
160     bool deleteDataTableEntry(int tabSetId,
161 			      const Chain& tableName,
162 			      CegoObject::ObjectType type,
163 			      const CegoDataPointer& dp,
164 			      const ListT<CegoField>& fvl,
165 			      const ListT<CegoTableObject>& idxList,
166 			      const ListT<CegoBTreeObject>& btreeList,
167 			      const ListT<CegoKeyObject>& keyList,
168 			      bool doCheckKey = true,
169 			      bool doIgnoreIndexError = false);
170 
171     unsigned long long updateDataTable(int tabSetId,
172 				       const Chain& tableName,
173 				       const Chain& tableAlias,
174 				       CegoPredDesc* pPred,
175 				       const ListT<CegoField>& updList,
176 				       ListT<CegoExpr*>& exprList,
177 				       bool returnOnFirst,
178 				       ListT<CegoField>& returnList,
179 				       CegoProcBlock* pBlock = 0);
180 
181     unsigned long long updateDataTable(int tabSetId,
182 				       const Chain& tableName,
183 				       const Chain& tableAlias,
184 				       const ListT<CegoTableObject>& idxList,
185 				       const ListT<CegoBTreeObject>& btreeList,
186 				       const ListT<CegoKeyObject>& keyList,
187 				       const ListT<CegoCheckObject>& checkList,
188 				       CegoPredDesc* pPred,
189 				       const ListT<CegoField>& updList,
190 				       ListT<CegoExpr*>& exprList,
191 				       bool returnOnFirst,
192 				       ListT<CegoField>& returnList,
193 				       CegoProcBlock* pBlock);
194 
195     void truncateTable(int tabSetId, const Chain& tableName);
196     void dropObjectSynced(int tabSetId, const Chain& objName, CegoObject::ObjectType type);
197 
198     void dropTable(int tabSetId,
199 		   const Chain& tableName);
200 
201     void dropIndex(int tabSetId, const Chain& idxName);
202     void dropBTree(int tabSetId, const Chain& btreeName);
203 
204     void dropView(int tabSetId, const Chain& viewName);
205     void dropProcedure(int tabSetId, const Chain& procName);
206     void dropFKey(int tabSetId, const Chain& fkey);
207     void dropCheck(int tabSetId, const Chain& check);
208     void dropTrigger(int tabSetId, const Chain& trigger);
209     void dropAlias(int tabSetId, const Chain& aliasName);
210     void correctTableSet(int tabSetId, bool doSync);
211 
212     void correctIndexForTable(int tabSetId, const Chain& tableName, bool doSync, bool doForce);
213 
214     int cleanTableSet(int tabSetId);
215 
216     // utility cursor for native table traverse
217 
218     bool getFirstTuple(CegoObjectCursor* pC, ListT<CegoField>& fl, CegoDataPointer& dp);
219     bool getNextTuple(CegoObjectCursor* pC, ListT<CegoField>& fl, CegoDataPointer& dp);
220 
221 
222     // index and key creation
223 
224     void createIndexTableSynced(int tabSetId, const Chain& indexName, const Chain& tableName, CegoObject::ObjectType type, ListT<CegoField>& idxList, bool isCached);
225 
226     void createPrimaryIndexTable(int tabSetId, const Chain& indexName, const Chain& tableName, ListT<CegoField>& schema);
227 
228     void createIndexTable(int tabSetId, const Chain& indexName, const Chain& tabName, ListT<CegoField>& schema, CegoObject::ObjectType type, bool isCached);
229 
230     void createBTree(int tabSetId, const Chain& btreeName, const Chain& tableName, ListT<CegoField>& schema, CegoObject::ObjectType type, bool doSync, bool isCached);
231 
232     void createAVLIndexTable(int tabSetId, const Chain& indexName, const Chain& tableName, ListT<CegoField>& schema, CegoObject::ObjectType type);
233 
234     void createForeignKey(int tabSetId, const Chain& fkey, const Chain& tableName, const ListT<CegoField>& keyList, const Chain& refTable, const ListT<CegoField>& refList);
235 
236     void createCheck(int tabSetId, const Chain& checkName, const Chain& tableName, CegoPredDesc *pPredDesc);
237 
238     void createTrigger(int tabSetId, const Chain& triggerName,
239 		       bool isBefore, bool isOnInsert, bool isOnUpdate, bool isOnDelete,
240 		       const Chain& tableName, const Chain& triggerText);
241 
242     void createAlias(int tabSetId, const Chain& aliasName, const Chain& tableName, const ListT<CegoAttrAlias>& aliasList);
243 
244 
245     // info methods
246 
247     void getPoolInfo(CegoTableObject& oe, ListT< ListT<CegoFieldValue> > &info);
248     void getSystemInfo(const Chain& tableSet, CegoTableObject& oe,  ListT< ListT<CegoFieldValue> > &fa, Chain& format);
249 
250     // debugging methods
251 
252     void dumpObject(const Chain& tableSet, const Chain& tableName, CegoObject::ObjectType type, Chain& chainDump);
253     void dumpObject(int tabSetId, const Chain& tableName, CegoObject::ObjectType type, Chain& chainDump);
254     // void dumpLockHandler(Chain& lockDump);
255 
256     void regDataFiles(const Chain& tableSet);
257 
258     void abort();
259     void proceed();
260     bool isAborted() const;
261 
262     void addCompProcedure(int tabSetId, CegoProcedure *pProc);
263     void removeCompProcedure(int tabSetId, const Chain& procName);
264     bool checkCompProcedure(int tabSetId, const Chain& procName);
265     CegoProcedure* getCompProcedure(int tabSetId, const Chain& procName);
266 
267     void addCompView(int tabSetId, CegoView *pView);
268     void removeCompView(int tabSetId, const Chain& viewName);
269     bool checkCompView(int tabSetId, const Chain& viewName);
270     CegoView* getCompView(int tabSetId, const Chain& viewName);
271 
272     void addCompTrigger(int tabSetId, CegoTrigger *pTrigger);
273     void removeCompTrigger(int tabSetId, const Chain& triggerName);
274     bool checkCompTrigger(int tabSetId, const Chain& triggerName);
275     CegoTrigger* getCompTrigger(int tabSetId, const Chain& triggerName);
276 
277     void removeAllComp(int tabSetId);
278 
279     void getTSLockStat(int tabSetId, Chain& lockName, int& lockCount, unsigned long long &numRdLock, unsigned long long &numWrLock, unsigned long long &sumRdDelay, unsigned long long &sumWrDelay);
280     void getLHLockStat(unsigned long long lockId, Chain& lockName, int& lockCount, unsigned long long &numRdLock, unsigned long long &numWrLock, unsigned long long &sumRdDelay, unsigned long long &sumWrDelay);
281     void getLHAggLockStat(const Chain& lockName, int& numLock, int& lockCount, unsigned long long &numRdLock, unsigned long long &numWrLock, unsigned long long &sumRdDelay, unsigned long long &sumWrDelay);
282 
283     void extractIndexValue(const ListT<CegoField>& tableSchema, const ListT<CegoField>& indexSchema, char* p, int len, int& idxLen);
284 
285     void increaseBlobRef(int tabSetId, PageIdType pageId);
286     void decreaseBlobRef(int tabSetId, PageIdType pageId);
287 
288     void increaseClobRef(int tabSetId, PageIdType pageId);
289     void decreaseClobRef(int tabSetId, PageIdType pageId);
290 
291     unsigned char* getBlobData(int tabSetId, PageIdType pageId, unsigned long long& blobSize);
292     char* getClobData(int tabSetId, PageIdType pageId, unsigned long long& clobSize);
293 
294     void putBlobData(int tabSetId, unsigned char* data, unsigned long long blobSize, PageIdType& pageId);
295     void putClobData(int tabSetId, char* data, unsigned long long clobSize, PageIdType& pageId);
296 
297     CegoTransactionManager* getTransactionManager();
298 
299     void setPoolSyncInfo(CegoDbThreadPool *pPool, int thrIdx);
300 
301 protected:
302 
303     CegoTransactionManager *_pTM;
304 
305     void resetTemp(const Chain& tableSet);
306 
307     void finishOpenTransaction(int tabSetId);
308     unsigned long long _threadId;
309 
310     bool _doAppend;
311 
312 
313 private:
314 
315     void renameTable(int tabSetId, const Chain& tableName, const Chain& newTableName);
316     void renameIndex(int tabSetId, const Chain& idxName, CegoObject::ObjectType type, const Chain& newIdxName);
317 
318     void renameBTree(int tabSetId, const Chain& btreeName, CegoObject::ObjectType type, const Chain& newBTreeName);
319 
320     void renameKey(int tabSetId, const Chain& keyName, const Chain& newKeyName);
321     void renameProcedure(int tabSetId, const Chain& keyName, const Chain& newKeyName);
322     void renameView(int tabSetId, const Chain& viewName, const Chain& newViewName);
323     void renameCheck(int tabSetId, const Chain& checkName, const Chain& newCheckName);
324     void renameTrigger(int tabSetId, const Chain& triggerName, const Chain& newTriggerName);
325     void renameRBO(int tabSetId, const Chain& rboName, const Chain& newRboName);
326 
327     bool updateTuple(CegoTableObject& oe,  const CegoDataPointer& sysEntry, const CegoDataPointer& dp,
328 		     ListT<CegoField>& fl,
329 		     const ListT<CegoField>& nfvl,
330 		     ListT<CegoExpr*>& exprList,
331 		     ListT<CegoField>& updSchema,
332 		     const ListT<CegoTableObject>& idxList,
333 		     const ListT<CegoBTreeObject>& btreeList,
334 		     const ListT<CegoKeyObject>& keyList,
335 		     const ListT<CegoCheckObject>& checkList,
336 		     const Chain& virginIndex,
337 		     bool doAppend,
338 		     CegoProcBlock* pBlock);
339 
340 
341     void checkTypes(const ListT<CegoField>& fl, const ListT<CegoField>& nfl);
342     void checkIntegrity(int tabSetId, const Chain& tableName,
343 			const ListT<CegoTableObject>& idxList,
344 			const ListT<CegoBTreeObject>& btreeList,
345 			const ListT<CegoKeyObject>& keyList,
346 			const ListT<CegoCheckObject>& checkList,
347 			const CegoDataPointer& dp, const ListT<CegoField>& fvl, const ListT<CegoField>& nfvl);
348 
349     void getKeyAndIdxRef(int tabSetId, const Chain& tableName, const Chain& attrName, ListT<CegoKeyObject>& refKeyList, ListT<CegoTableObject>& refIdxList, ListT<CegoBTreeObject>& refBTreeList);
350     void checkKeyIntegrity(const ListT<CegoKeyObject>& keyList, int tabSetId, const Chain& tableName, const ListT<CegoField>& fvl, const ListT<CegoField>& nfvl);
351     void checkIndexIntegrity(const ListT<CegoTableObject>& idxList, int tabSetId, const CegoDataPointer& dp, const ListT<CegoField>& fvl, const ListT<CegoField>& nfvl);
352     void checkBTreeIntegrity(const ListT<CegoBTreeObject>& btreeList, int tabSetId, const CegoDataPointer& dp, const ListT<CegoField>& fvl, const ListT<CegoField>& nfvl);
353     void checkCheckIntegrity(const ListT<CegoCheckObject>& checkList, const ListT<CegoField>& nfvl);
354     bool checkNullValue(int tabSetId, const Chain& tableName, const CegoField& f);
355 
356     bool keyReferenceExists(int tabSetId, const Chain& tableName, const ListT<CegoField>& fl, const ListT<CegoKeyObject>& keyList);
357 
358     const CegoFieldValue& getIndexSearchValue(const ListT<CegoField>& idxSchema, const ListT<CegoField>& nfvl);
359 
360     void getBlobs(int tabSetId, const ListT<CegoField>& fvl, ListT<CegoBlob>& blobList);
361     void getClobs(int tabSetId, const ListT<CegoField>& fvl, ListT<CegoClob>& clobList);
362 
363     void getBlobs(int tabSetId, ListT<CegoExpr*>& exprList, CegoProcBlock* pBlock, ListT<CegoBlob>& blobList);
364     void getClobs(int tabSetId, ListT<CegoExpr*>& exprList, CegoProcBlock* pBlock, ListT<CegoClob>& clobList);
365 
366     void addBUStat(int tabSetId, const Chain& butype, const Chain& msg);
367 
368     bool typeConversionAllowed(CegoDataType fromType, CegoDataType toType);
369 
370     void getClobForExprList(int tabSetId, ListT<CegoExpr*>& exprList, ListT<CegoClob>& clobList);
371 
372     void poolP();
373     void poolV();
374 
375     unsigned long long _tid[TABMNG_MAXTABSET];
376 
377     // tastep is an opertion counter inside a single transaction
378     // it is used for object cursors to filter out the current transaction operation
379     // but rather acces previous modified tuples ( condition : taset(tuple) < tastep(current) )
380 
381     unsigned long long _tastep[TABMNG_MAXTABSET];
382 
383     ListT<CegoField> _streamSchema;
384 
385     bool _isAborted;
386     bool _autoCommit;
387 
388     int _encBufLen;
389     char *_pEncBuf;
390 
391     IsolationLevel _isolationLevel;
392 
393     ListT<CegoProcedure*> _procList[TABMNG_MAXTABSET];
394     ListT<CegoView*> _viewList[TABMNG_MAXTABSET];
395     ListT<CegoTrigger*> _triggerList[TABMNG_MAXTABSET];
396 
397     CegoDbThreadPool *_pPool;
398     int _thrIdx;
399 
400     unsigned long _modId;
401 };
402 
403 #endif
404