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_dctnrycompress.cpp 4726 2013-08-07 03:38:36Z bwilkinson $
19 
20 
21 /** @file */
22 
23 #include <stdio.h>
24 #include <string.h>
25 #include <vector>
26 using namespace std;
27 
28 #include "we_log.h"
29 #include "we_brm.h"
30 #include "we_chunkmanager.h"
31 
32 #include "we_dctnrycompress.h"
33 
34 
35 namespace WriteEngine
36 {
37 class ChunkManager;
38 // -----------------------------------------------------------------------------
39 // Dctnry with compression type 0
40 // -----------------------------------------------------------------------------
41 
42 /**
43  * Constructor
44  */
DctnryCompress0()45 DctnryCompress0::DctnryCompress0()
46 {
47     m_compressionType = 0;
48 }
49 
DctnryCompress0(Log * logger)50 DctnryCompress0::DctnryCompress0(Log* logger)
51 {
52     m_compressionType = 0;
53     setDebugLevel( logger->getDebugLevel() );
54     setLogger    ( logger );
55 }
56 
57 /**
58  * Default Destructor
59  */
~DctnryCompress0()60 DctnryCompress0::~DctnryCompress0()
61 {}
62 
63 // -----------------------------------------------------------------------------
64 // Dctnry with compression type 1
65 // -----------------------------------------------------------------------------
66 
67 /**
68  * Constructor
69  */
DctnryCompress1(Log * logger)70 DctnryCompress1::DctnryCompress1(Log* logger)
71 {
72     m_compressionType = 1;
73     m_chunkManager = new ChunkManager();
74 
75     if (logger)
76     {
77         setDebugLevel( logger->getDebugLevel() );
78         setLogger    ( logger );
79     }
80 
81     m_chunkManager->fileOp(this);
82 }
83 
84 /**
85  * Default Destructor
86  */
~DctnryCompress1()87 DctnryCompress1::~DctnryCompress1()
88 {
89     if (m_chunkManager)
90         delete m_chunkManager;
91 }
92 
updateDctnryExtent(IDBDataFile * pFile,int nBlocks)93 int DctnryCompress1::updateDctnryExtent(IDBDataFile* pFile, int nBlocks)
94 {
95     return m_chunkManager->updateDctnryExtent(pFile, nBlocks);
96 }
97 
98 
createDctnryFile(const char * name,int width,const char * mode,int ioBuffSize)99 IDBDataFile* DctnryCompress1::createDctnryFile(const char* name, int width, const char* mode, int ioBuffSize)
100 {
101     return m_chunkManager->createDctnryFile(
102                m_dctnryOID, width, m_dbRoot, m_partition, m_segment, name, mode, ioBuffSize);
103 }
104 
105 
106 // @bug 5572 - HDFS usage: add *.tmp file backup flag
openDctnryFile(bool useTmpSuffix)107 IDBDataFile* DctnryCompress1::openDctnryFile(bool useTmpSuffix)
108 {
109     return m_chunkManager->getFilePtr(
110                m_dctnryOID, m_dbRoot, m_partition, m_segment, m_segFileName, "r+b", DEFAULT_BUFSIZ, useTmpSuffix);
111 }
112 
113 
closeDctnryFile(bool doFlush,std::map<FID,FID> & columnOids)114 void DctnryCompress1::closeDctnryFile(bool doFlush, std::map<FID, FID>& columnOids)
115 {
116     if (doFlush)
117         m_chunkManager->flushChunks(NO_ERROR, columnOids);
118     else
119         m_chunkManager->cleanUp(columnOids);
120 
121     m_dFile = NULL;
122 }
123 
124 
numOfBlocksInFile()125 int DctnryCompress1::numOfBlocksInFile()
126 {
127     return m_chunkManager->getBlockCount(m_dFile);
128 }
129 
130 
readDBFile(IDBDataFile * pFile,unsigned char * readBuf,const uint64_t lbid,const bool isFbo)131 int DctnryCompress1::readDBFile(IDBDataFile* pFile, unsigned char* readBuf, const uint64_t lbid,
132                                 const bool isFbo)
133 {
134     int fbo = lbid;
135 
136     if (!isFbo)
137         RETURN_ON_ERROR(lbidToFbo(lbid, fbo));
138 
139     return m_chunkManager->readBlock(pFile, readBuf, fbo);
140 }
141 
142 
writeDBFile(IDBDataFile * pFile,const unsigned char * writeBuf,const uint64_t lbid,const int numOfBlock)143 int DctnryCompress1::writeDBFile(IDBDataFile* pFile, const unsigned char* writeBuf, const uint64_t lbid,
144                                  const int numOfBlock)
145 {
146     int fbo = 0;
147     RETURN_ON_ERROR(lbidToFbo(lbid, fbo));
148 
149     for (int i = 0; i < numOfBlock; i++)
150         RETURN_ON_ERROR(m_chunkManager->saveBlock(pFile, writeBuf, fbo + i));
151 
152     return NO_ERROR;
153 }
154 
writeDBFileNoVBCache(IDBDataFile * pFile,const unsigned char * writeBuf,const int fbo,const int numOfBlock)155 int DctnryCompress1::writeDBFileNoVBCache(IDBDataFile* pFile,
156         const unsigned char* writeBuf, const int fbo,
157         const int numOfBlock)
158 {
159     //int fbo = 0;
160     //RETURN_ON_ERROR(lbidToFbo(lbid, fbo));
161 
162     for (int i = 0; i < numOfBlock; i++)
163         RETURN_ON_ERROR(m_chunkManager->saveBlock(pFile, writeBuf, fbo + i));
164 
165     return NO_ERROR;
166 }
167 
flushFile(int rc,std::map<FID,FID> & columnOids)168 int DctnryCompress1::flushFile(int rc, std::map<FID, FID>& columnOids)
169 {
170     return m_chunkManager->flushChunks(rc, columnOids);
171 }
172 
173 
lbidToFbo(const uint64_t lbid,int & fbo)174 int DctnryCompress1::lbidToFbo(const uint64_t lbid, int& fbo)
175 {
176     uint16_t dbRoot;
177     uint16_t segment;
178     uint32_t partition;
179     return BRMWrapper::getInstance()->getFboOffset(lbid, dbRoot, partition, segment, fbo);
180 }
181 
182 
183 } //end of namespace
184 
185