1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "logger/logcursor.h"
40 #include "test.h"
41 
42 const int N = 2;
43 
44 int
test_main(int argc,const char * argv[])45 test_main (int argc, const char *argv[]) {
46     default_parse_args(argc, argv);
47 
48     int r;
49     toku_os_recursive_delete(TOKU_TEST_FILENAME);
50     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU);    assert(r==0);
51     TOKULOGGER logger;
52     LSN lsn = ZERO_LSN;
53 
54     int helloseq = 0;
55 
56     // create N log files with a hello message
57     for (int i=0; i<N; i++) {
58         r = toku_logger_create(&logger);
59         assert(r == 0);
60 
61         r = toku_logger_open(TOKU_TEST_FILENAME, logger);
62         assert(r == 0);
63 
64         char str[32];
65         sprintf(str, "hello%d", helloseq++);
66         BYTESTRING bs0 = { .len = (uint32_t) strlen(str), .data = str };
67         toku_log_comment(logger, &lsn, 0, 0, bs0);
68 
69         r = toku_logger_close(&logger);
70         assert(r == 0);
71     }
72 
73     // create N empty log files
74     for (int i=0; i<N; i++) {
75         r = toku_logger_create(&logger);
76         assert(r == 0);
77 
78         r = toku_logger_open(TOKU_TEST_FILENAME, logger);
79         assert(r == 0);
80 
81         r = toku_logger_close(&logger);
82         assert(r == 0);
83     }
84 
85     // create N log files with a hello message
86     for (int i=0; i<N; i++) {
87         r = toku_logger_create(&logger);
88         assert(r == 0);
89 
90         r = toku_logger_open(TOKU_TEST_FILENAME, logger);
91         assert(r == 0);
92 
93         char str[32];
94         sprintf(str, "hello%d", helloseq++);
95         BYTESTRING bs0 = { .len = (uint32_t) strlen(str), .data = str };
96         toku_log_comment(logger, &lsn, 0, 0, bs0);
97 
98         r = toku_logger_close(&logger);
99         assert(r == 0);
100     }
101 
102     // verify the log forwards
103     TOKULOGCURSOR lc = NULL;
104     struct log_entry *le;
105 
106     r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
107     assert(r == 0 && lc != NULL);
108 
109     helloseq = 0;
110     for (int i=0; i<2*N; i++) {
111 
112         r = toku_logcursor_next(lc, &le);
113         assert(r == 0 && le->cmd == LT_comment);
114         char expect[32];
115         sprintf(expect, "hello%d", helloseq++);
116         assert(le->u.comment.comment.len == strlen(expect) && memcmp(le->u.comment.comment.data, expect, le->u.comment.comment.len) == 0);
117     }
118 
119     r = toku_logcursor_next(lc, &le);
120     assert(r != 0);
121 
122     r = toku_logcursor_destroy(&lc);
123     assert(r == 0 && lc == NULL);
124 
125     // verify the log backwards
126     r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
127     assert(r == 0 && lc != NULL);
128 
129     helloseq = 2*N;
130     for (int i=0; i<2*N; i++) {
131 
132         r = toku_logcursor_prev(lc, &le);
133         assert(r == 0 && le->cmd == LT_comment);
134         char expect[32];
135         sprintf(expect, "hello%d", --helloseq);
136         assert(le->u.comment.comment.len == strlen(expect) && memcmp(le->u.comment.comment.data, expect, le->u.comment.comment.len) == 0);
137     }
138 
139     r = toku_logcursor_prev(lc, &le);
140     assert(r != 0);
141 
142     r = toku_logcursor_destroy(&lc);
143     assert(r == 0 && lc == NULL);
144 
145     toku_os_recursive_delete(TOKU_TEST_FILENAME);
146 
147     return 0;
148 }
149