1 /*
2 * Copyright (C) 2017, Google Inc.
3 * and other copyright owners as documented in the project's IP log.
4 *
5 * This program and the accompanying materials are made available
6 * under the terms of the Eclipse Distribution License v1.0 which
7 * accompanies this distribution, is reproduced below, and is
8 * available at http://www.eclipse.org/org/documents/edl-v10.php
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials provided
22 * with the distribution.
23 *
24 * - Neither the name of the Eclipse Foundation, Inc. nor the
25 * names of its contributors may be used to endorse or promote
26 * products derived from this software without specific prior
27 * written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 /** Block based file stored in {@link DfsBlockCache}. */
56 abstract class BlockBasedFile {
57 /** Cache that owns this file and its data. */
58 final DfsBlockCache cache;
59
60 /** Unique identity of this file while in-memory. */
61 final DfsStreamKey key;
62
63 /** Description of the associated pack file's storage. */
64 final DfsPackDescription desc;
65 final PackExt ext;
66
67 /**
68 * Preferred alignment for loading blocks from the backing file.
69 * <p>
70 * It is initialized to 0 and filled in on the first read made from the
71 * file. Block sizes may be odd, e.g. 4091, caused by the underling DFS
72 * storing 4091 user bytes and 5 bytes block metadata into a lower level
73 * 4096 byte block on disk.
74 */
75 volatile int blockSize;
76
77 /**
78 * Total number of bytes in this pack file.
79 * <p>
80 * This field initializes to -1 and gets populated when a block is loaded.
81 */
82 volatile long length;
83
84 /** True once corruption has been detected that cannot be worked around. */
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 // If the block alignment is not yet known, discover it. Prefer the
119 // larger size from either the cache or the file itself.
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 // If the size of the file is not yet known, try to discover it.
150 // Channels may choose to return -1 to indicate they don't
151 // know the length yet, in this case read up to the size unit
152 // given by the caller, then recheck the length.
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 // Assume the entire thing was read in a single shot, compact
180 // the buffer to only the space required.
181 byte[] n = new byte[cnt];
182 System.arraycopy(buf, 0, n, 0, n.length);
183 buf = n;
184 } else if (len < 0) {
185 // With no length at the start of the read, the channel should
186 // have the length available at the end.
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 }