Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
IndexReader.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using Lucene.Net.Documents;
21 using Document = Lucene.Net.Documents.Document;
22 using FieldSelector = Lucene.Net.Documents.FieldSelector;
23 using Lucene.Net.Store;
24 using Similarity = Lucene.Net.Search.Similarity;
25 
26 namespace Lucene.Net.Index
27 {
28 
65  public abstract class IndexReader : System.ICloneable, System.IDisposable
66  {
67  private class AnonymousClassFindSegmentsFile : SegmentInfos.FindSegmentsFile
68  {
69  private void InitBlock(Lucene.Net.Store.Directory directory2)
70  {
71  this.directory2 = directory2;
72  }
73  private Lucene.Net.Store.Directory directory2;
74  internal AnonymousClassFindSegmentsFile(Lucene.Net.Store.Directory directory2, Lucene.Net.Store.Directory Param1):base(Param1)
75  {
76  InitBlock(directory2);
77  }
78  public override System.Object DoBody(System.String segmentFileName)
79  {
80  return (long) directory2.FileModified(segmentFileName);
81  }
82  }
83 
87  public sealed class FieldOption
88  {
89  private readonly System.String option;
90  internal FieldOption()
91  {
92  }
93  internal FieldOption(System.String option)
94  {
95  this.option = option;
96  }
97  public override System.String ToString()
98  {
99  return this.option;
100  }
102  public static readonly FieldOption ALL = new FieldOption("ALL");
104  public static readonly FieldOption INDEXED = new FieldOption("INDEXED");
106  public static readonly FieldOption STORES_PAYLOADS = new FieldOption("STORES_PAYLOADS");
108  public static readonly FieldOption OMIT_TERM_FREQ_AND_POSITIONS = new FieldOption("OMIT_TERM_FREQ_AND_POSITIONS");
110  public static readonly FieldOption UNINDEXED = new FieldOption("UNINDEXED");
112  public static readonly FieldOption INDEXED_WITH_TERMVECTOR = new FieldOption("INDEXED_WITH_TERMVECTOR");
114  public static readonly FieldOption INDEXED_NO_TERMVECTOR = new FieldOption("INDEXED_NO_TERMVECTOR");
116  public static readonly FieldOption TERMVECTOR = new FieldOption("TERMVECTOR");
118  public static readonly FieldOption TERMVECTOR_WITH_POSITION = new FieldOption("TERMVECTOR_WITH_POSITION");
120  public static readonly FieldOption TERMVECTOR_WITH_OFFSET = new FieldOption("TERMVECTOR_WITH_OFFSET");
122  public static readonly FieldOption TERMVECTOR_WITH_POSITION_OFFSET = new FieldOption("TERMVECTOR_WITH_POSITION_OFFSET");
123  }
124 
125  private bool closed;
126  protected internal bool hasChanges;
127 
128  private int refCount;
129 
130  protected internal static int DEFAULT_TERMS_INDEX_DIVISOR = 1;
131 
133  public virtual int RefCount
134  {
135  get
136  {
137  lock (this)
138  {
139  return refCount;
140  }
141  }
142  }
143 
158  public virtual void IncRef()
159  {
160  lock (this)
161  {
162  System.Diagnostics.Debug.Assert(refCount > 0);
163  EnsureOpen();
164  refCount++;
165  }
166  }
167 
179  public virtual void DecRef()
180  {
181  lock (this)
182  {
183  System.Diagnostics.Debug.Assert(refCount > 0);
184  EnsureOpen();
185  if (refCount == 1)
186  {
187  Commit();
188  DoClose();
189  }
190  refCount--;
191  }
192  }
193 
194  protected internal IndexReader()
195  {
196  refCount = 1;
197  }
198 
200  protected internal void EnsureOpen()
201  {
202  if (refCount <= 0)
203  {
204  throw new AlreadyClosedException("this IndexReader is closed");
205  }
206  }
207 
218  public static IndexReader Open(Directory directory, bool readOnly)
219  {
220  return Open(directory, null, null, readOnly, DEFAULT_TERMS_INDEX_DIVISOR);
221  }
222 
235  public static IndexReader Open(IndexCommit commit, bool readOnly)
236  {
237  return Open(commit.Directory, null, commit, readOnly, DEFAULT_TERMS_INDEX_DIVISOR);
238  }
239 
257  public static IndexReader Open(Directory directory, IndexDeletionPolicy deletionPolicy, bool readOnly)
258  {
259  return Open(directory, deletionPolicy, null, readOnly, DEFAULT_TERMS_INDEX_DIVISOR);
260  }
261 
291  public static IndexReader Open(Directory directory, IndexDeletionPolicy deletionPolicy, bool readOnly, int termInfosIndexDivisor)
292  {
293  return Open(directory, deletionPolicy, null, readOnly, termInfosIndexDivisor);
294  }
295 
315  public static IndexReader Open(IndexCommit commit, IndexDeletionPolicy deletionPolicy, bool readOnly)
316  {
317  return Open(commit.Directory, deletionPolicy, commit, readOnly, DEFAULT_TERMS_INDEX_DIVISOR);
318  }
319 
351  public static IndexReader Open(IndexCommit commit, IndexDeletionPolicy deletionPolicy, bool readOnly, int termInfosIndexDivisor)
352  {
353  return Open(commit.Directory, deletionPolicy, commit, readOnly, termInfosIndexDivisor);
354  }
355 
356  private static IndexReader Open(Directory directory, IndexDeletionPolicy deletionPolicy, IndexCommit commit, bool readOnly, int termInfosIndexDivisor)
357  {
358  return DirectoryReader.Open(directory, deletionPolicy, commit, readOnly, termInfosIndexDivisor);
359  }
360 
405  public virtual IndexReader Reopen()
406  {
407  lock (this)
408  {
409  throw new NotSupportedException("This reader does not support reopen().");
410  }
411  }
412 
413 
419  public virtual IndexReader Reopen(bool openReadOnly)
420  {
421  lock (this)
422  {
423  throw new NotSupportedException("This reader does not support reopen().");
424  }
425  }
426 
434  public virtual IndexReader Reopen(IndexCommit commit)
435  {
436  lock (this)
437  {
438  throw new NotSupportedException("This reader does not support reopen(IndexCommit).");
439  }
440  }
441 
460  public virtual System.Object Clone()
461  {
462  throw new System.NotSupportedException("This reader does not implement clone()");
463  }
464 
470  public virtual IndexReader Clone(bool openReadOnly)
471  {
472  lock (this)
473  {
474  throw new System.NotSupportedException("This reader does not implement clone()");
475  }
476  }
477 
484  public virtual Directory Directory()
485  {
486  EnsureOpen();
487  throw new NotSupportedException("This reader does not support this method.");
488  }
489 
496  public static long LastModified(Directory directory2)
497  {
498  return (long) ((System.Int64) new AnonymousClassFindSegmentsFile(directory2, directory2).Run());
499  }
500 
512  public static long GetCurrentVersion(Directory directory)
513  {
514  return SegmentInfos.ReadCurrentVersion(directory);
515  }
516 
533  public static System.Collections.Generic.IDictionary<string, string> GetCommitUserData(Directory directory)
534  {
535  return SegmentInfos.ReadCurrentUserData(directory);
536  }
537 
564  public virtual long Version
565  {
566  get { throw new System.NotSupportedException("This reader does not support this method."); }
567  }
568 
577  public virtual IDictionary<string, string> CommitUserData
578  {
579  get { throw new System.NotSupportedException("This reader does not support this method."); }
580  }
581 
612  public virtual bool IsCurrent()
613  {
614  throw new NotSupportedException("This reader does not support this method.");
615  }
616 
622  public virtual bool IsOptimized()
623  {
624  throw new NotSupportedException("This reader does not support this method.");
625  }
626 
644  abstract public ITermFreqVector[] GetTermFreqVectors(int docNumber);
645 
646 
664  abstract public ITermFreqVector GetTermFreqVector(int docNumber, String field);
665 
678  abstract public void GetTermFreqVector(int docNumber, String field, TermVectorMapper mapper);
679 
686  abstract public void GetTermFreqVector(int docNumber, TermVectorMapper mapper);
687 
696  public static bool IndexExists(Directory directory)
697  {
698  return SegmentInfos.GetCurrentSegmentGeneration(directory) != - 1;
699  }
700 
702  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
703  public abstract int NumDocs();
704 
709  public abstract int MaxDoc { get; }
710 
712  public virtual int NumDeletedDocs
713  {
714  get { return MaxDoc - NumDocs(); }
715  }
716 
729  public virtual Document Document(int n)
730  {
731  EnsureOpen();
732  return Document(n, null);
733  }
734 
747  public Document this[int doc]
748  {
749  get { return Document(doc); }
750  }
751 
788  // TODO (1.5): When we convert to JDK 1.5 make this Set<String>
789  public abstract Document Document(int n, FieldSelector fieldSelector);
790 
792  public abstract bool IsDeleted(int n);
793 
795  public abstract bool HasDeletions { get; }
796 
798  public virtual bool HasNorms(System.String field)
799  {
800  // backward compatible implementation.
801  // SegmentReader has an efficient implementation.
802  EnsureOpen();
803  return Norms(field) != null;
804  }
805 
811  public abstract byte[] Norms(System.String field);
812 
818  public abstract void Norms(System.String field, byte[] bytes, int offset);
819 
842  public virtual void SetNorm(int doc, String field, byte value)
843  {
844  lock (this)
845  {
846  EnsureOpen();
847  AcquireWriteLock();
848  hasChanges = true;
849  DoSetNorm(doc, field, value);
850  }
851  }
852 
854  protected internal abstract void DoSetNorm(int doc, System.String field, byte value_Renamed);
855 
873  public virtual void SetNorm(int doc, System.String field, float value)
874  {
875  EnsureOpen();
876  SetNorm(doc, field, Similarity.EncodeNorm(value));
877  }
878 
889  public abstract TermEnum Terms();
890 
900  public abstract TermEnum Terms(Term t);
901 
904  public abstract int DocFreq(Term t);
905 
919  public virtual TermDocs TermDocs(Term term)
920  {
921  EnsureOpen();
922  TermDocs termDocs = TermDocs();
923  termDocs.Seek(term);
924  return termDocs;
925  }
926 
929  public abstract TermDocs TermDocs();
930 
948  public virtual TermPositions TermPositions(Term term)
949  {
950  EnsureOpen();
951  TermPositions termPositions = TermPositions();
952  termPositions.Seek(term);
953  return termPositions;
954  }
955 
958  public abstract TermPositions TermPositions();
959 
960 
961 
978  public virtual void DeleteDocument(int docNum)
979  {
980  lock (this)
981  {
982  EnsureOpen();
983  AcquireWriteLock();
984  hasChanges = true;
985  DoDelete(docNum);
986  }
987  }
988 
989 
993  protected internal abstract void DoDelete(int docNum);
994 
995 
1014  public virtual int DeleteDocuments(Term term)
1015  {
1016  EnsureOpen();
1017  TermDocs docs = TermDocs(term);
1018  if (docs == null)
1019  return 0;
1020  int n = 0;
1021  try
1022  {
1023  while (docs.Next())
1024  {
1025  DeleteDocument(docs.Doc);
1026  n++;
1027  }
1028  }
1029  finally
1030  {
1031  docs.Close();
1032  }
1033  return n;
1034  }
1035 
1047  public virtual void UndeleteAll()
1048  {
1049  lock (this)
1050  {
1051  EnsureOpen();
1052  AcquireWriteLock();
1053  hasChanges = true;
1054  DoUndeleteAll();
1055  }
1056  }
1057 
1059  protected internal abstract void DoUndeleteAll();
1060 
1065  protected internal virtual void AcquireWriteLock()
1066  {
1067  lock (this)
1068  {
1069  /* NOOP */
1070  }
1071  }
1072 
1075  public void Flush()
1076  {
1077  lock (this)
1078  {
1079  EnsureOpen();
1080  Commit();
1081  }
1082  }
1083 
1089  public void Flush(IDictionary<string, string> commitUserData)
1090  {
1091  lock (this)
1092  {
1093  EnsureOpen();
1094  Commit(commitUserData);
1095  }
1096  }
1097 
1106  public /*protected internal*/ void Commit()
1107  {
1108  lock (this)
1109  {
1110  Commit(null);
1111  }
1112  }
1113 
1122  public void Commit(IDictionary<string, string> commitUserData)
1123  {
1124  lock (this)
1125  {
1126  if (hasChanges)
1127  {
1128  DoCommit(commitUserData);
1129  }
1130  hasChanges = false;
1131  }
1132  }
1133 
1135  protected internal abstract void DoCommit(IDictionary<string, string> commitUserData);
1136 
1137  [Obsolete("Use Dispose() instead")]
1138  public void Close()
1139  {
1140  Dispose();
1141  }
1142 
1148  public void Dispose()
1149  {
1150  Dispose(true);
1151  }
1152 
1153  protected virtual void Dispose(bool disposing)
1154  {
1155  if (disposing)
1156  {
1157  lock (this)
1158  {
1159  if (!closed)
1160  {
1161  DecRef();
1162  closed = true;
1163  }
1164  }
1165  }
1166  }
1167 
1169  protected internal abstract void DoClose();
1170 
1171 
1181  public abstract ICollection<string> GetFieldNames(FieldOption fldOption);
1182 
1191  public virtual IndexCommit IndexCommit
1192  {
1193  get { throw new NotSupportedException("This reader does not support this method."); }
1194  }
1195 
1203  [STAThread]
1204  public static void Main(String[] args)
1205  {
1206  System.String filename = null;
1207  bool extract = false;
1208 
1209  foreach (string t in args)
1210  {
1211  if (t.Equals("-extract"))
1212  {
1213  extract = true;
1214  }
1215  else if (filename == null)
1216  {
1217  filename = t;
1218  }
1219  }
1220 
1221  if (filename == null)
1222  {
1223  System.Console.Out.WriteLine("Usage: Lucene.Net.Index.IndexReader [-extract] <cfsfile>");
1224  return ;
1225  }
1226 
1227  Directory dir = null;
1228  CompoundFileReader cfr = null;
1229 
1230  try
1231  {
1232  var file = new System.IO.FileInfo(filename);
1233  System.String dirname = new System.IO.FileInfo(file.FullName).DirectoryName;
1234  filename = file.Name;
1235  dir = FSDirectory.Open(new System.IO.DirectoryInfo(dirname));
1236  cfr = new CompoundFileReader(dir, filename);
1237 
1238  System.String[] files = cfr.ListAll();
1239  System.Array.Sort(files); // sort the array of filename so that the output is more readable
1240 
1241  foreach (string t in files)
1242  {
1243  long len = cfr.FileLength(t);
1244 
1245  if (extract)
1246  {
1247  System.Console.Out.WriteLine("extract " + t + " with " + len + " bytes to local directory...");
1248  IndexInput ii = cfr.OpenInput(t);
1249 
1250  var f = new System.IO.FileStream(t, System.IO.FileMode.Create);
1251 
1252  // read and write with a small buffer, which is more effectiv than reading byte by byte
1253  var buffer = new byte[1024];
1254  int chunk = buffer.Length;
1255  while (len > 0)
1256  {
1257  var bufLen = (int) System.Math.Min(chunk, len);
1258  ii.ReadBytes(buffer, 0, bufLen);
1259  f.Write(buffer, 0, bufLen);
1260  len -= bufLen;
1261  }
1262 
1263  f.Close();
1264  ii.Close();
1265  }
1266  else
1267  System.Console.Out.WriteLine(t + ": " + len + " bytes");
1268  }
1269  }
1270  catch (System.IO.IOException ioe)
1271  {
1272  System.Console.Error.WriteLine(ioe.StackTrace);
1273  }
1274  finally
1275  {
1276  try
1277  {
1278  if (dir != null)
1279  dir.Close();
1280  if (cfr != null)
1281  cfr.Close();
1282  }
1283  catch (System.IO.IOException ioe)
1284  {
1285  System.Console.Error.WriteLine(ioe.StackTrace);
1286  }
1287  }
1288  }
1289 
1303  public static System.Collections.Generic.ICollection<IndexCommit> ListCommits(Directory dir)
1304  {
1305  return DirectoryReader.ListCommits(dir);
1306  }
1307 
1324  public virtual IndexReader[] GetSequentialSubReaders()
1325  {
1326  return null;
1327  }
1328 
1330  public virtual object FieldCacheKey
1331  {
1332  get { return this; }
1333  }
1334 
1335  /* Expert. Warning: this returns null if the reader has
1336  * no deletions
1337  */
1338 
1339  public virtual object DeletesCacheKey
1340  {
1341  get { return this; }
1342  }
1343 
1358  public virtual long UniqueTermCount
1359  {
1360  get { throw new System.NotSupportedException("this reader does not implement getUniqueTermCount()"); }
1361  }
1362 
1369  public virtual int TermInfosIndexDivisor
1370  {
1371  get { throw new NotSupportedException("This reader does not support this method."); }
1372  }
1373  }
1374 }