1 #ifndef TNT_LOG_H_INCLUDED
2 #define TNT_LOG_H_INCLUDED
3 
4 /*
5  * Redistribution and use in source and binary forms, with or
6  * without modification, are permitted provided that the following
7  * conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above
10  *    copyright notice, this list of conditions and the
11  *    following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above
14  *    copyright notice, this list of conditions and the following
15  *    disclaimer in the documentation and/or other materials
16  *    provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22  * <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
26  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
29  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 
33 #define TNT_LOG_MAGIC_XLOG "XLOG\n"
34 #define TNT_LOG_MAGIC_SNAP "SNAP\n"
35 #define TNT_LOG_VERSION "0.11\n"
36 
37 enum tnt_log_error {
38 	TNT_LOG_EOK,
39 	TNT_LOG_EFAIL,
40 	TNT_LOG_EMEMORY,
41 	TNT_LOG_ETYPE,
42 	TNT_LOG_EVERSION,
43 	TNT_LOG_ECORRUPT,
44 	TNT_LOG_ESYSTEM,
45 	TNT_LOG_LAST
46 };
47 
48 struct tnt_log_header_v11 {
49 	uint32_t crc32_hdr;
50 	uint64_t lsn;
51 	double tm;
52 	uint32_t len;
53 	uint32_t crc32_data;
54 } __attribute__((packed));
55 
56 struct tnt_log_row_v11 {
57 	uint16_t tag;
58 	uint64_t cookie;
59 	uint16_t op;
60 } __attribute__((packed));
61 
62 struct tnt_log_row_snap_v11 {
63 	uint16_t tag;
64 	uint64_t cookie;
65 	uint32_t space;
66 	uint32_t tuple_size;
67 	uint32_t data_size;
68 } __attribute__((packed));
69 
70 enum tnt_log_type {
71 	TNT_LOG_NONE,
72 	TNT_LOG_XLOG,
73 	TNT_LOG_SNAPSHOT
74 };
75 
76 union tnt_log_value {
77 	struct tnt_request r;
78 	struct tnt_tuple t;
79 };
80 
81 struct tnt_log_row {
82 	struct tnt_log_header_v11 hdr;
83 	struct tnt_log_row_v11 row;
84 	struct tnt_log_row_snap_v11 row_snap;
85 	union tnt_log_value *value;
86 };
87 
88 struct tnt_log {
89 	enum tnt_log_type type;
90 	FILE *fd;
91 	off_t current_offset;
92 	off_t offset;
93 	int (*read)(struct tnt_log *l, char **buf, uint32_t *size);
94 	int (*process)(struct tnt_log *l, char *buf, uint32_t size,
95 		       union tnt_log_value *value);
96 	struct tnt_log_row current;
97 	union tnt_log_value current_value;
98 	enum tnt_log_error error;
99 	int errno_;
100 };
101 
102 extern const uint32_t tnt_log_marker_v11;
103 extern const uint32_t tnt_log_marker_eof_v11;
104 
105 enum tnt_log_type tnt_log_guess(char *file);
106 
107 enum tnt_log_error
108 tnt_log_open(struct tnt_log *l, char *file, enum tnt_log_type type);
109 int tnt_log_seek(struct tnt_log *l, off_t offset);
110 void tnt_log_close(struct tnt_log *l);
111 
112 struct tnt_log_row *tnt_log_next(struct tnt_log *l);
113 struct tnt_log_row *tnt_log_next_to(struct tnt_log *l, union tnt_log_value *value);
114 
115 enum tnt_log_error tnt_log_error(struct tnt_log *l);
116 char *tnt_log_strerror(struct tnt_log *l);
117 int tnt_log_errno(struct tnt_log *l);
118 
119 #endif /* TNT_LOG_H_INCLUDED */
120