1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.store.Directory;
21 import org.apache.lucene.store.IndexInput;
22 import org.apache.lucene.store.BufferedIndexInput;
23 import org.apache.lucene.store.IndexOutput;
24 import org.apache.lucene.store.Lock;
26 import java.util.HashMap;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
31 * Class for accessing a compound stream.
32 * This class implements a directory, but is limited to only read operations.
33 * Directory methods that would normally modify data throw an exception.
35 class CompoundFileReader extends Directory {
37 private int readBufferSize;
39 private static final class FileEntry {
45 private Directory directory;
46 private String fileName;
48 private IndexInput stream;
49 private HashMap<String,FileEntry> entries = new HashMap<String,FileEntry>();
51 public CompoundFileReader(Directory dir, String name) throws IOException {
52 this(dir, name, BufferedIndexInput.BUFFER_SIZE);
55 public CompoundFileReader(Directory dir, String name, int readBufferSize) throws IOException {
56 assert !(dir instanceof CompoundFileReader) : "compound file inside of compound file: " + name;
59 this.readBufferSize = readBufferSize;
61 boolean success = false;
64 stream = dir.openInput(name, readBufferSize);
66 // read the first VInt. If it is negative, it's the version number
67 // otherwise it's the count (pre-3.1 indexes)
68 int firstInt = stream.readVInt();
71 final boolean stripSegmentName;
72 if (firstInt < CompoundFileWriter.FORMAT_PRE_VERSION) {
73 if (firstInt < CompoundFileWriter.FORMAT_CURRENT) {
74 throw new CorruptIndexException("Incompatible format version: "
75 + firstInt + " expected " + CompoundFileWriter.FORMAT_CURRENT);
77 // It's a post-3.1 index, read the count.
78 count = stream.readVInt();
79 stripSegmentName = false;
82 stripSegmentName = true;
85 // read the directory and init files
86 FileEntry entry = null;
87 for (int i=0; i<count; i++) {
88 long offset = stream.readLong();
89 String id = stream.readString();
91 if (stripSegmentName) {
92 // Fix the id to not include the segment names. This is relevant for
94 id = IndexFileNames.stripSegmentName(id);
98 // set length of the previous entry
99 entry.length = offset - entry.offset;
102 entry = new FileEntry();
103 entry.offset = offset;
104 entries.put(id, entry);
107 // set the length of the final entry
109 entry.length = stream.length() - entry.offset;
115 if (!success && (stream != null)) {
118 } catch (IOException e) { }
123 public Directory getDirectory() {
127 public String getName() {
132 public synchronized void close() throws IOException {
134 throw new IOException("Already closed");
142 public synchronized IndexInput openInput(String id) throws IOException {
143 // Default to readBufferSize passed in when we were opened
144 return openInput(id, readBufferSize);
148 public synchronized IndexInput openInput(String id, int readBufferSize) throws IOException {
150 throw new IOException("Stream closed");
152 id = IndexFileNames.stripSegmentName(id);
153 FileEntry entry = entries.get(id);
155 throw new IOException("No sub-file with id " + id + " found (fileName=" + fileName + " files: " + entries.keySet() + ")");
158 return new CSIndexInput(stream, entry.offset, entry.length, readBufferSize);
161 /** Returns an array of strings, one for each file in the directory. */
163 public String[] listAll() {
164 String[] res = entries.keySet().toArray(new String[entries.size()]);
165 // Add the segment name
166 String seg = fileName.substring(0, fileName.indexOf('.'));
167 for (int i = 0; i < res.length; i++) {
168 res[i] = seg + res[i];
173 /** Returns true iff a file with the given name exists. */
175 public boolean fileExists(String name) {
176 return entries.containsKey(IndexFileNames.stripSegmentName(name));
179 /** Returns the time the compound file was last modified. */
181 public long fileModified(String name) throws IOException {
182 return directory.fileModified(fileName);
185 /** Set the modified time of the compound file to now.
186 * @deprecated Lucene never uses this API; it will be
190 public void touchFile(String name) throws IOException {
191 directory.touchFile(fileName);
195 * @throws UnsupportedOperationException */
197 public void deleteFile(String name) {
198 throw new UnsupportedOperationException();
202 * @throws UnsupportedOperationException */
203 public void renameFile(String from, String to) {
204 throw new UnsupportedOperationException();
207 /** Returns the length of a file in the directory.
208 * @throws IOException if the file does not exist */
210 public long fileLength(String name) throws IOException {
211 FileEntry e = entries.get(IndexFileNames.stripSegmentName(name));
213 throw new FileNotFoundException(name);
218 * @throws UnsupportedOperationException */
220 public IndexOutput createOutput(String name) {
221 throw new UnsupportedOperationException();
225 * @throws UnsupportedOperationException */
227 public Lock makeLock(String name) {
228 throw new UnsupportedOperationException();
231 /** Implementation of an IndexInput that reads from a portion of the
232 * compound file. The visibility is left as "package" *only* because
233 * this helps with testing since JUnit test cases in a different class
234 * can then access package fields of this class.
236 static final class CSIndexInput extends BufferedIndexInput {
241 CSIndexInput(final IndexInput base, final long fileOffset, final long length) {
242 this(base, fileOffset, length, BufferedIndexInput.BUFFER_SIZE);
245 CSIndexInput(final IndexInput base, final long fileOffset, final long length, int readBufferSize) {
246 super(readBufferSize);
247 this.base = (IndexInput)base.clone();
248 this.fileOffset = fileOffset;
249 this.length = length;
253 public Object clone() {
254 CSIndexInput clone = (CSIndexInput)super.clone();
255 clone.base = (IndexInput)base.clone();
256 clone.fileOffset = fileOffset;
257 clone.length = length;
261 /** Expert: implements buffer refill. Reads bytes from the current
262 * position in the input.
263 * @param b the array to read bytes into
264 * @param offset the offset in the array to start storing bytes
265 * @param len the number of bytes to read
268 protected void readInternal(byte[] b, int offset, int len) throws IOException {
269 long start = getFilePointer();
270 if(start + len > length)
271 throw new IOException("read past EOF");
272 base.seek(fileOffset + start);
273 base.readBytes(b, offset, len, false);
276 /** Expert: implements seek. Sets current position in this file, where
277 * the next {@link #readInternal(byte[],int,int)} will occur.
278 * @see #readInternal(byte[],int,int)
281 protected void seekInternal(long pos) {}
283 /** Closes the stream to further operations. */
285 public void close() throws IOException {
290 public long length() {
295 public void copyBytes(IndexOutput out, long numBytes) throws IOException {
296 // Copy first whatever is in the buffer
297 numBytes -= flushBuffer(out, numBytes);
299 // If there are more bytes left to copy, delegate the copy task to the
300 // base IndexInput, in case it can do an optimized copy.
302 long start = getFilePointer();
303 if (start + numBytes > length) {
304 throw new IOException("read past EOF");
306 base.seek(fileOffset + start);
307 base.copyBytes(out, numBytes);