public class IndexStatsUtil
extends java.lang.Object
This implementation assumes all tables/indexes belong to the current schema.
The timeout value is used to make the utility more resilient to differences in timing due to varying scheduling decisions, processor speeds, etc. If the system table contains the wrong number of statistics objects for the query, it will be queried repeatedly until the right number of statistics objects is obtained or the query times out.
Modifier and Type | Class and Description |
---|---|
static class |
IndexStatsUtil.IdxStats
Immutable class representing index statistics.
|
Modifier and Type | Field and Description |
---|---|
private java.sql.Connection |
con |
private static boolean |
INDEX |
private static int |
NO_EXPECTATION |
private java.sql.PreparedStatement |
psGetIdToNameMapConglom |
private java.sql.PreparedStatement |
psGetIdToNameMapTable |
private java.sql.PreparedStatement |
psGetIndexId |
private java.sql.PreparedStatement |
psGetStats |
private java.sql.PreparedStatement |
psGetStatsForIndex |
private java.sql.PreparedStatement |
psGetStatsForTable |
private java.sql.PreparedStatement |
psGetTableId |
private static java.lang.String |
SEP |
private static boolean |
TABLE |
private long |
timeout
Timeout in milliseconds.
|
Constructor and Description |
---|
IndexStatsUtil(java.sql.Connection con)
Creates an instance querying the given database with no timeout set.
|
IndexStatsUtil(java.sql.Connection con,
long timeout)
Creates an instance querying the given database with the specified
timeout value.
|
Modifier and Type | Method and Description |
---|---|
void |
assertIndexStats(java.lang.String index,
int expectedCount)
Asserts that the expected number of statistics exists for the specified
index.
|
void |
assertNoStats()
Asserts that there are no existing statistics in the database.
|
void |
assertNoStatsTable(java.lang.String table)
Asserts that there are no existing statistics for the specified table.
|
void |
assertStats(int expectedCount)
Asserts that the expected number of statistics exists.
|
void |
assertTableStats(java.lang.String table,
int expectedCount)
Asserts that the expected number of statistics exists for the specified
table.
|
private void |
awaitChange(IndexStatsUtil.IdxStats[] current,
long timeout)
Waits until all given statistics entries have been changed, or until
the call times out.
|
private IndexStatsUtil.IdxStats[] |
buildStatisticsList(java.sql.ResultSet rs,
java.util.Map<java.lang.String,java.lang.String> idToName)
Builds an array of statistics objects from data from the
SYS.SYSSTATISTICS system table. |
static java.lang.String |
buildStatString(IndexStatsUtil.IdxStats[] stats,
java.lang.String name)
Builds a human readable representation of a list of statistics objects.
|
private java.util.Map<java.lang.String,java.lang.String> |
getIdToNameMap()
Generates a map from ids to names for conglomerates in the database.
|
IndexStatsUtil.IdxStats[] |
getNewStatsTable(java.lang.String table,
IndexStatsUtil.IdxStats[] currentStats)
Waits for the current statistics to disappear and expects to fetch the
same number of new statistics for the table.
|
IndexStatsUtil.IdxStats[] |
getStats()
Obtains all existing statistics entries.
|
IndexStatsUtil.IdxStats[] |
getStatsIndex(java.lang.String index)
Obtains statistics for the specified index.
|
IndexStatsUtil.IdxStats[] |
getStatsIndex(java.lang.String index,
int expectedCount)
Obtains statistics for the specified index, fails if the number of
statistics objects isn't as expected within the timeout.
|
IndexStatsUtil.IdxStats[] |
getStatsTable(java.lang.String table)
Obtains statistics for the specified table.
|
IndexStatsUtil.IdxStats[] |
getStatsTable(java.lang.String table,
int expectedCount)
Obtains statistics for the specified table, fails if the number of
statistics objects isn't as expected within the timeout.
|
void |
printStats()
Prints all entries in the
SYS.SYSSTATISTICS system table. |
private IndexStatsUtil.IdxStats[] |
querySystemTable(java.lang.String conglomId,
boolean isTable,
int expectedCount)
Queries the system table
SYS.SYSSTATISTICS for statistics
associated with a specific table or index. |
void |
release()
Releases resources and closes the associated connection.
|
void |
release(boolean closeConnection)
Releases resources.
|
private static final boolean INDEX
private static final boolean TABLE
private static final int NO_EXPECTATION
private static final java.lang.String SEP
private final java.sql.Connection con
private final long timeout
private java.sql.PreparedStatement psGetTableId
private java.sql.PreparedStatement psGetStatsForTable
private java.sql.PreparedStatement psGetIndexId
private java.sql.PreparedStatement psGetStatsForIndex
private java.sql.PreparedStatement psGetStats
private java.sql.PreparedStatement psGetIdToNameMapConglom
private java.sql.PreparedStatement psGetIdToNameMapTable
public IndexStatsUtil(java.sql.Connection con)
Querying with no timeout means that if there are too few or too many statistics objects matching the query, a failure will be raised immediately.
con
- connection to the database to querypublic IndexStatsUtil(java.sql.Connection con, long timeout)
con
- connection to the database to querytimeout
- the longest time to wait to see if the expectations for a
query are met (milliseconds)public void assertNoStats() throws java.sql.SQLException
java.sql.SQLException
- if obtaining the statistics failspublic void assertNoStatsTable(java.lang.String table) throws java.sql.SQLException
java.sql.SQLException
- if obtaining the statistics failspublic void assertStats(int expectedCount) throws java.sql.SQLException
expectedCount
- expected number of statisticsjava.sql.SQLException
- if obtaining the statistics failspublic void assertTableStats(java.lang.String table, int expectedCount) throws java.sql.SQLException
table
- the target tableexpectedCount
- expected number of statisticsjava.sql.SQLException
- if obtaining the statistics failspublic void assertIndexStats(java.lang.String index, int expectedCount) throws java.sql.SQLException
index
- the target indexexpectedCount
- expected number of statisticsjava.sql.SQLException
- if obtaining the statistics failspublic static java.lang.String buildStatString(IndexStatsUtil.IdxStats[] stats, java.lang.String name)
stats
- a list of statistics (possibly empty)name
- the name of the table(s)/index(es) associated with the statspublic IndexStatsUtil.IdxStats[] getStats() throws java.sql.SQLException
java.sql.SQLException
- if obtaining the statistics failpublic IndexStatsUtil.IdxStats[] getStatsTable(java.lang.String table) throws java.sql.SQLException
table
- table namejava.sql.SQLException
- if obtaining the statistics failpublic IndexStatsUtil.IdxStats[] getStatsTable(java.lang.String table, int expectedCount) throws java.sql.SQLException
table
- table nameexpectedCount
- number of expected statistics objectsjava.sql.SQLException
- if obtaining the statistics failpublic IndexStatsUtil.IdxStats[] getNewStatsTable(java.lang.String table, IndexStatsUtil.IdxStats[] currentStats) throws java.sql.SQLException
table
- the table to get statistics forcurrentStats
- the current statisticsjava.sql.SQLException
- if obtaining statistics failspublic IndexStatsUtil.IdxStats[] getStatsIndex(java.lang.String index) throws java.sql.SQLException
index
- index namejava.sql.SQLException
- if obtaining the statistics failpublic IndexStatsUtil.IdxStats[] getStatsIndex(java.lang.String index, int expectedCount) throws java.sql.SQLException
index
- index nameexpectedCount
- number of expected statistics objectsjava.sql.SQLException
- if obtaining the statistics failprivate IndexStatsUtil.IdxStats[] querySystemTable(java.lang.String conglomId, boolean isTable, int expectedCount) throws java.sql.SQLException
SYS.SYSSTATISTICS
for statistics
associated with a specific table or index.conglomId
- conglomerate id (UUID)isTable
- tells if the conglomerate is a table or an indexexpectedCount
- the number of statistics objects expected, use
NO_EXPECTATION
to return whatever matches the query
immediatelyjava.sql.SQLException
public void printStats() throws java.sql.SQLException
SYS.SYSSTATISTICS
system table.java.sql.SQLException
- if obtaining the statistics failsprivate java.util.Map<java.lang.String,java.lang.String> getIdToNameMap() throws java.sql.SQLException
Convenience method, used for better reporting.
java.sql.SQLException
- if accessing the system tables failprivate IndexStatsUtil.IdxStats[] buildStatisticsList(java.sql.ResultSet rs, java.util.Map<java.lang.String,java.lang.String> idToName) throws java.sql.SQLException
SYS.SYSSTATISTICS
system table.rs
- a result set containing rows from SYS.SYSSTATISTICS
java.sql.SQLException
- if accessing the result set failspublic void release()
public void release(boolean closeConnection)
closeConnection
- whether to close the associated connectionprivate void awaitChange(IndexStatsUtil.IdxStats[] current, long timeout) throws java.sql.SQLException
NOTE: The method is built on the assumption that the UUIDs of statistics objects aren't reused. That is, when statistics are updated, the old row in SYS.SYSSTATISTICS will be dropped and a new row will be inserted.
current
- the statistics that must change / be replacedtimeout
- maximum number of milliseconds to wait before giving upjava.sql.SQLException
- if obtaining statistics failsApache Derby V10.13 Internals - Copyright © 2004,2016 The Apache Software Foundation. All Rights Reserved.