File: zlob0first.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 (722 lines) | stat: -rw-r--r-- 20,858 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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
/*****************************************************************************

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 "zlob0first.h"
#include "trx0trx.h"
#include "zlob0index.h"
#include "zlob0read.h"

namespace lob {

/** Given the page size, what is the number of index entries the first page
can contain. */
ulint z_first_page_t::get_n_index_entries() const {
  ut_ad(m_index != nullptr);

  const page_size_t page_size(dict_table_page_size(m_index->table));

  ulint len = page_size.physical();
  switch (len) {
    case KB16:
      /* For a page size of 16KB, there are 100 index entries in
      the first page of the zlob. */
      return (100);
    case 8192:
      /* 8KB. */
      return (80);
    case 4096:
      /* 4KB. */
      return (40);
    case 2048:
      return (20);
    case 1024:
      return (5);
    default:
      ut_error;
  }
}

/** Given the page size, what is the number of frag entries the first page
can contain. */
ulint z_first_page_t::get_n_frag_entries() const {
  ut_ad(m_index != nullptr);

  DBUG_EXECUTE_IF("innodb_zlob_first_use_only_1_frag_entries", return (1););

  const page_size_t page_size(dict_table_page_size(m_index->table));
  ulint len = page_size.physical();
  switch (len) {
    case KB16:
      /* For a page size of 16KB, there are 200 frag entries in
      the first page of the zlob. */
      return (200);
    case 8192:
      return (100);
    case 4096:
      return (40);
    case 2048:
      return (20);
    case 1024:
      return (5);
    default:
      ut_error;
  }
}

buf_block_t *z_first_page_t::alloc(bool bulk) {
  ut_ad(m_block == nullptr);

  page_no_t hint = FIL_NULL;
  m_block = alloc_lob_page(m_index, m_mtr, hint, bulk);

  if (m_block == nullptr) {
    return (nullptr);
  }

  init();

  ut_ad(m_block->get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST);
  return (m_block);
}

/** Print the index entries. */
std::ostream &z_first_page_t::print_index_entries(std::ostream &out) const {
  flst_base_node_t *flst = index_list();
  fil_addr_t node_loc = flst_get_first(flst, m_mtr);

  space_id_t space = dict_index_get_space(m_index);
  const page_size_t page_size = dict_table_page_size(m_index->table);

  out << "Index Entries: " << flst_bnode_t(flst, m_mtr) << std::endl;

  while (!fil_addr_is_null(node_loc)) {
    flst_node_t *node =
        fut_get_ptr(space, page_size, node_loc, RW_X_LATCH, m_mtr);
    z_index_entry_t entry(node, m_mtr, m_index);
    out << entry << std::endl;

    flst_base_node_t *vers = entry.get_versions_list();
    fil_addr_t ver_loc = flst_get_first(vers, m_mtr);

    uint32_t depth = 0;
    while (!fil_addr_is_null(ver_loc)) {
      depth++;

      for (uint32_t i = 0; i < depth; ++i) {
        out << "+";
      }
      flst_node_t *ver_node = addr2ptr_x(ver_loc);
      z_index_entry_t vers_entry(ver_node, m_mtr, m_index);
      out << vers_entry << std::endl;
      ver_loc = vers_entry.get_next();
    }

    node_loc = entry.get_next();
  }

  return (out);
}

/** Print the frag entries. */
std::ostream &z_first_page_t::print_frag_entries(std::ostream &out) const {
  flst_base_node_t *flst = frag_list();
  fil_addr_t node_loc = flst_get_first(flst, m_mtr);
  space_id_t space = dict_index_get_space(m_index);
  const page_size_t page_size = dict_table_page_size(m_index->table);

  out << "Frag Entries: " << flst_bnode_t(flst, m_mtr) << std::endl;

  while (!fil_addr_is_null(node_loc)) {
    flst_node_t *node =
        fut_get_ptr(space, page_size, node_loc, RW_X_LATCH, m_mtr);
    z_frag_entry_t entry(node, m_mtr);
    out << entry << std::endl;
    node_loc = entry.get_next();
  }

  return (out);
}

