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 java.io.EOFException;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.util.HashMap;
25 import org.apache.lucene.store.BufferedIndexInput;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.store.IndexInput;
28 import org.apache.lucene.store.IndexOutput;
29 import org.apache.lucene.store.Lock;
32 * Class for accessing a compound stream.
33 * This class implements a directory, but is limited to only read operations.
34 * Directory methods that would normally modify data throw an exception.
36 class CompoundFileReader extends Directory {
38 private int readBufferSize;
40 private static final class FileEntry {
46 private Directory directory;
47 private String fileName;
49 private IndexInput stream;
50 private HashMap<String,FileEntry> entries = new HashMap<String,FileEntry>();
52 public CompoundFileReader(Directory dir, String name) throws IOException {
53 this(dir, name, BufferedIndexInput.BUFFER_SIZE);
56 public CompoundFileReader(Directory dir, String name, int readBufferSize) throws IOException {
57 assert !(dir instanceof CompoundFileReader) : "compound file inside of compound file: " + name;
60 this.readBufferSize = readBufferSize;
62 boolean success = false;
65 stream = dir.openInput(name, readBufferSize);
67 // read the first VInt. If it is negative, it's the version number
68 // otherwise it's the count (pre-3.1 indexes)
69 int firstInt = stream.readVInt();
72 final boolean stripSegmentName;
73 if (firstInt < CompoundFileWriter.FORMAT_PRE_VERSION) {
74 if (firstInt < CompoundFileWriter.FORMAT_CURRENT) {
75 throw new CorruptIndexException("Incompatible format version: "
76 + firstInt + " expected " + CompoundFileWriter.FORMAT_CURRENT + " (resource: " + stream + ")");
78 // It's a post-3.1 index, read the count.
79 count = stream.readVInt();
80 stripSegmentName = false;
83 stripSegmentName = true;
86 // read the directory and init files
87 FileEntry entry = null;
88 for (int i=0; i<count; i++) {
89 long offset = stream.readLong();
90 String id = stream.readString();
92 if (stripSegmentName) {
93 // Fix the id to not include the segment names. This is relevant for
95 id = IndexFileNames.stripSegmentName(id);
99 // set length of the previous entry
100 entry.length = offset - entry.offset;
103 entry = new FileEntry();
104 entry.offset = offset;
105 entries.put(id, entry);
108 // set the length of the final entry
110 entry.length = stream.length() - entry.offset;
116 if (!success && (stream != null)) {
119 } catch (IOException e) { }
124 public Directory getDirectory() {
128 public String getName() {
133 public synchronized void close() throws IOException {
135 throw new IOException("Already closed");
143 public synchronized IndexInput openInput(String id) throws IOException {
144 // Default to readBufferSize passed in when we were opened
145 return openInput(id, readBufferSize);
149 public synchronized IndexInput openInput(String id, int readBufferSize) throws IOException {
151 throw new IOException("Stream closed");
153 id = IndexFileNames.stripSegmentName(id);
154 FileEntry entry = entries.get(id);
156 throw new IOException("No sub-file with id " + id + " found (fileName=" + fileName + " files: " + entries.keySet() + ")");
159 return new CSIndexInput(stream, entry.offset, entry.length, readBufferSize);
162 /** Returns an array of strings, one for each file in the directory. */
164 public String[] listAll() {
165 String[] res = entries.keySet().toArray(new String[entries.size()]);
166 // Add the segment name
167 String seg = fileName.substring(0, fileName.indexOf('.'));
168 for (int i = 0; i < res.length; i++) {
169 res[i] = seg + res[i];
174 /** Returns true iff a file with the given name exists. */
176 public boolean fileExists(String name) {
177 return entries.containsKey(IndexFileNames.stripSegmentName(name));
180 /** Returns the time the compound file was last modified. */
182 public long fileModified(String name) throws IOException {
183 return directory.fileModified(fileName);
186 /** Set the modified time of the compound file to now.
187 * @deprecated Lucene never uses this API; it will be
191 public void touchFile(String name) throws IOException {
192 directory.touchFile(fileName);
196 * @throws UnsupportedOperationException */
198 public void deleteFile(String name) {
199 throw new UnsupportedOperationException();
203 * @throws UnsupportedOperationException */
204 public void renameFile(String from, String to) {
205 throw new UnsupportedOperationException();
208 /** Returns the length of a file in the directory.
209 * @throws IOException if the file does not exist */
211 public long fileLength(String name) throws IOException {
212 FileEntry e = entries.get(IndexFileNames.stripSegmentName(name));
214 throw new FileNotFoundException(name);
219 * @throws UnsupportedOperationException */
221 public IndexOutput createOutput(String name) {
222 throw new UnsupportedOperationException();
226 * @throws UnsupportedOperationException */
228 public Lock makeLock(String name) {
229 throw new UnsupportedOperationException();
232 /** Implementation of an IndexInput that reads from a portion of the
233 * compound file. The visibility is left as "package" *only* because
234 * this helps with testing since JUnit test cases in a different class
235 * can then access package fields of this class.
237 static final class CSIndexInput extends BufferedIndexInput {
242 CSIndexInput(final IndexInput base, final long fileOffset, final long length) {
243 this(base, fileOffset, length, BufferedIndexInput.BUFFER_SIZE);
246 CSIndexInput(final IndexInput base, final long fileOffset, final long length, int readBufferSize) {
247 super(readBufferSize);
248 this.base = (IndexInput)base.clone();
249 this.fileOffset = fileOffset;
250 this.length = length;
254 public Object clone() {
255 CSIndexInput clone = (CSIndexInput)super.clone();
256 clone.base = (IndexInput)base.clone();
257 clone.fileOffset = fileOffset;
258 clone.length = length;
262 /** Expert: implements buffer refill. Reads bytes from the current
263 * position in the input.
264 * @param b the array to read bytes into
265 * @param offset the offset in the array to start storing bytes
266 * @param len the number of bytes to read
269 protected void readInternal(byte[] b, int offset, int len) throws IOException {
270 long start = getFilePointer();
271 if(start + len > length)
272 throw new EOFException("read past EOF (resource: " + base + ")");
273 base.seek(fileOffset + start);
274 base.readBytes(b, offset, len, false);
277 /** Expert: implements seek. Sets current position in this file, where
278 * the next {@link #readInternal(byte[],int,int)} will occur.
279 * @see #readInternal(byte[],int,int)
282 protected void seekInternal(long pos) {}
284 /** Closes the stream to further operations. */
286 public void close() throws IOException {
291 public long length() {
296 public void copyBytes(IndexOutput out, long numBytes) throws IOException {
297 // Copy first whatever is in the buffer
298 numBytes -= flushBuffer(out, numBytes);
300 // If there are more bytes left to copy, delegate the copy task to the
301 // base IndexInput, in case it can do an optimized copy.
303 long start = getFilePointer();
304 if (start + numBytes > length) {
305 throw new EOFException("read past EOF (resource: " + base + ")");
307 base.seek(fileOffset + start);
308 base.copyBytes(out, numBytes);