1 /*****************************************************************************
2 
3 Copyright (c) 2015, 2018, Oracle and/or its affiliates. All Rights Reserved.
4 
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8 
9 This program is also distributed with certain software (including but not
10 limited to OpenSSL) that is licensed under separate terms, as designated in a
11 particular file or component or in included license documentation. The authors
12 of MySQL hereby grant you an additional permission to link the program and
13 your derivative works with the separately licensed software that they have
14 included with MySQL.
15 
16 This program is distributed in the hope that it will be useful, but WITHOUT
17 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
19 for more details.
20 
21 You should have received a copy of the GNU General Public License along with
22 this program; if not, write to the Free Software Foundation, Inc.,
23 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
24 
25 *****************************************************************************/
26 #ifndef lob0del_h
27 #define lob0del_h
28 
29 #include "lob0lob.h"
30 
31 namespace lob {
32 
33 /* Delete a LOB */
34 class Deleter {
35  public:
36   /** Constructor */
Deleter(DeleteContext & ctx)37   Deleter(DeleteContext &ctx) : m_ctx(ctx) {
38     ut_ad(ctx.index()->is_clustered());
39     ut_ad(mtr_memo_contains_flagged(ctx.get_mtr(),
40                                     dict_index_get_lock(ctx.index()),
41                                     MTR_MEMO_X_LOCK | MTR_MEMO_SX_LOCK) ||
42           ctx.table()->is_intrinsic());
43     ut_ad(mtr_is_page_fix(ctx.get_mtr(), ctx.m_blobref.page_align(),
44                           MTR_MEMO_PAGE_X_FIX, ctx.table()));
45     ut_ad(ctx.rec_offs_validate());
46     ut_ad(ctx.validate_blobref());
47   }
48 
49   /** Free the LOB object.
50   @return DB_SUCCESS on success. */
51   dberr_t destroy();
52 
53   /** Free the first page of the BLOB and update the BLOB reference
54   in the clustered index.
55   @return DB_SUCCESS on pass, error code on failure. */
56   dberr_t free_first_page();
57 
58  private:
59   /** Obtain an x-latch on the clustered index record page.*/
60   void x_latch_rec_page();
61 
62   /** Validate the page type of the given page frame.
63   @param[in]	page	the page frame.
64   @return true if valid, false otherwise. */
validate_page_type(const page_t * page)65   bool validate_page_type(const page_t *page) const {
66     return (m_ctx.is_compressed() ? validate_zblob_page_type(page)
67                                   : validate_blob_page_type(page));
68   }
69 
70   /** Check if the page type is set correctly.
71   @param[in]	page	the page frame.
72   @return true if page type is correct. */
validate_zblob_page_type(const page_t * page)73   bool validate_zblob_page_type(const page_t *page) const {
74     const page_type_t pt = fil_page_get_type(page);
75     switch (pt) {
76       case FIL_PAGE_TYPE_ZBLOB:
77       case FIL_PAGE_TYPE_ZBLOB2:
78       case FIL_PAGE_SDI_ZBLOB:
79         break;
80       default:
81         ut_error;
82     }
83     return (true);
84   }
85 
86   /** Check if the page type is set correctly.
87   @param[in]	page	the page frame.
88   @return true if page type is correct. */
validate_blob_page_type(const page_t * page)89   bool validate_blob_page_type(const page_t *page) const {
90     const page_type_t type = fil_page_get_type(page);
91 
92     switch (type) {
93       case FIL_PAGE_TYPE_BLOB:
94       case FIL_PAGE_SDI_BLOB:
95         break;
96       default:
97 #ifndef UNIV_DEBUG /* Improve debug test coverage */
98         if (!m_ctx.has_atomic_blobs()) {
99           /* Old versions of InnoDB did not initialize
100           FIL_PAGE_TYPE on BLOB pages.  Do not print
101           anything about the type mismatch when reading
102           a BLOB page that may be from old versions. */
103           return (true);
104         }
105 #endif /* !UNIV_DEBUG */
106         ut_error;
107     }
108     return (true);
109   }
110 
111   /** Check if the BLOB can be freed.  If the clustered index record
112   is not the owner of the LOB, then it cannot be freed.  Also, during
113   rollback, if inherited flag is set, then LOB will not be freed.
114   @return true if the BLOB can be freed, false otherwise. */
115   bool can_free() const;
116 
117   DeleteContext &m_ctx;
118   mtr_t m_mtr;
119 };
120 
121 }  // namespace lob
122 
123 #endif  // lob0del_h
124