/** Allocate one index entry.  If there is no free index entry, allocate
an index page (a page full of z_index_entry_t objects) and service the
request.
@return the allocated index entry. */
z_index_entry_t z_first_page_t::alloc_index_entry(bool bulk) {
  flst_base_node_t *free_lst = free_list();
  fil_addr_t first_loc = flst_get_first(free_lst, m_mtr);

  if (fil_addr_is_null(first_loc)) {
    z_index_page_t page(m_mtr, m_index);
    page.alloc(*this, bulk);
    first_loc = flst_get_first(free_lst, m_mtr);
  }

  if (fil_addr_is_null(first_loc)) {
    return (z_index_entry_t());
  }

  flst_node_t *first_ptr = addr2ptr_x(first_loc);
  z_index_entry_t entry(first_ptr, m_mtr);
  entry.remove(free_lst);

  return (entry);
}

/** Allocate one frag page entry.  If there is no free frag entry, allocate
an frag node page (a page full of z_frag_entry_t objects) and service the
request.
@return the allocated frag entry. */
z_frag_entry_t z_first_page_t::alloc_frag_entry(bool bulk) {
  flst_base_node_t *free_lst = free_frag_list();
  flst_base_node_t *used_lst = frag_list();

  fil_addr_t first_loc = flst_get_first(free_lst, m_mtr);

  if (fil_addr_is_null(first_loc)) {
    z_frag_node_page_t page(m_mtr, m_index);
    page.alloc(*this, bulk);
    first_loc = flst_get_first(free_lst, m_mtr);
  }

  if (fil_addr_is_null(first_loc)) {
    return (z_frag_entry_t());
  }

  flst_node_t *first_ptr = addr2ptr_x(first_loc);
  z_frag_entry_t entry(first_ptr, m_mtr);
  entry.remove(free_lst);
  entry.push_front(used_lst);
  return (entry);
}

frag_id_t z_first_page_t::alloc_fragment(bool bulk, ulint len,
                                         z_frag_page_t &frag_page,
                                         z_frag_entry_t &entry) {
  ut_ad(m_mtr != nullptr);

  frag_id_t frag_id = FRAG_ID_NULL;

  frag_page.set_mtr(m_mtr);
  frag_page.set_index(m_index);
  frag_page.set_block_null();

  const page_no_t first_page_no = get_page_no();

  /* Make sure that there will be some extra space for page directory
  entry and meta data.  Adding a margin to provide for this.  This is
  for exact fit. */
  const ulint look_size = len + frag_node_t::header_size();

  ut_ad(look_size <= z_frag_page_t::max_payload(m_index));

  flst_base_node_t *frag_lst = frag_list();

  /* Iterate through the list of frag entries in the page. */
  fil_addr_t loc = flst_get_first(frag_lst, m_mtr);

  while (!fil_addr_is_null(loc)) {
    flst_node_t *node = addr2ptr_x(loc);
    entry.reset(node);

    ulint big_free = entry.get_big_free_len();

    if (big_free >= look_size) {
      /* Double check if the information in the index
      entry matches with the fragment page. If not, update
      the index entry. */
      frag_page.load_x(entry.get_page_no());

      const ulint big_free_len_1 = frag_page.get_big_free_len();
      const ulint big_free_len_2 = entry.get_big_free_len();

      if (big_free_len_1 == big_free_len_2) {
        frag_id = frag_page.alloc_fragment(len, entry);
        if (frag_id != FRAG_ID_NULL) {
          break;
        }
      } else {
        entry.update(frag_page);

        /* Check again */
        big_free = entry.get_big_free_len();

        if (big_free >= look_size) {
          frag_id = frag_page.alloc_fragment(len, entry);
          if (frag_id != FRAG_ID_NULL) {
            break;
          }
        }
      }
    }

    loc = flst_get_next_addr(node, m_mtr);
    entry.reset(nullptr);
  }

  if (frag_id != FRAG_ID_NULL) {
    return (frag_id);
  }

  if (fil_addr_is_null(loc)) {
    /* Need to allocate a new fragment page. */
    buf_block_t *tmp_block = frag_page.alloc(*this, first_page_no + 1, bulk);

    if (tmp_block == nullptr) {
      return (FRAG_ID_NULL);
    }

    entry = alloc_frag_entry(bulk);

    if (entry.is_null()) {
      return (FRAG_ID_NULL);
    }

    entry.set_page_no(frag_page.get_page_no());
    frag_page.set_frag_entry(entry.get_self_addr());

    /* Update the index entry with new space information. */
    entry.update(frag_page);
  }

#ifdef UNIV_DEBUG
  /* Adding more checks to ensure that an alloc fragment doesn't fail
  for the selected fragment page. */
  fil_addr_t addr1 = frag_page.get_frag_entry();
  fil_addr_t addr2 = entry.get_self_addr();
  ut_ad(addr1.is_equal(addr2));

  const ulint big_free_len_1 = frag_page.get_big_free_len();
  const ulint big_free_len_2 = entry.get_big_free_len();
  ut_ad(big_free_len_1 == big_free_len_2);

  ut_ad(big_free_len_1 >= look_size);
  ut_ad(big_free_len_1 > len);
#endif /* UNIV_DEBUG */

  frag_id = frag_page.alloc_fragment(len, entry);

  ut_ad(frag_id != FRAG_ID_NULL);

  return (frag_id);
}

