1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.internal.storage.dfs;
45
46 import java.io.EOFException;
47 import java.io.IOException;
48 import java.nio.ByteBuffer;
49 import java.text.MessageFormat;
50
51 import org.eclipse.jgit.errors.PackInvalidException;
52 import org.eclipse.jgit.internal.storage.pack.PackExt;
53
54
55 abstract class BlockBasedFile {
56
57 final DfsBlockCache cache;
58
59
60 final DfsStreamKey key;
61
62
63 final DfsPackDescription desc;
64 final PackExt ext;
65
66
67
68
69
70
71
72
73
74 volatile int blockSize;
75
76
77
78
79
80
81 volatile long length;
82
83
84 volatile boolean invalid;
85
86 BlockBasedFile(DfsBlockCache cache, DfsPackDescription desc, PackExt ext) {
87 this.cache = cache;
88 this.key = desc.getStreamKey(ext);
89 this.desc = desc;
90 this.ext = ext;
91 }
92
93 String getFileName() {
94 return desc.getFileName(ext);
95 }
96
97 boolean invalid() {
98 return invalid;
99 }
100
101 void setInvalid() {
102 invalid = true;
103 }
104
105 void setBlockSize(int newSize) {
106 blockSize = newSize;
107 }
108
109 long alignToBlock(long pos) {
110 int size = blockSize;
111 if (size == 0)
112 size = cache.getBlockSize();
113 return (pos / size) * size;
114 }
115
116 int blockSize(ReadableChannel rc) {
117
118
119 int size = blockSize;
120 if (size == 0) {
121 size = rc.blockSize();
122 if (size <= 0)
123 size = cache.getBlockSize();
124 else if (size < cache.getBlockSize())
125 size = (cache.getBlockSize() / size) * size;
126 blockSize = size;
127 }
128 return size;
129 }
130
131 DfsBlock getOrLoadBlock(long pos, DfsReader ctx) throws IOException {
132 try (LazyChannel c = new LazyChannel(ctx, desc, ext)) {
133 return cache.getOrLoad(this, pos, ctx, c);
134 }
135 }
136
137 DfsBlock readOneBlock(long pos, DfsReader ctx, ReadableChannel rc)
138 throws IOException {
139 if (invalid)
140 throw new PackInvalidException(getFileName());
141
142 ctx.stats.readBlock++;
143 long start = System.nanoTime();
144 try {
145 int size = blockSize(rc);
146 pos = (pos / size) * size;
147
148
149
150
151
152 long len = length;
153 if (len < 0) {
154 len = rc.size();
155 if (0 <= len)
156 length = len;
157 }
158
159 if (0 <= len && len < pos + size)
160 size = (int) (len - pos);
161 if (size <= 0)
162 throw new EOFException(MessageFormat.format(
163 DfsText.get().shortReadOfBlock, Long.valueOf(pos),
164 getFileName(), Long.valueOf(0), Long.valueOf(0)));
165
166 byte[] buf = new byte[size];
167 rc.position(pos);
168 int cnt = read(rc, ByteBuffer.wrap(buf, 0, size));
169 ctx.stats.readBlockBytes += cnt;
170 if (cnt != size) {
171 if (0 <= len) {
172 throw new EOFException(MessageFormat.format(
173 DfsText.get().shortReadOfBlock, Long.valueOf(pos),
174 getFileName(), Integer.valueOf(size),
175 Integer.valueOf(cnt)));
176 }
177
178
179
180 byte[] n = new byte[cnt];
181 System.arraycopy(buf, 0, n, 0, n.length);
182 buf = n;
183 } else if (len < 0) {
184
185
186 length = len = rc.size();
187 }
188
189 return new DfsBlock(key, pos, buf);
190 } finally {
191 ctx.stats.readBlockMicros += elapsedMicros(start);
192 }
193 }
194
195 static int read(ReadableChannel rc, ByteBuffer buf) throws IOException {
196 int n;
197 do {
198 n = rc.read(buf);
199 } while (0 < n && buf.hasRemaining());
200 return buf.position();
201 }
202
203 static long elapsedMicros(long start) {
204 return (System.nanoTime() - start) / 1000L;
205 }
206
207
208
209
210 private static class LazyChannel
211 implements AutoCloseable, DfsBlockCache.ReadableChannelSupplier {
212 private final DfsReader ctx;
213 private final DfsPackDescription desc;
214 private final PackExt ext;
215
216 private ReadableChannel rc;
217
218 LazyChannel(DfsReader ctx, DfsPackDescription desc, PackExt ext) {
219 this.ctx = ctx;
220 this.desc = desc;
221 this.ext = ext;
222 }
223
224 @Override
225 public ReadableChannel get() throws IOException {
226 if (rc == null) {
227 rc = ctx.db.openFile(desc, ext);
228 }
229 return rc;
230 }
231
232 @Override
233 public void close() throws IOException {
234 if (rc != null) {
235 rc.close();
236 }
237 }
238 }
239 }