KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > db > debug > DebugStore


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.db.debug;
31
32 import com.caucho.db.store.Store;
33 import com.caucho.util.L10N;
34 import com.caucho.util.Log;
35 import com.caucho.vfs.Path;
36 import com.caucho.vfs.ReadStream;
37 import com.caucho.vfs.Vfs;
38 import com.caucho.vfs.WriteStream;
39
40 import java.io.IOException JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42
43 /**
44  * Manager for a basic Java-based database.
45  */

46 public class DebugStore {
47   private static final Logger JavaDoc log = Log.open(DebugStore.class);
48   private static final L10N L = new L10N(DebugStore.class);
49
50   Path _path;
51   Store _store;
52   
53   public DebugStore(Path path)
54     throws Exception JavaDoc
55   {
56     _path = path;
57
58     _store = Store.create(path);
59   }
60
61   public static void main(String JavaDoc []args)
62     throws Exception JavaDoc
63   {
64     if (args.length == 0) {
65       System.out.println("usage: DebugStore store.db");
66       return;
67     }
68
69     Path path = Vfs.lookup(args[0]);
70
71     WriteStream out = Vfs.openWrite(System.out);
72     
73     new DebugStore(path).test(out);
74
75     out.close();
76   }
77   
78   public void test(WriteStream out)
79     throws Exception JavaDoc
80   {
81     out.println("file-size : " + _store.getFileSize());
82     out.println("block-count : " + _store.getBlockCount());
83
84     debugAllocation(out, _store.getAllocationTable(), _store.getBlockCount());
85     
86     debugFragments(out, _store.getAllocationTable(), _store.getBlockCount());
87   }
88
89   private void debugAllocation(WriteStream out, byte []allocTable, long count)
90     throws IOException JavaDoc
91   {
92     out.println();
93
94     for (int i = 0; i < 2 * count; i += 2) {
95       int v = allocTable[i];
96
97       int code = v & 0xf;
98
99       switch (code) {
100       case Store.ALLOC_FREE:
101     out.print('.');
102     break;
103       case Store.ALLOC_ROW:
104     out.print('r');
105     break;
106       case Store.ALLOC_USED:
107     out.print('u');
108     break;
109       case Store.ALLOC_FRAGMENT:
110     out.print('f');
111     break;
112       case Store.ALLOC_INDEX:
113     out.print('i');
114     break;
115       default:
116     out.print('?');
117       }
118       
119       if (i % 64 == 63)
120     out.println();
121       else if (i % 8 == 7)
122     out.print(' ');
123     }
124     
125     out.println();
126   }
127
128   private void debugFragments(WriteStream out, byte []allocTable, long count)
129     throws Exception JavaDoc
130   {
131     long totalUsed = 0;
132     
133     byte []block = new byte[Store.BLOCK_SIZE];
134     
135     for (int i = 0; i < 2 * count; i += 2) {
136       int code = allocTable[i];
137
138       if (code == Store.ALLOC_FRAGMENT) {
139     int fragCount = 0;
140     
141     for (int j = 0; j < 8; j++) {
142       if ((allocTable[i + 1] & (1 << j)) != 0)
143         fragCount++;
144     }
145
146     totalUsed += fragCount;
147
148     out.println();
149     
150     out.print("Fragment Block " + (i / 2) + ": ");
151     for (int j = 0; j < 8; j++) {
152       if ((allocTable[i + 1] & (1 << j)) != 0)
153         out.print("1");
154       else
155         out.print(".");
156     }
157       }
158     }
159
160     out.println();
161     out.println("Total-used: " + totalUsed);
162   }
163
164   private void readBlock(byte []block, long count)
165     throws Exception JavaDoc
166   {
167     ReadStream is = _path.openRead();
168
169     try {
170       is.skip(count * Store.BLOCK_SIZE);
171
172       is.read(block, 0, block.length);
173     } finally {
174       is.close();
175     }
176   }
177
178   private int readShort(byte []block, int offset)
179   {
180     return ((block[offset] & 0xff) << 8) + (block[offset + 1] & 0xff);
181   }
182 }
183
Popular Tags