1 /* Copyright (C) 2006 MySQL AB
2    Use is subject to license terms
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation; version 2 of the License.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software
15    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1335  USA */
16 
17 #include "azlib.h"
18 #include <string.h>
19 #include <assert.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <my_getopt.h>
23 #include <mysql_version.h>
24 
25 #define ARCHIVE_ROW_HEADER_SIZE 4
26 
27 #define COMMENT_STRING "Your bases"
28 #define FRM_STRING "My bases"
29 #define TEST_FILENAME "test.az"
30 #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..."
31 #define TEST_LOOP_NUM 100
32 
33 
34 #define ARCHIVE_ROW_HEADER_SIZE 4
35 
36 #define BUFFER_LEN (1024 + ARCHIVE_ROW_HEADER_SIZE)
37 
38 char test_string[BUFFER_LEN];
39 
40 #define TWOGIG LL(2147483648)
41 #define FOURGIG LL(4294967296)
42 #define EIGHTGIG LL(8589934592)
43 
44 /* prototypes */
45 int size_test(unsigned long long length, unsigned long long rows_to_test_for);
46 
47 
main(int argc,char * argv[])48 int main(int argc, char *argv[])
49 {
50   unsigned int ret;
51   char comment_str[10];
52 
53   int error;
54   unsigned int x;
55   int written_rows= 0;
56   azio_stream writer_handle, reader_handle;
57   char buffer[BUFFER_LEN];
58 
59   int4store(test_string, 1024);
60   memcpy(test_string+sizeof(unsigned int), TEST_STRING_INIT, 1024);
61 
62   unlink(TEST_FILENAME);
63 
64   if (argc > 1)
65     return 0;
66 
67   MY_INIT(argv[0]);
68 
69   if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_CREAT|O_RDWR|O_BINARY)))
70   {
71     printf("Could not create test file\n");
72     return 0;
73   }
74 
75   azwrite_comment(&writer_handle, (char *)COMMENT_STRING,
76                   (unsigned int)strlen(COMMENT_STRING));
77   azread_comment(&writer_handle, comment_str);
78   assert(!memcmp(COMMENT_STRING, comment_str,
79                 strlen(COMMENT_STRING)));
80 
81   azwrite_frm(&writer_handle, (char *)FRM_STRING,
82                   (unsigned int)strlen(FRM_STRING));
83   azread_frm(&writer_handle, comment_str);
84   assert(!memcmp(FRM_STRING, comment_str,
85                 strlen(FRM_STRING)));
86 
87 
88   if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
89   {
90     printf("Could not open test file\n");
91     return 0;
92   }
93 
94   assert(reader_handle.rows == 0);
95   assert(reader_handle.auto_increment == 0);
96   assert(reader_handle.check_point == 0);
97   assert(reader_handle.forced_flushes == 0);
98   assert(reader_handle.dirty == AZ_STATE_DIRTY);
99 
100   for (x= 0; x < TEST_LOOP_NUM; x++)
101   {
102     ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
103     assert(ret == BUFFER_LEN);
104     written_rows++;
105   }
106   azflush(&writer_handle,  Z_SYNC_FLUSH);
107 
108   azread_comment(&writer_handle, comment_str);
109   assert(!memcmp(COMMENT_STRING, comment_str,
110                 strlen(COMMENT_STRING)));
111 
112   /* Lets test that our internal stats are good */
113   assert(writer_handle.rows == TEST_LOOP_NUM);
114 
115   /* Reader needs to be flushed to make sure it is up to date */
116   azflush(&reader_handle,  Z_SYNC_FLUSH);
117   assert(reader_handle.rows == TEST_LOOP_NUM);
118   assert(reader_handle.auto_increment == 0);
119   assert(reader_handle.check_point == 96);
120   assert(reader_handle.forced_flushes == 1);
121   assert(reader_handle.comment_length == 10);
122   assert(reader_handle.dirty == AZ_STATE_SAVED);
123 
124   writer_handle.auto_increment= 4;
125   azflush(&writer_handle, Z_SYNC_FLUSH);
126   assert(writer_handle.rows == TEST_LOOP_NUM);
127   assert(writer_handle.auto_increment == 4);
128   assert(writer_handle.check_point == 96);
129   assert(writer_handle.forced_flushes == 2);
130   assert(writer_handle.dirty == AZ_STATE_SAVED);
131 
132   if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
133   {
134     printf("Could not open test file\n");
135     return 0;
136   }
137 
138   /* Read the original data */
139   for (x= 0; x < writer_handle.rows; x++)
140   {
141     ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
142     assert(!error);
143     assert(ret == BUFFER_LEN);
144     assert(!memcmp(buffer, test_string, ret));
145   }
146   assert(writer_handle.rows == TEST_LOOP_NUM);
147 
148   /* Test here for falling off the planet */
149 
150   /* Final Write before closing */
151   ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
152   assert(ret == BUFFER_LEN);
153 
154   /* We don't use FINISH, but I want to have it tested */
155   azflush(&writer_handle,  Z_FINISH);
156 
157   assert(writer_handle.rows == TEST_LOOP_NUM+1);
158 
159   /* Read final write */
160   azrewind(&reader_handle);
161   for (x= 0; x < writer_handle.rows; x++)
162   {
163     ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
164     assert(ret == BUFFER_LEN);
165     assert(!error);
166     assert(!memcmp(buffer, test_string, ret));
167   }
168 
169 
170   azclose(&writer_handle);
171 
172   /* Rewind and full test */
173   azrewind(&reader_handle);
174   for (x= 0; x < writer_handle.rows; x++)
175   {
176     ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
177     assert(ret == BUFFER_LEN);
178     assert(!error);
179     assert(!memcmp(buffer, test_string, ret));
180   }
181 
182   printf("Finished reading\n");
183 
184   if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_RDWR|O_BINARY)))
185   {
186     printf("Could not open file (%s) for appending\n", TEST_FILENAME);
187     return 0;
188   }
189   ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
190   assert(ret == BUFFER_LEN);
191   azflush(&writer_handle,  Z_SYNC_FLUSH);
192 
193   /* Rewind and full test */
194   azrewind(&reader_handle);
195   for (x= 0; x < writer_handle.rows; x++)
196   {
197     ret= azread(&reader_handle, buffer, BUFFER_LEN, &error);
198     assert(!error);
199     assert(ret == BUFFER_LEN);
200     assert(!memcmp(buffer, test_string, ret));
201   }
202 
203   /* Reader needs to be flushed to make sure it is up to date */
204   azflush(&reader_handle,  Z_SYNC_FLUSH);
205   assert(reader_handle.rows == 102);
206   assert(reader_handle.auto_increment == 4);
207   assert(reader_handle.check_point == 1290);
208   assert(reader_handle.forced_flushes == 4);
209   assert(reader_handle.dirty == AZ_STATE_SAVED);
210 
211   azflush(&writer_handle, Z_SYNC_FLUSH);
212   assert(writer_handle.rows == reader_handle.rows);
213   assert(writer_handle.auto_increment == reader_handle.auto_increment);
214   assert(writer_handle.check_point == reader_handle.check_point);
215   /* This is +1 because  we do a flush right before we read */
216   assert(writer_handle.forced_flushes == reader_handle.forced_flushes + 1);
217   assert(writer_handle.dirty == reader_handle.dirty);
218 
219   azclose(&writer_handle);
220   azclose(&reader_handle);
221   unlink(TEST_FILENAME);
222 
223   /* Start size tests */
224   printf("About to run 2/4/8 gig tests now, you may want to hit CTRL-C\n");
225   size_test(TWOGIG, 2088992L);
226   size_test(FOURGIG, 4177984L);
227   size_test(EIGHTGIG, 8355968L);
228 
229   return 0;
230 }
231 
size_test(unsigned long long length,unsigned long long rows_to_test_for)232 int size_test(unsigned long long length, unsigned long long rows_to_test_for)
233 {
234   azio_stream writer_handle, reader_handle;
235   unsigned long long write_length;
236   unsigned long long read_length= 0;
237   unsigned long long count;
238   unsigned int ret;
239   char buffer[BUFFER_LEN];
240   int error;
241 
242   if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_CREAT|O_RDWR|O_TRUNC|O_BINARY)))
243   {
244     printf("Could not create test file\n");
245     return 0;
246   }
247 
248   for (count= 0, write_length= 0; write_length < length ;
249        write_length+= ret)
250   {
251     count++;
252     ret= azwrite(&writer_handle, test_string, BUFFER_LEN);
253     if (ret != BUFFER_LEN)
254     {
255       printf("Size %u\n", ret);
256       assert(ret != BUFFER_LEN);
257     }
258     if ((write_length % 14031) == 0)
259     {
260       azflush(&writer_handle,  Z_SYNC_FLUSH);
261     }
262   }
263   assert(write_length != count * BUFFER_LEN); /* Number of rows time BUFFER_LEN */
264   azflush(&writer_handle,  Z_SYNC_FLUSH);
265 
266   printf("Reading back data\n");
267 
268   if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY)))
269   {
270     printf("Could not open test file\n");
271     return 0;
272   }
273 
274   while ((ret= azread(&reader_handle, buffer, BUFFER_LEN, &error)))
275   {
276     read_length+= ret;
277     assert(!memcmp(buffer, test_string, ret));
278     if (ret != BUFFER_LEN)
279     {
280       printf("Size %u\n", ret);
281       assert(ret != BUFFER_LEN);
282     }
283   }
284 
285   assert(read_length == write_length);
286   assert(writer_handle.rows == rows_to_test_for);
287   azclose(&writer_handle);
288   azclose(&reader_handle);
289   unlink(TEST_FILENAME);
290 
291   return 0;
292 }
293