This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
/
ybinlogp.h
140 lines (119 loc) · 3.54 KB
/
ybinlogp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
* binlogp: A mysql binary log parser and query tool
*
* (C) 2010 Yelp, Inc.
*
* This work is licensed under the ISC/OpenBSD License. The full
* contents of that license can be found under license.txt
*/
#ifndef _YBINLOGP_H_
#define _YBINLOGP_H_
#include <stdint.h>
#include <sys/types.h>
#define BINLOG_VERSION 4
#define EVENT_HEADER_SIZE 19 /* we tack on extra stuff at the end */
#pragma pack(push)
#pragma pack(1) /* force byte alignment */
struct event {
uint32_t timestamp;
uint8_t type_code;
uint32_t server_id;
uint32_t length;
uint32_t next_position;
uint16_t flags;
char* data;
off64_t offset;
};
#define format_description_event_data(e) (e->data + ((struct format_description_event*)e->data)->header_length)
#define format_description_event_data_len(e) (((struct format_description_event*)e->data)->header_len - EVENT_HEADER_SIZE)
struct format_description_event {
uint16_t format_version; /* ought to be 4 */
char server_version[50];
uint32_t timestamp;
uint8_t header_len;
// random data
};
#define query_event_statement(e) (e->data + sizeof(struct query_event) + ((struct query_event*)e->data)->status_var_len + ((struct query_event*)e->data)->db_name_len + 1)
#define query_event_status_vars(e) (e->data + sizeof(struct query_event))
#define query_event_statement_len(e) (e->length - EVENT_HEADER_SIZE - sizeof(struct query_event) - ((struct query_event*)e->data)->status_var_len - ((struct query_event*)e->data)->db_name_len - 1)
#define query_event_db_name(e) (e->data + sizeof(struct query_event) + ((struct query_event*)e->data)->status_var_len)
struct query_event {
uint32_t thread_id;
uint32_t query_time;
uint8_t db_name_len;
uint16_t error_code;
uint16_t status_var_len;
// status variables (status_var_len)
// database name (db_name_len + 1, NUL)
// statement (the rest, not NUL)
};
struct rand_event {
uint64_t seed_1;
uint64_t seed_2;
};
struct xid_event {
uint64_t id;
};
struct intvar_event {
uint8_t type;
uint64_t value;
};
#define rotate_event_file_name(e) (e->data + 8)
#define rotate_event_file_name_len(e) (e->length - EVENT_HEADER_SIZE -8)
struct rotate_event {
uint64_t next_position;
// file name of the next file (not NUL)
};
#pragma pack(pop)
/**
* Initialize an event object. Event objects must live on the heap
* and must be destroyed with dispose_event().
*
* Just sets everything to 0 for now.
**/
void init_event(struct event*);
/**
* Reset an event object, making it re-fillable
*
* Deletes the extra data and re-inits the object
*/
void reset_event(struct event *);
/**
* Copy an event object, including any extra data
*/
int copy_event(struct event *, struct event *);
/**
* Destroy an event object and any associated data
**/
void dispose_event(struct event *);
/**
* Print out only statement events, and only the statement
**/
void print_statement_event(struct event *e);
/**
* Print out an event
**/
void print_event(struct event *e);
/**
* Read an event from the specified offset into the given event buffer.
*
* Will also malloc() space for any dynamic portions of the event, if the
* event passes check_event.
**/
int read_event(int, struct event *, off64_t);
/**
* Check to see if an event looks valid.
**/
int check_event(struct event *);
/**
* Find the offset of the next event after the one passed in.
* Uses the built-in event chaining.
*
* Usage:
* struct event *next;
* struct event *evbuf = ...
* off_t next_offset = next_after(evbuf);
* read_event(fd, next, next_offset);
**/
off64_t next_after(struct event *evbuf);
#endif /* _YBINLOGP_H_ */