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
|
/*
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.
*/
#include "db2i_ioBuffers.h"
/**
Request another block of rows
Request the next set of rows from DB2. This must only be called after
newReadRequest().
@param orientation The direction to use when reading through the table.
*/
void IOAsyncReadBuffer::loadNewRows(char orientation)
{
rewind();
maxRows() = rowsToBlock;
DBUG_PRINT("db2i_ioBuffers::loadNewRows", ("Requesting %d rows, async = %d", rowsToBlock, readIsAsync));
rc = getBridge()->expectErrors(QMY_ERR_END_OF_BLOCK, QMY_ERR_LOB_SPACE_TOO_SMALL)
->read(file,
ptr(),
accessIntent,
commitLevel,
orientation,
readIsAsync,
rrnList,
0,
0,
0);
DBUG_PRINT("db2i_ioBuffers::loadNewRows", ("recordsRead: %d, rc: %d", (uint32)rowCount(), rc));
*releaseRowNeeded = true;
if (rc == QMY_ERR_END_OF_BLOCK)
{
// This is really just an informational error, so we ignore it.
rc = 0;
DBUG_PRINT("db2i_ioBuffers::loadNewRows", ("End of block signalled"));
}
else if (rc == QMY_ERR_END_OF_FILE)
{
// If we reach EOF or end-of-key, DB2 guarantees that no rows will be locked.
rc = HA_ERR_END_OF_FILE;
*releaseRowNeeded = false;
}
else if (rc == QMY_ERR_KEY_NOT_FOUND)
{
rc = HA_ERR_KEY_NOT_FOUND;
*releaseRowNeeded = false;
}
if (rc) closePipe();
}
/**
Empty the message pipe to prepare for another read.
*/
void IOAsyncReadBuffer::drainPipe()
{
DBUG_ASSERT(pipeState == PendingFullBufferMsg);
PipeRpy_t msg[32];
int bytes;
PipeRpy_t* lastMsg;
while ((bytes = read(msgPipe, msg, sizeof(msg))) > 0)
{
DBUG_PRINT("db2i_ioBuffers::drainPipe",("Pipe returned %d bytes", bytes));
lastMsg = &msg[bytes / (sizeof(msg[0]))-1];
if (lastMsg->CumRowCnt == maxRows() ||
lastMsg->RtnCod != 0)
{
pipeState = ConsumedFullBufferMsg;
break;
}
}
DBUG_PRINT("db2i_ioBuffers::drainPipe",("rc = %d, rows = %d, max = %d", lastMsg->RtnCod, lastMsg->CumRowCnt, (uint32)maxRows()));
}
/**
Poll the message pipe for async read messages
Only valid in async
@param orientation The direction to use when reading through the table.
*/
void IOAsyncReadBuffer::pollNextRow(char orientation)
{
DBUG_ASSERT(readIsAsync);
// Handle the case in which the buffer is full.
if (rowCount() == maxRows())
{
// If we haven't read to the end, exit here.
if (readCursor < rowCount())
return;
if (pipeState == PendingFullBufferMsg)
drainPipe();
if (pipeState == ConsumedFullBufferMsg)
loadNewRows(orientation);
}
if (!rc)
{
PipeRpy_t* lastMsg = NULL;
while (true)
{
PipeRpy_t msg[32];
int bytes = read(msgPipe, msg, sizeof(msg));
DBUG_PRINT("db2i_ioBuffers::pollNextRow",("Pipe returned %d bytes", bytes));
if (unlikely(bytes < 0))
{
DBUG_PRINT("db2i_ioBuffers::pollNextRow", ("Error"));
rc = errno;
break;
}
else if (bytes == 0)
break;
DBUG_ASSERT(bytes % sizeof(msg[0]) == 0);
lastMsg = &msg[bytes / (sizeof(msg[0]))-1];
if (lastMsg->RtnCod || (lastMsg->CumRowCnt == usedRows()))
{
rc = lastMsg->RtnCod;
break;
}
}
*releaseRowNeeded = true;
if (rc == QMY_ERR_END_OF_BLOCK)
rc = 0;
else if (rc == QMY_ERR_END_OF_FILE)
{
// If we reach EOF or end-of-key, DB2 guarantees that no rows will be locked.
rc = HA_ERR_END_OF_FILE;
*releaseRowNeeded = false;
}
else if (rc == QMY_ERR_KEY_NOT_FOUND)
{
rc = HA_ERR_KEY_NOT_FOUND;
*releaseRowNeeded = false;
}
if (lastMsg)
DBUG_PRINT("db2i_ioBuffers::pollNextRow", ("Good data: rc=%d; rows=%d; usedRows=%d", lastMsg->RtnCod, lastMsg->CumRowCnt, (uint32)usedRows()));
if (lastMsg && likely(!rc))
{
if (lastMsg->CumRowCnt < maxRows())
pipeState = PendingFullBufferMsg;
else
pipeState = ConsumedFullBufferMsg;
DBUG_ASSERT(lastMsg->CumRowCnt <= usedRows());
}
DBUG_ASSERT(rowCount() <= getRowCapacity());
}
DBUG_PRINT("db2i_ioBuffers::pollNextRow", ("filledRows: %d, rc: %d", rowCount(), rc));
if (rc) closePipe();
}
/**
Prepare for the destruction of the row buffer storage.
*/
void IOAsyncReadBuffer::prepForFree()
{
interruptRead();
rewind();
IORowBuffer::prepForFree();
}
/**
Initialize the newly allocated storage.
@param sizeChanged Indicates whether the storage capacity is being changed.
*/
void IOAsyncReadBuffer::initAfterAllocate(bool sizeChanged)
{
rewind();
if (sizeChanged || ((void*)rrnList == NULL))
rrnList.realloc(getRowCapacity() * sizeof(uint32));
}
/**
Send an initial read request
@param infile The file (table/index) being read from
@param orientation The orientation to use for this read request
@param rowsToBuffer The number of rows to request each time
@param useAsync Whether reads should be performed asynchronously.
@param key The key to use (if any)
@param keyLength The length of key (if any)
@param keyParts The number of columns in the key (if any)
*/
void IOAsyncReadBuffer::newReadRequest(FILE_HANDLE infile,
char orientation,
uint32 rowsToBuffer,
bool useAsync,
ILEMemHandle key,
int keyLength,
int keyParts)
{
DBUG_ENTER("db2i_ioBuffers::newReadRequest");
DBUG_ASSERT(rowsToBuffer <= getRowCapacity());
#ifndef DBUG_OFF
if (readCursor < rowCount())
DBUG_PRINT("PERF:",("Wasting %d buffered rows!\n", rowCount() - readCursor));
#endif
int fildes[2];
int ileDescriptor = QMY_REUSE;
interruptRead();
if (likely(useAsync))
{
if (rowsToBuffer == 1)
{
// Async provides little or no benefit for single row reads, so we turn it off
DBUG_PRINT("db2i_ioBuffers::newReadRequest", ("Disabling async"));
useAsync = false;
}
else
{
rc = pipe(fildes);
if (rc) DBUG_VOID_RETURN;
// Translate the pipe write descriptor into the equivalent ILE descriptor
rc = fstatx(fildes[1], (struct stat*)&ileDescriptor, sizeof(ileDescriptor), STX_XPFFD_PASE);
if (rc)
{
close(fildes[0]);
close(fildes[1]);
DBUG_VOID_RETURN;
}
pipeState = Untouched;
msgPipe = fildes[0];
DBUG_PRINT("db2i_ioBuffers::newReadRequest", ("Opened pipe %d", fildes[0]));
}
}
file = infile;
readIsAsync = useAsync;
rowsToBlock = rowsToBuffer;
rewind();
maxRows() = 1;
rc = getBridge()->expectErrors(QMY_ERR_END_OF_BLOCK, QMY_ERR_LOB_SPACE_TOO_SMALL)
->read(file,
ptr(),
accessIntent,
commitLevel,
orientation,
useAsync,
rrnList,
key,
keyLength,
keyParts,
ileDescriptor);
// Having shared the pipe with ILE, we relinquish our claim on the write end
// of the pipe.
if (useAsync)
close(fildes[1]);
// If we reach EOF or end-of-key, DB2 guarantees that no rows will be locked.
if (rc == QMY_ERR_END_OF_FILE)
{
rc = HA_ERR_END_OF_FILE;
*releaseRowNeeded = false;
}
else if (rc == QMY_ERR_KEY_NOT_FOUND)
{
if (rowCount())
rc = HA_ERR_END_OF_FILE;
else
rc = HA_ERR_KEY_NOT_FOUND;
*releaseRowNeeded = false;
}
else
*releaseRowNeeded = true;
DBUG_VOID_RETURN;
}
|