/** Print the page. */
std::ostream &z_first_page_t::print(std::ostream &out) const {
  print_index_entries(out);
  print_frag_entries(out);
  return (out);
}

/** Free all the z_frag_page_t pages. All the z_frag_page_t pages are
singly linked to each other.  The head of the list is maintained in the
first page. */
size_t z_first_page_t::free_all_frag_node_pages() {
  size_t n_pages_freed = 0;
  mtr_t local_mtr;
  mtr_start(&local_mtr);
  local_mtr.set_log_mode(m_mtr->get_log_mode());

  load_x(&local_mtr);

  while (true) {
    page_no_t page_no = get_frag_node_page_no();
    if (page_no == FIL_NULL) {
      break;
    }

    z_frag_node_page_t frag_node_page(&local_mtr, m_index);
    frag_node_page.load_x(page_no);
    page_no_t next_page = frag_node_page.get_next_page_no();

    /* Make all changes to the first page using local_mtr. */
    set_frag_node_page_no(next_page, &local_mtr);
    frag_node_page.dealloc();
    n_pages_freed++;

    ut_ad(!local_mtr.conflicts_with(m_mtr));
    restart_mtr(&local_mtr);
  }
  ut_ad(!local_mtr.conflicts_with(m_mtr));
  mtr_commit(&local_mtr);

  return (n_pages_freed);
}

/** Free all the index pages. */
size_t z_first_page_t::free_all_index_pages() {
  size_t n_pages_freed = 0;
  mtr_t local_mtr;
  mtr_start(&local_mtr);
  local_mtr.set_log_mode(m_mtr->get_log_mode());

  load_x(&local_mtr);
  while (true) {
    page_no_t page_no = get_index_page_no();
    if (page_no == FIL_NULL) {
      break;
    }
    z_index_page_t index_page(&local_mtr, m_index);
    index_page.load_x(page_no);
    page_no_t next_page = index_page.get_next_page_no();

    /* Make all changes to the first page using local_mtr. */
    set_index_page_no(next_page, &local_mtr);

    index_page.dealloc();
    n_pages_freed++;
    ut_ad(!local_mtr.conflicts_with(m_mtr));
    restart_mtr(&local_mtr);
  }
  ut_ad(!local_mtr.conflicts_with(m_mtr));
  mtr_commit(&local_mtr);
  return (n_pages_freed);
}

ulint z_first_page_t::size_of_index_entries() const {
  return (z_index_entry_t::SIZE * get_n_index_entries());
}

