You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BlockBasedFile.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. package org.eclipse.jgit.internal.storage.dfs;
  44. import java.io.EOFException;
  45. import java.io.IOException;
  46. import java.nio.ByteBuffer;
  47. import java.text.MessageFormat;
  48. import org.eclipse.jgit.errors.PackInvalidException;
  49. import org.eclipse.jgit.internal.storage.pack.PackExt;
  50. /** Block based file stored in {@link DfsBlockCache}. */
  51. abstract class BlockBasedFile {
  52. /** Cache that owns this file and its data. */
  53. final DfsBlockCache cache;
  54. /** Unique identity of this file while in-memory. */
  55. final DfsStreamKey key;
  56. /** Description of the associated pack file's storage. */
  57. final DfsPackDescription desc;
  58. final PackExt ext;
  59. /**
  60. * Preferred alignment for loading blocks from the backing file.
  61. * <p>
  62. * It is initialized to 0 and filled in on the first read made from the
  63. * file. Block sizes may be odd, e.g. 4091, caused by the underling DFS
  64. * storing 4091 user bytes and 5 bytes block metadata into a lower level
  65. * 4096 byte block on disk.
  66. */
  67. volatile int blockSize;
  68. /**
  69. * Total number of bytes in this pack file.
  70. * <p>
  71. * This field initializes to -1 and gets populated when a block is loaded.
  72. */
  73. volatile long length;
  74. /** True once corruption has been detected that cannot be worked around. */
  75. volatile boolean invalid;
  76. /** Exception that caused the packfile to be flagged as invalid */
  77. protected volatile Exception invalidatingCause;
  78. BlockBasedFile(DfsBlockCache cache, DfsPackDescription desc, PackExt ext) {
  79. this.cache = cache;
  80. this.key = desc.getStreamKey(ext);
  81. this.desc = desc;
  82. this.ext = ext;
  83. }
  84. String getFileName() {
  85. return desc.getFileName(ext);
  86. }
  87. boolean invalid() {
  88. return invalid;
  89. }
  90. void setInvalid() {
  91. invalid = true;
  92. }
  93. void setBlockSize(int newSize) {
  94. blockSize = newSize;
  95. }
  96. long alignToBlock(long pos) {
  97. int size = blockSize;
  98. if (size == 0)
  99. size = cache.getBlockSize();
  100. return (pos / size) * size;
  101. }
  102. int blockSize(ReadableChannel rc) {
  103. // If the block alignment is not yet known, discover it. Prefer the
  104. // larger size from either the cache or the file itself.
  105. int size = blockSize;
  106. if (size == 0) {
  107. size = rc.blockSize();
  108. if (size <= 0)
  109. size = cache.getBlockSize();
  110. else if (size < cache.getBlockSize())
  111. size = (cache.getBlockSize() / size) * size;
  112. blockSize = size;
  113. }
  114. return size;
  115. }
  116. DfsBlock getOrLoadBlock(long pos, DfsReader ctx) throws IOException {
  117. try (LazyChannel c = new LazyChannel(ctx, desc, ext)) {
  118. return cache.getOrLoad(this, pos, ctx, c);
  119. }
  120. }
  121. DfsBlock readOneBlock(long pos, DfsReader ctx, ReadableChannel rc)
  122. throws IOException {
  123. if (invalid) {
  124. throw new PackInvalidException(getFileName(), invalidatingCause);
  125. }
  126. ctx.stats.readBlock++;
  127. long start = System.nanoTime();
  128. try {
  129. int size = blockSize(rc);
  130. pos = (pos / size) * size;
  131. // If the size of the file is not yet known, try to discover it.
  132. // Channels may choose to return -1 to indicate they don't
  133. // know the length yet, in this case read up to the size unit
  134. // given by the caller, then recheck the length.
  135. long len = length;
  136. if (len < 0) {
  137. len = rc.size();
  138. if (0 <= len)
  139. length = len;
  140. }
  141. if (0 <= len && len < pos + size)
  142. size = (int) (len - pos);
  143. if (size <= 0)
  144. throw new EOFException(MessageFormat.format(
  145. DfsText.get().shortReadOfBlock, Long.valueOf(pos),
  146. getFileName(), Long.valueOf(0), Long.valueOf(0)));
  147. byte[] buf = new byte[size];
  148. rc.position(pos);
  149. int cnt = read(rc, ByteBuffer.wrap(buf, 0, size));
  150. ctx.stats.readBlockBytes += cnt;
  151. if (cnt != size) {
  152. if (0 <= len) {
  153. throw new EOFException(MessageFormat.format(
  154. DfsText.get().shortReadOfBlock, Long.valueOf(pos),
  155. getFileName(), Integer.valueOf(size),
  156. Integer.valueOf(cnt)));
  157. }
  158. // Assume the entire thing was read in a single shot, compact
  159. // the buffer to only the space required.
  160. byte[] n = new byte[cnt];
  161. System.arraycopy(buf, 0, n, 0, n.length);
  162. buf = n;
  163. } else if (len < 0) {
  164. // With no length at the start of the read, the channel should
  165. // have the length available at the end.
  166. length = len = rc.size();
  167. }
  168. return new DfsBlock(key, pos, buf);
  169. } finally {
  170. ctx.stats.readBlockMicros += elapsedMicros(start);
  171. }
  172. }
  173. static int read(ReadableChannel rc, ByteBuffer buf) throws IOException {
  174. int n;
  175. do {
  176. n = rc.read(buf);
  177. } while (0 < n && buf.hasRemaining());
  178. return buf.position();
  179. }
  180. static long elapsedMicros(long start) {
  181. return (System.nanoTime() - start) / 1000L;
  182. }
  183. /**
  184. * A supplier of readable channel that opens the channel lazily.
  185. */
  186. private static class LazyChannel
  187. implements AutoCloseable, DfsBlockCache.ReadableChannelSupplier {
  188. private final DfsReader ctx;
  189. private final DfsPackDescription desc;
  190. private final PackExt ext;
  191. private ReadableChannel rc;
  192. LazyChannel(DfsReader ctx, DfsPackDescription desc, PackExt ext) {
  193. this.ctx = ctx;
  194. this.desc = desc;
  195. this.ext = ext;
  196. }
  197. @Override
  198. public ReadableChannel get() throws IOException {
  199. if (rc == null) {
  200. rc = ctx.db.openFile(desc, ext);
  201. }
  202. return rc;
  203. }
  204. @Override
  205. public void close() throws IOException {
  206. if (rc != null) {
  207. rc.close();
  208. }
  209. }
  210. }
  211. }