1/*****************************************************************************
2
3Copyright (c) 1997, 2012, Oracle and/or its affiliates. All Rights Reserved.
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License, version 2.0,
7as published by the Free Software Foundation.
8
9This program is also distributed with certain software (including
10but not limited to OpenSSL) that is licensed under separate terms,
11as designated in a particular file or component or in included license
12documentation.  The authors of MySQL hereby grant you an additional
13permission to link the program and your derivative works with the
14separately licensed software that they have included with MySQL.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU General Public License, version 2.0, for more details.
20
21You should have received a copy of the GNU General Public License along with
22this program; if not, write to the Free Software Foundation, Inc.,
2351 Franklin Street, Suite 500, Boston, MA 02110-1335 USA
24
25*****************************************************************************/
26
27/**************************************************//**
28@file include/read0read.ic
29Cursor read
30
31Created 2/16/1997 Heikki Tuuri
32*******************************************************/
33
34#include "trx0sys.h"
35
36#ifdef UNIV_DEBUG
37/*********************************************************************//**
38Validates a read view object. */
39static
40bool
41read_view_validate(
42/*===============*/
43	const read_view_t*	view)	/*!< in: view to validate */
44{
45	ut_ad(mutex_own(&trx_sys->mutex));
46	ut_ad(view->max_descr >= view->n_descr);
47	ut_ad(view->descriptors == NULL || view->max_descr > 0);
48
49	/* Check that the view->descriptors array is in ascending order. */
50	for (ulint i = 1; i < view->n_descr; ++i) {
51
52		ut_a(view->descriptors[i] > view->descriptors[i - 1]);
53	}
54
55	return(true);
56}
57
58/** Functor to validate the view list. */
59struct	ViewCheck {
60
61	ViewCheck() : m_prev_view(0) { }
62
63	void	operator()(const read_view_t* view)
64	{
65		ut_a(m_prev_view == NULL
66		     || m_prev_view->low_limit_no >= view->low_limit_no);
67
68		m_prev_view = view;
69	}
70
71	const read_view_t*	m_prev_view;
72};
73
74/*********************************************************************//**
75Validates a read view list. */
76static
77bool
78read_view_list_validate(void)
79/*=========================*/
80{
81	ut_ad(mutex_own(&trx_sys->mutex));
82
83	ut_list_map(trx_sys->view_list, &read_view_t::view_list, ViewCheck());
84
85	return(true);
86}
87#endif /* UNIV_DEBUG */
88
89/*********************************************************************//**
90Checks if a read view sees the specified transaction.
91@return	true if sees */
92UNIV_INLINE
93bool
94read_view_sees_trx_id(
95/*==================*/
96	const read_view_t*	view,	/*!< in: read view */
97	trx_id_t		trx_id)	/*!< in: trx id */
98{
99	if (trx_id < view->up_limit_id) {
100
101		return(true);
102	} else if (trx_id >= view->low_limit_id) {
103
104		return(false);
105	}
106
107	/* Do a binary search over this view's descriptors array */
108
109	return(trx_find_descriptor(view->descriptors, view->n_descr,
110				   trx_id) == NULL);
111}
112
113/*********************************************************************//**
114Remove a read view from the trx_sys->view_list. */
115UNIV_INLINE
116void
117read_view_remove(
118/*=============*/
119	read_view_t*	view,		/*!< in: read view, can be 0 */
120	bool		own_mutex)	/*!< in: true if caller owns the
121					trx_sys_t::mutex */
122{
123	if (view != 0) {
124		if (!own_mutex) {
125			mutex_enter(&trx_sys->mutex);
126		}
127
128		ut_ad(read_view_validate(view));
129
130		UT_LIST_REMOVE(view_list, trx_sys->view_list, view);
131
132		ut_ad(read_view_list_validate());
133
134		if (!own_mutex) {
135			mutex_exit(&trx_sys->mutex);
136		}
137	}
138}
139
140