summaryrefslogtreecommitdiff
path: root/storage/ibmdb2i/db2i_file.h
blob: 6cc6ae8947beb0e88d367ff1e3bcb173d74f5b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/*
Licensed Materials - Property of IBM
DB2 Storage Engine Enablement
Copyright IBM Corporation 2007,2008
All rights reserved

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met: 
 (a) Redistributions of source code must retain this list of conditions, the
     copyright notice in section {d} below, and the disclaimer following this
     list of conditions. 
 (b) Redistributions in binary form must reproduce this list of conditions, the
     copyright notice in section (d) below, and the disclaimer following this
     list of conditions, in the documentation and/or other materials provided
     with the distribution. 
 (c) The name of IBM may not be used to endorse or promote products derived from
     this software without specific prior written permission. 
 (d) The text of the required copyright notice is: 
       Licensed Materials - Property of IBM
       DB2 Storage Engine Enablement 
       Copyright IBM Corporation 2007,2008 
       All rights reserved

THIS SOFTWARE IS PROVIDED BY IBM CORPORATION "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL IBM CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/


#ifndef DB2I_FILE_H
#define DB2I_FILE_H

#include "db2i_global.h"
#include "db2i_ileBridge.h"
#include "db2i_validatedPointer.h"
#include "my_atomic.h"
#include "db2i_iconv.h"
#include "db2i_charsetSupport.h"

const char FID_EXT[] = ".FID";

class db2i_file;
  
#pragma pack(1)
struct DB2LobField
{
  char reserved1;
  uint32 length;
  char reserved2[4];
  uint32 ordinal;
  ILEMemHandle dataHandle;
  char reserved3[8];
};
#pragma pack(pop)

class DB2Field
{ 
  public:
    uint16 getType() const { return *(uint16*)(&definition.ColType); }
    uint16 getByteLengthInRecord() const { return definition.ColLen; }
    uint16 getDataLengthInRecord() const
    { 
      return (getType() == QMY_VARCHAR || getType() == QMY_VARGRAPHIC ? definition.ColLen - 2 : definition.ColLen); 
    }
    uint16 getCCSID() const { return *(uint16*)(&definition.ColCCSID); }
    bool isBlob() const
    { 
      uint16 type = getType();
      return (type == QMY_BLOBCLOB || type == QMY_DBCLOB); 
    }
    uint16 getBufferOffset() const { return definition.ColBufOff; }
    uint16 calcBlobPad() const
    {
      DBUG_ASSERT(isBlob());
      return getByteLengthInRecord() - sizeof (DB2LobField);
    }
    DB2LobField* asBlobField(char* buf) const
    {
      DBUG_ASSERT(isBlob());
      return (DB2LobField*)(buf + getBufferOffset() + calcBlobPad());
    }
  private:
   col_def_t definition;
};
  

/**
  @class db2i_table
  
  @details 
  This class describes the logical SQL table provided by DB2. 
  It stores "table-scoped" information such as the name of the
  DB2 schema, BLOB descriptions, and the corresponding MySQL table definition.
  Only one instance exists per SQL table.
*/
class db2i_table
{
  public: 
  enum NameFormatFlags
  {
    ASCII_SQL,
    ASCII_NATIVE,
    EBCDIC_NATIVE
  };
    
  db2i_table(const TABLE_SHARE* myTable, const char* path = NULL);
  
  ~db2i_table();

  int32 initDB2Objects(const char* path);

  const TABLE_SHARE* getMySQLTable() const
  {
    return mysqlTable;
  }
  
  uint64 getStartId() const
  {
    return db2StartId;
  }

  void updateStartId(uint64 newStartId)
  {
     db2StartId = newStartId;
  }

  bool hasBlobs() const
  {
    return (blobFieldCount > 0);
  }
  
  uint16 getBlobCount() const
  {
    return blobFieldCount;
  }
  
  uint getBlobFieldActualSize(uint fieldIndex) const
  {
    return blobFieldActualSizes[getBlobIdFromField(fieldIndex)];
  }

  void updateBlobFieldActualSize(uint fieldIndex, uint32 newSize)
  {
    // It's OK that this isn't threadsafe, since this is just an advisory
    // value. If a race condition causes the lesser of two values to be stored,
    // that's OK.
    uint16 blobID = getBlobIdFromField(fieldIndex);
    DBUG_ASSERT(blobID < blobFieldCount);
    
    if (blobFieldActualSizes[blobID] < newSize)
    {
      blobFieldActualSizes[blobID] = newSize;
    }
  }

  
  
