KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > database > GTStatisticsDataSource


1 /**
2  * com.mckoi.database.GTStatisticsDataSource 28 Apr 2001
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.database;
26
27 import com.mckoi.util.Stats;
28
29 /**
30  * An implementation of MutableTableDataSource that presents database
31  * statistical information.
32  * <p>
33  * NOTE: This is not designed to be a long kept object. It must not last
34  * beyond the lifetime of a transaction.
35  *
36  * @author Tobias Downer
37  */

38
39 final class GTStatisticsDataSource extends GTDataSource {
40
41   /**
42    * Contains all the statistics information for this session.
43    */

44   private String JavaDoc[] statistics_info;
45
46   /**
47    * The system database stats.
48    */

49   private Stats stats;
50
51   /**
52    * Constructor.
53    */

54   public GTStatisticsDataSource(DatabaseConnection connection) {
55     super(connection.getSystem());
56     stats = connection.getDatabase().stats();
57   }
58
59   /**
60    * Initialize the data source.
61    */

62   public GTStatisticsDataSource init() {
63
64     synchronized (stats) {
65       stats.set((int) (Runtime.getRuntime().freeMemory() / 1024),
66                                                     "Runtime.memory.freeKB");
67       stats.set((int) (Runtime.getRuntime().totalMemory() / 1024),
68                                                     "Runtime.memory.totalKB");
69
70       String JavaDoc[] key_set = stats.keyList();
71       int glob_length = key_set.length * 2;
72       statistics_info = new String JavaDoc[glob_length];
73
74       for (int i = 0; i < glob_length; i += 2) {
75         String JavaDoc key_name = key_set[i / 2];
76         statistics_info[i] = key_name;
77         statistics_info[i + 1] = stats.statString(key_name);
78       }
79
80     }
81     return this;
82   }
83
84   // ---------- Implemented from GTDataSource ----------
85

86   public DataTableDef getDataTableDef() {
87     return DEF_DATA_TABLE_DEF;
88   }
89
90   public int getRowCount() {
91     return statistics_info.length / 2;
92   }
93
94   public TObject getCellContents(final int column, final int row) {
95     switch (column) {
96       case 0: // stat_name
97
return columnValue(column, statistics_info[row * 2]);
98       case 1: // value
99
return columnValue(column, statistics_info[(row * 2) + 1]);
100       default:
101         throw new Error JavaDoc("Column out of bounds.");
102     }
103   }
104
105   // ---------- Overwritten from GTDataSource ----------
106

107   public void dispose() {
108     super.dispose();
109     statistics_info = null;
110     stats = null;
111   }
112
113   // ---------- Static ----------
114

115   /**
116    * The data table def that describes this table of data source.
117    */

118   static final DataTableDef DEF_DATA_TABLE_DEF;
119
120   static {
121
122     DataTableDef def = new DataTableDef();
123     def.setTableName(
124              new TableName(Database.SYSTEM_SCHEMA, "sUSRDatabaseStatistics"));
125
126     // Add column definitions
127
def.addColumn(stringColumn("stat_name"));
128     def.addColumn(stringColumn("value"));
129
130     // Set to immutable
131
def.setImmutable();
132
133     DEF_DATA_TABLE_DEF = def;
134
135   }
136
137 }
138
Popular Tags