Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

BlockBasedFile.java 6.5KB

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