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