optimizing single table aggregate queries with no index
[csql.git] / include / TableImpl.h
blobb742a9675c612fa553b154d6ed3e9b68bb50b414
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #ifndef TABLE_IMPL_H
17 #define TABLE_IMPL_H
18 #include<os.h>
19 #include<DataType.h>
20 #include<Transaction.h>
21 #include<Database.h>
22 #include<Index.h>
23 #include<CatalogTables.h>
24 #include<Info.h>
25 #include<Debug.h>
26 #include<DatabaseManagerImpl.h>
27 #include<Predicate.h>
28 #include<AggTableImpl.h>//for AggType
29 enum ScanType
31 fullTableScan = 0,
32 hashIndexScan,
33 treeIndexScan,
34 unknownScan
37 static char ScanTypeNames[][10] =
39 "TableScan", "HashScan", "TreeScan", "Invalid"
42 class Predicate;
44 class TupleIterator
46 Predicate *pred_;
47 ScanType scanType_;
48 ChunkIterator *cIter;
49 BucketIter *bIter;
50 TreeIter *tIter;
51 IndexInfo *info;
52 void *chunkPtr_;
53 int procSlot;
54 bool isBetween;
55 bool isPointLook;
57 TupleIterator(){}
58 public:
59 TupleIterator(Predicate *p, ScanType t, IndexInfo *i, void *cptr, int pslot,bool between , bool isPtLook)
60 { bIter = NULL; pred_ = p ; scanType_ = t; info = i; chunkPtr_ = cptr; procSlot =pslot; isBetween=between; isPointLook=isPtLook; }
62 ~TupleIterator()
64 if (bIter) delete bIter;
65 bIter = NULL;
67 bool isBetInvolved(){ return isBetween;}
68 void setBetInvolved(bool between){ isBetween=between;}
69 bool isPointLookInvolved(){return isPointLook;}
70 DbRetVal open();
71 void* next();
72 void* prev();//used only for tree iter during deleteTuple
73 void reset();
74 DbRetVal close();
77 class TableImpl:public Table
79 private:
82 LockManager *lMgr_;
83 Transaction **trans;
84 //This is pointer to the pointer stored in the
85 //Transaction manager.
86 //If the transaction commits/aborts this pointer changes
87 //and this will get that newly allocated transaction
89 char tblName_[IDENTIFIER_LENGTH];
90 int tblID_;
91 size_t length_; //length of the tuple
92 int numFlds_;
93 void* chunkPtr_;
94 void *curTuple_; //holds the current tuple ptr. moved during fetch() calls
96 Predicate *pred_;
97 ScanType scanType_;
98 //ChunkIterator *iter;
99 //BucketIter *bIter;
101 TupleIterator *iter;
103 bool undoFlag;
105 public:
106 FieldList fldList_;
107 int numIndexes_;
108 char** indexPtr_; // array of index ptrs to the catalog table for the indexes of this table.
109 IndexInfo **idxInfo;
110 int useIndex_;//offet in the above array indexPtr_ for scan
111 bool isPlanCreated;
112 bool isPointLook;
113 bool isBetween;
114 Database *db_;
115 Database *sysDB_;
117 //Either one of the below is populated based on the no of fields and
118 //is used for tuple insertions
119 bool isIntUsedForNULL;
120 int iNullInfo;
121 char *cNullInfo;
122 int iNotNullInfo;
123 char *cNotNullInfo;
125 private:
127 //copy Values from binded buffer to tuple pointed by arg
128 DbRetVal copyValuesFromBindBuffer(void *tuple, bool isInsert=true);
129 DbRetVal copyValuesToBindBuffer(void *tuple);
130 void setNullBit(int fldpos);
131 void clearNullBit(int fldpos);
132 DbRetVal insertIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
133 DbRetVal updateIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
134 DbRetVal deleteIndexNode(Transaction *trans, void *indexPtr, IndexInfo *info, void *tuple);
136 DbRetVal createPlan();
137 Chunk* getSystemTableChunk(CatalogTableID id)
139 return sysDB_->getSystemDatabaseChunk(id);
142 public:
143 TableImpl() { db_ = NULL; chunkPtr_ = NULL; iter = NULL;
144 idxInfo = NULL; indexPtr_ = NULL; scanType_ = unknownScan;
145 pred_ = NULL; useIndex_ = -1; numFlds_ = 0;
146 iNullInfo = 0; cNullInfo = NULL; isIntUsedForNULL = true;
147 iNotNullInfo = 0; cNotNullInfo = NULL; curTuple_ = NULL;
148 isPlanCreated = false; undoFlag = true;}
149 ~TableImpl();
151 void setDB(Database *db) { db_ = db; }
152 Database* getDB() { return db_;}
153 void setSystemDB(Database *db) { sysDB_ = db; }
154 void setLockManager(LockManager *lmgr) { lMgr_ = lmgr; }
155 void setTrans(Transaction **t) { trans = t; }
156 void setCurTuple(void *tuple){ curTuple_=tuple; }
157 DataType getFieldType(const char *name)
158 { return fldList_.getFieldType(name); }
159 int getFieldOffset(const char *name)
160 { return fldList_.getFieldOffset(name); }
161 size_t getFieldLength(const char *name)
162 { return fldList_.getFieldLength(name); }
164 DbRetVal getFieldInfo(const char *fieldName, FieldInfo *&info)
166 char tblName[IDENTIFIER_LENGTH];
167 char fldName[IDENTIFIER_LENGTH];
168 getTableNameAlone((char*)fieldName, tblName);
169 getFieldNameAlone((char*)fieldName, fldName);
170 if (0 == strcmp(tblName, "") || 0 ==strcmp(tblName, getName()))
171 return fldList_.getFieldInfo(fldName, info);
172 else
173 return ErrNotExists;
176 List getFieldNameList();
178 // search predicate
179 void setCondition(Condition *p)
180 { isPlanCreated = false; if (p) pred_ = p->getPredicate(); else pred_ = NULL;}
182 //binding
183 DbRetVal bindFld(const char *name, void *val);
184 void *getBindFldAddr(const char *name);
185 int getFldPos(char *name);
186 DbRetVal markFldNull(const char *name);
187 DbRetVal markFldNull(int colpos);
188 bool isFldNull(const char *name);
189 bool isFldNull(int colpos);
191 void clearFldNull(const char *name);
192 void clearFldNull(int colpos);
193 void resetNullinfo();
194 DbRetVal insertTuple();
195 DbRetVal updateTuple();
197 DbRetVal deleteTuple();
198 int deleteWhere();
199 int truncate();
201 DbRetVal execute();
203 void* fetch();
204 void* fetch(DbRetVal &rv);
205 void* fetchNoBind();
206 void* fetchNoBind(DbRetVal &rv);
207 DbRetVal fetchAgg(const char *fldName, AggType aType, void *buf);
209 DbRetVal close();
210 DbRetVal closeScan();
213 long spaceUsed();
214 long numTuples();
215 int pagesUsed();
216 void printInfo();
217 void printPlan(int space);
219 DbRetVal lock(bool shared);
220 DbRetVal unlock();
222 DbRetVal setUndoLogging(bool flag) { undoFlag = flag; }
224 void printSQLIndexString();
225 bool isTableInvolved(char *tblName);
226 bool pushPredicate(Predicate *pred);
227 void setPredicate(Predicate *pred);
228 char* getName() { return tblName_; }
229 void setTableInfo(char *name, int tblid, size_t length,
230 int numFld, int numIdx, void *chunk);
231 friend class DatabaseManagerImpl;
235 #endif