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.

DfsReftable.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 static org.eclipse.jgit.internal.storage.pack.PackExt.REFTABLE;
  45. import java.io.IOException;
  46. import java.nio.ByteBuffer;
  47. import org.eclipse.jgit.internal.storage.io.BlockSource;
  48. import org.eclipse.jgit.internal.storage.reftable.ReftableReader;
  49. /**
  50. * A reftable stored in {@link org.eclipse.jgit.internal.storage.dfs.DfsBlockCache}.
  51. */
  52. public class DfsReftable extends BlockBasedFile {
  53. /**
  54. * Construct a reader for an existing reftable.
  55. *
  56. * @param desc
  57. * description of the reftable within the DFS.
  58. */
  59. public DfsReftable(DfsPackDescription desc) {
  60. this(DfsBlockCache.getInstance(), desc);
  61. }
  62. /**
  63. * Construct a reader for an existing reftable.
  64. *
  65. * @param cache
  66. * cache that will store the reftable data.
  67. * @param desc
  68. * description of the reftable within the DFS.
  69. */
  70. public DfsReftable(DfsBlockCache cache, DfsPackDescription desc) {
  71. super(cache, desc, REFTABLE);
  72. int bs = desc.getBlockSize(REFTABLE);
  73. if (bs > 0) {
  74. setBlockSize(bs);
  75. }
  76. long sz = desc.getFileSize(REFTABLE);
  77. length = sz > 0 ? sz : -1;
  78. }
  79. /**
  80. * Get description that was originally used to configure this file.
  81. *
  82. * @return description that was originally used to configure this file.
  83. */
  84. public DfsPackDescription getPackDescription() {
  85. return desc;
  86. }
  87. /**
  88. * Open reader on the reftable.
  89. * <p>
  90. * The returned reader is not thread safe.
  91. *
  92. * @param ctx
  93. * reader to access the DFS storage.
  94. * @return cursor to read the table; caller must close.
  95. * @throws java.io.IOException
  96. * table cannot be opened.
  97. */
  98. public ReftableReader open(DfsReader ctx) throws IOException {
  99. return new ReftableReader(new CacheSource(this, cache, ctx));
  100. }
  101. private static final class CacheSource extends BlockSource {
  102. private final DfsReftable file;
  103. private final DfsBlockCache cache;
  104. private final DfsReader ctx;
  105. private ReadableChannel ch;
  106. private int readAhead;
  107. CacheSource(DfsReftable file, DfsBlockCache cache, DfsReader ctx) {
  108. this.file = file;
  109. this.cache = cache;
  110. this.ctx = ctx;
  111. }
  112. @Override
  113. public ByteBuffer read(long pos, int cnt) throws IOException {
  114. if (ch == null && readAhead > 0 && notInCache(pos)) {
  115. open().setReadAheadBytes(readAhead);
  116. }
  117. DfsBlock block = cache.getOrLoad(file, pos, ctx, () -> open());
  118. if (block.start == pos && block.size() >= cnt) {
  119. return block.zeroCopyByteBuffer(cnt);
  120. }
  121. byte[] dst = new byte[cnt];
  122. ByteBuffer buf = ByteBuffer.wrap(dst);
  123. buf.position(ctx.copy(file, pos, dst, 0, cnt));
  124. return buf;
  125. }
  126. private boolean notInCache(long pos) {
  127. return cache.get(file.key, file.alignToBlock(pos)) == null;
  128. }
  129. @Override
  130. public long size() throws IOException {
  131. long n = file.length;
  132. if (n < 0) {
  133. n = open().size();
  134. file.length = n;
  135. }
  136. return n;
  137. }
  138. @Override
  139. public void adviseSequentialRead(long start, long end) {
  140. int sz = ctx.getOptions().getStreamPackBufferSize();
  141. if (sz > 0) {
  142. readAhead = (int) Math.min(sz, end - start);
  143. }
  144. }
  145. private ReadableChannel open() throws IOException {
  146. if (ch == null) {
  147. ch = ctx.db.openFile(file.desc, file.ext);
  148. }
  149. return ch;
  150. }
  151. @Override
  152. public void close() {
  153. if (ch != null) {
  154. try {
  155. ch.close();
  156. } catch (IOException e) {
  157. // Ignore read close failures.
  158. } finally {
  159. ch = null;
  160. }
  161. }
  162. }
  163. }
  164. }