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