1 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 ; 41 import java.util.logging.Logger ; 42 43 46 public class DebugStore { 47 private static final Logger 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 55 { 56 _path = path; 57 58 _store = Store.create(path); 59 } 60 61 public static void main(String []args) 62 throws Exception 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 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 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 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 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 |