1/* Copyright (C) 2006-2008 MySQL AB
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 of the License.
6
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */
15
16#include "../maria_def.h"
17#include <stdio.h>
18#include <errno.h>
19#include <tap.h>
20#include "../trnman.h"
21
22extern my_bool maria_log_remove(const char *testdir);
23extern char *create_tmpdir(const char *progname);
24
25#ifndef DBUG_OFF
26static const char *default_dbug_option;
27#endif
28
29#define PCACHE_SIZE (1024*1024*10)
30#define PCACHE_PAGE TRANSLOG_PAGE_SIZE
31#define LOG_FILE_SIZE (8*1024L*1024L)
32#define LOG_FLAGS 0
33#define LONG_BUFFER_SIZE (LOG_FILE_SIZE + LOG_FILE_SIZE / 2)
34
35
36int main(int argc __attribute__((unused)), char *argv[])
37{
38 ulong i;
39 size_t pagen;
40 uchar long_tr_id[6];
41 PAGECACHE pagecache;
42 LSN lsn;
43 LEX_CUSTRING parts[TRANSLOG_INTERNAL_PARTS + 1];
44 uchar *long_buffer= malloc(LONG_BUFFER_SIZE);
45
46 MY_INIT(argv[0]);
47
48 plan(4);
49
50 bzero(&pagecache, sizeof(pagecache));
51 bzero(long_buffer, LONG_BUFFER_SIZE);
52 maria_data_root= create_tmpdir(argv[0]);
53 if (maria_log_remove(0))
54 exit(1);
55
56 bzero(long_tr_id, 6);
57#ifndef DBUG_OFF
58#if defined(__WIN__)
59 default_dbug_option= "d:t:i:O,\\ma_test_loghandler.trace";
60#else
61 default_dbug_option= "d:t:i:o,/tmp/ma_test_loghandler.trace";
62#endif
63 if (argc > 1)
64 {
65 DBUG_SET(default_dbug_option);
66 DBUG_SET_INITIAL(default_dbug_option);
67 }
68#endif
69
70 if (ma_control_file_open(TRUE, TRUE))
71 {
72 fprintf(stderr, "Can't init control file (%d)\n", errno);
73 exit(1);
74 }
75 if ((pagen= init_pagecache(&pagecache, PCACHE_SIZE, 0, 0,
76 PCACHE_PAGE, 0, 0)) == 0)
77 {
78 fprintf(stderr, "Got error: init_pagecache() (errno: %d)\n", errno);
79 exit(1);
80 }
81 if (translog_init_with_table(maria_data_root, LOG_FILE_SIZE, 50112, 0, &pagecache,
82 LOG_FLAGS, 0, &translog_example_table_init,
83 0))
84 {
85 fprintf(stderr, "Can't init loghandler (%d)\n", errno);
86 exit(1);
87 }
88 /* Suppressing of automatic record writing */
89 dummy_transaction_object.first_undo_lsn|= TRANSACTION_LOGGED_LONG_ID;
90
91 /* write more then 1 file */
92 int4store(long_tr_id, 0);
93 parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
94 parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
95 if (translog_write_record(&lsn,
96 LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
97 &dummy_transaction_object, NULL, 6,
98 TRANSLOG_INTERNAL_PARTS + 1,
99 parts, NULL, NULL))
100 {
101 fprintf(stderr, "Can't write record #%lu\n", (ulong) 0);
102 translog_destroy();
103 exit(1);
104 }
105
106 translog_purge(lsn);
107 if (!translog_is_file(1))
108 {
109 fprintf(stderr, "First file was removed after first record\n");
110 translog_destroy();
111 exit(1);
112 }
113 ok(1, "First is not removed");
114
115 for(i= 0; i < LOG_FILE_SIZE/6 && LSN_FILE_NO(lsn) == 1; i++)
116 {
117 if (translog_write_record(&lsn,
118 LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
119 &dummy_transaction_object, NULL, 6,
120 TRANSLOG_INTERNAL_PARTS + 1,
121 parts, NULL, NULL))
122 {
123 fprintf(stderr, "Can't write record #%lu\n", (ulong) 0);
124 translog_destroy();
125 exit(1);
126 }
127 }
128
129 translog_purge(lsn);
130 if (translog_is_file(1))
131 {
132 fprintf(stderr, "First file was not removed.\n");
133 translog_destroy();
134 exit(1);
135 }
136
137 ok(1, "First file is removed");
138
139 parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_buffer;
140 parts[TRANSLOG_INTERNAL_PARTS + 0].length= LONG_BUFFER_SIZE;
141 if (translog_write_record(&lsn,
142 LOGREC_VARIABLE_RECORD_0LSN_EXAMPLE,
143 &dummy_transaction_object, NULL, LONG_BUFFER_SIZE,
144 TRANSLOG_INTERNAL_PARTS + 1, parts, NULL, NULL))
145 {
146 fprintf(stderr, "Can't write variable record\n");
147 translog_destroy();
148 exit(1);
149 }
150
151 translog_purge(lsn);
152 if (!translog_is_file(2) || !translog_is_file(3))
153 {
154 fprintf(stderr, "Second file (%d) or third file (%d) is not present.\n",
155 translog_is_file(2), translog_is_file(3));
156 translog_destroy();
157 exit(1);
158 }
159
160 ok(1, "Second and third files are not removed");
161
162 int4store(long_tr_id, 0);
163 parts[TRANSLOG_INTERNAL_PARTS + 0].str= long_tr_id;
164 parts[TRANSLOG_INTERNAL_PARTS + 0].length= 6;
165 if (translog_write_record(&lsn,
166 LOGREC_FIXED_RECORD_0LSN_EXAMPLE,
167 &dummy_transaction_object, NULL, 6,
168 TRANSLOG_INTERNAL_PARTS + 1,
169 parts, NULL, NULL))
170 {
171 fprintf(stderr, "Can't write last record\n");
172 translog_destroy();
173 exit(1);
174 }
175
176 translog_purge(lsn);
177 if (translog_is_file(2))
178 {
179 fprintf(stderr, "Second file is not removed\n");
180 translog_destroy();
181 exit(1);
182 }
183
184 ok(1, "Second file is removed");
185
186 translog_destroy();
187 end_pagecache(&pagecache, 1);
188 ma_control_file_end();
189 if (maria_log_remove(maria_data_root))
190 exit(1);
191
192 my_uuid_end();
193 my_free_open_file_info();
194 my_end(0);
195
196 exit(0);
197}
198
199#include "../ma_check_standalone.h"
200