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 static org.eclipse.jgit.internal.storage.pack.PackExt.REFTABLE;
47
48 import java.io.IOException;
49 import java.nio.ByteBuffer;
50
51 import org.eclipse.jgit.internal.storage.io.BlockSource;
52 import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
53
54
55
56
57 public class DfsReftable extends BlockBasedFile {
58
59
60
61
62
63
64 public DfsReftable(DfsPackDescription desc) {
65 this(DfsBlockCache.getInstance(), desc);
66 }
67
68
69
70
71
72
73
74
75
76 public DfsReftable(DfsBlockCache cache, DfsPackDescription desc) {
77 super(cache, desc, REFTABLE);
78
79 int bs = desc.getBlockSize(REFTABLE);
80 if (bs > 0) {
81 setBlockSize(bs);
82 }
83
84 long sz = desc.getFileSize(REFTABLE);
85 length = sz > 0 ? sz : -1;
86 }
87
88
89
90
91
92
93 public DfsPackDescription getPackDescription() {
94 return desc;
95 }
96
97
98
99
100
101
102
103
104
105
106
107
108 public ReftableReader open(DfsReader ctx) throws IOException {
109 return new ReftableReader(new CacheSource(this, cache, ctx));
110 }
111
112 private static final class CacheSource extends BlockSource {
113 private final DfsReftable file;
114 private final DfsBlockCache cache;
115 private final DfsReader ctx;
116 private ReadableChannel ch;
117 private int readAhead;
118
119 CacheSource(DfsReftable file, DfsBlockCache cache, DfsReader ctx) {
120 this.file = file;
121 this.cache = cache;
122 this.ctx = ctx;
123 }
124
125 @Override
126 public ByteBuffer read(long pos, int cnt) throws IOException {
127 if (ch == null && readAhead > 0 && notInCache(pos)) {
128 open().setReadAheadBytes(readAhead);
129 }
130
131 DfsBlock block = cache.getOrLoad(file, pos, ctx, () -> open());
132 if (block.start == pos && block.size() >= cnt) {
133 return block.zeroCopyByteBuffer(cnt);
134 }
135
136 byte[] dst = new byte[cnt];
137 ByteBuffer buf = ByteBuffer.wrap(dst);
138 buf.position(ctx.copy(file, pos, dst, 0, cnt));
139 return buf;
140 }
141
142 private boolean notInCache(long pos) {
143 return cache.get(file.key, file.alignToBlock(pos)) == null;
144 }
145
146 @Override
147 public long size() throws IOException {
148 long n = file.length;
149 if (n < 0) {
150 n = open().size();
151 file.length = n;
152 }
153 return n;
154 }
155
156 @Override
157 public void adviseSequentialRead(long start, long end) {
158 int sz = ctx.getOptions().getStreamPackBufferSize();
159 if (sz > 0) {
160 readAhead = (int) Math.min(sz, end - start);
161 }
162 }
163
164 private ReadableChannel open() throws IOException {
165 if (ch == null) {
166 ch = ctx.db.openFile(file.desc, file.ext);
167 }
168 return ch;
169 }
170
171 @Override
172 public void close() {
173 if (ch != null) {
174 try {
175 ch.close();
176 } catch (IOException e) {
177
178 } finally {
179 ch = null;
180 }
181 }
182 }
183 }
184 }