Add --enable-deletion option to buildindex. If used, buildindex will remove deleted...
[beagle.git] / beagled / Lucene.Net / Index / CompoundFileReader.cs
blob74d2b2d114a5ce68eab304198d689d3a5c6d2a8b
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using BufferedIndexInput = Lucene.Net.Store.BufferedIndexInput;
18 using Directory = Lucene.Net.Store.Directory;
19 using IndexInput = Lucene.Net.Store.IndexInput;
20 using IndexOutput = Lucene.Net.Store.IndexOutput;
21 using Lock = Lucene.Net.Store.Lock;
22 namespace Lucene.Net.Index
26 /// <summary> Class for accessing a compound stream.
27 /// This class implements a directory, but is limited to only read operations.
28 /// Directory methods that would normally modify data throw an exception.
29 ///
30 /// </summary>
31 /// <author> Dmitry Serebrennikov
32 /// </author>
33 /// <version> $Id: CompoundFileReader.cs,v 1.3 2005/10/06 19:29:55 dsd Exp $
34 /// </version>
35 public class CompoundFileReader : Directory
38 private sealed class FileEntry
40 internal long offset;
41 internal long length;
45 // Base info
46 private Directory directory;
47 private System.String fileName;
50 private IndexInput stream;
51 private System.Collections.Hashtable entries = new System.Collections.Hashtable();
54 public CompoundFileReader(Directory dir, System.String name)
56 directory = dir;
57 fileName = name;
59 bool success = false;
61 try
63 stream = dir.OpenInput(name);
65 // read the directory and init files
66 int count = stream.ReadVInt();
67 FileEntry entry = null;
68 for (int i = 0; i < count; i++)
70 long offset = stream.ReadLong();
71 System.String id = stream.ReadString();
73 if (entry != null)
75 // set length of the previous entry
76 entry.length = offset - entry.offset;
79 entry = new FileEntry();
80 entry.offset = offset;
81 entries[id] = entry;
84 // set the length of the final entry
85 if (entry != null)
87 entry.length = stream.Length() - entry.offset;
90 success = true;
92 finally
94 if (!success && (stream != null))
96 try
98 stream.Close();
100 catch (System.IO.IOException e)
107 public virtual Directory GetDirectory()
109 return directory;
112 public virtual System.String GetName()
114 return fileName;
117 public override void Close()
119 lock (this)
121 if (stream == null)
122 throw new System.IO.IOException("Already closed");
124 entries.Clear();
125 stream.Close();
126 stream = null;
130 public override IndexInput OpenInput(System.String id)
132 lock (this)
134 if (stream == null)
135 throw new System.IO.IOException("Stream closed");
137 FileEntry entry = (FileEntry) entries[id];
138 if (entry == null)
139 throw new System.IO.IOException("No sub-file with id " + id + " found");
141 return new CSIndexInput(stream, entry.offset, entry.length);
145 /// <summary>Returns an array of strings, one for each file in the directory. </summary>
146 public override System.String[] List()
148 System.String[] res = new System.String[entries.Count];
149 entries.Keys.CopyTo(res, 0);
150 return res;
153 /// <summary>Returns true iff a file with the given name exists. </summary>
154 public override bool FileExists(System.String name)
156 return entries.ContainsKey(name);
159 /// <summary>Returns the time the named file was last modified. </summary>
160 public override long FileModified(System.String name)
162 return directory.FileModified(fileName);
165 /// <summary>Set the modified time of an existing file to now. </summary>
166 public override void TouchFile(System.String name)
168 directory.TouchFile(fileName);
171 /// <summary>Not implemented</summary>
172 /// <throws> UnsupportedOperationException </throws>
173 public override void DeleteFile(System.String name)
175 throw new System.NotSupportedException();
178 /// <summary>Not implemented</summary>
179 /// <throws> UnsupportedOperationException </throws>
180 public override void RenameFile(System.String from, System.String to)
182 throw new System.NotSupportedException();
185 /// <summary>Returns the length of a file in the directory.</summary>
186 /// <throws> IOException if the file does not exist </throws>
187 public override long FileLength(System.String name)
189 FileEntry e = (FileEntry) entries[name];
190 if (e == null)
191 throw new System.IO.IOException("File " + name + " does not exist");
192 return e.length;
195 /// <summary>Not implemented</summary>
196 /// <throws> UnsupportedOperationException </throws>
197 public override IndexOutput CreateOutput(System.String name)
199 throw new System.NotSupportedException();
202 /// <summary>Not implemented</summary>
203 /// <throws> UnsupportedOperationException </throws>
204 public override Lock MakeLock(System.String name)
206 throw new System.NotSupportedException();
209 /// <summary>Implementation of an IndexInput that reads from a portion of the
210 /// compound file. The visibility is left as "package" *only* because
211 /// this helps with testing since JUnit test cases in a different class
212 /// can then access package fields of this class.
213 /// </summary>
214 public /*internal*/ sealed class CSIndexInput : BufferedIndexInput
217 public /*internal*/ IndexInput base_Renamed;
218 internal long fileOffset;
219 internal long length;
221 internal CSIndexInput(IndexInput base_Renamed, long fileOffset, long length)
223 this.base_Renamed = base_Renamed;
224 this.fileOffset = fileOffset;
225 this.length = length;
228 /// <summary>Expert: implements buffer refill. Reads bytes from the current
229 /// position in the input.
230 /// </summary>
231 /// <param name="b">the array to read bytes into
232 /// </param>
233 /// <param name="offset">the offset in the array to start storing bytes
234 /// </param>
235 /// <param name="len">the number of bytes to read
236 /// </param>
237 public override void ReadInternal(byte[] b, int offset, int len)
239 lock (base_Renamed)
241 long start = GetFilePointer();
242 if (start + len > length)
243 throw new System.IO.IOException("read past EOF");
244 base_Renamed.Seek(fileOffset + start);
245 base_Renamed.ReadBytes(b, offset, len);
249 /// <summary>Expert: implements seek. Sets current position in this file, where
250 /// the next {@link #ReadInternal(byte[],int,int)} will occur.
251 /// </summary>
252 /// <seealso cref="#ReadInternal(byte[],int,int)">
253 /// </seealso>
254 public override void SeekInternal(long pos)
258 /// <summary>Closes the stream to further operations. </summary>
259 public override void Close()
263 public override long Length()
265 return length;