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.IOException;
28
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 *
36 * @version $Id: CompoundFileReader.java 673371 2008-07-02 11:57:27Z mikemccand $
37 */
38 class CompoundFileReader extends Directory {
39
40 private int readBufferSize;
41
42 private static final class FileEntry {
43 long offset;
44 long length;
45 }
46
47
48 // Base info
49 private Directory directory;
50 private String fileName;
51
52 private IndexInput stream;
53 private HashMap entries = new HashMap();
54
55
56 public CompoundFileReader(Directory dir, String name) throws IOException {
57 this(dir, name, BufferedIndexInput.BUFFER_SIZE);
58 }
59
60 public CompoundFileReader(Directory dir, String name, int readBufferSize)
61 throws IOException
62 {
63 directory = dir;
64 fileName = name;
65 this.readBufferSize = readBufferSize;
66
67 boolean success = false;
68
69 try {
70 stream = dir.openInput(name, readBufferSize);
71
72 // read the directory and init files
73 int count = stream.readVInt();
74 FileEntry entry = null;
75 for (int i=0; i<count; i++) {
76 long offset = stream.readLong();
77 String id = stream.readString();
78
79 if (entry != null) {
80 // set length of the previous entry
81 entry.length = offset - entry.offset;
82 }
83
84 entry = new FileEntry();
85 entry.offset = offset;
86 entries.put(id, entry);
87 }
88
89 // set the length of the final entry
90 if (entry != null) {
91 entry.length = stream.length() - entry.offset;
92 }
93
94 success = true;
95
96 } finally {
97 if (! success && (stream != null)) {
98 try {
99 stream.close();
100 } catch (IOException e) { }
101 }
102 }
103 }
104
105 public Directory getDirectory() {
106 return directory;
107 }
108
109 public String getName() {
110 return fileName;
111 }
112
113 public synchronized void close() throws IOException {
114 if (stream == null)
115 throw new IOException("Already closed");
116
117 entries.clear();
118 stream.close();
119 stream = null;
120 }
121
122 public synchronized IndexInput openInput(String id)
123 throws IOException
124 {
125 // Default to readBufferSize passed in when we were opened
126 return openInput(id, readBufferSize);
127 }
128
129 public synchronized IndexInput openInput(String id, int readBufferSize)
130 throws IOException
131 {
132 if (stream == null)
133 throw new IOException("Stream closed");
134
135 FileEntry entry = (FileEntry) entries.get(id);
136 if (entry == null)
137 throw new IOException("No sub-file with id " + id + " found");
138
139 return new CSIndexInput(stream, entry.offset, entry.length, readBufferSize);
140 }
141
142 /** Returns an array of strings, one for each file in the directory. */
143 public String[] list() {
144 String res[] = new String[entries.size()];
145 return (String[]) entries.keySet().toArray(res);
146 }
147
148 /** Returns true iff a file with the given name exists. */
149 public boolean fileExists(String name) {
150 return entries.containsKey(name);
151 }
152
153 /** Returns the time the compound file was last modified. */
154 public long fileModified(String name) throws IOException {
155 return directory.fileModified(fileName);
156 }
157
158 /** Set the modified time of the compound file to now. */
159 public void touchFile(String name) throws IOException {
160 directory.touchFile(fileName);
161 }
162
163 /** Not implemented
164 * @throws UnsupportedOperationException */
165 public void deleteFile(String name)
166 {
167 throw new UnsupportedOperationException();
168 }
169
170 /** Not implemented
171 * @throws UnsupportedOperationException */
172 public void renameFile(String from, String to)
173 {
174 throw new UnsupportedOperationException();
175 }
176
177 /** Returns the length of a file in the directory.
178 * @throws IOException if the file does not exist */
179 public long fileLength(String name)
180 throws IOException
181 {
182 FileEntry e = (FileEntry) entries.get(name);
183 if (e == null)
184 throw new IOException("File " + name + " does not exist");
185 return e.length;
186 }
187
188 /** Not implemented
189 * @throws UnsupportedOperationException */
190 public IndexOutput createOutput(String name)
191 {
192 throw new UnsupportedOperationException();
193 }
194
195 /** Not implemented
196 * @throws UnsupportedOperationException */
197 public Lock makeLock(String name)
198 {
199 throw new UnsupportedOperationException();
200 }
201
202 /** Implementation of an IndexInput that reads from a portion of the
203 * compound file. The visibility is left as "package" *only* because
204 * this helps with testing since JUnit test cases in a different class
205 * can then access package fields of this class.
206 */
207 static final class CSIndexInput extends BufferedIndexInput {
208
209 IndexInput base;
210 long fileOffset;
211 long length;
212
213 CSIndexInput(final IndexInput base, final long fileOffset, final long length)
214 {
215 this(base, fileOffset, length, BufferedIndexInput.BUFFER_SIZE);
216 }
217
218 CSIndexInput(final IndexInput base, final long fileOffset, final long length, int readBufferSize)
219 {
220 super(readBufferSize);
221 this.base = (IndexInput)base.clone();
222 this.fileOffset = fileOffset;
223 this.length = length;
224 }
225
226 public Object clone() {
227 CSIndexInput clone = (CSIndexInput)super.clone();
228 clone.base = (IndexInput)base.clone();
229 clone.fileOffset = fileOffset;
230 clone.length = length;
231 return clone;
232 }
233
234 /** Expert: implements buffer refill. Reads bytes from the current
235 * position in the input.
236 * @param b the array to read bytes into
237 * @param offset the offset in the array to start storing bytes
238 * @param len the number of bytes to read
239 */
240 protected void readInternal(byte[] b, int offset, int len)
241 throws IOException
242 {
243 long start = getFilePointer();
244 if(start + len > length)
245 throw new IOException("read past EOF");
246 base.seek(fileOffset + start);
247 base.readBytes(b, offset, len, false);
248 }
249
250 /** Expert: implements seek. Sets current position in this file, where
251 * the next {@link #readInternal(byte[],int,int)} will occur.
252 * @see #readInternal(byte[],int,int)
253 */
254 protected void seekInternal(long pos) {}
255
256 /** Closes the stream to further operations. */
257 public void close() throws IOException {
258 base.close();
259 }
260
261 public long length() {
262 return length;
263 }
264
265
266 }
267
268 }