19 using System.Collections;
20 using System.Collections.Specialized;
21 using System.Configuration;
22 using System.Diagnostics;
25 using Lucene.Net.Analysis;
26 using Lucene.Net.Analysis.Standard;
27 using Lucene.Net.Documents;
28 using Lucene.Net.Index;
29 using Lucene.Net.Store;
30 using Lucene.Net.Distributed;
32 namespace Lucene.Net.Distributed.Configuration
66 private static readonly
string CURRENTINDEX =
"currentIndex";
67 private static readonly
string INDEX_A =
"indexA";
68 private static readonly
string INDEX_B =
"indexB";
69 private static readonly
string TOGGLE =
"toggle";
70 private string _strLocalPath;
71 private string _strStatusDir;
72 private string _strIndexAPath;
73 private string _strIndexBPath;
74 private bool _bIndexChanged=
false;
76 private int _mergeFactor = (ConfigurationManager.AppSettings[
"IndexMergeFactor"] != null ? Convert.ToInt32(ConfigurationManager.AppSettings[
"IndexMergeFactor"]) : 5);
77 private int _maxMergeDocs = (ConfigurationManager.AppSettings[
"IndexMaxMergeDocs"] != null ? Convert.ToInt32(ConfigurationManager.AppSettings[
"IndexMaxMergeDocs"]) : 9999999);
89 this._strLocalPath=strLocalPath;
90 this.LoadValues(node);
100 this._strStatusDir=sStatusDir;
110 internal void LoadValues(XmlNode node)
112 foreach (XmlNode c
in node.ChildNodes)
114 if (c.Name.ToLower()==
"targetpath")
116 this._strIndexAPath = c.Attributes[
"indexA"].Value;
117 this._strIndexBPath = c.Attributes[
"indexB"].Value;
119 else if (c.Name.ToLower()==
"statusdir")
121 this._strStatusDir = c.Attributes[
"value"].Value;
124 this.CheckValidConfiguration(node);
128 #region Public properties
132 public string LocalPath
134 get {
return this._strLocalPath;}
139 public string StatusDirectory
141 get {
return this._strStatusDir;}
151 string input=(this.GetCurrentIndex());
180 get {
return (!this.GetToggle() && (this.LocalIndexVersion!=this.TargetIndexVersion));}
187 public bool HasChanged
189 get {
return this.GetToggle();}
195 public string CopyTargetPath
201 #region Public methods
211 if (this.CanCopy && this.CopyTargetPath!=
"")
213 this.DeleteDirectoryFiles(this.CopyTargetPath);
214 this.CopyDirectory(this._strLocalPath, this.CopyTargetPath);
234 public bool CopyIncremental()
238 if (this.CanCopy && this.CopyTargetPath!=
"")
240 this.CopyDirectoryIncremental(this._strLocalPath, this.CopyTargetPath);
260 public void ProcessLocalIndexDeletes(NameValueCollection nvcDeleteCollection)
265 string[] arKeys = nvcDeleteCollection.AllKeys;
267 for (
int k=0;k<arKeys.Length;k++)
269 string[] arKeyValues=nvcDeleteCollection.GetValues(arKeys[k]);
270 for (
int v=0;v<arKeyValues.Length;v++)
271 xDelete=idxDeleter.
DeleteDocuments(
new Term(arKeys[k].ToString(),arKeyValues[v].ToString()));
283 public void ProcessLocalIndexAdditions(
Analyzer oAnalyzer, Hashtable htAddDocuments,
bool bCompoundFile)
285 IndexWriter idxWriter = this.GetIndexWriter(this._strLocalPath, oAnalyzer, bCompoundFile);
286 idxWriter.SetMergeFactor(5);
287 idxWriter.SetMaxMergeDocs(9999999);
289 foreach (DictionaryEntry de
in htAddDocuments)
293 idxWriter.AddDocument(d,a);
301 public void IndexRefresh()
305 this.SetCurrentIndex(this.IndexSettingRefresh);
306 this.SetToggle(
false);
313 public void UpdateRefresh()
315 this.SetToggle(
true);
319 #region Private properties
323 private string CurrentIndexFile
325 get {
return (this._strStatusDir+(this._strStatusDir.EndsWith(
@"\") ?
"" :
@"\")+CURRENTINDEX);}
330 private string ToggleFile
332 get {
return (this._strStatusDir+(this._strStatusDir.EndsWith(
@"\") ?
"" :
@"\")+TOGGLE);}
337 #region Private methods
344 private void CheckValidConfiguration(XmlNode node)
346 if (this._strLocalPath == null)
throw new ConfigurationErrorsException(
"CurrentIndex local path invalid: "+Environment.NewLine+node.OuterXml);
347 if (this._strStatusDir == null)
throw new ConfigurationErrorsException(
"CurrentIndex statusDir invalid: " + Environment.NewLine + node.OuterXml);
348 if (this._strIndexAPath == null)
throw new ConfigurationErrorsException(
"CurrentIndex indexA invalid: " + Environment.NewLine + node.OuterXml);
349 if (this._strIndexBPath == null)
throw new ConfigurationErrorsException(
"CurrentIndex indexB invalid: " + Environment.NewLine + node.OuterXml);
356 private bool GetToggle()
362 if (!File.Exists(
this.ToggleFile))
364 this.SetToggle(
false);
368 StreamReader sr = File.OpenText(this.ToggleFile);
369 input = sr.ReadLine();
371 bValue = (input.ToLower()==
"true" ?
true :
false);
385 private string GetCurrentIndex()
390 if (!File.Exists(
this.CurrentIndexFile))
397 StreamReader sr = File.OpenText(this.CurrentIndexFile);
398 input = sr.ReadLine();
413 private void SetCurrentIndex(
IndexSetting eIndexSetting)
417 StreamWriter sw = File.CreateText(this.CurrentIndexFile);
418 sw.WriteLine((eIndexSetting==
IndexSetting.IndexA ? CurrentIndex.INDEX_A : CurrentIndex.INDEX_B));
434 private IndexWriter GetIndexWriter(
string indexPath,
Analyzer oAnalyzer,
bool bCompoundFile)
436 bool bExists = System.IO.
Directory.Exists(indexPath);
438 System.IO.Directory.CreateDirectory(indexPath);
441 idxWriter.SetUseCompoundFile(bCompoundFile);
449 private void SetToggle(
bool bValue)
453 StreamWriter sw = File.CreateText(this.ToggleFile);
454 sw.WriteLine(bValue.ToString());
456 this._bIndexChanged=bValue;
467 private long LocalIndexVersion
474 private long TargetIndexVersion
483 private void DeleteDirectoryFiles(
string directoryPath)
487 if(!System.IO.Directory.Exists(directoryPath))
489 DirectoryInfo di =
new DirectoryInfo(directoryPath);
490 FileInfo[] arFi = di.GetFiles();
491 foreach(FileInfo fi
in arFi)
505 private void CopyDirectory(
string sourceDirPath,
string destDirPath)
509 if(destDirPath[destDirPath.Length-1]!=Path.DirectorySeparatorChar)
510 destDirPath+=Path.DirectorySeparatorChar;
511 if(!System.IO.Directory.Exists(destDirPath)) System.IO.Directory.CreateDirectory(destDirPath);
512 Files=System.IO.Directory.GetFileSystemEntries(sourceDirPath);
513 foreach(
string Element
in Files)
516 if(System.IO.Directory.Exists(Element))
517 CopyDirectory(Element,destDirPath+Path.GetFileName(Element));
520 File.Copy(Element,destDirPath+Path.GetFileName(Element),
true);
530 private void CopyDirectoryIncremental(
string sourceDirPath,
string destDirPath)
534 if(destDirPath[destDirPath.Length-1]!=Path.DirectorySeparatorChar)
535 destDirPath+=Path.DirectorySeparatorChar;
536 Files=System.IO.Directory.GetFileSystemEntries(sourceDirPath);
537 if(!System.IO.Directory.Exists(destDirPath))
539 System.IO.Directory.CreateDirectory(destDirPath);
540 foreach(
string Element
in Files)
543 if(System.IO.Directory.Exists(Element))
544 CopyDirectory(Element,destDirPath+Path.GetFileName(Element));
547 File.Copy(Element,destDirPath+Path.GetFileName(Element),
true);
552 foreach(
string Element
in Files)
554 if(System.IO.Directory.Exists(Element))
556 CopyDirectoryIncremental(Element,destDirPath+Path.GetFileName(Element));
560 if (System.IO.File.Exists(destDirPath+Path.GetFileName(Element)))
561 this.CopyFileIncremental(Element, destDirPath+Path.GetFileName(Element));
563 File.Copy(Element,destDirPath+Path.GetFileName(Element),
true);
575 private void CopyFileIncremental(
string filepath1,
string filepath2)
577 FileInfo fi1 =
new FileInfo(filepath1);
578 FileInfo fi2 =
new FileInfo(filepath2);
579 if ((fi1.LastWriteTime!=fi2.LastWriteTime)||(fi1.Length!=fi2.Length))
580 File.Copy(filepath1,filepath2,
true);
584 #region Static methods
593 switch (oAnalyzerType)