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 //  $Id: we_colop.h 4728 2013-08-08 19:26:10Z chao $
19 
20 
21 /** @file */
22 
23 #ifndef _WE_COLOP_H_
24 #define _WE_COLOP_H_
25 
26 #include <stdlib.h>
27 
28 #include "we_dbfileop.h"
29 #include "brmtypes.h"
30 #include "we_dbrootextenttracker.h"
31 #include "we_tablemetadata.h"
32 #include "../dictionary/we_dctnry.h"
33 #if defined(_MSC_VER) && defined(WRITEENGINE_DLLEXPORT)
34 #define EXPORT __declspec(dllexport)
35 #else
36 #define EXPORT
37 #endif
38 
39 /** Namespace WriteEngine */
40 namespace WriteEngine
41 {
42 class Log;
43 
44 //const int   COL_DATATYPE_NUM = 15;
45 //const char  ColDataTypeStr[COL_DATATYPE_NUM][20] = { "bit", "tinyint", "char", "smallint", "decimal", "medint", "integer", "float", "date", "bigint",
46 //            "double", "datetime", "varchar", "clob", "blob" };
47 
48 /** Class ColumnOp */
49 class ColumnOp : public DbFileOp
50 {
51 public:
52     /**
53      * @brief Constructor
54      */
55     EXPORT ColumnOp();
56     EXPORT ColumnOp(Log* logger);
57 
58     /**
59      * @brief Default Destructor
60      */
61     EXPORT virtual ~ColumnOp();
62 
63     EXPORT virtual int allocRowId(const TxnID& txnid,
64                                   bool    useStartingExtent,
65                                   Column& column,
66                                   uint64_t totalRow,
67                                   RID* rowIdArray,
68                                   HWM& hwm,
69                                   bool& newExtent,
70                                   uint64_t& rowsLeft,
71                                   HWM& newHwm,
72                                   bool& newFile,
73                                   ColStructList& newColStructList,
74                                   DctnryStructList& newDctnryStructList,
75                                   std::vector<boost::shared_ptr<DBRootExtentTracker> >&   dbRootExtentTrackers,
76                                   bool insertSelect = false,
77                                   bool isBatchInsert = false,
78                                   OID tableOid = 0, bool isFirstBatchPm = false);
79 
80     /**
81      * @brief Create column file(s)
82      */
83     EXPORT virtual int createColumn(Column& column,
84                                     int colNo,
85                                     int colWidth,
86                                     execplan::CalpontSystemCatalog::ColDataType colDataType,
87                                     ColType colType,
88                                     FID dataFid,
89                                     uint16_t dbRoot,
90                                     uint32_t partition);
91 
92     /**
93      * @brief Fills up a column with null/default values in all non-empty rows as the reference column. The reference column
94      * would typically be another column of the same table.
95      *
96      * @param column The new colum
97      * @param refCol The reference column for identifying valid rows
98      * @param defaultVal The default value of the new column
99      * @param dictOid The dictionary store OID for a dictionary column
100      * @param dictColWidth The dictionary string width for a dictionary column
101      */
102     //BUG931
103     EXPORT virtual int fillColumn(const TxnID& txnid,
104                                   Column& column,
105                                   Column& refCol,
106                                   void* defaultVal,
107                                   Dctnry* dctnry,
108                                   ColumnOp* refColOp,
109                                   const OID dictOid = 0,
110                                   const int dictColWidth = 0,
111                                   const std::string defaultValStr = "",
112                                   bool autoincrement = false);
113 
114     /**
115      * @brief Create a table file
116      */
117     EXPORT virtual int createTable() const;
118 
119     /**
120      * @brief Drop column file(s)
121      */
122     EXPORT virtual int dropColumn(const FID dataFid);
123 
124     /**
125     * @brief Drop column and dictionary file(s)
126     */
127     EXPORT virtual int dropFiles(const std::vector<int32_t>& dataFids);
128 
129     /**
130      * @brief Delete file(s) for the given partition
131      */
132     EXPORT virtual int dropPartitions(const std::vector<OID>& dataFids,
133                                       const std::vector<BRM::PartitionInfo>& partitions);
134 
135 
136     EXPORT virtual int deleteOIDsFromExtentMap(const std::vector<int32_t>& dataFids);
137     /**
138      * @brief Drop a table file
139      */
dropTable()140     int dropTable() const
141     {
142         return NO_ERROR;
143     }
144 
145     /**
146      * @brief Expand the abbreviated extent to a full extent for this column.
147      */
148     EXPORT virtual int expandAbbrevExtent(const Column& column);
149 
150     /**
151      * @brief Add an extent to the specified column OID and DBRoot.
152      * When this function returns, the file position will be located at the
153      * end of the file.  If the applicable column segment file does not exist,
154      * extendColumn() will create the new segment file.
155      * The extent must already exist in the extentmap prior to calling this fctn.
156      *
157      * @param column Column struct with input column attributes.
158      * @param leaveFileOpen Leave the db file open when leaving this function
159      * @param hwm The fbo of the column segment file where the new extent begins
160      * @param startLbid The starting LBID for the new extent.
161      * @param allocSize Number of blocks to be written for an extent
162      * @param dbRoot The DBRoot of the file with the new extent.
163      * @param partition Partition num of the file with the new extent.
164      * @param segment The segment number of the file with the new extent.
165      * @param segFile (out) Name of segment file to which the extent is added.
166      * @param pFile (out) FILE ptr to the file where the extent is added.
167      * @param newFile (out) Indicates if extent was added to new or existing file
168      * @param hdrs (out) Contents of headers if file is compressed.
169      * @return returns NO_ERROR if success.
170      */
171     EXPORT int extendColumn(const Column& column,
172                             bool          leaveFileOpen,
173                             HWM           hwm,
174                             BRM::LBID_t   startLbid,
175                             int           allocSize,
176                             uint16_t      dbRoot,
177                             uint32_t      partition,
178                             uint16_t      segment,
179                             std::string&  segFile,
180                             IDBDataFile*&        pFile,
181                             bool&         newFile,
182                             char*         hdrs = NULL);
183 
184     /**
185     * @brief Add an extent to the OID specified in the column argument.
186     * When this function returns, the file position will be located at the
187     * end of the file.
188     *
189     * @param column Column struct with input column attributes.
190     * @param dbRoot (in) The DBRoot of the file with the new extent.
191     * @param partition (in) The partition num of the file with the new extent.
192     * @param segment (in) The segment number of the file with the new extent.
193     * @param segFile (out) Name of segment file to which the extent is added.
194     * @param startLbid (out) The starting LBID for the new extent.
195     * @param newFile (out) Indicates if extent was added to new or existing file
196     * @param hdsr (out) Contents of headers if file is compressed.
197     * @param allocSize (out) number of blocks to be written for an extent
198     * @return returns NO_ERROR if success.
199     */
200     EXPORT int addExtent(const Column& column,
201                          uint16_t     dbRoot,
202                          uint32_t     partition,
203                          uint16_t     segment,
204                          std::string&  segFile,
205                          BRM::LBID_t&  startLbid,
206                          bool&         newFile,
207                          int&          allocSize,
208                          char*         hdrs = NULL);
209 
210     /**
211      * @brief Get columne data type
212      */
213     EXPORT virtual bool getColDataType(const char* name, execplan::CalpontSystemCatalog::ColDataType& colDataType) const;
214 
215     /**
216      * @brief Initialize the column
217      */
218     EXPORT virtual void initColumn(Column& column) const;
219 
220     /**
221      * @brief Check whether it is an empty row
222      */
223     EXPORT virtual bool isEmptyRow(unsigned char* buf, int offset, const Column& column);
224 
225     /**
226      * @brief Check whether it is a valid column
227      */
228     EXPORT virtual bool isValid(Column& column) const;
229 
230     /**
231      * @brief Open column file, segFile is set to the name of the column
232      *        segment file that is opened.
233      */
234     EXPORT virtual int openColumnFile(Column& column,
235                                       std::string& segFile,
236                                       bool useTmpSuffix,
237                                       int ioBuffSize = DEFAULT_BUFSIZ) const;
238 
239     /**
240      * @brief Open table file
241      */
openTableFile()242     int      openTableFile() const
243     {
244         return NO_ERROR;
245     }
246 
247     /**
248      * @brief Delete a file
249      */
250     EXPORT virtual void setColParam(Column& column, int colNo = 0,
251                                     int colWidth = 0,
252                                     execplan::CalpontSystemCatalog::ColDataType colDataType = execplan::CalpontSystemCatalog::INT,
253                                     ColType colType = WR_INT,
254                                     FID dataFid = 0,
255                                     int comppre = 0,
256                                     uint16_t dbRoot = 0,
257                                     uint32_t partition = 0,
258                                     uint16_t segment = 0) const;
259 
260     /**
261      * @brief Write row(s)
262      */
263     EXPORT virtual int writeRow(Column& curCol,
264                                 uint64_t totalRow,
265                                 const RID* rowIdArray,
266                                 const void* valArray,
267                                 bool bDelete = false);
268 
269     /**
270      * @brief Write row(s) for delete  @Bug 1886,2870
271      */
272     EXPORT virtual int writeRows(Column& curCol,
273                                  uint64_t totalRow,
274                                  const RIDList& ridList,
275                                  const void* valArray,
276                                  const void* oldValArray = 0,
277                                  bool bDelete = false);
278 
279     /**
280      * @brief Write row(s) for update @Bug 1886,2870
281      */
282     EXPORT virtual int writeRowsValues(Column& curCol,
283                                        uint64_t totalRow,
284                                        const RIDList& ridList,
285                                        const void* valArray);
286 
287     /**
288      * @brief Test if the pFile is an abbreviated extent.
289      */
290     virtual bool abbreviatedExtent(IDBDataFile* pFile, int colWidth) const = 0;
291 
292     /**
293      * @brief Caculate the number of blocks in file.
294      */
295     virtual int blocksInFile(IDBDataFile* pFile) const = 0;
296 
297     /**
298      * @brief Clear a column
299      */
300     EXPORT void clearColumn(Column& column) const;
301 
302     /**
303      * @brief open a data file of column
304      */
305     virtual IDBDataFile*  openFile(const Column& column, uint16_t dbRoot, uint32_t partition,
306                                    uint16_t segment, std::string& segFile, bool useTmpSuffix, const char* mode = "r+b",
307                                    int ioBuffSize = DEFAULT_BUFSIZ) const = 0;
308 
309 
310     /**
311      * @brief backup blocks to version buffer
312      */
313     int writeVB(IDBDataFile* pSource, const OID sourceOid, IDBDataFile* pTarget, const OID targetOid,
314                 const std::vector<uint32_t>& fboList, const BRM::VBRange& freeList,
315                 size_t& nBlocksProcessed, const size_t fboCurrentOffset);
316 
317     /**
318      * @brief restore blocks from version buffer
319      */
320     int copyVB(IDBDataFile* pSource, const BRM::VER_t txnD, const OID oid, std::vector<uint32_t>& fboList,
321                std::vector<BRM::LBIDRange>& rangeList);
322     /**
323      * @brief close column file
324      */
325     EXPORT virtual void closeColumnFile(Column& column) const;
326 
327 protected:
328 
329     /**
330      * @brief populate readBuf with data in block #lbid
331      */
332     virtual int readBlock(IDBDataFile* pFile, unsigned char* readBuf, const uint64_t fbo) = 0;
333 
334     /**
335      * @brief output writeBuf to pFile starting at position fbo
336      */
337     virtual int saveBlock(IDBDataFile* pFile, const unsigned char* writeBuf, const uint64_t fbo) = 0;
338 
339 private:
340 };
341 
342 } //end of namespace
343 
344 #undef EXPORT
345 
346 #endif // _WE_COLOP_H_
347