void z_first_page_t::init_index_entries() {
  flst_base_node_t *flst = free_list();
  ulint n = get_n_index_entries();
  for (ulint i = 0; i < n; ++i) {
    flst_node_t *ptr = frame() + OFFSET_INDEX_BEGIN;
    ptr += (i * z_index_entry_t::SIZE);
    z_index_entry_t entry(ptr, m_mtr);
    entry.init();
    entry.push_back(flst);
  }
}

void z_first_page_t::load_entry_s(fil_addr_t &addr, z_index_entry_t &entry) {
  entry.load_s(addr);
}

void z_first_page_t::load_entry_x(fil_addr_t &addr, z_index_entry_t &entry) {
  entry.load_x(addr);
}

/** Deallocate the first page of a compressed LOB. */
void z_first_page_t::dealloc() {
  ut_ad(m_mtr != nullptr);
  btr_page_free_low(m_index, m_block, ULINT_UNDEFINED, m_mtr);
  m_block = nullptr;
}

buf_block_t *z_first_page_t::load_x(const page_id_t &page_id,
                                    const page_size_t &page_size) {
  m_block =
      buf_page_get(page_id, page_size, RW_X_LATCH, UT_LOCATION_HERE, m_mtr);

#ifdef UNIV_DEBUG
  /* Dump the page into the log file, if the page type is not matching
  one of the first page types. */
  page_type_t page_type = get_page_type();

  switch (page_type) {
    case FIL_PAGE_TYPE_ZBLOB:
    case FIL_PAGE_TYPE_ZLOB_FIRST:
      /* Valid first page type for compressed LOB.*/
      break;
    default:
      ut_print_buf(std::cout, m_block->frame, page_size.physical());
      ut_error;
  }
#endif /* UNIV_DEBUG */

  return (m_block);
}

/** Increment the LOB version by 1. */
uint32_t z_first_page_t::incr_lob_version() {
  ut_ad(m_mtr != nullptr);

  const uint32_t cur = get_lob_version();
  const uint32_t val = cur + 1;
  mlog_write_ulint(frame() + OFFSET_LOB_VERSION, val, MLOG_4BYTES, m_mtr);

  return (val);
}

/** When the bit is set, the LOB is not partially updatable anymore.
Enable the bit.
@param[in]      trx     the current transaction.*/
void z_first_page_t::mark_cannot_be_partially_updated(trx_t *trx) {
  const trx_id_t trxid = (trx == nullptr) ? 0 : trx->id;
  const undo_no_t undo_no = (trx == nullptr) ? 0 : (trx->undo_no - 1);

#ifdef LOB_DEBUG
  std::cout << "thread=" << std::this_thread::get_id()
            << ", ZLOB first page=" << get_page_id()
            << ", mark_cannot_be_partially_updated()" << std::endl;
#endif /* LOB_DEBUG */

  uint8_t flags = get_flags();
  flags |= 0x01;
  mlog_write_ulint(frame() + OFFSET_FLAGS, flags, MLOG_1BYTE, m_mtr);

  set_last_trx_id(trxid);
  set_last_trx_undo_no(undo_no);
}

