-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdb.h
79 lines (65 loc) · 2.52 KB
/
db.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
#ifndef OCELOT_DB_H
#define OCELOT_DB_H
#pragma GCC visibility push(default)
#include <mysql++/mysql++.h>
#include <string>
#include <unordered_map>
#include <queue>
#include <boost/thread/mutex.hpp>
#include "logger.h"
class mysql {
private:
mysqlpp::Connection conn;
std::string update_user_buffer;
std::string update_torrent_buffer;
std::string update_peer_buffer;
std::string update_snatch_buffer;
std::string update_token_buffer;
std::string update_peer_hist_buffer;
std::queue<std::string> user_queue;
std::queue<std::string> torrent_queue;
std::queue<std::string> peer_queue;
std::queue<std::string> snatch_queue;
std::queue<std::string> token_queue;
std::queue<std::string> peer_hist_queue;
std::string db, server, db_user, pw;
bool u_active, t_active, p_active, s_active, tok_active, hist_active;
// These locks prevent more than one thread from reading/writing the buffers.
// These should be held for the minimum time possible.
boost::mutex user_buffer_lock;
boost::mutex torrent_buffer_lock;
boost::mutex peer_buffer_lock;
boost::mutex snatch_buffer_lock;
boost::mutex user_token_lock;
boost::mutex peer_hist_buffer_lock;
void do_flush_users();
void do_flush_torrents();
void do_flush_snatches();
void do_flush_peers();
void do_flush_tokens();
void do_flush_peer_hist();
void flush_users();
void flush_torrents();
void flush_snatches();
void flush_peers();
void flush_tokens();
void flush_peer_hist();
public:
mysql(std::string mysql_db, std::string mysql_host, std::string username, std::string password);
void load_torrents(std::unordered_map<std::string, torrent> &torrents);
void load_users(std::unordered_map<std::string, user> &users);
void load_tokens(std::unordered_map<std::string, torrent> &torrents);
void load_whitelist(std::vector<std::string> &whitelist);
void record_user(std::string &record); // (id,uploaded_change,downloaded_change)
void record_torrent(std::string &record); // (id,seeders,leechers,snatched_change,balance)
void record_snatch(std::string &record); // (uid,fid,tstamp)
void record_peer(std::string &record, std::string &ip, std::string &peer_id, std::string &useragent); // (uid,fid,active,peerid,useragent,ip,uploaded,downloaded,upspeed,downspeed,left,timespent,announces)
void record_token(std::string &record);
void record_peer_hist(std::string &record, std::string &peer_id, int tid);
void flush();
bool all_clear();
boost::mutex torrent_list_mutex;
logger* logger_ptr;
};
#pragma GCC visibility pop
#endif