dbreader.h 2.2 KB
Newer Older
pryanikov's avatar
pryanikov committed
1
2
3
4
5
6
#ifndef REPLICATOR_DBREADER_H
#define REPLICATOR_DBREADER_H

#include <vector>
#include <string>
#include <utility>
pryanikov's avatar
pryanikov committed
7
#include <functional>
pryanikov's avatar
pryanikov committed
8
9
10
11
12
13
14
15
16

#include <Slave.h>
#include <DefaultExtState.h>
#include <nanomysql.h>

#include "serializable.h"

namespace replicator {

17
typedef std::function<bool (SerializableBinlogEventPtr&&)> BinlogEventCallback;
pryanikov's avatar
pryanikov committed
18
19
20

struct DBTable
{
pryanikov's avatar
pryanikov committed
21
22
23
24
25
26
27
28
29
30
31
32
	DBTable(
		const std::string& db,
		const std::string& table,
		const std::map<std::string, std::pair<unsigned, bool>>& filter_,
		bool is_primary_
	) :
		name(db, table), filter(filter_), is_primary(is_primary_)
	{}

	const std::pair<std::string, std::string> name;
	const std::map<std::string, std::pair<unsigned, bool>> filter;
	const bool is_primary;
pryanikov's avatar
pryanikov committed
33
34
35
36
37
};

class DBReader
{
public:
pryanikov's avatar
pryanikov committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
	DBReader(nanomysql::mysql_conn_opts& opts, unsigned connect_retry)
		: state(), slave(slave::MasterInfo(opts, connect_retry), state), stopped(false) {}

	~DBReader() {
		slave.close_connection();
	}

	void AddTable(
		const std::string& db,
		const std::string& table,
		const std::map<std::string, std::pair<unsigned, bool>>& filter,
		bool is_primary
	) {
		tables.emplace_back(db, table, filter, is_primary);
	}

	void DumpTables(std::string& binlog_name, unsigned long& binlog_pos, const BinlogEventCallback& cb);
	void ReadBinlog(const std::string& binlog_name, unsigned long binlog_pos, const BinlogEventCallback& cb);
pryanikov's avatar
pryanikov committed
56
57
	void Stop();

pryanikov's avatar
pryanikov committed
58
59
60
61
62
63
64
65
66
67
68
	void EventCallbackNormal(
		const slave::RecordSet& event,
		const std::map<std::string, std::pair<unsigned, bool>>& filter,
		const BinlogEventCallback& cb
	);
	void EventCallbackNullify(
		const slave::RecordSet& event,
		const std::map<std::string, std::pair<unsigned, bool>>& filter,
		const BinlogEventCallback& cb
	);
	void XidEventCallback(unsigned int server_id, const BinlogEventCallback& cb);
pryanikov's avatar
pryanikov committed
69
	void DumpTablesCallback(
pryanikov's avatar
pryanikov committed
70
71
		const std::string& db_name,
		const std::string& tbl_name,
pryanikov's avatar
pryanikov committed
72
		const std::vector<std::pair<unsigned, slave::PtrField>>& filter,
pryanikov's avatar
pryanikov committed
73
74
		const nanomysql::fields_t& f,
		const BinlogEventCallback& cb
pryanikov's avatar
pryanikov committed
75
	);
pryanikov's avatar
pryanikov committed
76

pryanikov's avatar
pryanikov committed
77
	// unsigned GetSecondsBehindMaster() const;
pryanikov's avatar
pryanikov committed
78
79
80
81

private:
	slave::DefaultExtState state;
	slave::Slave slave;
pryanikov's avatar
pryanikov committed
82
	std::vector<DBTable> tables;
pryanikov's avatar
pryanikov committed
83
84
	bool stopped;

pryanikov's avatar
pryanikov committed
85
	// ::time_t last_event_when;
pryanikov's avatar
pryanikov committed
86
87
};

pryanikov's avatar
pryanikov committed
88
} // replicator
pryanikov's avatar
pryanikov committed
89
90

#endif // REPLICATOR_DBREADER_H