size_t z_first_page_t::free_all_data_pages() {
  size_t n_pages_freed = 0;
  mtr_t local_mtr;
  mtr_start(&local_mtr);
  local_mtr.set_log_mode(m_mtr->get_log_mode());
  load_x(&local_mtr);

  flst_base_node_t *flst = index_list();
  fil_addr_t node_loc = flst_get_first(flst, &local_mtr);

  z_index_entry_t cur_entry(&local_mtr, m_index);

  while (!fil_addr_is_null(node_loc)) {
    flst_node_t *node = addr2ptr_x(node_loc, &local_mtr);
    cur_entry.reset(node);
    n_pages_freed += cur_entry.free_data_pages(&local_mtr);

    flst_base_node_t *vers = cur_entry.get_versions_list();
    fil_addr_t ver_loc = flst_get_first(vers, &local_mtr);

    while (!fil_addr_is_null(ver_loc)) {
      flst_node_t *ver_node = addr2ptr_x(ver_loc, &local_mtr);
      z_index_entry_t vers_entry(ver_node, &local_mtr, m_index);
      n_pages_freed += vers_entry.free_data_pages(&local_mtr);
      ver_loc = vers_entry.get_next();

      ut_ad(!local_mtr.conflicts_with(m_mtr));
      restart_mtr(&local_mtr);
      node = addr2ptr_x(node_loc, &local_mtr);
      cur_entry.reset(node);
    }

    node_loc = cur_entry.get_next();
    cur_entry.reset(nullptr);
    ut_ad(!local_mtr.conflicts_with(m_mtr));
    restart_mtr(&local_mtr);
  }
  flst_init(flst, &local_mtr);
  flst_base_node_t *free_flst = free_list();
  flst_init(free_flst, &local_mtr);
  ut_ad(!local_mtr.conflicts_with(m_mtr));
  mtr_commit(&local_mtr);

  return (n_pages_freed);
}

#ifdef UNIV_DEBUG
bool z_first_page_t::validate_low() {
  mtr_t local_mtr;
  mtr_start(&local_mtr);
  local_mtr.set_log_mode(m_mtr->get_log_mode());
  load_x(&local_mtr);

  ut_ad(get_page_type() == FIL_PAGE_TYPE_ZLOB_FIRST);

  flst_base_node_t *flst = index_list();
  fil_addr_t node_loc = flst_get_first(flst, &local_mtr);

  z_index_entry_t cur_entry(&local_mtr, m_index);

  while (!fil_addr_is_null(node_loc)) {
    flst_node_t *node = addr2ptr_x(node_loc, &local_mtr);
    cur_entry.reset(node);

    ut_ad(z_validate_strm(m_index, cur_entry, &local_mtr));

    flst_base_node_t *vers = cur_entry.get_versions_list();
    fil_addr_t ver_loc = flst_get_first(vers, &local_mtr);

    while (!fil_addr_is_null(ver_loc)) {
      flst_node_t *ver_node = addr2ptr_x(ver_loc, &local_mtr);
      z_index_entry_t vers_entry(ver_node, &local_mtr, m_index);
      ut_ad(z_validate_strm(m_index, vers_entry, &local_mtr));
      ver_loc = vers_entry.get_next();
      restart_mtr(&local_mtr);
      node = addr2ptr_x(node_loc, &local_mtr);
      cur_entry.reset(node);
    }

    node_loc = cur_entry.get_next();
    cur_entry.reset(nullptr);

    restart_mtr(&local_mtr);
  }

  mtr_commit(&local_mtr);
  return (true);
}
#endif /* UNIV_DEBUG */

void z_first_page_t::import(trx_id_t trx_id) {
  set_trx_id_no_redo(trx_id);
  set_last_trx_id_no_redo(trx_id);

  ulint n = get_n_index_entries();
  for (ulint i = 0; i < n; ++i) {
    flst_node_t *ptr = frame() + OFFSET_INDEX_BEGIN;
    ptr += (i * z_index_entry_t::SIZE);
    z_index_entry_t entry(ptr);
    entry.set_trx_id_no_redo(trx_id);
    entry.set_trx_id_modifier_no_redo(trx_id);
  }
}

