You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

446 lines
12 KiB

  1. /*
  2. Licensed Materials - Property of IBM
  3. DB2 Storage Engine Enablement
  4. Copyright IBM Corporation 2007,2008
  5. All rights reserved
  6. Redistribution and use in source and binary forms, with or without modification,
  7. are permitted provided that the following conditions are met:
  8. (a) Redistributions of source code must retain this list of conditions, the
  9. copyright notice in section {d} below, and the disclaimer following this
  10. list of conditions.
  11. (b) Redistributions in binary form must reproduce this list of conditions, the
  12. copyright notice in section (d) below, and the disclaimer following this
  13. list of conditions, in the documentation and/or other materials provided
  14. with the distribution.
  15. (c) The name of IBM may not be used to endorse or promote products derived from
  16. this software without specific prior written permission.
  17. (d) The text of the required copyright notice is:
  18. Licensed Materials - Property of IBM
  19. DB2 Storage Engine Enablement
  20. Copyright IBM Corporation 2007,2008
  21. All rights reserved
  22. THIS SOFTWARE IS PROVIDED BY IBM CORPORATION "AS IS" AND ANY EXPRESS OR IMPLIED
  23. WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  24. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  25. SHALL IBM CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  27. OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  30. IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  31. OF SUCH DAMAGE.
  32. */
  33. #ifndef DB2I_FILE_H
  34. #define DB2I_FILE_H
  35. #include "db2i_global.h"
  36. #include "db2i_ileBridge.h"
  37. #include "db2i_validatedPointer.h"
  38. #include "my_atomic.h"
  39. #include "db2i_iconv.h"
  40. #include "db2i_charsetSupport.h"
  41. const char FID_EXT[] = ".FID";
  42. class db2i_file;
  43. #pragma pack(1)
  44. struct DB2LobField
  45. {
  46. char reserved1;
  47. uint32 length;
  48. char reserved2[4];
  49. uint32 ordinal;
  50. ILEMemHandle dataHandle;
  51. char reserved3[8];
  52. };
  53. #pragma pack(pop)
  54. class DB2Field
  55. {
  56. public:
  57. uint16 getType() const { return *(uint16*)(&definition.ColType); }
  58. uint16 getByteLengthInRecord() const { return definition.ColLen; }
  59. uint16 getDataLengthInRecord() const
  60. {
  61. return (getType() == QMY_VARCHAR || getType() == QMY_VARGRAPHIC ? definition.ColLen - 2 : definition.ColLen);
  62. }
  63. uint16 getCCSID() const { return *(uint16*)(&definition.ColCCSID); }
  64. bool isBlob() const
  65. {
  66. uint16 type = getType();
  67. return (type == QMY_BLOBCLOB || type == QMY_DBCLOB);
  68. }
  69. uint16 getBufferOffset() const { return definition.ColBufOff; }
  70. uint16 calcBlobPad() const
  71. {
  72. DBUG_ASSERT(isBlob());
  73. return getByteLengthInRecord() - sizeof (DB2LobField);
  74. }
  75. DB2LobField* asBlobField(char* buf) const
  76. {
  77. DBUG_ASSERT(isBlob());
  78. return (DB2LobField*)(buf + getBufferOffset() + calcBlobPad());
  79. }
  80. private:
  81. col_def_t definition;
  82. };
  83. /**
  84. @class db2i_table
  85. @details
  86. This class describes the logical SQL table provided by DB2.
  87. It stores "table-scoped" information such as the name of the
  88. DB2 schema, BLOB descriptions, and the corresponding MySQL table definition.
  89. Only one instance exists per SQL table.
  90. */
  91. class db2i_table
  92. {
  93. public:
  94. enum NameFormatFlags
  95. {
  96. ASCII_SQL,
  97. ASCII_NATIVE,
  98. EBCDIC_NATIVE
  99. };
  100. db2i_table(const TABLE_SHARE* myTable, const char* path = NULL);
  101. ~db2i_table();
  102. int32 initDB2Objects(const char* path);
  103. const TABLE_SHARE* getMySQLTable() const
  104. {
  105. return mysqlTable;
  106. }
  107. uint64 getStartId() const
  108. {
  109. return db2StartId;
  110. }
  111. void updateStartId(uint64 newStartId)
  112. {
  113. db2StartId = newStartId;
  114. }
  115. bool hasBlobs() const
  116. {
  117. return (blobFieldCount > 0);
  118. }
  119. uint16 getBlobCount() const
  120. {
  121. return blobFieldCount;
  122. }
  123. uint getBlobFieldActualSize(uint fieldIndex) const
  124. {
  125. return blobFieldActualSizes[getBlobIdFromField(fieldIndex)];
  126. }
  127. void updateBlobFieldActualSize(uint fieldIndex, uint32 newSize)
  128. {
  129. // It's OK that this isn't threadsafe, since this is just an advisory
  130. // value. If a race condition causes the lesser of two values to be stored,
  131. // that's OK.
  132. uint16 blobID = getBlobIdFromField(fieldIndex);
  133. DBUG_ASSERT(blobID < blobFieldCount);
  134. if (blobFieldActualSizes[blobID] < newSize)
  135. {
  136. blobFieldActualSizes[blobID] = newSize;
  137. }
  138. }
  139. const char* getDB2LibName(NameFormatFlags format = EBCDIC_NATIVE)
  140. {
  141. switch (format)
  142. {
  143. case EBCDIC_NATIVE:
  144. return db2LibNameEbcdic; break;
  145. case ASCII_NATIVE:
  146. return db2LibNameAscii; break;
  147. case ASCII_SQL:
  148. return db2LibNameSQLAscii; break;
  149. default:
  150. DBUG_ASSERT(0);
  151. }
  152. return NULL;
  153. }
  154. const char* getDB2TableName(NameFormatFlags format = EBCDIC_NATIVE) const
  155. {
  156. switch (format)
  157. {
  158. case EBCDIC_NATIVE:
  159. return db2TableNameEbcdic; break;
  160. case ASCII_NATIVE:
  161. return db2TableNameAscii; break;
  162. case ASCII_SQL:
  163. return db2TableNameAscii; break;
  164. break;
  165. default:
  166. DBUG_ASSERT(0);
  167. }
  168. return NULL;
  169. }
  170. DB2Field& db2Field(int fieldID) const { return db2Fields[fieldID]; }
  171. DB2Field& db2Field(const Field* field) const { return db2Field(field->field_index); }
  172. void processFormatSpace();
  173. void* getFormatSpace(size_t& spaceNeeded)
  174. {
  175. DBUG_ASSERT(formatSpace == NULL);
  176. spaceNeeded = sizeof(format_hdr_t) + mysqlTable->fields * sizeof(DB2Field);
  177. formatSpace.alloc(spaceNeeded);
  178. return (void*)formatSpace;
  179. }
  180. bool isTemporary() const
  181. {
  182. return isTemporaryTable;
  183. }
  184. void getDB2QualifiedName(char* to);
  185. static void getDB2LibNameFromPath(const char* path, char* lib, NameFormatFlags format=ASCII_SQL);
  186. static void getDB2FileNameFromPath(const char* path, char* file, NameFormatFlags format=ASCII_SQL);
  187. static void getDB2QualifiedNameFromPath(const char* path, char* to);
  188. static int32 appendQualifiedIndexFileName(const char* indexName,
  189. const char* tableName,
  190. String& to,
  191. NameFormatFlags format=ASCII_SQL,
  192. enum_DB2I_INDEX_TYPE type=typeDefault);
  193. uint16 getBlobIdFromField(uint16 fieldID) const
  194. {
  195. for (int i = 0; i < blobFieldCount; ++i)
  196. {
  197. if (blobFields[i] == fieldID)
  198. return i;
  199. }
  200. DBUG_ASSERT(0);
  201. return 0;
  202. }
  203. iconv_t& getConversionDefinition(enum_conversionDirection direction,
  204. uint16 fieldID)
  205. {
  206. if (conversionDefinitions[direction][fieldID] == (iconv_t)(-1))
  207. findConversionDefinition(direction, fieldID);
  208. return conversionDefinitions[direction][fieldID];
  209. }
  210. const db2i_file* dataFile() const
  211. {
  212. return physicalFile;
  213. }
  214. const db2i_file* indexFile(uint idx) const
  215. {
  216. return logicalFiles[idx];
  217. }
  218. const char* getFileLevelID() const
  219. {
  220. return fileLevelID;
  221. }
  222. static void deleteAssocFiles(const char* name);
  223. static void renameAssocFiles(const char* from, const char* to);
  224. int fastInitForCreate(const char* path);
  225. int initDiscoveredTable(const char* path);
  226. uint16* blobFields;
  227. private:
  228. void findConversionDefinition(enum_conversionDirection direction, uint16 fieldID);
  229. static void filenameToTablename(const char* in, char* out, size_t outlen);
  230. static size_t smartFilenameToTableName(const char *in, char* out, size_t outlen);
  231. void convertNativeToSQLName(const char* input,
  232. char* output)
  233. {
  234. output[0] = input[0];
  235. uint o = 1;
  236. uint i = 1;
  237. do
  238. {
  239. output[o++] = input[i];
  240. if (input[i] == '"' && input[i+1])
  241. output[o++] = '"';
  242. } while (input[++i]);
  243. output[o] = 0; // This isn't the most user-friendly way to handle overflows,
  244. // but at least its safe.
  245. }
  246. bool doFileIDsMatch(const char* path);
  247. ValidatedPointer<format_hdr_t> formatSpace;
  248. DB2Field* db2Fields;
  249. uint64 db2StartId; // Starting value for identity column
  250. uint16 blobFieldCount; // Count of LOB fields in the DB2 table
  251. uint* blobFieldActualSizes; // Array of LOB field lengths (actual vs. allocated).
  252. // This is updated as LOBs are read and will contain
  253. // the length of the longest known LOB in that field.
  254. iconv_t* conversionDefinitions[2];
  255. const TABLE_SHARE* mysqlTable;
  256. uint16 logicalFileCount;
  257. char* db2LibNameEbcdic; // Quoted and in EBCDIC
  258. char* db2LibNameAscii;
  259. char* db2TableNameEbcdic;
  260. char* db2TableNameAscii;
  261. char* db2TableNameSQLAscii;
  262. char* db2LibNameSQLAscii;
  263. db2i_file* physicalFile;
  264. db2i_file** logicalFiles;
  265. bool isTemporaryTable;
  266. char fileLevelID[13];
  267. };
  268. /**
  269. @class db2i_file
  270. @details This class describes a file object underlaying a particular SQL
  271. table. Both "physical files" (data) and "logical files" (indices) are
  272. described by this class. Only one instance of the class exists per DB2 file
  273. object. The single instance is responsible for de/allocating the multiple
  274. handles used by the handlers.
  275. */
  276. class db2i_file
  277. {
  278. public:
  279. struct RowFormat
  280. {
  281. uint16 readRowLen;
  282. uint16 readRowNullOffset;
  283. uint16 writeRowLen;
  284. uint16 writeRowNullOffset;
  285. char inited;
  286. };
  287. public:
  288. // Construct an instance for a physical file.
  289. db2i_file(db2i_table* table);
  290. // Construct an instance for a logical file.
  291. db2i_file(db2i_table* table, int index);
  292. ~db2i_file()
  293. {
  294. if (masterDefn)
  295. db2i_ileBridge::getBridgeForThread()->deallocateFile(masterDefn);
  296. if (db2FileName != (char*)db2Table->getDB2TableName(db2i_table::EBCDIC_NATIVE))
  297. my_free(db2FileName, MYF(0));
  298. }
  299. // This is roughly equivalent to an "open". It tells ILE to allocate a descriptor
  300. // for the file. The associated handle is returned to the caller.
  301. int allocateNewInstance(FILE_HANDLE* newHandle, ILEMemHandle inuseSpace) const
  302. {
  303. int rc;
  304. rc = db2i_ileBridge::getBridgeForThread()->allocateFileInstance(masterDefn,
  305. inuseSpace,
  306. newHandle);
  307. if (rc) *newHandle = 0;
  308. return rc;
  309. }
  310. // This obtains the row layout associated with a particular access intent for
  311. // an open instance of the file.
  312. int obtainRowFormat(FILE_HANDLE instanceHandle,
  313. char intent,
  314. char commitLevel,
  315. const RowFormat** activeFormat) const
  316. {
  317. DBUG_ENTER("db2i_file::obtainRowFormat");
  318. RowFormat* rowFormat;
  319. if (intent == QMY_UPDATABLE)
  320. rowFormat = &(formats[readWrite]);
  321. else if (intent == QMY_READ_ONLY)
  322. rowFormat = &(formats[readOnly]);
  323. if (unlikely(!rowFormat->inited))
  324. {
  325. int rc = db2i_ileBridge::getBridgeForThread()->
  326. initFileForIO(instanceHandle,
  327. intent,
  328. commitLevel,
  329. &(rowFormat->writeRowLen),
  330. &(rowFormat->writeRowNullOffset),
  331. &(rowFormat->readRowLen),
  332. &(rowFormat->readRowNullOffset));
  333. if (rc) DBUG_RETURN(rc);
  334. rowFormat->inited = 1;
  335. }
  336. *activeFormat = rowFormat;
  337. DBUG_RETURN(0);
  338. }
  339. const char* getDB2FileName() const
  340. {
  341. return db2FileName;
  342. }
  343. void fillILEDefn(ShrDef* defn, bool readInArrivalSeq);
  344. void setMasterDefnHandle(FILE_HANDLE handle)
  345. {
  346. masterDefn = handle;
  347. }
  348. FILE_HANDLE getMasterDefnHandle() const
  349. {
  350. return masterDefn;
  351. }
  352. private:
  353. enum RowFormats
  354. {
  355. readOnly = 0,
  356. readWrite,
  357. maxRowFormats
  358. };
  359. mutable RowFormat formats[maxRowFormats];
  360. void commonCtorInit();
  361. char* db2FileName; // Quoted and in EBCDIC
  362. db2i_table* db2Table; // The logical SQL table contained by this file.
  363. bool db2CanSort;
  364. FILE_HANDLE masterDefn;
  365. };
  366. #endif