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