summaryrefslogtreecommitdiff
path: root/storage/ndb/src/kernel/blocks/diskpage.hpp
blob: 579b538c9104132f8a86694323418bbbca66fb91 (plain)
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
/* Copyright (C) 2003 MySQL AB

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; version 2 of the License.

   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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */

#ifndef __NDB_DISKPAGE_HPP
#define __NDB_DISKPAGE_HPP

#include <ndb_types.h>

struct File_formats 
{
  STATIC_CONST( NDB_PAGE_SIZE = 32768 );
  STATIC_CONST( NDB_PAGE_SIZE_WORDS = NDB_PAGE_SIZE >> 2);
  
  enum File_type
  {
    FT_Datafile = 0x1,
    FT_Undofile = 0x2
  };

  struct Page_header 
  {
    Uint32 m_page_lsn_hi;
    Uint32 m_page_lsn_lo;
    Uint32 m_page_type;
  };

  enum Page_type 
  {
    PT_Unallocated        = 0x0,
    PT_Extent_page        = 0x1,
    PT_Tup_fixsize_page   = 0x2,
    PT_Tup_varsize_page   = 0x3,
    PT_Undopage           = 0x4
  };

  struct Zero_page_header
  {
    char   m_magic[8];
    Uint32 m_byte_order;
    Uint32 m_page_size;
    Uint32 m_ndb_version;
    Uint32 m_node_id;
    Uint32 m_file_type;
    Uint32 m_time; // time(0)
    void init(File_type ft, Uint32 node_id, Uint32 version, Uint32 now);
    int validate(File_type ft, Uint32 node_id, Uint32 version, Uint32 now);
  };
  
  STATIC_CONST( NDB_PAGE_HEADER_WORDS = sizeof(Page_header) >> 2);
  
  struct Datafile 
  {
    struct Zero_page 
    {
      struct Zero_page_header m_page_header;
      Uint32 m_file_no; // Local_key
      Uint32 m_file_id; // DICT id
      Uint32 m_tablespace_id;
      Uint32 m_tablespace_version;
      Uint32 m_data_pages;
      Uint32 m_extent_pages;
      Uint32 m_extent_size;
      Uint32 m_extent_count;
      Uint32 m_extent_headers_per_page;
      Uint32 m_extent_header_words;
      Uint32 m_extent_header_bits_per_page;
    };
    
    struct Extent_header 
    {
      Uint32 m_table;
      union 
      {
	Uint32 m_fragment_id;
	Uint32 m_next_free_extent;
      };
      Uint32 m_page_bitmask[1]; // (BitsPerPage*ExtentSize)/(32*PageSize)
      Uint32 get_free_bits(Uint32 page) const;
      Uint32 get_free_word_offset(Uint32 page) const;
      void update_free_bits(Uint32 page, Uint32 bit);
      bool check_free(Uint32 extent_size) const ;
    };
    
    STATIC_CONST( EXTENT_HEADER_BITMASK_BITS_PER_PAGE = 4 );
    STATIC_CONST( EXTENT_HEADER_FIXED_WORDS = (sizeof(Extent_header)>>2) - 1);
    static Uint32 extent_header_words(Uint32 extent_size_in_pages);
    
    struct Extent_page
    {
      struct Page_header m_page_header;
      Extent_header m_extents[1];
      
      Extent_header* get_header(Uint32 extent_no, Uint32 extent_size);
    };
    
    STATIC_CONST( EXTENT_PAGE_WORDS = NDB_PAGE_SIZE_WORDS - NDB_PAGE_HEADER_WORDS );
    
    struct Data_page 
    {
      struct Page_header m_page_header;
    };
  };

  struct Undofile 
  {
    struct Zero_page
    {
      struct Zero_page_header m_page_header;
      Uint32 m_file_id;
      Uint32 m_logfile_group_id;
      Uint32 m_logfile_group_version;
      Uint32 m_undo_pages;
    };
    struct Undo_page 
    {
      struct Page_header m_page_header;
      Uint32 m_words_used;
      Uint32 m_data[1];
    };

    struct Undo_entry
    {
      Uint32 m_file_no;
      Uint32 m_page_no;
      struct 
      {
	Uint32 m_len_offset;
	Uint32 m_data[1];
      } m_changes[1];
      Uint32 m_length; // [ 16-bit type | 16 bit length of entry ]
    };

    enum Undo_type {
      UNDO_LCP_FIRST  = 1 // First LCP record with specific lcp id
      ,UNDO_LCP = 2       // LCP Start
      
      /**
       * TUP Undo record
       */
      ,UNDO_TUP_ALLOC  = 3
      ,UNDO_TUP_UPDATE = 4
      ,UNDO_TUP_FREE   = 5
      ,UNDO_TUP_CREATE = 6
      ,UNDO_TUP_DROP   = 7
      ,UNDO_TUP_ALLOC_EXTENT = 8
      ,UNDO_TUP_FREE_EXTENT  = 9
      
      ,UNDO_END        = 0x7FFF 
      ,UNDO_NEXT_LSN   = 0x8000
    };

    struct Undo_lcp
    {
      Uint32 m_lcp_id;
      Uint32 m_type_length; // 16 bit type, 16 bit length
    };
  };
  STATIC_CONST( UNDO_PAGE_WORDS = NDB_PAGE_SIZE_WORDS - NDB_PAGE_HEADER_WORDS - 1);
};


/**
 * Compute size of extent header in words
 */
inline Uint32 
File_formats::Datafile::extent_header_words(Uint32 extent_size_in_pages)
{
  return EXTENT_HEADER_FIXED_WORDS + 
    ((extent_size_in_pages * EXTENT_HEADER_BITMASK_BITS_PER_PAGE + 31) >> 5);
}

inline
File_formats::Datafile::Extent_header*
File_formats::Datafile::Extent_page::get_header(Uint32 no, Uint32 extent_size)
{
  Uint32 * tmp = (Uint32*)m_extents;
  tmp += no*File_formats::Datafile::extent_header_words(extent_size);
  return (Extent_header*)tmp;
}

inline
Uint32
File_formats::Datafile::Extent_header::get_free_bits(Uint32 page) const
{
  return ((m_page_bitmask[page >> 3] >> ((page & 7) << 2))) & 15;
}

inline
Uint32
File_formats::Datafile::Extent_header::get_free_word_offset(Uint32 page) const
{
  return page >> 3;
}

inline
void
File_formats::Datafile::Extent_header::update_free_bits(Uint32 page, 
							Uint32 bit)
{
  Uint32 shift = (page & 7) << 2;
  Uint32 mask = (15 << shift);
  Uint32 org = m_page_bitmask[page >> 3];
  m_page_bitmask[page >> 3] = (org & ~mask) | (bit << shift);
}

inline
bool
File_formats::Datafile::Extent_header::check_free(Uint32 extent_size) const 
{
  Uint32 words = (extent_size * EXTENT_HEADER_BITMASK_BITS_PER_PAGE + 31) >> 5;
  Uint32 sum = 0;
  for(; words; words--)
    sum |= m_page_bitmask[words-1];

  if(sum & 0x3333)
    return false;
  
  return true;
}

#include <NdbOut.hpp>
NdbOut& operator<<(NdbOut& out, const File_formats::Zero_page_header&);
NdbOut& operator<<(NdbOut& out, const File_formats::Datafile::Zero_page&);
NdbOut& operator<<(NdbOut& out, const File_formats::Undofile::Zero_page&);

#endif