File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\mvstore\Chunk.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.nio.ByteBuffer;
10 import java.util.HashMap;
11
12 /**
13 * A chunk of data, containing one or multiple pages.
14 * <p>
15 * Chunks are page aligned (each page is usually 4096 bytes).
16 * There are at most 67 million (2^26) chunks,
17 * each chunk is at most 2 GB large.
18 * File format:
19 * 1 byte: 'c'
20 * 4 bytes: length
21 * 4 bytes: chunk id (an incrementing number)
22 * 4 bytes: pageCount
23 * 8 bytes: metaRootPos
24 * 8 bytes: maxLengthLive
25 * [ Page ] *
26 */
27 public class Chunk {
28
29 /**
30 * The chunk id.
31 */
32 final int id;
33
34 /**
35 * The start position within the file.
36 */
37 long start;
38
39 /**
40 * The length in bytes.
41 */
42 int length;
43
44 /**
45 * The number of pages.
46 */
47 int pageCount;
48
49 /**
50 * The sum of the max length of all pages.
51 */
52 long maxLength;
53
54 /**
55 * The sum of the max length of all pages that are in use.
56 */
57 long maxLengthLive;
58
59 /**
60 * The garbage collection priority.
61 */
62 int collectPriority;
63
64 /**
65 * The position of the meta root.
66 */
67 long metaRootPos;
68
69 /**
70 * The version stored in this chunk.
71 */
72 long version;
73
74 Chunk(int id) {
75 this.id = id;
76 }
77
78 /**
79 * Read the header from the byte buffer.
80 *
81 * @param buff the source buffer
82 * @param start the start of the chunk in the file
83 * @return the chunk
84 */
85 static Chunk fromHeader(ByteBuffer buff, long start) {
86 if (buff.get() != 'c') {
87 throw new RuntimeException("File corrupt");
88 }
89 int length = buff.getInt();
90 int chunkId = buff.getInt();
91 int pageCount = buff.getInt();
92 long metaRootPos = buff.getLong();
93 long maxLength = buff.getLong();
94 long maxLengthLive = buff.getLong();
95 Chunk c = new Chunk(chunkId);
96 c.length = length;
97 c.pageCount = pageCount;
98 c.start = start;
99 c.metaRootPos = metaRootPos;
100 c.maxLength = maxLength;
101 c.maxLengthLive = maxLengthLive;
102 return c;
103 }
104
105 /**
106 * Write the header.
107 *
108 * @param buff the target buffer
109 */
110 void writeHeader(ByteBuffer buff) {
111 buff.put((byte) 'c');
112 buff.putInt(length);
113 buff.putInt(id);
114 buff.putInt(pageCount);
115 buff.putLong(metaRootPos);
116 buff.putLong(maxLength);
117 buff.putLong(maxLengthLive);
118 }
119
120 /**
121 * Build a block from the given string.
122 *
123 * @param s the string
124 * @return the block
125 */
126 public static Chunk fromString(String s) {
127 HashMap<String, String> map = DataUtils.parseMap(s);
128 int id = Integer.parseInt(map.get("id"));
129 Chunk c = new Chunk(id);
130 c.start = Long.parseLong(map.get("start"));
131 c.length = Integer.parseInt(map.get("length"));
132 c.pageCount = Integer.parseInt(map.get("pageCount"));
133 c.maxLength = Long.parseLong(map.get("maxLength"));
134 c.maxLengthLive = Long.parseLong(map.get("maxLengthLive"));
135 c.metaRootPos = Long.parseLong(map.get("metaRoot"));
136 c.version = Long.parseLong(map.get("version"));
137 return c;
138 }
139
140 public int getFillRate() {
141 return (int) (maxLength == 0 ? 0 : 100 * maxLengthLive / maxLength);
142 }
143
144 public int hashCode() {
145 return id;
146 }
147
148 public boolean equals(Object o) {
149 return o instanceof Chunk && ((Chunk) o).id == id;
150 }
151
152 /**
153 * Get the chunk data as a string.
154 *
155 * @return the string
156 */
157 public String asString() {
158 return
159 "id:" + id + "," +
160 "start:" + start + "," +
161 "length:" + length + "," +
162 "pageCount:" + pageCount + "," +
163 "maxLength:" + maxLength + "," +
164 "maxLengthLive:" + maxLengthLive + "," +
165 "metaRoot:" + metaRootPos + "," +
166 "version:" + version;
167 }
168
169 }
170
171