/
CachedQuery.java
65 lines (56 loc) · 1.88 KB
/
CachedQuery.java
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
/*-------------------------------------------------------------------------
*
* Copyright (c) 2015, PostgreSQL Global Development Group
*
*
*-------------------------------------------------------------------------
*/
package org.postgresql.core;
import org.postgresql.util.CanEstimateSize;
/**
* Stores information on the parsed JDBC query. It is used to cut parsing overhead when executing
* the same query through {@link java.sql.Connection#prepareStatement(String)}.
*/
public class CachedQuery implements CanEstimateSize {
/**
* Cache key. {@link String} or {@code org.postgresql.jdbc.CallableQueryKey}. It is assumed that
* {@code String.valueOf(key)*2} would give reasonable estimate of the number of retained bytes by
* given key (see {@link #getSize}).
*/
public final Object key;
public final Query query;
public final boolean isFunction;
public final boolean outParmBeforeFunc;
private int executeCount;
public CachedQuery(Object key, Query query, boolean isFunction, boolean outParmBeforeFunc) {
this.key = key;
this.query = query;
this.isFunction = isFunction;
this.outParmBeforeFunc = outParmBeforeFunc;
}
public void increaseExecuteCount() {
if (executeCount < Integer.MAX_VALUE) {
executeCount++;
}
}
public void increaseExecuteCount(int inc) {
int newValue = executeCount + inc;
if (newValue > 0) { // if overflows, just ignore the update
executeCount = newValue;
}
}
/**
* Number of times this statement has been used
*
* @return number of times this statement has been used
*/
public int getExecuteCount() {
return executeCount;
}
@Override
public long getSize() {
int queryLength = String.valueOf(key).length() * 2 /* 2 bytes per char */;
return queryLength * 2 /* original query and native sql */
+ 100L /* entry in hash map, CachedQuery wrapper, etc */;
}
}