  const char* getDB2LibName(NameFormatFlags format = EBCDIC_NATIVE)
  {
    switch (format)
    {
      case EBCDIC_NATIVE:
        return db2LibNameEbcdic; break;
      case ASCII_NATIVE:
        return db2LibNameAscii; break;
      case ASCII_SQL:
        return db2LibNameSQLAscii; break;
      default:
        DBUG_ASSERT(0);
    }
    return NULL;
  }
  
  const char* getDB2TableName(NameFormatFlags format = EBCDIC_NATIVE) const
  {
    switch (format)
    {
      case EBCDIC_NATIVE:
        return db2TableNameEbcdic; break;
      case ASCII_NATIVE:
        return db2TableNameAscii; break;
      case ASCII_SQL:
        return db2TableNameAscii; break;
        break;
      default:
        DBUG_ASSERT(0);
    }
    return NULL;
  }
  
  DB2Field& db2Field(int fieldID) const { return db2Fields[fieldID]; }
  DB2Field& db2Field(const Field* field) const { return db2Field(field->field_index); }

  void processFormatSpace();
  
  void* getFormatSpace(size_t& spaceNeeded)
  {
    DBUG_ASSERT(formatSpace == NULL);
    spaceNeeded = sizeof(format_hdr_t) + mysqlTable->fields * sizeof(DB2Field);
    formatSpace.alloc(spaceNeeded);
    return (void*)formatSpace;
  }  
  
  bool isTemporary() const
  {
    return isTemporaryTable;
  }
  
  void getDB2QualifiedName(char* to);
  static void getDB2LibNameFromPath(const char* path, char* lib, NameFormatFlags format=ASCII_SQL);
  static void getDB2FileNameFromPath(const char* path, char* file, NameFormatFlags format=ASCII_SQL);
  static void getDB2QualifiedNameFromPath(const char* path, char* to);
  static int32 appendQualifiedIndexFileName(const char* indexName, 
                                            const char* tableName, 
                                            String& to, 
                                            NameFormatFlags format=ASCII_SQL,
                                            enum_DB2I_INDEX_TYPE type=typeDefault);
  
  uint16 getBlobIdFromField(uint16 fieldID) const
  {
    for (int i = 0; i < blobFieldCount; ++i)
    {
      if (blobFields[i] == fieldID)
        return i;
    }
    DBUG_ASSERT(0);
    return 0;
  }
    
  iconv_t& getConversionDefinition(enum_conversionDirection direction,
                                   uint16 fieldID)
  {
    if (conversionDefinitions[direction][fieldID] == (iconv_t)(-1))
      findConversionDefinition(direction, fieldID);
    
    return conversionDefinitions[direction][fieldID];
  }
  
  const db2i_file* dataFile() const
  {
    return physicalFile;
  }
  
  const db2i_file* indexFile(uint idx) const
  {    
    return logicalFiles[idx];
  }
  
  const char* getFileLevelID() const
  {
    return fileLevelID;
  }

  static void deleteAssocFiles(const char* name);
  static void renameAssocFiles(const char* from, const char* to);

  int fastInitForCreate(const char* path);
  int initDiscoveredTable(const char* path);
      
  uint16* blobFields;
 
private: 

  void findConversionDefinition(enum_conversionDirection direction, uint16 fieldID);
  static void filenameToTablename(const char* in, char* out, size_t outlen);  
  static size_t smartFilenameToTableName(const char *in, char* out, size_t outlen);
  void convertNativeToSQLName(const char* input, 
                              char* output) 
  {
    
    output[0] = input[0];
    
    uint o = 1;
    uint i = 1;
    do
    {
      output[o++] = input[i];
      if (input[i] == '"' && input[i+1])
        output[o++] = '"';
    } while (input[++i]);

    output[o] = 0; // This isn't the most user-friendly way to handle overflows,
                                    // but at least its safe.
  }

  bool doFileIDsMatch(const char* path);
    
