19 using System.Collections.Generic;
20 using Lucene.Net.Support;
26 namespace Lucene.Net.Index
38 internal const int NO = - 1;
39 internal const int YES = 1;
40 internal const int CHECK_DIR = 0;
41 internal const int WITHOUT_GEN = 0;
47 private bool preLockless;
55 private long[] normGen;
64 private sbyte isCompoundFile;
68 private bool hasSingleNormFile;
74 private IList<string> files;
77 internal long sizeInBytes = - 1;
79 private int docStoreOffset;
81 private System.String docStoreSegment;
83 private bool docStoreIsCompoundFile;
90 private IDictionary<string, string> diagnostics;
92 public override System.String ToString()
94 return "si: " + dir.ToString() +
" " + name +
" docCount: " + docCount +
" delCount: " + delCount +
" delFileName: " + GetDelFileName();
100 this.docCount = docCount;
103 isCompoundFile = (sbyte) (CHECK_DIR);
105 hasSingleNormFile =
false;
106 docStoreOffset = - 1;
107 docStoreSegment = name;
108 docStoreIsCompoundFile =
false;
113 public SegmentInfo(System.String name,
int docCount,
Directory dir,
bool isCompoundFile,
bool hasSingleNormFile):this(name, docCount, dir, isCompoundFile, hasSingleNormFile, - 1, null, false, true)
117 public SegmentInfo(System.String name,
int docCount,
Directory dir,
bool isCompoundFile,
bool hasSingleNormFile,
int docStoreOffset, System.String docStoreSegment,
bool docStoreIsCompoundFile,
bool hasProx):this(name, docCount, dir)
119 this.isCompoundFile = (sbyte) (isCompoundFile?YES:NO);
120 this.hasSingleNormFile = hasSingleNormFile;
122 this.docStoreOffset = docStoreOffset;
123 this.docStoreSegment = docStoreSegment;
124 this.docStoreIsCompoundFile = docStoreIsCompoundFile;
125 this.hasProx = hasProx;
127 System.Diagnostics.Debug.Assert(docStoreOffset == - 1 || docStoreSegment != null,
"dso=" + docStoreOffset +
" dss=" + docStoreSegment +
" docCount=" + docCount);
137 preLockless = src.preLockless;
139 docStoreOffset = src.docStoreOffset;
140 docStoreIsCompoundFile = src.docStoreIsCompoundFile;
141 if (src.normGen == null)
147 normGen =
new long[src.normGen.Length];
148 Array.Copy(src.normGen, 0, normGen, 0, src.normGen.Length);
150 isCompoundFile = src.isCompoundFile;
151 hasSingleNormFile = src.hasSingleNormFile;
152 delCount = src.delCount;
155 public IDictionary<string, string> Diagnostics
157 get {
return diagnostics; }
158 internal set { this.diagnostics = value; }
174 name = input.ReadString();
175 docCount = input.ReadInt();
178 delGen = input.ReadLong();
181 docStoreOffset = input.ReadInt();
182 if (docStoreOffset != - 1)
184 docStoreSegment = input.ReadString();
185 docStoreIsCompoundFile = (1 == input.ReadByte());
189 docStoreSegment = name;
190 docStoreIsCompoundFile =
false;
195 docStoreOffset = - 1;
196 docStoreSegment = name;
197 docStoreIsCompoundFile =
false;
199 if (format <= SegmentInfos.FORMAT_SINGLE_NORM_FILE)
201 hasSingleNormFile = (1 == input.ReadByte());
205 hasSingleNormFile =
false;
207 int numNormGen = input.ReadInt();
208 if (numNormGen == NO)
214 normGen =
new long[numNormGen];
215 for (
int j = 0; j < numNormGen; j++)
217 normGen[j] = input.ReadLong();
220 isCompoundFile = (sbyte) input.ReadByte();
221 preLockless = (isCompoundFile == CHECK_DIR);
222 if (format <= SegmentInfos.FORMAT_DEL_COUNT)
224 delCount = input.ReadInt();
225 System.
Diagnostics.Debug.Assert(delCount <= docCount);
229 if (format <= SegmentInfos.FORMAT_HAS_PROX)
230 hasProx = input.ReadByte() == 1;
234 if (format <= SegmentInfos.FORMAT_DIAGNOSTICS)
236 diagnostics = input.ReadStringStringMap();
240 diagnostics =
new Dictionary<string,string>();
247 isCompoundFile = (sbyte) (CHECK_DIR);
249 hasSingleNormFile =
false;
250 docStoreOffset = - 1;
251 docStoreIsCompoundFile =
false;
252 docStoreSegment = null;
255 diagnostics =
new Dictionary<string,string>();
259 internal void SetNumFields(
int numFields)
266 normGen =
new long[numFields];
277 for (
int i = 0; i < numFields; i++)
288 public long SizeInBytes()
290 if (sizeInBytes == - 1)
292 IList<string> files = Files();
293 int size = files.Count;
295 for (
int i = 0; i < size; i++)
297 System.String fileName = files[i];
300 if (docStoreOffset == - 1 || !
IndexFileNames.IsDocStoreFile(fileName))
301 sizeInBytes += dir.FileLength(fileName);
307 public bool HasDeletions()
327 else if (delGen >= YES)
333 return dir.FileExists(GetDelFileName());
337 internal void AdvanceDelGen()
351 internal void ClearDelGen()
357 public System.Object Clone()
360 si.isCompoundFile = isCompoundFile;
362 si.delCount = delCount;
363 si.hasProx = hasProx;
364 si.preLockless = preLockless;
365 si.hasSingleNormFile = hasSingleNormFile;
366 si.diagnostics =
new HashMap<string, string>(this.diagnostics);
367 if (this.diagnostics != null)
369 si.diagnostics =
new System.Collections.Generic.Dictionary<string,
string>();
370 foreach (
string o
in diagnostics.Keys)
372 si.diagnostics.Add(o,diagnostics[o]);
377 si.normGen =
new long[normGen.Length];
378 normGen.CopyTo(si.normGen, 0);
380 si.docStoreOffset = docStoreOffset;
381 si.docStoreSegment = docStoreSegment;
382 si.docStoreIsCompoundFile = docStoreIsCompoundFile;
383 if (this.files != null)
385 si.files =
new System.Collections.Generic.List<
string>();
386 foreach (
string file
in files)
395 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1024:UsePropertiesWhereAppropriate")]
396 public System.String GetDelFileName()
416 public bool HasSeparateNorms(
int fieldNumber)
418 if ((normGen == null && preLockless) || (normGen != null && normGen[fieldNumber] == CHECK_DIR))
421 System.String fileName = name +
".s" + fieldNumber;
422 return dir.FileExists(fileName);
424 else if (normGen == null || normGen[fieldNumber] == NO)
435 public bool HasSeparateNorms()
450 System.String[] result = dir.ListAll();
453 throw new System.IO.IOException(
"cannot read directory " + dir +
": ListAll() returned null");
457 System.String pattern;
458 pattern = name +
".s";
459 int patternLength = pattern.Length;
460 for (
int i = 0; i < result.Length; i++)
462 string fileName = result[i];
463 if (filter.
Accept(null, fileName) && fileName.StartsWith(pattern) &&
char.IsDigit(fileName[patternLength]))
474 for (
int i = 0; i < normGen.Length; i++)
476 if (normGen[i] >= YES)
483 for (
int i = 0; i < normGen.Length; i++)
485 if (normGen[i] == CHECK_DIR)
487 if (HasSeparateNorms(i))
504 internal void AdvanceNormGen(
int fieldIndex)
506 if (normGen[fieldIndex] == NO)
508 normGen[fieldIndex] = YES;
512 normGen[fieldIndex]++;
522 public System.String GetNormFileName(
int number)
524 System.String prefix;
533 gen = normGen[number];
536 if (HasSeparateNorms(number))
543 if (hasSingleNormFile)
558 internal void SetUseCompoundFile(
bool value)
562 this.isCompoundFile = (sbyte) (YES);
566 this.isCompoundFile = (sbyte) (NO);
574 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1024:UsePropertiesWhereAppropriate")]
575 public bool GetUseCompoundFile()
577 if (isCompoundFile == NO)
581 if (isCompoundFile == YES)
588 [System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Design",
"CA1024:UsePropertiesWhereAppropriate")]
589 public int GetDelCount()
595 System.String delFileName = GetDelFileName();
596 delCount =
new BitVector(dir, delFileName).Count();
601 System.Diagnostics.Debug.Assert(delCount <= docCount);
605 internal void SetDelCount(
int delCount)
607 this.delCount = delCount;
608 System.Diagnostics.Debug.Assert(delCount <= docCount);
611 public int DocStoreOffset
613 get {
return docStoreOffset; }
616 docStoreOffset = value;
621 public bool DocStoreIsCompoundFile
623 get {
return docStoreIsCompoundFile; }
626 docStoreIsCompoundFile = value;
631 public string DocStoreSegment
633 get {
return docStoreSegment; }
636 internal void SetDocStore(
int offset, System.String segment,
bool isCompoundFile)
638 docStoreOffset = offset;
639 docStoreSegment = segment;
640 docStoreIsCompoundFile = isCompoundFile;
646 output.WriteString(name);
647 output.WriteInt(docCount);
648 output.WriteLong(delGen);
649 output.WriteInt(docStoreOffset);
650 if (docStoreOffset != - 1)
652 output.WriteString(docStoreSegment);
653 output.WriteByte((byte) (docStoreIsCompoundFile?1:0));
656 output.WriteByte((byte) (hasSingleNormFile?1:0));
663 output.WriteInt(normGen.Length);
664 for (
int j = 0; j < normGen.Length; j++)
666 output.WriteLong(normGen[j]);
669 output.WriteByte((byte) isCompoundFile);
670 output.WriteInt(delCount);
671 output.WriteByte((byte) (hasProx?1:0));
672 output.WriteStringStringMap(diagnostics);
677 get {
return hasProx; }
680 this.hasProx = value;
685 private void AddIfExists(IList<string> files, System.String fileName)
687 if (dir.FileExists(fileName))
697 public IList<string> Files()
706 var fileList =
new System.Collections.Generic.List<
string>();
708 bool useCompoundFile = GetUseCompoundFile();
717 for (
int i = 0; i < exts.Length; i++)
718 AddIfExists(fileList, name +
"." + exts[i]);
721 if (docStoreOffset != - 1)
725 System.Diagnostics.Debug.Assert(docStoreSegment != null);
726 if (docStoreIsCompoundFile)
733 for (
int i = 0; i < exts.Length; i++)
734 AddIfExists(fileList, docStoreSegment +
"." + exts[i]);
737 else if (!useCompoundFile)
742 for (
int i = 0; i < exts.Length; i++)
743 AddIfExists(fileList, name +
"." + exts[i]);
747 if (delFileName != null && (delGen >= YES || dir.FileExists(delFileName)))
749 fileList.Add(delFileName);
755 for (
int i = 0; i < normGen.Length; i++)
757 long gen = normGen[i];
767 if (!hasSingleNormFile && !useCompoundFile)
770 if (dir.FileExists(fileName))
772 fileList.Add(fileName);
776 else if (CHECK_DIR == gen)
779 System.String fileName = null;
784 else if (!hasSingleNormFile)
788 if (fileName != null && dir.FileExists(fileName))
790 fileList.Add(fileName);
795 else if (preLockless || (!hasSingleNormFile && !useCompoundFile))
799 System.String prefix;
804 int prefixLength = prefix.Length;
805 System.String[] allFiles = dir.ListAll();
807 for (
int i = 0; i < allFiles.Length; i++)
809 System.String fileName = allFiles[i];
810 if (filter.
Accept(null, fileName) && fileName.Length > prefixLength && System.Char.IsDigit(fileName[prefixLength]) && fileName.StartsWith(prefix))
812 fileList.Add(fileName);
823 private void ClearFiles()
835 if (GetUseCompoundFile())
840 catch (System.IO.IOException)
845 System.String docStore;
847 if (docStoreOffset != - 1)
848 docStore =
"->" + docStoreSegment;
852 return name +
":" + cfs + (this.dir == dir?
"":
"x") + docCount + docStore;
858 public override bool Equals(System.Object obj)
860 if (
this == obj)
return true;
864 SegmentInfo other = (SegmentInfo) obj;
865 return other.
dir == dir && other.
name.Equals(name);
870 public override int GetHashCode()
872 return dir.GetHashCode() + name.GetHashCode();