File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\mvstore\MVStoreTool.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.mvstore;
8
9 import java.io.ByteArrayInputStream;
10 import java.io.IOException;
11 import java.io.PrintWriter;
12 import java.io.StringReader;
13 import java.nio.ByteBuffer;
14 import java.nio.channels.FileChannel;
15 import java.util.Properties;
16 import org.h2.store.fs.FilePath;
17 import org.h2.store.fs.FileUtils;
18
19 /**
20 * Utility methods used in combination with the MVStore.
21 */
22 public class MVStoreTool {
23
24 /**
25 * Runs this tool.
26 * Options are case sensitive. Supported options are:
27 * <table>
28 * <tr><td>[-dump &lt;dir&gt;]</td>
29 * <td>Dump the contends of the file</td></tr>
30 * </table>
31 *
32 * @param args the command line arguments
33 */
34 public static void main(String... args) throws IOException {
35 for (int i = 0; i < args.length; i++) {
36 if ("-dump".equals(args[i])) {
37 String fileName = args[++i];
38 dump(fileName, new PrintWriter(System.out));
39 }
40 }
41 }
42
43 /**
44 * Read the contents of the file and display them in a human-readable
45 * format.
46 *
47 * @param fileName the name of the file
48 * @param writer the print writer
49 */
50 public static void dump(String fileName, PrintWriter writer) throws IOException {
51 if (!FileUtils.exists(fileName)) {
52 writer.println("File not found: " + fileName);
53 return;
54 }
55 FileChannel file = null;
56 int blockSize = MVStore.BLOCK_SIZE;
57 try {
58 file = FilePath.get(fileName).open("r");
59 long fileLength = file.size();
60 byte[] header = new byte[blockSize];
61 file.read(ByteBuffer.wrap(header), 0);
62 Properties prop = new Properties();
63 prop.load(new ByteArrayInputStream(header));
64 prop.load(new StringReader(new String(header, "UTF-8")));
65 writer.println("file " + fileName);
66 writer.println(" length " + fileLength);
67 writer.println(" " + prop);
68 ByteBuffer block = ByteBuffer.allocate(40);
69 for (long pos = 0; pos < fileLength;) {
70 block.rewind();
71 DataUtils.readFully(file, pos, block);
72 block.rewind();
73 if (block.get() != 'c') {
74 pos += blockSize;
75 continue;
76 }
77 int chunkLength = block.getInt();
78 int chunkId = block.getInt();
79 int pageCount = block.getInt();
80 long metaRootPos = block.getLong();
81 long maxLength = block.getLong();
82 long maxLengthLive = block.getLong();
83 writer.println(" chunk " + chunkId +
84 " at " + pos +
85 " length " + chunkLength +
86 " pageCount " + pageCount +
87 " root " + metaRootPos +
88 " maxLength " + maxLength +
89 " maxLengthLive " + maxLengthLive);
90 ByteBuffer chunk = ByteBuffer.allocate(chunkLength);
91 DataUtils.readFully(file, pos, chunk);
92 int p = block.position();
93 pos = (pos + chunkLength + blockSize) / blockSize * blockSize;
94 chunkLength -= p;
95 while (chunkLength > 0) {
96 chunk.position(p);
97 int pageLength = chunk.getInt();
98 // check value (ignored)
99 chunk.getShort();
100 long mapId = DataUtils.readVarInt(chunk);
101 int len = DataUtils.readVarInt(chunk);
102 int type = chunk.get();
103 boolean compressed = (type & 2) != 0;
104 boolean node = (type & 1) != 0;
105 writer.println(" map " + mapId + " at " + p + " " +
106 (node ? "node" : "leaf") + " " +
107 (compressed ? "compressed " : "") +
108 "len: " + pageLength + " entries: " + len);
109 p += pageLength;
110 chunkLength -= pageLength;
111 }
112 }
113 } catch (IOException e) {
114 writer.println("ERROR: " + e);
115 throw e;
116 } finally {
117 if (file != null) {
118 try {
119 file.close();
120 } catch (IOException e) {
121 // ignore
122 }
123 }
124 }
125 writer.println();
126 writer.flush();
127 }
128
129 }
130