| 1 |
/***************************************************************************** |
| 2 |
|
| 3 |
Copyright (c) 1997, 2026, Oracle and/or its affiliates. |
| 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 designed to work with certain software (including |
| 10 |
but not limited to OpenSSL) that is licensed under separate terms, |
| 11 |
as designated in a particular file or component or in included license |
| 12 |
documentation. The authors of MySQL hereby grant you an additional |
| 13 |
permission to link the program and your derivative works with the |
| 14 |
separately licensed software that they have either included with |
| 15 |
the program or referenced in the documentation. |
| 16 |
|
| 17 |
This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 |
FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 |
for more details. |
| 21 |
|
| 22 |
You should have received a copy of the GNU General Public License along with |
| 23 |
this program; if not, write to the Free Software Foundation, Inc., |
| 24 |
51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 |
|
| 26 |
*****************************************************************************/ |
| 27 |
|
| 28 |
/** @file include/read0read.h |
| 29 |
Cursor read |
| 30 |
|
| 31 |
Created 2/16/1997 Heikki Tuuri |
| 32 |
*******************************************************/ |
| 33 |
|
| 34 |
#ifndef read0read_h |
| 35 |
#define read0read_h |
| 36 |
|
| 37 |
#include <stddef.h> |
| 38 |
#include <algorithm> |
| 39 |
|
| 40 |
#include "read0types.h" |
| 41 |
#include "univ.i" |
| 42 |
|
| 43 |
/** The MVCC read view manager */ |
| 44 |
class MVCC { |
| 45 |
public: |
| 46 |
/** Constructor |
| 47 |
@param size Number of views to pre-allocate */ |
| 48 |
explicit MVCC(ulint size); |
| 49 |
|
| 50 |
/** Destructor. |
| 51 |
Free all the views in the m_free list */ |
| 52 |
~MVCC(); |
| 53 |
|
| 54 |
/** Allocate and create a view. |
| 55 |
@param view View owned by this class created for the caller. Must be |
| 56 |
freed by calling view_close() |
| 57 |
@param trx Transaction instance of caller */ |
| 58 |
void view_open(ReadView *&view, trx_t *trx); |
| 59 |
|
| 60 |
/** |
| 61 |
Close a view created by the above function. |
| 62 |
@param view view allocated by trx_open. |
| 63 |
@param own_mutex true if caller owns trx_sys_t::mutex */ |
| 64 |
void view_close(ReadView *&view, bool own_mutex); |
| 65 |
|
| 66 |
/** |
| 67 |
Release a view that is inactive but not closed. Caller must own |
| 68 |
the trx_sys_t::mutex. |
| 69 |
@param view View to release */ |
| 70 |
void view_release(ReadView *&view); |
| 71 |
|
| 72 |
/** Clones the oldest view and stores it in view. No need to |
| 73 |
call view_close(). The caller owns the view that is passed in. |
| 74 |
It will also move the closed views from the m_views list to the |
| 75 |
m_free list. This function is called by Purge to determine whether it should |
| 76 |
purge the delete marked record or not. |
| 77 |
@param view Preallocated view, owned by the caller */ |
| 78 |
void clone_oldest_view(ReadView *view); |
| 79 |
|
| 80 |
/** |
| 81 |
@return the number of active views */ |
| 82 |
ulint size() const; |
| 83 |
|
| 84 |
/** |
| 85 |
@return true if the view is active and valid */ |
| 86 |
static bool is_view_active(ReadView *view) { |
| 87 |
ut_a(view != reinterpret_cast<ReadView *>(0x1)); |
| 88 |
|
| 89 |
return (view != nullptr && !(intptr_t(view) & 0x1)); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
Set the view creator transaction id. Note: This should be set only |
| 94 |
for views created by RW transactions. |
| 95 |
@param view Set the creator trx id for this view |
| 96 |
@param id Transaction id to set */ |
| 97 |
static void set_view_creator_trx_id(ReadView *view, trx_id_t id) { |
| 98 |
ut_ad(id > 0); |
| 99 |
|
| 100 |
view->creator_trx_id(id); |
| 101 |
} |
| 102 |
|
| 103 |
private: |
| 104 |
/** |
| 105 |
Validates a read view list. */ |
| 106 |
bool validate() const; |
| 107 |
|
| 108 |
/** |
| 109 |
Find a free view from the active list, if none found then allocate |
| 110 |
a new view. This function will also attempt to move delete marked |
| 111 |
views from the active list to the freed list. |
| 112 |
@return a view to use */ |
| 113 |
inline ReadView *get_view(); |
| 114 |
|
| 115 |
/** |
| 116 |
Get the oldest view in the system. It will also move the delete |
| 117 |
marked read views from the views list to the freed list. |
| 118 |
@return oldest view if found or NULL */ |
| 119 |
inline ReadView *get_oldest_view() const; |
| 120 |
ReadView *get_view_created_by_trx_id(trx_id_t trx_id) const; |
| 121 |
|
| 122 |
private: |
| 123 |
// Prevent copying |
| 124 |
MVCC(const MVCC &); |
| 125 |
MVCC &operator=(const MVCC &); |
| 126 |
|
| 127 |
private: |
| 128 |
typedef UT_LIST_BASE_NODE_T(ReadView, m_view_list) view_list_t; |
| 129 |
|
| 130 |
/** Free views ready for reuse. */ |
| 131 |
view_list_t m_free; |
| 132 |
|
| 133 |
/** Active and closed views, the closed views will have the |
| 134 |
creator trx id set to TRX_ID_MAX */ |
| 135 |
view_list_t m_views; |
| 136 |
}; |
| 137 |
|
| 138 |
#endif /* read0read_h */ |