MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
archive_test.c
1 /* Copyright (C) 2006 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 St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 #include "azlib.h"
17 #include <string.h>
18 #include <assert.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <my_getopt.h>
22 #include <mysql_version.h>
23 
24 #define ARCHIVE_ROW_HEADER_SIZE 4
25 
26 #define COMMENT_STRING "Your bases"
27 #define FRM_STRING "My bases"
28 #define TEST_FILENAME "test.az"
29 #define TEST_STRING_INIT "YOU don't know about me without you have read a book by the name of The Adventures of Tom Sawyer; but that ain't no matter. That book was made by Mr. Mark Twain, and he told the truth, mainly. There was things which he stretched, but mainly he told the truth. That is nothing. I never seen anybody but lied one time or another, without it was Aunt Polly, or the widow, or maybe Mary. Aunt Polly--Tom's Aunt Polly, she is--and Mary, and the Widow Douglas is all told about in that book, which is mostly a true book, with some stretchers, as I said before. Now the way that the book winds up is this: Tom and me found the money that the robbers hid in the cave, and it made us rich. We got six thousand dollars apiece--all gold. It was an awful sight of money when it was piled up. Well, Judge Thatcher he took it and put it out at interest, and it fetched us a dollar a day apiece all the year round --more than a body could tell what to do with. The Widow Douglas she took me for her son, and allowed she would..."
30 #define TEST_LOOP_NUM 100
31 
32 
33 #define ARCHIVE_ROW_HEADER_SIZE 4
34 
35 #define BUFFER_LEN 1024 + ARCHIVE_ROW_HEADER_SIZE
36 
37 char test_string[BUFFER_LEN];
38 
39 #define TWOGIG LL(2147483648)
40 #define FOURGIG LL(4294967296)
41 #define EIGHTGIG LL(8589934592)
42 
43 /* prototypes */
44 int size_test(unsigned long long length, unsigned long long rows_to_test_for);
45 
46 
47 int main(int argc, char *argv[])
48 {
49  unsigned int ret;
50  char comment_str[10];
51 
52  int error;
53  unsigned int x;
54  int written_rows= 0;
55  azio_stream writer_handle, reader_handle;
56  char buffer[BUFFER_LEN];
57 
58  int4store(test_string, 1024);
59  memcpy(test_string+sizeof(unsigned int), TEST_STRING_INIT, 1024);
60 
61  unlink(TEST_FILENAME);
62 
63  if (argc > 1)
64  return 0;
65 
66  MY_INIT(argv[0]);
67 
68  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_CREAT|O_RDWR|O_BINARY)))
69  {
70  printf("Could not create test file\n");
71  return 0;
72  }
73 
74  azwrite_comment(&writer_handle, (char *)COMMENT_STRING,
75  (unsigned int)strlen(COMMENT_STRING));
76  azread_comment(&writer_handle, comment_str);
77  assert(!memcmp(COMMENT_STRING, comment_str,
78  strlen(COMMENT_STRING)));
79 
80  azwrite_frm(&writer_handle, (char *)FRM_STRING,
81  (unsigned int)strlen(FRM_STRING));
82  azread_frm(&writer_handle, comment_str);
83  assert(!memcmp(FRM_STRING, comment_str,
84  strlen(FRM_STRING)));
85 
86 
87  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
88  {
89  printf("Could not open test file\n");
90  return 0;
91  }
92 
93  assert(reader_handle.rows == 0);
94  assert(reader_handle.auto_increment == 0);
95  assert(reader_handle.check_point == 0);
96  assert(reader_handle.forced_flushes == 0);
97  assert(reader_handle.dirty == AZ_STATE_DIRTY);
98 
99  for (x= 0; x < TEST_LOOP_NUM; x++)
100  {
101  ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
102  assert(ret == BUFFER_LEN);
103  written_rows++;
104  }
105  azflush(&writer_handle, Z_SYNC_FLUSH);
106 
107  azread_comment(&writer_handle, comment_str);
108  assert(!memcmp(COMMENT_STRING, comment_str,
109  strlen(COMMENT_STRING)));
110 
111  /* Lets test that our internal stats are good */
112  assert(writer_handle.rows == TEST_LOOP_NUM);
113 
114  /* Reader needs to be flushed to make sure it is up to date */
115  azflush(&reader_handle, Z_SYNC_FLUSH);
116  assert(reader_handle.rows == TEST_LOOP_NUM);
117  assert(reader_handle.auto_increment == 0);
118  assert(reader_handle.check_point == 96);
119  assert(reader_handle.forced_flushes == 1);
120  assert(reader_handle.comment_length == 10);
121  assert(reader_handle.dirty == AZ_STATE_SAVED);
122 
123  writer_handle.auto_increment= 4;
124  azflush(&writer_handle, Z_SYNC_FLUSH);
125  assert(writer_handle.rows == TEST_LOOP_NUM);
126  assert(writer_handle.auto_increment == 4);
127  assert(writer_handle.check_point == 96);
128  assert(writer_handle.forced_flushes == 2);
129  assert(writer_handle.dirty == AZ_STATE_SAVED);
130 
131  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
132  {
133  printf("Could not open test file\n");
134  return 0;
135  }
136 
137  /* Read the original data */
138  for (x= 0; x < writer_handle.rows; x++)
139  {
140  ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
141  assert(!error);
142  assert(ret == BUFFER_LEN);
143  assert(!memcmp(buffer, test_string, ret));
144  }
145  assert(writer_handle.rows == TEST_LOOP_NUM);
146 
147  /* Test here for falling off the planet */
148 
149  /* Final Write before closing */
150  ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
151  assert(ret == BUFFER_LEN);
152 
153  /* We don't use FINISH, but I want to have it tested */
154  azflush(&writer_handle, Z_FINISH);
155 
156  assert(writer_handle.rows == TEST_LOOP_NUM+1);
157 
158  /* Read final write */
159  azrewind(&reader_handle);
160  for (x= 0; x < writer_handle.rows; x++)
161  {
162  ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
163  assert(ret == BUFFER_LEN);
164  assert(!error);
165  assert(!memcmp(buffer, test_string, ret));
166  }
167 
168 
169  azclose(&writer_handle);
170 
171  /* Rewind and full test */
172  azrewind(&reader_handle);
173  for (x= 0; x < writer_handle.rows; x++)
174  {
175  ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
176  assert(ret == BUFFER_LEN);
177  assert(!error);
178  assert(!memcmp(buffer, test_string, ret));
179  }
180 
181  printf("Finished reading\n");
182 
183  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_RDWR|O_BINARY)))
184  {
185  printf("Could not open file (%s) for appending\n", TEST_FILENAME);
186  return 0;
187  }
188  ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
189  assert(ret == BUFFER_LEN);
190  azflush(&writer_handle, Z_SYNC_FLUSH);
191 
192  /* Rewind and full test */
193  azrewind(&reader_handle);
194  for (x= 0; x < writer_handle.rows; x++)
195  {
196  ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
197  assert(!error);
198  assert(ret == BUFFER_LEN);
199  assert(!memcmp(buffer, test_string, ret));
200  }
201 
202  /* Reader needs to be flushed to make sure it is up to date */
203  azflush(&reader_handle, Z_SYNC_FLUSH);
204  assert(reader_handle.rows == 102);
205  assert(reader_handle.auto_increment == 4);
206  assert(reader_handle.check_point == 1290);
207  assert(reader_handle.forced_flushes == 4);
208  assert(reader_handle.dirty == AZ_STATE_SAVED);
209 
210  azflush(&writer_handle, Z_SYNC_FLUSH);
211  assert(writer_handle.rows == reader_handle.rows);
212  assert(writer_handle.auto_increment == reader_handle.auto_increment);
213  assert(writer_handle.check_point == reader_handle.check_point);
214  /* This is +1 because we do a flush right before we read */
215  assert(writer_handle.forced_flushes == reader_handle.forced_flushes + 1);
216  assert(writer_handle.dirty == reader_handle.dirty);
217 
218  azclose(&writer_handle);
219  azclose(&reader_handle);
220  unlink(TEST_FILENAME);
221 
222  /* Start size tests */
223  printf("About to run 2/4/8 gig tests now, you may want to hit CTRL-C\n");
224  size_test(TWOGIG, 2088992L);
225  size_test(FOURGIG, 4177984L);
226  size_test(EIGHTGIG, 8355968L);
227 
228  return 0;
229 }
230 
231 int size_test(unsigned long long length, unsigned long long rows_to_test_for)
232 {
233  azio_stream writer_handle, reader_handle;
234  unsigned long long write_length;
235  unsigned long long read_length= 0;
236  unsigned long long count;
237  unsigned int ret;
238  char buffer[BUFFER_LEN];
239  int error;
240 
241  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC|O_BINARY)))
242  {
243  printf("Could not create test file\n");
244  return 0;
245  }
246 
247  for (count= 0, write_length= 0; write_length < length ;
248  write_length+= ret)
249  {
250  count++;
251  ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
252  if (ret != BUFFER_LEN)
253  {
254  printf("Size %u\n", ret);
255  assert(ret != BUFFER_LEN);
256  }
257  if ((write_length % 14031) == 0)
258  {
259  azflush(&writer_handle, Z_SYNC_FLUSH);
260  }
261  }
262  assert(write_length != count * BUFFER_LEN); /* Number of rows time BUFFER_LEN */
263  azflush(&writer_handle, Z_SYNC_FLUSH);
264 
265  printf("Reading back data\n");
266 
267  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
268  {
269  printf("Could not open test file\n");
270  return 0;
271  }
272 
273  while ((ret= azread(&reader_handle, buffer, BUFFER_LEN, &error)))
274  {
275  read_length+= ret;
276  assert(!memcmp(buffer, test_string, ret));
277  if (ret != BUFFER_LEN)
278  {
279  printf("Size %u\n", ret);
280  assert(ret != BUFFER_LEN);
281  }
282  }
283 
284  assert(read_length == write_length);
285  assert(writer_handle.rows == rows_to_test_for);
286  azclose(&writer_handle);
287  azclose(&reader_handle);
288  unlink(TEST_FILENAME);
289 
290  return 0;
291 }