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 empty log files
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         r = toku_logger_close(&logger);
65         assert(r == 0);
66     }
67 
68     // create N log files with a hello message
69     for (int i=0; i<N; i++) {
70         r = toku_logger_create(&logger);
71         assert(r == 0);
72 
73         r = toku_logger_open(TOKU_TEST_FILENAME, logger);
74         assert(r == 0);
75 
76         char str[32];
77         sprintf(str, "hello%d", helloseq++);
78         BYTESTRING bs0 = { .len = (uint32_t) strlen(str), .data = str };
79         toku_log_comment(logger, &lsn, 0, 0, bs0);
80 
81         r = toku_logger_close(&logger);
82         assert(r == 0);
83     }
84 
85     // create N empty log files
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         r = toku_logger_close(&logger);
94         assert(r == 0);
95     }
96 
97     // verify the log forwards
98     TOKULOGCURSOR lc = NULL;
99     struct log_entry *le;
100 
101     r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
102     assert(r == 0 && lc != NULL);
103 
104     helloseq = 0;
105     for (int i=0; i<N; i++) {
106 
107         r = toku_logcursor_next(lc, &le);
108         assert(r == 0 && le->cmd == LT_comment);
109         char expect[32];
110         sprintf(expect, "hello%d", helloseq++);
111         assert(le->u.comment.comment.len == strlen(expect) && memcmp(le->u.comment.comment.data, expect, le->u.comment.comment.len) == 0);
112     }
113 
114     r = toku_logcursor_next(lc, &le);
115     assert(r != 0);
116 
117     r = toku_logcursor_destroy(&lc);
118     assert(r == 0 && lc == NULL);
119 
120     // verify the log backwards
121     r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
122     assert(r == 0 && lc != NULL);
123 
124     helloseq = N;
125     for (int i=0; i<N; i++) {
126 
127         r = toku_logcursor_prev(lc, &le);
128         assert(r == 0 && le->cmd == LT_comment);
129         char expect[32];
130         sprintf(expect, "hello%d", --helloseq);
131         assert(le->u.comment.comment.len == strlen(expect) && memcmp(le->u.comment.comment.data, expect, le->u.comment.comment.len) == 0);
132     }
133 
134     r = toku_logcursor_prev(lc, &le);
135     assert(r != 0);
136 
137     r = toku_logcursor_destroy(&lc);
138     assert(r == 0 && lc == NULL);
139 
140     toku_os_recursive_delete(TOKU_TEST_FILENAME);
141 
142     return 0;
143 }
144