public void Initialize(String directoryProviderName, IDictionary<string, string> properties, ISearchFactoryImplementor searchFactory) { DirectoryInfo indexDir = DirectoryProviderHelper.DetermineIndexDir(directoryProviderName, (IDictionary) properties); try { bool create = !IndexReader.IndexExists(indexDir.FullName); indexName = indexDir.FullName; directory = FSDirectory.Open(indexDir); LockFactory lockFactory = DirectoryProviderHelper.CreateLockFactory(indexDir, properties); directory.SetLockFactory(lockFactory); if (create) { IndexWriter iw = new IndexWriter(directory, new StandardAnalyzer(), create, new KeepOnlyLastCommitDeletionPolicy(), IndexWriter.MaxFieldLength.UNLIMITED); iw.Close(); } //searchFactory.RegisterDirectoryProviderForLocks(this); } catch (IOException e) { throw new HibernateException("Unable to initialize index: " + directoryProviderName, e); } }
public void Initialize(string directoryProviderName, IDictionary<string, string> properties, ISearchFactoryImplementor searchFactory) { this.properties = properties; this.directoryProviderName = directoryProviderName; // source guessing source = DirectoryProviderHelper.GetSourceDirectory(Environment.SourceBase, Environment.Source, directoryProviderName, (IDictionary) properties); if (source == null) { throw new ArgumentException("FSMasterDirectoryProvider requires a viable source directory"); } log.Debug("Source directory: " + source); indexDir = DirectoryProviderHelper.DetermineIndexDir(directoryProviderName, (IDictionary) properties); log.Debug("Index directory: " + indexDir); try { // NB Do we need to do this since we are passing the create flag to Lucene? bool create = !IndexReader.IndexExists(indexDir.FullName); if (create) { log.DebugFormat("Index directory not found, creating '{0}'", indexDir.FullName); indexDir.Create(); } indexName = indexDir.FullName; directory = FSDirectory.GetDirectory(indexName, create); LockFactory lockFactory = DirectoryProviderHelper.CreateLockFactory(indexDir, properties); directory.SetLockFactory(lockFactory); if (create) { indexName = indexDir.FullName; IndexWriter iw = new IndexWriter(directory, new StandardAnalyzer(), create); iw.Close(); } } catch (IOException e) { throw new HibernateException("Unable to initialize index: " + directoryProviderName, e); } this.searchFactory = searchFactory; }
public void Start() { string refreshPeriod = properties.ContainsKey("refresh") ? properties["refresh"] : "3600"; long period; if (!long.TryParse(refreshPeriod, out period)) { period = 3600; log.Warn("Error parsing refresh period, defaulting to 1 hour"); } log.DebugFormat("Refresh period {0} seconds", period); period *= 1000; // per second try { bool create; DirectoryInfo subDir = new DirectoryInfo(Path.Combine(indexName, "1")); create = !IndexReader.IndexExists(subDir.FullName); directory1 = FSDirectory.GetDirectory(subDir.FullName, create); LockFactory lockFactory = DirectoryProviderHelper.CreateLockFactory(subDir, properties); directory1.SetLockFactory(lockFactory); if (create) { log.DebugFormat("Initialize index: '{0}'", subDir.FullName); IndexWriter iw1 = new IndexWriter(directory1, new StandardAnalyzer(), create); iw1.Close(); } subDir = new DirectoryInfo(Path.Combine(indexName, "2")); create = !IndexReader.IndexExists(subDir.FullName); directory2 = FSDirectory.GetDirectory(subDir.FullName, create); lockFactory = DirectoryProviderHelper.CreateLockFactory(subDir, properties); directory2.SetLockFactory(lockFactory); if (create) { log.DebugFormat("Initialize index: '{0}'", subDir.FullName); IndexWriter iw2 = new IndexWriter(directory2, new StandardAnalyzer(), create); iw2.Close(); } string current1Marker = Path.Combine(indexName, "current1"); string current2Marker = Path.Combine(indexName, "current2"); if (File.Exists(current1Marker)) { current = 1; } else if (File.Exists(current2Marker)) { current = 2; } else { // no default log.Debug("Setting directory 1 as current"); current = 1; DirectoryInfo srcDir = new DirectoryInfo(source); DirectoryInfo destDir = new DirectoryInfo(Path.Combine(indexName, current.ToString())); int sourceCurrent = -1; if (File.Exists(Path.Combine(srcDir.Name, "current1"))) { sourceCurrent = 1; } else if (File.Exists(Path.Combine(srcDir.Name, "current2"))) { sourceCurrent = 2; } if (sourceCurrent != -1) { try { FileHelper.Synchronize( new DirectoryInfo(Path.Combine(source, sourceCurrent.ToString())), destDir, true); } catch (IOException e) { throw new HibernateException("Umable to synchonize directory: " + indexName, e); } } try { File.Create(current1Marker).Dispose(); } catch (IOException e) { throw new HibernateException("Unable to create the directory marker file: " + indexName, e); } } log.Debug("Current directory: " + current); } catch (IOException e) { throw new HibernateException("Unable to initialize index: " + directoryProviderName, e); } task = new TriggerTask(this, source, indexName); timer = new Timer(task.Run, null, period, period); }