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.annotations.Nullable;
52 import org.eclipse.jgit.errors.PackInvalidException;
53 import org.eclipse.jgit.internal.storage.pack.PackExt;
54
55
56 abstract class BlockBasedFile {
57
58 final DfsBlockCache cache;
59
60
61 final DfsStreamKey key;
62
63
64 final DfsPackDescription desc;
65 final PackExt ext;
66
67
68
69
70
71
72
73
74
75 volatile int blockSize;
76
77
78
79
80
81
82 volatile long length;
83
84
85 volatile boolean invalid;
86
87 BlockBasedFile(DfsBlockCache cache, DfsPackDescription desc, PackExt ext) {
88 this.cache = cache;
89 this.key = desc.getStreamKey(ext);
90 this.desc = desc;
91 this.ext = ext;
92 }
93
94 String getFileName() {
95 return desc.getFileName(ext);
96 }
97
98 boolean invalid() {
99 return invalid;
100 }
101
102 void setInvalid() {
103 invalid = true;
104 }
105
106 void setBlockSize(int newSize) {
107 blockSize = newSize;
108 }
109
110 long alignToBlock(long pos) {
111 int size = blockSize;
112 if (size == 0)
113 size = cache.getBlockSize();
114 return (pos / size) * size;
115 }
116
117 int blockSize(ReadableChannel rc) {
118
119
120 int size = blockSize;
121 if (size == 0) {
122 size = rc.blockSize();
123 if (size <= 0)
124 size = cache.getBlockSize();
125 else if (size < cache.getBlockSize())
126 size = (cache.getBlockSize() / size) * size;
127 blockSize = size;
128 }
129 return size;
130 }
131
132 DfsBlock getOrLoadBlock(long pos, DfsReader ctx) throws IOException {
133 return cache.getOrLoad(this, pos, ctx, null);
134 }
135
136 DfsBlock readOneBlock(long pos, DfsReader ctx,
137 @Nullable ReadableChannel fileChannel) throws IOException {
138 if (invalid)
139 throw new PackInvalidException(getFileName());
140
141 ctx.stats.readBlock++;
142 long start = System.nanoTime();
143 ReadableChannel rc = fileChannel != null ? fileChannel
144 : ctx.db.openFile(desc, ext);
145 try {
146 int size = blockSize(rc);
147 pos = (pos / size) * size;
148
149
150
151
152
153 long len = length;
154 if (len < 0) {
155 len = rc.size();
156 if (0 <= len)
157 length = len;
158 }
159
160 if (0 <= len && len < pos + size)
161 size = (int) (len - pos);
162 if (size <= 0)
163 throw new EOFException(MessageFormat.format(
164 DfsText.get().shortReadOfBlock, Long.valueOf(pos),
165 getFileName(), Long.valueOf(0), Long.valueOf(0)));
166
167 byte[] buf = new byte[size];
168 rc.position(pos);
169 int cnt = read(rc, ByteBuffer.wrap(buf, 0, size));
170 ctx.stats.readBlockBytes += cnt;
171 if (cnt != size) {
172 if (0 <= len) {
173 throw new EOFException(MessageFormat.format(
174 DfsText.get().shortReadOfBlock, Long.valueOf(pos),
175 getFileName(), Integer.valueOf(size),
176 Integer.valueOf(cnt)));
177 }
178
179
180
181 byte[] n = new byte[cnt];
182 System.arraycopy(buf, 0, n, 0, n.length);
183 buf = n;
184 } else if (len < 0) {
185
186
187 length = len = rc.size();
188 }
189
190 return new DfsBlock(key, pos, buf);
191 } finally {
192 if (rc != fileChannel) {
193 rc.close();
194 }
195 ctx.stats.readBlockMicros += elapsedMicros(start);
196 }
197 }
198
199 static int read(ReadableChannel rc, ByteBuffer buf) throws IOException {
200 int n;
201 do {
202 n = rc.read(buf);
203 } while (0 < n && buf.hasRemaining());
204 return buf.position();
205 }
206
207 static long elapsedMicros(long start) {
208 return (System.nanoTime() - start) / 1000L;
209 }
210 }