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