20 using Lucene.Net.Support;
21 using Document = Lucene.Net.Documents.Document;
28 namespace Lucene.Net.Index
38 private bool[] decrefOnClose;
39 private System.Collections.Generic.IDictionary<string, byte[]> normsCache =
new HashMap<string,byte[]>();
40 private int maxDoc = 0;
41 private int numDocs = - 1;
42 private bool hasDeletions =
false;
54 Initialize(subReaders,
true);
69 Initialize(subReaders, closeSubReaders);
72 private void Initialize(
IndexReader[] subReaders,
bool closeSubReaders)
75 this.subReaders = subReaders.ToArray();
76 starts =
new int[subReaders.Length + 1];
77 decrefOnClose =
new bool[subReaders.Length];
78 for (
int i = 0; i < subReaders.Length; i++)
81 maxDoc += subReaders[i].
MaxDoc;
86 decrefOnClose[i] =
true;
90 decrefOnClose[i] =
false;
93 if (subReaders[i].HasDeletions)
96 starts[subReaders.Length] = maxDoc;
121 return DoReopen(
false);
133 public override System.Object Clone()
137 return DoReopen(
true);
139 catch (System.Exception ex)
141 throw new System.SystemException(ex.Message, ex);
153 protected internal virtual IndexReader DoReopen(
bool doClone)
157 bool reopened =
false;
160 bool success =
false;
163 for (
int i = 0; i < subReaders.Length; i++)
166 newSubReaders[i] = (
IndexReader) subReaders[i].Clone();
168 newSubReaders[i] = subReaders[i].
Reopen();
171 if (newSubReaders[i] != subReaders[i])
180 if (!success && reopened)
182 for (
int i = 0; i < newSubReaders.Length; i++)
184 if (newSubReaders[i] != subReaders[i])
188 newSubReaders[i].
Close();
190 catch (System.IO.IOException)
201 bool[] newDecrefOnClose =
new bool[subReaders.Length];
202 for (
int i = 0; i < subReaders.Length; i++)
204 if (newSubReaders[i] == subReaders[i])
206 newSubReaders[i].
IncRef();
207 newDecrefOnClose[i] =
true;
210 MultiReader mr =
new MultiReader(newSubReaders);
211 mr.decrefOnClose = newDecrefOnClose;
223 int i = ReaderIndex(n);
230 int i = ReaderIndex(n);
235 public override void GetTermFreqVector(
int docNumber, System.String field,
TermVectorMapper mapper)
238 int i = ReaderIndex(docNumber);
245 int i = ReaderIndex(docNumber);
249 public override bool IsOptimized()
254 public override int NumDocs()
263 for (
int i = 0; i < subReaders.Length; i++)
264 n += subReaders[i].NumDocs();
270 public override int MaxDoc
283 int i = ReaderIndex(n);
284 return subReaders[i].
Document(n - starts[i], fieldSelector);
287 public override bool IsDeleted(
int n)
290 int i = ReaderIndex(n);
291 return subReaders[i].
IsDeleted(n - starts[i]);
294 public override bool HasDeletions
303 protected internal override void DoDelete(
int n)
306 int i = ReaderIndex(n);
311 protected internal override void DoUndeleteAll()
313 for (
int i = 0; i < subReaders.Length; i++)
314 subReaders[i].UndeleteAll();
316 hasDeletions =
false;
320 private int ReaderIndex(
int n)
323 return DirectoryReader.ReaderIndex(n, this.starts, this.subReaders.Length);
326 public override bool HasNorms(System.String field)
329 for (
int i = 0; i < subReaders.Length; i++)
331 if (subReaders[i].HasNorms(field))
337 public override byte[] Norms(System.String field)
342 byte[] bytes = normsCache[field];
345 if (!HasNorms(field))
348 bytes =
new byte[MaxDoc];
349 for (
int i = 0; i < subReaders.Length; i++)
350 subReaders[i].Norms(field, bytes, starts[i]);
351 normsCache[field] = bytes;
356 public override void Norms(System.String field, byte[] result,
int offset)
361 byte[] bytes = normsCache[field];
362 for (
int i = 0; i < subReaders.Length; i++)
364 subReaders[i].Norms(field, result, offset + starts[i]);
366 if (bytes == null && !HasNorms(field))
368 for (
int i = offset; i < result.Length; i++)
373 else if (bytes != null)
376 Array.Copy(bytes, 0, result, offset, MaxDoc);
380 for (
int i = 0; i < subReaders.Length; i++)
383 subReaders[i].
Norms(field, result, offset + starts[i]);
389 protected internal override void DoSetNorm(
int n, System.String field, byte value_Renamed)
393 normsCache.Remove(field);
395 int i = ReaderIndex(n);
396 subReaders[i].
SetNorm(n - starts[i], field, value_Renamed);
411 public override int DocFreq(
Term t)
415 for (
int i = 0; i < subReaders.Length; i++)
416 total += subReaders[i].DocFreq(t);
432 protected internal override void DoCommit(System.Collections.Generic.IDictionary<
string,
string> commitUserData)
434 for (
int i = 0; i < subReaders.Length; i++)
435 subReaders[i].Commit(commitUserData);
438 protected internal override void DoClose()
442 for (
int i = 0; i < subReaders.Length; i++)
444 if (decrefOnClose[i])
450 subReaders[i].
Close();
458 Lucene.Net.Search.FieldCache_Fields.DEFAULT.Purge(
this);
461 public override System.Collections.Generic.ICollection<
string> GetFieldNames(
IndexReader.FieldOption fieldNames)
468 public override bool IsCurrent()
470 for (
int i = 0; i < subReaders.Length; i++)
472 if (!subReaders[i].IsCurrent())
486 get {
throw new System.NotSupportedException(
"MultiReader does not support this method."); }