  ValidatedPointer<format_hdr_t> formatSpace;
  DB2Field* db2Fields;
  uint64 db2StartId;          // Starting value for identity column
  uint16 blobFieldCount; // Count of LOB fields in the DB2 table
  uint* blobFieldActualSizes; // Array of LOB field lengths (actual vs. allocated).
                              // This is updated as LOBs are read and will contain
                              // the length of the longest known LOB in that field.
  iconv_t* conversionDefinitions[2];
  
  const TABLE_SHARE* mysqlTable;
  uint16 logicalFileCount;
  char* db2LibNameEbcdic; // Quoted and in EBCDIC
  char* db2LibNameAscii;
  char* db2TableNameEbcdic;
  char* db2TableNameAscii;
  char* db2TableNameSQLAscii;
  char* db2LibNameSQLAscii;
          
  db2i_file* physicalFile;
  db2i_file** logicalFiles;
  
  bool isTemporaryTable;
  char fileLevelID[13];
};

/**
  @class db2i_file

  @details  This class describes a file object underlaying a particular SQL
  table. Both "physical files" (data) and "logical files" (indices) are
  described by this class. Only one instance of the class exists per DB2 file
  object. The single instance is responsible for de/allocating the multiple
  handles used by the handlers.
*/
class db2i_file
{

public: 
  struct RowFormat
  {
    uint16 readRowLen;
    uint16 readRowNullOffset;
    uint16 writeRowLen;
    uint16 writeRowNullOffset;
    char inited;
  };
  
public:

  // Construct an instance for a physical file.
  db2i_file(db2i_table* table);
    
  // Construct an instance for a logical file.
  db2i_file(db2i_table* table, int index);
   
  ~db2i_file()
  {
    if (masterDefn)
      db2i_ileBridge::getBridgeForThread()->deallocateFile(masterDefn);
    
    if (db2FileName != (char*)db2Table->getDB2TableName(db2i_table::EBCDIC_NATIVE))
      my_free(db2FileName);
  }

  // This is roughly equivalent to an "open". It tells ILE to allocate a descriptor
  // for the file. The associated handle is returned to the caller.
  int allocateNewInstance(FILE_HANDLE* newHandle, ILEMemHandle inuseSpace) const
  {
    int rc;
    
    rc = db2i_ileBridge::getBridgeForThread()->allocateFileInstance(masterDefn,
                                                                    inuseSpace,
                                                                    newHandle);
    
    if (rc) *newHandle = 0;
       
    return rc;
  }
  
  // This obtains the row layout associated with a particular access intent for
  // an open instance of the file.
  int obtainRowFormat(FILE_HANDLE instanceHandle, 
                       char intent,
                       char commitLevel,
                       const RowFormat** activeFormat) const
  {
    DBUG_ENTER("db2i_file::obtainRowFormat");    
    RowFormat* rowFormat;
        
    if (intent == QMY_UPDATABLE)
      rowFormat = &(formats[readWrite]);
    else if (intent == QMY_READ_ONLY)
      rowFormat = &(formats[readOnly]);
        
    if (unlikely(!rowFormat->inited))
    {
      int rc = db2i_ileBridge::getBridgeForThread()->
                                 initFileForIO(instanceHandle,
                                               intent,
                                               commitLevel,
                                               &(rowFormat->writeRowLen),
                                               &(rowFormat->writeRowNullOffset),
                                               &(rowFormat->readRowLen),
                                               &(rowFormat->readRowNullOffset));
      if (rc) DBUG_RETURN(rc);
      rowFormat->inited = 1;
    }

    *activeFormat = rowFormat;
    DBUG_RETURN(0);
  }  
    
  const char* getDB2FileName() const
  {
    return db2FileName; 
  }
  
  void fillILEDefn(ShrDef* defn, bool readInArrivalSeq);

  void setMasterDefnHandle(FILE_HANDLE handle)
  {
    masterDefn = handle;
  }
  
  FILE_HANDLE getMasterDefnHandle() const 
  {
    return masterDefn;
  }
  
private:  
  enum RowFormats
  {
    readOnly = 0,
    readWrite,
    maxRowFormats
  };
    
  mutable RowFormat formats[maxRowFormats];
  
  void commonCtorInit();
  
  char* db2FileName; // Quoted and in EBCDIC

  db2i_table* db2Table;  // The logical SQL table contained by this file.
  
  bool db2CanSort;
  
  FILE_HANDLE masterDefn;
};


#endif