summaryrefslogtreecommitdiff
path: root/Data.h
blob: b1e690ff0d25e28a6b381a99c5f5996d7e3d80fd (plain)
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
#pragma once


#define LIB_PATH "lib/"
#define DATA_PATH "data/"
#define CDN_FILE LIB_PATH "CdnDomainList.dat"
#define URL_FILE LIB_PATH "UrlDomainList.dat"

#define SPCDN_NAME  "spcdn"
#define OTHER_NAME "other"
#define STATIS_NAME "statis"
#define ALL_NAME "all"

#define STATIS_FILE DATA_PATH STATIS_NAME ".txt"

#define OUTPUT_INTERVAL 500000


//×Ö¶Îö¾Ù
enum FileForm
{
	e_sip, e_dip, e_domain, e_qtype, e_qcnt,
	e_ratio, e_dir, e_auth, e_rval, e_rtype,
	e_rcode, e_ttl, e_time, e_qlen, e_rlen,
	e_rother,
	e_end,
};


//»®·Ö½á¹¹
struct Partition
{
	ofstream ofs;
	long long cnt = 0;
	void Init(const string &name, const string &strHead)
	{
		string formName = name;
		auto lbdForm = [](char ch) {
			if(IsIdChar(ch))
				return ch;
			else
				return '_';
		};
		std::transform(SHOW_BEGIN_END(formName), formName.begin(), lbdForm);
		ofs.open(name);
		ofs <<strHead;
	}
};


//¹æ·¶»¯ÓòÃû
inline bool IsDomainChar(char ch)
{
	return (ch>='0' && ch<='9')
		|| (ch>='a' && ch<='z')
		|| ch=='-' || ch=='_' || ch=='.';
}
inline bool FormDomain(string &domain)
{
	//´óдת»»Ð¡Ð´
	std::transform(domain.begin(), domain.end(), domain.begin(), UppCharToLowChar);
	//²âÊÔÊÇ·ñÓÐÆæ¹Ö×Ö·û
	if(std::find_if_not(domain.begin(), domain.end(), IsDomainChar)!=domain.end())
		return false;
	return true;
}


//²éÕÒCDNº¯Êý
inline string *FastFindCdn(AcMachine<char, string> &mtCdn, string &domain)
{
	//´¦Àíºó׺µã
	bool bBackDot = domain.back()=='.';
	if(bBackDot)
		domain.pop_back();
	//Ôö¼Óкó׺
	domain.push_back('#');
	//Æ¥Åä
	auto ret = mtCdn.Judge(SHOW_BEGIN_END(domain));
	domain.pop_back();
	if(bBackDot)
		domain.push_back('.');
	return ret;
}


//Ìí¼ÓÐÅÏ¢º¯Êý
inline void AddMap(std::map<string, Partition> &mapPar, const string &strHead,
	const string &key, const string &strLine)
{
	auto res = mapPar.emplace(piecewise_construct, std::tie(key), make_tuple());
	if(res.second)
		res.first->second.Init(DATA_PATH+res.first->first, strHead);
	res.first->second.ofs <<strLine <<"\n";
	++ res.first->second.cnt;
}


//ͳ¼Æº¯Êý
inline void Statistic(ofstream &ofs, std::map<string, Partition> &mapPar, long long cntValid)
{
	ofs.seekp(0);
	ofs <<std::fixed;
	ofs <<ALL_NAME <<" " <<cntValid <<" " <<"100%\n";
	for(auto &pr: mapPar) {
		ofs <<pr.first <<" " <<pr.second.cnt <<" " <<(double)pr.second.cnt*100/cntValid <<"%\n";
		pr.second.ofs <<flush;
	}
	ofs <<"end\n\n\n" <<flush;
}