001: /**
002: * com.mckoi.database.GTStatisticsDataSource 28 Apr 2001
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.database;
024:
025: import com.mckoi.util.Stats;
026:
027: /**
028: * An implementation of MutableTableDataSource that presents database
029: * statistical information.
030: * <p>
031: * NOTE: This is not designed to be a long kept object. It must not last
032: * beyond the lifetime of a transaction.
033: *
034: * @author Tobias Downer
035: */
036:
037: final class GTStatisticsDataSource extends GTDataSource {
038:
039: /**
040: * Contains all the statistics information for this session.
041: */
042: private String[] statistics_info;
043:
044: /**
045: * The system database stats.
046: */
047: private Stats stats;
048:
049: /**
050: * Constructor.
051: */
052: public GTStatisticsDataSource(DatabaseConnection connection) {
053: super (connection.getSystem());
054: stats = connection.getDatabase().stats();
055: }
056:
057: /**
058: * Initialize the data source.
059: */
060: public GTStatisticsDataSource init() {
061:
062: synchronized (stats) {
063: stats.set((int) (Runtime.getRuntime().freeMemory() / 1024),
064: "Runtime.memory.freeKB");
065: stats.set(
066: (int) (Runtime.getRuntime().totalMemory() / 1024),
067: "Runtime.memory.totalKB");
068:
069: String[] key_set = stats.keyList();
070: int glob_length = key_set.length * 2;
071: statistics_info = new String[glob_length];
072:
073: for (int i = 0; i < glob_length; i += 2) {
074: String key_name = key_set[i / 2];
075: statistics_info[i] = key_name;
076: statistics_info[i + 1] = stats.statString(key_name);
077: }
078:
079: }
080: return this ;
081: }
082:
083: // ---------- Implemented from GTDataSource ----------
084:
085: public DataTableDef getDataTableDef() {
086: return DEF_DATA_TABLE_DEF;
087: }
088:
089: public int getRowCount() {
090: return statistics_info.length / 2;
091: }
092:
093: public TObject getCellContents(final int column, final int row) {
094: switch (column) {
095: case 0: // stat_name
096: return columnValue(column, statistics_info[row * 2]);
097: case 1: // value
098: return columnValue(column, statistics_info[(row * 2) + 1]);
099: default:
100: throw new Error("Column out of bounds.");
101: }
102: }
103:
104: // ---------- Overwritten from GTDataSource ----------
105:
106: public void dispose() {
107: super .dispose();
108: statistics_info = null;
109: stats = null;
110: }
111:
112: // ---------- Static ----------
113:
114: /**
115: * The data table def that describes this table of data source.
116: */
117: static final DataTableDef DEF_DATA_TABLE_DEF;
118:
119: static {
120:
121: DataTableDef def = new DataTableDef();
122: def.setTableName(new TableName(Database.SYSTEM_SCHEMA,
123: "sUSRDatabaseStatistics"));
124:
125: // Add column definitions
126: def.addColumn(stringColumn("stat_name"));
127: def.addColumn(stringColumn("value"));
128:
129: // Set to immutable
130: def.setImmutable();
131:
132: DEF_DATA_TABLE_DEF = def;
133:
134: }
135:
136: }
|