UserSqlLargeStat.java
2.58 KB
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
package io.mycat.statistic.stat;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.concurrent.ConcurrentSkipListSet;
public class UserSqlLargeStat {
private final int count;
private SortedSet<SqlLarge> sqls;
public UserSqlLargeStat(int count) {
this.count = count;
this.sqls = new ConcurrentSkipListSet<>();
}
public List<SqlLarge> getSqls() {
List<SqlLarge> list = new ArrayList<>(sqls);
return list;
}
public void add(String sql, long sqlRows, long executeTime, long startTime, long endTime) {
SqlLarge sqlLarge = new SqlLarge(sql, sqlRows, executeTime, startTime, endTime);
this.add( sqlLarge );
}
public void add(SqlLarge sql) {
sqls.add(sql);
}
public void reset() {
this.clear();
}
public void clear() {
sqls.clear();
}
public void recycle() {
if(sqls.size() > count){
SortedSet<SqlLarge> sqls2 = new ConcurrentSkipListSet<>();
List<SqlLarge> keyList = new ArrayList<SqlLarge>(sqls);
int i = 0;
for(SqlLarge key : keyList){
if(i == count) {
break;
}
sqls2.add(key);
i++;
}
sqls = sqls2;
}
}
/**
* 记录 SQL 及返回行数
*/
public static class SqlLarge implements Comparable<SqlLarge> {
private String sql;
private long sqlRows;
private long executeTime;
private long startTime;
private long endTime;
public SqlLarge(String sql, long sqlRows, long executeTime, long startTime, long endTime) {
super();
this.sql = sql;
this.sqlRows = sqlRows;
this.executeTime = executeTime;
this.startTime = startTime;
this.endTime = endTime;
}
public String getSql() {
return sql;
}
public long getSqlRows() {
return sqlRows;
}
public long getStartTime() {
return startTime;
}
public long getExecuteTime() {
return executeTime;
}
public long getEndTime() {
return endTime;
}
@Override
public int compareTo(SqlLarge o) {
long para = o.sqlRows - sqlRows;
return para == 0 ? (o.sql.hashCode() - sql.hashCode()) :(int) (para);
}
@Override
public boolean equals(Object arg0) {
return super.equals(arg0);
}
@Override
public int hashCode() {
return super.hashCode();
}
}
}