size_t z_first_page_t::free_all_frag_pages_old() {
  size_t n_pages_freed = 0;
  mtr_t local_mtr;
  mtr_start(&local_mtr);
  local_mtr.set_log_mode(m_mtr->get_log_mode());
  load_x(&local_mtr);

  /* There is no list of fragment pages maintained.  We have to identify the
  list of fragment pages from the following two lists. */
  flst_base_node_t *frag_lst = frag_list();
  flst_base_node_t *free_frag_lst = free_frag_list();

  std::vector<flst_base_node_t *> two_list = {frag_lst, free_frag_lst};

  for (auto cur_lst : two_list) {
    while (flst_get_len(cur_lst) > 0) {
      fil_addr_t loc = flst_get_first(cur_lst, &local_mtr);
      flst_node_t *node = addr2ptr_x(loc, &local_mtr);
      z_frag_entry_t entry(node, &local_mtr);
      page_no_t frag_page_no = entry.get_page_no();
      loc = entry.get_next();
      entry.remove(cur_lst);

      if (frag_page_no == FIL_NULL) {
        continue;
      }

      /* Multiple entries can point to the same fragment page.  So scan through
      the list and remove all entries pointing to the same fragment page. */
      while (!fil_addr_is_null(loc)) {
        node = addr2ptr_x(loc, &local_mtr);
        z_frag_entry_t entry2(node, &local_mtr);

        loc = entry2.get_next();
        if (frag_page_no == entry2.get_page_no()) {
          entry2.set_page_no(FIL_NULL);
          entry2.remove(cur_lst);
        }
      }

      /* Free the fragment page. */
      entry.free_frag_page(&local_mtr, m_index);
      n_pages_freed++;
      ut_ad(!local_mtr.conflicts_with(m_mtr));
      restart_mtr(&local_mtr);
    }
  }
  flst_init(frag_lst, &local_mtr);
  flst_init(free_frag_lst, &local_mtr);

  ut_ad(!local_mtr.conflicts_with(m_mtr));
  mtr_commit(&local_mtr);
  return (n_pages_freed);
}

size_t z_first_page_t::free_all_frag_pages() {
  size_t n_pages_freed = 0;
  if (get_frag_page_no() == 0) {
    n_pages_freed = free_all_frag_pages_old();
  } else {
    n_pages_freed = free_all_frag_pages_new();
  }
  return (n_pages_freed);
}

size_t z_first_page_t::free_all_frag_pages_new() {
  size_t n_pages_freed = 0;
  mtr_t local_mtr;
  mtr_start(&local_mtr);
  local_mtr.set_log_mode(m_mtr->get_log_mode());
  load_x(&local_mtr);

  while (true) {
    page_no_t page_no = get_frag_page_no(&local_mtr);
    if (page_no == FIL_NULL) {
      break;
    }
    z_frag_page_t frag_page(&local_mtr, m_index);
    frag_page.load_x(page_no);
    page_no_t next_page = frag_page.get_next_page_no();
    set_frag_page_no(&local_mtr, next_page);
    frag_page.dealloc();
    n_pages_freed++;
    ut_ad(!local_mtr.conflicts_with(m_mtr));
    restart_mtr(&local_mtr);
  }
  ut_ad(!local_mtr.conflicts_with(m_mtr));
  mtr_commit(&local_mtr);
  return (n_pages_freed);
}

size_t z_first_page_t::destroy() {
  size_t n_pages_freed = make_empty();
  dealloc();
  n_pages_freed++;
  return (n_pages_freed);
}

size_t z_first_page_t::make_empty() {
  size_t n_pages_freed = 0;
  n_pages_freed += free_all_data_pages();
  n_pages_freed += free_all_frag_pages();
  n_pages_freed += free_all_frag_node_pages();
  n_pages_freed += free_all_index_pages();
  return (n_pages_freed);
}

#ifdef UNIV_DEBUG
bool z_first_page_t::verify_frag_page_no() {
  mtr_t local_mtr;
  mtr_start(&local_mtr);
  page_no_t page_no = get_frag_page_no();

  /* If the page_no is 0, then FIL_PAGE_PREV is not used to store the list of
  fragment pages.  So modifying it is not allowed and hence verification is
  not needed. */
  ut_ad(page_no != 0);

  if (page_no == FIL_NULL) {
    return (true);
  }

  z_frag_page_t frag_page(&local_mtr, m_index);
  frag_page.load_x(page_no);
  page_type_t ptype = frag_page.get_page_type();
  mtr_commit(&local_mtr);

  ut_ad(ptype == FIL_PAGE_TYPE_ZLOB_FRAG);
  return (ptype == FIL_PAGE_TYPE_ZLOB_FRAG);
}
#endif /* UNIV_DEBUG */

} /* namespace lob */