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
47	/* Check that the view->trx_ids array is in descending order. */
48	for (ulint i = 1; i < view->n_trx_ids; ++i) {
49
50		ut_a(view->trx_ids[i] < view->trx_ids[i - 1]);
51	}
52
53	return(true);
54}
55
56/** Functor to validate the view list. */
57struct	ViewCheck {
58
59	ViewCheck() : m_prev_view(0) { }
60
61	void	operator()(const read_view_t* view)
62	{
63		ut_a(m_prev_view == NULL
64		     || m_prev_view->low_limit_no >= view->low_limit_no);
65
66		m_prev_view = view;
67	}
68
69	const read_view_t*	m_prev_view;
70};
71
72/*********************************************************************//**
73Validates a read view list. */
74static
75bool
76read_view_list_validate(void)
77/*=========================*/
78{
79	ut_ad(mutex_own(&trx_sys->mutex));
80
81	ut_list_map(trx_sys->view_list, &read_view_t::view_list, ViewCheck());
82
83	return(true);
84}
85#endif /* UNIV_DEBUG */
86
87/*********************************************************************//**
88Checks if a read view sees the specified transaction.
89@return	true if sees */
90UNIV_INLINE
91bool
92read_view_sees_trx_id(
93/*==================*/
94	const read_view_t*	view,	/*!< in: read view */
95	trx_id_t		trx_id)	/*!< in: trx id */
96{
97	if (trx_id < view->up_limit_id) {
98
99		return(true);
100	} else if (trx_id >= view->low_limit_id) {
101
102		return(false);
103	} else {
104		ulint	lower = 0;
105		ulint	upper = view->n_trx_ids - 1;
106
107		ut_a(view->n_trx_ids > 0);
108
109		do {
110			ulint		mid	= (lower + upper) >> 1;
111			trx_id_t	mid_id	= view->trx_ids[mid];
112
113			if (mid_id == trx_id) {
114				return(FALSE);
115			} else if (mid_id < trx_id) {
116				if (mid > 0) {
117					upper = mid - 1;
118				} else {
119					break;
120				}
121			} else {
122				lower = mid + 1;
123			}
124		} while (lower <= upper);
125	}
126
127	return(true);
128}
129
130/*********************************************************************//**
131Remove a read view from the trx_sys->view_list. */
132UNIV_INLINE
133void
134read_view_remove(
135/*=============*/
136	read_view_t*	view,		/*!< in: read view, can be 0 */
137	bool		own_mutex)	/*!< in: true if caller owns the
138					trx_sys_t::mutex */
139{
140	if (view != 0) {
141		if (!own_mutex) {
142			mutex_enter(&trx_sys->mutex);
143		}
144
145		ut_ad(read_view_validate(view));
146
147		UT_LIST_REMOVE(view_list, trx_sys->view_list, view);
148
149		ut_ad(read_view_list_validate());
150
151		if (!own_mutex) {
152			mutex_exit(&trx_sys->mutex);
153		}
154	}
155}
156
157