File: zlob0read.cc

package info (click to toggle)
mysql-8.0 8.0.44-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,272,892 kB
  • sloc: cpp: 4,685,345; ansic: 412,712; pascal: 108,395; java: 83,641; perl: 30,221; cs: 27,067; sql: 26,594; python: 21,816; sh: 17,285; yacc: 17,169; php: 11,522; xml: 7,388; javascript: 7,083; makefile: 1,793; lex: 1,075; awk: 670; asm: 520; objc: 183; ruby: 97; lisp: 86
file content (352 lines) | stat: -rw-r--r-- 11,471 bytes parent folder | download
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
/*****************************************************************************

Copyright (c) 2016, 2025, Oracle and/or its affiliates.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License, version 2.0, as published by the
Free Software Foundation.

This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation.  The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.

This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA

*****************************************************************************/

#include "zlob0read.h"
#include "lob0impl.h"
#include "lob0lob.h"
#include "lob0zip.h"
#include "my_dbug.h"
#include "trx0trx.h"
#include "univ.i"
#include "ut0ut.h"
#include "zlob0first.h"

namespace lob {

/** Fetch a compressed large object (ZLOB) from the system.
@param[in] ctx    the read context information.
@param[in] ref    the LOB reference identifying the LOB.
@param[in] offset read the LOB from the given offset.
@param[in] len    the length of LOB data that needs to be fetched.
@param[out] buf   the output buffer (owned by caller) of minimum len bytes.
@return the amount of data (in bytes) that was actually read. */
ulint z_read(ReadContext *ctx, lob::ref_t ref, ulint offset, ulint len,
             byte *buf) {
  ut_ad(offset == 0);
  ut_ad(len > 0);

  const ulint avail_lob = ref.length();

  if (avail_lob == 0) {
    return (0);
  }

  if (ref.is_being_modified()) {
    /* This should happen only for READ UNCOMMITTED transactions. */
    ut_ad(ctx->assert_read_uncommitted());
    return (0);
  }

  const uint32_t lob_version = ref.version();

  fil_addr_t old_node_loc = fil_addr_null;
  fil_addr_t node_loc = fil_addr_null;

  ut_ad(ctx->m_index->is_clustered());
  ut_ad(ctx->m_space_id == ref.space_id());

  const page_no_t first_page_no = ref.page_no();
  const space_id_t space_id = ctx->m_space_id;
  const page_id_t first_page_id(space_id, first_page_no);

  mtr_t mtr;
  mtr_start(&mtr);

  /* The current entry - it is the latest version. */
  z_index_entry_t cur_entry(&mtr, ctx->m_index);

  z_first_page_t first(&mtr, ctx->m_index);
  first.load_x(first_page_no);

  page_type_t page_type = first.get_page_type();

  if (page_type == FIL_PAGE_TYPE_ZBLOB || page_type == FIL_PAGE_SDI_ZBLOB) {
    mtr_commit(&mtr);
    lob::zReader reader(*ctx);
    reader.fetch();
    return (reader.length());
  }

  if (page_type != FIL_PAGE_TYPE_ZLOB_FIRST) {
    /* Assume that the BLOB has been freed and return without taking further
    action.  This condition is hit when there are stale LOB references in the
    clustered index record, especially when there are server crashes during
    updation of delete-marked clustered index record with external fields. */
    mtr_commit(&mtr);
    return (0);
  }

  flst_base_node_t *flst = first.index_list();

#ifdef UNIV_DEBUG
  /* The length of this list cannot be equal to 0 */
  ulint flst_len = flst_get_len(flst);
  ut_a(flst_len != 0);

  std::vector<trx_id_t> trxids;
#endif /* UNIV_DEBUG */

  node_loc = flst_get_first(flst, &mtr);

  byte *ptr = buf;
  ulint remain = len;

  while (remain > 0 && !fil_addr_is_null(node_loc)) {
    flst_node_t *node = first.addr2ptr_x(node_loc);
    cur_entry.reset(node);

#ifdef UNIV_DEBUG
    trxids.push_back(cur_entry.get_trx_id());
#endif /* UNIV_DEBUG */

    /* Obtain the next index entry location. */
    node_loc = cur_entry.get_next();
    const uint32_t entry_lob_version = cur_entry.get_lob_version();

    if (entry_lob_version <= lob_version) {
      /* Can read the entry. */
      ut_ad(entry_lob_version <= lob_version);
      z_read_chunk(ctx->m_index, cur_entry, 0, remain, ptr, &mtr);
    } else {
      ut_ad(entry_lob_version > lob_version);

      /* Cannot read the entry. Look at older versions. */
      flst_base_node_t *ver_lst = cur_entry.get_versions_list();
      old_node_loc = flst_get_first(ver_lst, &mtr);

      bool got_data = false;
      while (!fil_addr_is_null(old_node_loc)) {
        flst_node_t *old_node = first.addr2ptr_x(old_node_loc);

        /* The older version of the current entry. */
        z_index_entry_t old_version(old_node, &mtr, ctx->m_index);
#ifdef UNIV_DEBUG
        trxids.push_back(old_version.get_trx_id());
#endif /* UNIV_DEBUG */
        const uint32_t old_lob_version = old_version.get_lob_version();

        if (old_lob_version <= lob_version) {
          /* Can read the entry. */
          ut_ad(old_version.get_lob_version() <= lob_version);
          z_read_chunk(ctx->m_index, old_version, 0, remain, ptr, &mtr);
          got_data = true;
          break;
        }
        old_node_loc = old_version.get_next();
      }

      /* For this offset, if none of the versions are
      visible, go ahead and read the latest version.  The
      DB_TRX_ID in the clustered index record can be checked
      here to verify that this is correct.*/
      if (!got_data) {
        z_read_chunk(ctx->m_index, cur_entry, 0, remain, ptr, &mtr);
      }
    }

    cur_entry.reset(nullptr);
    mtr_commit(&mtr);
    mtr_start(&mtr);
    first.load_x(first_page_no);
  }

  const ulint total_read = len - remain;
  ut_ad(total_read == len || total_read == avail_lob);
  mtr_commit(&mtr);
  return (total_read);
}

/** Read one data chunk associated with one index entry.
@param[in]      index   The clustered index containing the LOB.
@param[in]      entry   Pointer to the index entry
@param[in]      offset  The offset from which to read the chunk.
@param[in,out]  len     The length of the output buffer. This length can
                        be greater than the chunk size.
@param[in,out]  buf     The output buffer.
@param[in]      mtr     Mini-transaction context.
@return number of bytes copied into the output buffer. */
ulint z_read_chunk(dict_index_t *index, z_index_entry_t &entry, ulint offset,
                   ulint &len, byte *&buf, mtr_t *mtr) {
  const ulint data_len = entry.get_data_len();

  if (entry.get_z_page_no() == FIL_NULL || data_len == 0) {
    return (0);
  }

  const ulint zbuf_size = entry.get_zdata_len();
  std::unique_ptr<byte[]> zbuf(new byte[zbuf_size]);

  ulint zbytes = z_read_strm(index, entry, zbuf.get(), zbuf_size, mtr);
  ut_a(zbytes == zbuf_size);

  z_stream strm;
  strm.zalloc = nullptr;
  strm.zfree = nullptr;
  strm.opaque = nullptr;

  int ret = inflateInit(&strm);
  ut_a(ret == Z_OK);

  strm.avail_in = static_cast<uInt>(zbytes);
  strm.next_in = zbuf.get();

  ulint to_copy = 0;
  if (offset == 0 && len >= data_len) {
    /* The full chunk is needed for output. */
    strm.avail_out = static_cast<uInt>(len);
    strm.next_out = buf;

    ret = inflate(&strm, Z_FINISH);
    ut_a(ret == Z_STREAM_END);

    to_copy = strm.total_out;

  } else {
    /* Only part of the chunk is needed. */
    byte ubuf[Z_CHUNK_SIZE];
    strm.avail_out = Z_CHUNK_SIZE;
    strm.next_out = ubuf;

    ret = inflate(&strm, Z_FINISH);
    ut_a(ret == Z_STREAM_END);

    ulint chunk_size = strm.total_out;
    ut_a(chunk_size == data_len);
    ut_a(offset < chunk_size);

    byte *ptr = ubuf + offset;
    ulint remain = chunk_size - offset;
    to_copy = (len > remain) ? remain : len;
    memcpy(buf, ptr, to_copy);
  }

  len -= to_copy;
  buf += to_copy;
  inflateEnd(&strm);

  return (to_copy);
}

/** Read one zlib stream fully, given its index entry.
@param[in]      index      The index dictionary object.
@param[in]      entry      The index entry (memory copy).
@param[in,out]  zbuf       The output buffer
@param[in]      zbuf_size  The size of the output buffer.
@param[in,out]  mtr        Mini-transaction.
@return the size of the zlib stream.*/
ulint z_read_strm(dict_index_t *index, z_index_entry_t &entry, byte *zbuf,
                  ulint zbuf_size, mtr_t *mtr) {
  page_no_t page_no = entry.get_z_page_no();
  byte *ptr = zbuf;
  ulint remain = zbuf_size;

  while (remain > 0 && page_no != FIL_NULL) {
    buf_block_t *block = buf_page_get(
        page_id_t(dict_index_get_space(index), page_no),
        dict_table_page_size(index->table), RW_X_LATCH, UT_LOCATION_HERE, mtr);

    page_type_t ptype = block->get_page_type();
    byte *data = nullptr;
    ulint data_size = 0;
    if (ptype == FIL_PAGE_TYPE_ZLOB_FRAG) {
      frag_id_t fid = entry.get_z_frag_id();
      z_frag_page_t frag_page(block, mtr, index);
      frag_node_t node = frag_page.get_frag_node(fid);
      data = node.data_begin();
      data_size = node.payload();
    } else if (ptype == FIL_PAGE_TYPE_ZLOB_FIRST) {
      z_first_page_t first(block, mtr, index);
      data = first.begin_data_ptr();
      data_size = first.get_data_len();
    } else {
      ut_a(ptype == FIL_PAGE_TYPE_ZLOB_DATA);
      z_data_page_t data_page(block, mtr, index);
      data = data_page.begin_data_ptr();
      data_size = data_page.get_data_len();
      ut_a(data_size <= data_page.payload());
    }
    memcpy(ptr, data, data_size);
    ptr += data_size;
    ut_a(data_size <= remain);
    remain -= data_size;
    page_no = block->get_next_page_no();
  }

  ulint zbytes = (zbuf_size - remain);
  return (zbytes);
}

#ifdef UNIV_DEBUG
static bool z_validate_strm_low(dict_index_t *index, z_index_entry_t &entry,
                                mtr_t *mtr) {
  /* Expected length of compressed data. */
  const ulint exp_zlen = entry.get_zdata_len();
  page_no_t page_no = entry.get_z_page_no();
  ulint remain = exp_zlen;

  while (remain > 0 && page_no != FIL_NULL) {
    buf_block_t *block = buf_page_get(
        page_id_t(dict_index_get_space(index), page_no),
        dict_table_page_size(index->table), RW_X_LATCH, UT_LOCATION_HERE, mtr);

    page_type_t ptype = block->get_page_type();
    ulint data_size = 0;
    if (ptype == FIL_PAGE_TYPE_ZLOB_FRAG) {
      frag_id_t fid = entry.get_z_frag_id();
      z_frag_page_t frag_page(block, mtr, index);
      frag_node_t node = frag_page.get_frag_node(fid);
      data_size = node.payload();
    } else if (ptype == FIL_PAGE_TYPE_ZLOB_FIRST) {
      z_first_page_t first(block, mtr, index);
      data_size = first.get_data_len();
    } else {
      ut_a(ptype == FIL_PAGE_TYPE_ZLOB_DATA);
      z_data_page_t data_page(block, mtr, index);
      data_size = data_page.get_data_len();
      ut_a(data_size <= data_page.payload());
    }
    ut_a(data_size <= remain);
    remain -= data_size;
    page_no = block->get_next_page_no();
  }

  ut_ad(remain == 0);
  return (true);
}

bool z_validate_strm(dict_index_t *index, z_index_entry_t &entry, mtr_t *mtr) {
  static const uint32_t FREQ = 50;
  static std::atomic<uint32_t> n{0};
  bool ret = true;
  if (++n % FREQ == 0) {
    ret = z_validate_strm_low(index, entry, mtr);
  }
  return (ret);
}
#endif /* UNIV_DEBUG */

}  // namespace lob