19 #include <ndb_global.h>
20 #include "tuppage.hpp"
31 operator <<(NdbOut& out,
const Record&
rec)
33 out <<
"[ idx: " << rec.idx <<
" sz: " << rec.size <<
" ]";
41 cmp(
const Uint32 *p1,
const Uint32 *p2, Uint32 words)
43 if(memcmp(p1, p2, 4*words) == 0)
46 for(Uint32
i = 0;
i<words;
i++)
47 printf(
" %.8x", p1[
i]);
50 for(Uint32 i = 0; i<words; i++)
51 printf(
" %.8x", p2[i]);
59 do_test(
int loops,
int dist[5])
61 fprintf(stderr,
"do_test(%d, [ %d %d %d %d %d ])\n",
74 for(
int i = 0; i<loops; i++)
76 assert(page.high_index + page.insert_pos <= page.DATA_WORDS);
78 for(
int j = 0; j<allocated; j++)
81 Uint32* ptr= page.get_ptr(rec.idx);
82 Uint32 pos = page.get_ptr(rec.idx) - page.m_data;
83 if (page.get_entry_len(rec.idx) != rec.size)
85 ndbout <<
"INVALID LEN " << j <<
" " << rec <<
" pos: " << pos << endl;
86 ndbout << page << endl;
90 if(!cmp(ptr, rec.data, rec.size))
92 ndbout <<
"FAILED " << j <<
" " << rec <<
" pos: " << pos << endl;
93 ndbout << page << endl;
100 int rnd= rand() % 100;
101 for(op= 0; op<5; op++)
107 if(page.free_space <= 2 && op == 0)
goto loop;
113 rec.size= 1 + (rand() % (page.free_space-1));
114 rec.data =
new Uint32[rec.size];
115 for(Uint32 i= 0; i<rec.size; i++)
117 rec.data[
i] = rand();
119 ndbout <<
"Alloc hi: " << page.high_index <<
" (" <<
120 ((rnd < 30) ?
"any" :
122 (rnd < 80) ?
"exp" :
"fail") <<
") ";
123 ndbout << rec.size << flush;
132 for(Uint32 i = page.high_index - 1; i > 0; i--)
134 if (page.get_index_word(i) & page.FREE)
137 if (free.size() > 100)
143 rec.idx = free[rand() % free.size()];
144 if (page.
alloc_record(rec.idx, rec.size, &tmp) != rec.idx)
151 rec.idx = page.high_index;
152 if (page.
alloc_record(rec.idx, rec.size, &tmp) != rec.idx)
154 if (rec.size + 1 != page.free_space)
165 rec.idx = page.high_index + (rand() % (page.free_space - rec.size));
166 if (page.
alloc_record(rec.idx, rec.size, &tmp) != rec.idx)
173 rec.idx = page.high_index + (page.free_space - rec.size) + 1;
174 if (page.
alloc_record(rec.idx, rec.size, &tmp) == rec.idx)
183 Uint32 pos = page.get_ptr(rec.idx) - page.m_data;
184 ndbout <<
" -> " << rec.idx
185 <<
" pos: " << pos << endl;
186 Uint32* ptr= page.get_ptr(rec.idx);
187 memcpy(ptr, rec.data, 4*rec.size);
188 records[allocated++] =
rec;
193 int no= rand() % allocated;
195 Uint32 pos = page.get_ptr(rec.idx) - page.m_data;
196 ndbout <<
"Free hi: " << page.high_index <<
" no: " << no <<
" idx: " << rec.idx <<
" pos: " << pos << endl;
197 Uint32* ptr= page.get_ptr(rec.idx);
198 assert(page.get_entry_len(rec.idx) == rec.size);
199 cmp(ptr, rec.data, rec.size);
203 for (
unsigned k = no; k + 1 < allocated; k++)
204 records[k] = records[k+1];
210 ndbout <<
"Reorg" << endl;
215 Uint32 free = page.free_space;
221 int no = rand() % allocated;
223 ndbout <<
"Expand no: " << no <<
" idx: " << rec.idx
224 <<
" add: " << free <<
" reorg: "
230 Uint32 len = page.get_entry_len(rec.idx);
231 memcpy(buffer, page.get_ptr(rec.idx), 4*len);
232 page.set_entry_len(rec.idx, 0);
233 page.free_space += len;
235 memcpy(page.get_free_space_ptr(), buffer, 4*len);
236 page.set_entry_offset(rec.idx, page.insert_pos);
238 records[no].size = 0;
241 page.grow_entry(rec.idx, free);
242 records[no].size += free;
243 Uint32 *ptr = page.get_ptr(rec.idx);
244 Uint32 *new_data =
new Uint32[records[no].size];
245 for(Uint32 i= 0; i<records[no].size; i++)
247 ptr[
i] = new_data[
i] = rand();
250 records[no].data = new_data;
256 int no = rand() % allocated;
258 Uint32 sz = rec.size / 2 + 1;
259 ndbout <<
"Shrink no: " << no <<
" idx: " << rec.idx <<
" remove: "
260 << (rec.size - sz) << endl;
261 page.shrink_entry(rec.idx, sz);
262 records[no].size = sz;
268 ndbout << page << endl;
272 main(
int argc,
char **argv)
278 time_t seed = time(0);
280 fprintf(stderr,
"srand(%d)\n", seed);
284 int t1[] = { 10, 60, 70, 85, 100 };
285 int t2[] = { 30, 60, 70, 85, 100 };
286 int t3[] = { 50, 60, 70, 85, 100 };