001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.db.debug;
031:
032: import com.caucho.db.store.Store;
033: import com.caucho.util.L10N;
034: import com.caucho.util.Log;
035: import com.caucho.vfs.Path;
036: import com.caucho.vfs.ReadStream;
037: import com.caucho.vfs.Vfs;
038: import com.caucho.vfs.WriteStream;
039:
040: import java.io.IOException;
041: import java.util.logging.Logger;
042:
043: /**
044: * Manager for a basic Java-based database.
045: */
046: public class DebugStore {
047: private static final Logger log = Log.open(DebugStore.class);
048: private static final L10N L = new L10N(DebugStore.class);
049:
050: Path _path;
051: Store _store;
052:
053: public DebugStore(Path path) throws Exception {
054: _path = path;
055:
056: _store = Store.create(path);
057: }
058:
059: public static void main(String[] args) throws Exception {
060: if (args.length == 0) {
061: System.out.println("usage: DebugStore store.db");
062: return;
063: }
064:
065: Path path = Vfs.lookup(args[0]);
066:
067: WriteStream out = Vfs.openWrite(System.out);
068:
069: new DebugStore(path).test(out);
070:
071: out.close();
072: }
073:
074: public void test(WriteStream out) throws Exception {
075: out.println("file-size : " + _store.getFileSize());
076: out.println("block-count : " + _store.getBlockCount());
077:
078: debugAllocation(out, _store.getAllocationTable(), _store
079: .getBlockCount());
080:
081: debugFragments(out, _store.getAllocationTable(), _store
082: .getBlockCount());
083: }
084:
085: private void debugAllocation(WriteStream out, byte[] allocTable,
086: long count) throws IOException {
087: out.println();
088:
089: for (int i = 0; i < 2 * count; i += 2) {
090: int v = allocTable[i];
091:
092: int code = v & 0xf;
093:
094: switch (code) {
095: case Store.ALLOC_FREE:
096: out.print('.');
097: break;
098: case Store.ALLOC_ROW:
099: out.print('r');
100: break;
101: case Store.ALLOC_USED:
102: out.print('u');
103: break;
104: case Store.ALLOC_FRAGMENT:
105: out.print('f');
106: break;
107: case Store.ALLOC_MINI_FRAG:
108: out.print('m');
109: break;
110: case Store.ALLOC_INDEX:
111: out.print('i');
112: break;
113: default:
114: out.print('?');
115: }
116:
117: if (i % 64 == 63)
118: out.println();
119: else if (i % 8 == 7)
120: out.print(' ');
121: }
122:
123: out.println();
124: }
125:
126: private void debugFragments(WriteStream out, byte[] allocTable,
127: long count) throws Exception {
128: long totalUsed = 0;
129:
130: byte[] block = new byte[Store.BLOCK_SIZE];
131:
132: for (int i = 0; i < 2 * count; i += 2) {
133: int code = allocTable[i];
134:
135: if (code == Store.ALLOC_FRAGMENT) {
136: int fragCount = 0;
137:
138: for (int j = 0; j < 8; j++) {
139: if ((allocTable[i + 1] & (1 << j)) != 0)
140: fragCount++;
141: }
142:
143: totalUsed += fragCount;
144:
145: out.println();
146:
147: out.print("Fragment Block " + (i / 2) + ": ");
148: for (int j = 0; j < 8; j++) {
149: if ((allocTable[i + 1] & (1 << j)) != 0)
150: out.print("1");
151: else
152: out.print(".");
153: }
154: }
155: }
156:
157: out.println();
158: out.println("Total-used: " + totalUsed);
159: }
160:
161: private void readBlock(byte[] block, long count) throws Exception {
162: ReadStream is = _path.openRead();
163:
164: try {
165: is.skip(count * Store.BLOCK_SIZE);
166:
167: is.read(block, 0, block.length);
168: } finally {
169: is.close();
170: }
171: }
172:
173: private int readShort(byte[] block, int offset) {
174: return ((block[offset] & 0xff) << 8)
175: + (block[offset + 1] & 0xff);
176: }
177: }
|