UserSqlLastStat.java
2.24 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
package io.mycat.statistic.stat;
import java.util.*;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.locks.ReentrantLock;
/**
* 最后执行的 Sql
*
* @author zhuam
*
*/
public class UserSqlLastStat {
private static final int MAX_RECORDS = 1024;
private SortedSet<SqlLast> sqls;
public UserSqlLastStat(int count) {
this.sqls = new ConcurrentSkipListSet<>();
}
public List<SqlLast> getSqls() {
List<SqlLast> keyList = new ArrayList<SqlLast>(sqls);
return keyList;
}
public void add(String sql, long executeTime, long startTime, long endTime ) {
SqlLast sqlLast = new SqlLast(sql, executeTime, startTime, endTime);
sqls.add(sqlLast);
}
public void reset() {
this.clear();
}
public void clear() {
sqls.clear();
}
public void recycle(){
if(sqls.size() > MAX_RECORDS){
SortedSet<SqlLast> sqls2 = new ConcurrentSkipListSet<>();
List<SqlLast> keyList = new ArrayList<SqlLast>(sqls);
int i = 0;
for(SqlLast key : keyList){
if(i == MAX_RECORDS) {
break;
}
sqls2.add(key);
i++;
}
sqls = sqls2;
}
}
/**
* 记录SQL
*/
public static class SqlLast implements Comparable<SqlLast>{
private String sql;
private long executeTime;
private long startTime;
private long endTime;
public SqlLast(String sql, long executeTime, long startTime, long endTime) {
super();
this.sql = sql;
this.executeTime = executeTime;
this.startTime = startTime;
this.endTime = endTime;
}
public String getSql() {
return sql;
}
public long getStartTime() {
return startTime;
}
public long getExecuteTime() {
return executeTime;
}
public long getEndTime() {
return endTime;
}
@Override
public int compareTo(SqlLast o) {
long st1 = o == null ? 0 : o.getStartTime();
return (int) ( st1 - this.getStartTime());
}
@Override
public boolean equals(Object obj) {
if(obj instanceof SqlLast) {
return this.compareTo((SqlLast)obj) == 0;
} else {
return super.equals(obj);
}
}
}
}