add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / index / CompoundFileReader.java
1 package org.apache.lucene.index;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
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;
25
26 import java.util.HashMap;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29
30 /**
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.
34  */
35 class CompoundFileReader extends Directory {
36   
37   private int readBufferSize;
38   
39   private static final class FileEntry {
40     long offset;
41     long length;
42   }
43     
44   // Base info
45   private Directory directory;
46   private String fileName;
47   
48   private IndexInput stream;
49   private HashMap<String,FileEntry> entries = new HashMap<String,FileEntry>();
50   
51   public CompoundFileReader(Directory dir, String name) throws IOException {
52     this(dir, name, BufferedIndexInput.BUFFER_SIZE);
53   }
54   
55   public CompoundFileReader(Directory dir, String name, int readBufferSize) throws IOException {
56     assert !(dir instanceof CompoundFileReader) : "compound file inside of compound file: " + name;
57     directory = dir;
58     fileName = name;
59     this.readBufferSize = readBufferSize;
60     
61     boolean success = false;
62     
63     try {
64       stream = dir.openInput(name, readBufferSize);
65       
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();
69       
70       final int count;
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);
76         }
77         // It's a post-3.1 index, read the count.
78         count = stream.readVInt();
79         stripSegmentName = false;
80       } else {
81         count = firstInt;
82         stripSegmentName = true;
83       }
84       
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();
90         
91         if (stripSegmentName) {
92           // Fix the id to not include the segment names. This is relevant for
93           // pre-3.1 indexes.
94           id = IndexFileNames.stripSegmentName(id);
95         }
96         
97         if (entry != null) {
98           // set length of the previous entry
99           entry.length = offset - entry.offset;
100         }
101         
102         entry = new FileEntry();
103         entry.offset = offset;
104         entries.put(id, entry);
105       }
106       
107       // set the length of the final entry
108       if (entry != null) {
109         entry.length = stream.length() - entry.offset;
110       }
111       
112       success = true;
113       
114     } finally {
115       if (!success && (stream != null)) {
116         try {
117           stream.close();
118         } catch (IOException e) { }
119       }
120     }
121   }
122   
123   public Directory getDirectory() {
124     return directory;
125   }
126   
127   public String getName() {
128     return fileName;
129   }
130   
131   @Override
132   public synchronized void close() throws IOException {
133     if (stream == null)
134       throw new IOException("Already closed");
135     
136     entries.clear();
137     stream.close();
138     stream = null;
139   }
140   
141   @Override
142   public synchronized IndexInput openInput(String id) throws IOException {
143     // Default to readBufferSize passed in when we were opened
144     return openInput(id, readBufferSize);
145   }
146   
147   @Override
148   public synchronized IndexInput openInput(String id, int readBufferSize) throws IOException {
149     if (stream == null)
150       throw new IOException("Stream closed");
151     
152     id = IndexFileNames.stripSegmentName(id);
153     FileEntry entry = entries.get(id);
154     if (entry == null) {
155       throw new IOException("No sub-file with id " + id + " found (fileName=" + fileName + " files: " + entries.keySet() + ")");
156     }
157     
158     return new CSIndexInput(stream, entry.offset, entry.length, readBufferSize);
159   }
160   
161   /** Returns an array of strings, one for each file in the directory. */
162   @Override
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];
169     }
170     return res;
171   }
172   
173   /** Returns true iff a file with the given name exists. */
174   @Override
175   public boolean fileExists(String name) {
176     return entries.containsKey(IndexFileNames.stripSegmentName(name));
177   }
178   
179   /** Returns the time the compound file was last modified. */
180   @Override
181   public long fileModified(String name) throws IOException {
182     return directory.fileModified(fileName);
183   }
184   
185   /** Set the modified time of the compound file to now.
186    *  @deprecated Lucene never uses this API; it will be
187    *  removed in 4.0. */
188   @Override
189   @Deprecated
190   public void touchFile(String name) throws IOException {
191     directory.touchFile(fileName);
192   }
193   
194   /** Not implemented
195    * @throws UnsupportedOperationException */
196   @Override
197   public void deleteFile(String name) {
198     throw new UnsupportedOperationException();
199   }
200   
201   /** Not implemented
202    * @throws UnsupportedOperationException */
203   public void renameFile(String from, String to) {
204     throw new UnsupportedOperationException();
205   }
206   
207   /** Returns the length of a file in the directory.
208    * @throws IOException if the file does not exist */
209   @Override
210   public long fileLength(String name) throws IOException {
211     FileEntry e = entries.get(IndexFileNames.stripSegmentName(name));
212     if (e == null)
213       throw new FileNotFoundException(name);
214     return e.length;
215   }
216   
217   /** Not implemented
218    * @throws UnsupportedOperationException */
219   @Override
220   public IndexOutput createOutput(String name) {
221     throw new UnsupportedOperationException();
222   }
223   
224   /** Not implemented
225    * @throws UnsupportedOperationException */
226   @Override
227   public Lock makeLock(String name) {
228     throw new UnsupportedOperationException();
229   }
230   
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.
235    */
236   static final class CSIndexInput extends BufferedIndexInput {
237     IndexInput base;
238     long fileOffset;
239     long length;
240     
241     CSIndexInput(final IndexInput base, final long fileOffset, final long length) {
242       this(base, fileOffset, length, BufferedIndexInput.BUFFER_SIZE);
243     }
244     
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;
250     }
251     
252     @Override
253     public Object clone() {
254       CSIndexInput clone = (CSIndexInput)super.clone();
255       clone.base = (IndexInput)base.clone();
256       clone.fileOffset = fileOffset;
257       clone.length = length;
258       return clone;
259     }
260     
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
266      */
267     @Override
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);
274     }
275     
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)
279      */
280     @Override
281     protected void seekInternal(long pos) {}
282     
283     /** Closes the stream to further operations. */
284     @Override
285     public void close() throws IOException {
286       base.close();
287     }
288     
289     @Override
290     public long length() {
291       return length;
292     }
293     
294     @Override
295     public void copyBytes(IndexOutput out, long numBytes) throws IOException {
296       // Copy first whatever is in the buffer
297       numBytes -= flushBuffer(out, numBytes);
298       
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.
301       if (numBytes > 0) {
302         long start = getFilePointer();
303         if (start + numBytes > length) {
304           throw new IOException("read past EOF");
305         }
306         base.seek(fileOffset + start);
307         base.copyBytes(out, numBytes);
308       }
309     }
310   }
311 }