1 /* 2 * Copyright (C) 2011, 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.IOException; 47 import java.nio.channels.ReadableByteChannel; 48 49 /** 50 * Readable random access byte channel from a file. 51 */ 52 public interface ReadableChannel extends ReadableByteChannel { 53 /** 54 * Get the current position of the channel. 55 * 56 * @return r current offset. 57 * @throws java.io.IOException 58 * the channel's current position cannot be obtained. 59 */ 60 public long position() throws IOException; 61 62 /** 63 * Seek the current position of the channel to a new offset. 64 * 65 * @param newPosition 66 * position to move the channel to. The next read will start from 67 * here. This should be a multiple of the {@link #blockSize()}. 68 * @throws java.io.IOException 69 * the position cannot be updated. This may be because the 70 * channel only supports block aligned IO and the current 71 * position is not block aligned. 72 */ 73 public void position(long newPosition) throws IOException; 74 75 /** 76 * Get the total size of the channel. 77 * <p> 78 * Prior to reading from a channel the size might not yet be known. 79 * Implementors may return -1 until after the first read method call. Once a 80 * read has been completed, the underlying file size should be available. 81 * 82 * @return r total size of the channel; -1 if not yet available. 83 * @throws java.io.IOException 84 * the size cannot be determined. 85 */ 86 public long size() throws IOException; 87 88 /** 89 * Get the recommended alignment for reads. 90 * <p> 91 * Starting a read at multiples of the blockSize is more efficient than 92 * starting a read at any other position. If 0 or -1 the channel does not 93 * have any specific block size recommendation. 94 * <p> 95 * Channels should not recommend large block sizes. Sizes up to 1-4 MiB may 96 * be reasonable, but sizes above that may be horribly inefficient. The 97 * {@link org.eclipse.jgit.internal.storage.dfs.DfsBlockCache} favors the 98 * alignment suggested by the channel rather than the configured size under 99 * the assumption that reads are very expensive and the channel knows what 100 * size is best to access it with. 101 * 102 * @return recommended alignment size for randomly positioned reads. Does 103 * not need to be a power of 2. 104 */ 105 public int blockSize(); 106 107 /** 108 * Recommend the channel maintain a read-ahead buffer. 109 * <p> 110 * A read-ahead buffer of approximately {@code bufferSize} in bytes may be 111 * allocated and used by the channel to smooth out latency for read. 112 * <p> 113 * Callers can continue to read in smaller than {@code bufferSize} chunks. 114 * With read-ahead buffering enabled read latency may fluctuate in a pattern 115 * of one slower read followed by {@code (bufferSize / readSize) - 1} fast 116 * reads satisfied by the read-ahead buffer. When summed up overall time to 117 * read the same contiguous range should be lower than if read-ahead was not 118 * enabled, as the implementation can combine reads to increase throughput. 119 * <p> 120 * To avoid unnecessary IO callers should only enable read-ahead if the 121 * majority of the channel will be accessed in order. 122 * <p> 123 * Implementations may chose to read-ahead using asynchronous APIs or 124 * background threads, or may simply aggregate reads using a buffer. 125 * <p> 126 * This read ahead stays in effect until the channel is closed or the buffer 127 * size is set to 0. 128 * 129 * @param bufferSize 130 * requested size of the read ahead buffer, in bytes. 131 * @throws java.io.IOException 132 * if the read ahead cannot be adjusted. 133 */ 134 public void setReadAheadBytes(int bufferSize) throws IOException; 135 }