diff --git a/src/UmbracoExamine/BaseUmbracoIndexer.cs b/src/UmbracoExamine/BaseUmbracoIndexer.cs index f37116e037..22e89bf94a 100644 --- a/src/UmbracoExamine/BaseUmbracoIndexer.cs +++ b/src/UmbracoExamine/BaseUmbracoIndexer.cs @@ -298,17 +298,14 @@ namespace UmbracoExamine /// protected bool CanInitialize() { - if (_configBased) + //We need to check if we actually can initialize, if not then don't continue + if (_configBased + && (ApplicationContext.Current == null + || ApplicationContext.Current.IsConfigured == false + || ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured == false)) { - //We need to check if we actually can initialize, if not then don't continue - if (ApplicationContext.Current == null - || ApplicationContext.Current.IsConfigured == false - || ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured == false) - { - return false; - } + return false; } - return true; } diff --git a/src/UmbracoExamine/LocalStorage/LocalTempStorageDirectoryTracker.cs b/src/UmbracoExamine/LocalStorage/LocalTempStorageDirectoryTracker.cs index 7cda2cee50..d7c65edced 100644 --- a/src/UmbracoExamine/LocalStorage/LocalTempStorageDirectoryTracker.cs +++ b/src/UmbracoExamine/LocalStorage/LocalTempStorageDirectoryTracker.cs @@ -6,7 +6,7 @@ namespace UmbracoExamine.LocalStorage { internal class LocalTempStorageDirectoryTracker { - private readonly static LocalTempStorageDirectoryTracker Instance = new LocalTempStorageDirectoryTracker(); + private static readonly LocalTempStorageDirectoryTracker Instance = new LocalTempStorageDirectoryTracker(); private readonly ConcurrentDictionary _directories = new ConcurrentDictionary(); public static LocalTempStorageDirectoryTracker Current diff --git a/src/UmbracoExamine/UmbracoExamineSearcher.cs b/src/UmbracoExamine/UmbracoExamineSearcher.cs index 71df2b7dff..4ca6c7f310 100644 --- a/src/UmbracoExamine/UmbracoExamineSearcher.cs +++ b/src/UmbracoExamine/UmbracoExamineSearcher.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.IO; using System.Linq; using System.Security; @@ -17,6 +18,7 @@ using Examine.LuceneEngine.SearchCriteria; using Lucene.Net.Analysis; using Umbraco.Core.Logging; using UmbracoExamine.LocalStorage; +using Directory = Lucene.Net.Store.Directory; namespace UmbracoExamine @@ -27,22 +29,42 @@ namespace UmbracoExamine public class UmbracoExamineSearcher : LuceneSearcher { - private volatile Lucene.Net.Store.Directory _localTempDirectory; - private static readonly object Locker = new object(); - private string _localTempPath = null; + private Lazy _localTempDirectory; private LocalStorageType _localStorageType = LocalStorageType.Sync; - - #region Constructors + private string _name; + private readonly bool _configBased = false; /// - /// Default constructor + /// Default constructor for config based construction /// + [EditorBrowsable(EditorBrowsableState.Never)] public UmbracoExamineSearcher() : base() { + _configBased = true; } - private string _name; + /// + /// Constructor to allow for creating an indexer at runtime + /// + /// + /// + public UmbracoExamineSearcher(DirectoryInfo indexPath, Analyzer analyzer) + : base(indexPath, analyzer) + { + _configBased = false; + } + + /// + /// Constructor to allow for creating an indexer at runtime + /// + /// + /// + public UmbracoExamineSearcher(Directory luceneDirectory, Analyzer analyzer) + : base(luceneDirectory, analyzer) + { + _configBased = false; + } /// /// we override name because we need to manually set it if !CanInitialize() @@ -50,13 +72,14 @@ namespace UmbracoExamine /// public override string Name { - get - { - return _name; - } + get { return _name; } } - + /// + /// Method used for initializing based on a configuration based searcher + /// + /// + /// public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config) { if (name == null) throw new ArgumentNullException("name"); @@ -103,36 +126,33 @@ namespace UmbracoExamine var configuredPath = indexSet.IndexPath; var tempPath = localStorageDir.GetLocalStorageDirectory(config, configuredPath); if (tempPath == null) throw new InvalidOperationException("Could not resolve a temp location from the " + localStorageDir.GetType() + " specified"); - _localTempPath = tempPath.FullName; + var localTempPath = tempPath.FullName; _localStorageType = attemptUseTempStorage.Result; + + //initialize the lazy callback + _localTempDirectory = new Lazy(() => + { + switch (_localStorageType) + { + case LocalStorageType.Sync: + var fsDir = base.GetLuceneDirectory() as FSDirectory; + if (fsDir != null) + { + return LocalTempStorageDirectoryTracker.Current.GetDirectory( + new DirectoryInfo(localTempPath), + fsDir); + } + return base.GetLuceneDirectory(); + case LocalStorageType.LocalOnly: + return new SimpleFSDirectory(new DirectoryInfo(localTempPath)); + default: + throw new ArgumentOutOfRangeException(); + } + }); } } } - /// - /// Constructor to allow for creating an indexer at runtime - /// - /// - /// - - public UmbracoExamineSearcher(DirectoryInfo indexPath, Analyzer analyzer) - : base(indexPath, analyzer) - { - } - - /// - /// Constructor to allow for creating an indexer at runtime - /// - /// - /// - - public UmbracoExamineSearcher(Lucene.Net.Store.Directory luceneDirectory, Analyzer analyzer) - : base(luceneDirectory, analyzer) - { - } - - #endregion - /// /// Returns true if the Umbraco application is in a state that we can initialize the examine indexes /// @@ -141,29 +161,15 @@ namespace UmbracoExamine protected bool CanInitialize() { //We need to check if we actually can initialize, if not then don't continue - if (ApplicationContext.Current == null - || !ApplicationContext.Current.IsConfigured - || !ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured) + if (_configBased + && (ApplicationContext.Current == null + || ApplicationContext.Current.IsConfigured == false + || ApplicationContext.Current.DatabaseContext.IsDatabaseConfigured == false)) { return false; } return true; - } - - /// - /// Override in order to set the nodeTypeAlias field name of the underlying SearchCriteria to __NodeTypeAlias - /// - /// - /// - /// - public override ISearchCriteria CreateSearchCriteria(string type, BooleanOperation defaultOperation) - { - throw new NotImplementedException("Fix strongly typed CreateSearchCriteria"); - - var criteria = base.CreateSearchCriteria(type, defaultOperation) as LuceneSearchCriteria; - //criteria.NodeTypeAliasField = UmbracoContentIndexer.NodeTypeAliasFieldName; - return criteria; - } + } /// /// Returns a list of fields to search on, this will also exclude the IndexPathFieldName and node type alias @@ -173,53 +179,17 @@ namespace UmbracoExamine { var fields = base.GetSearchFields(); return fields - .Where(x => x != UmbracoContentIndexer.IndexPathFieldName) - .Where(x => x != UmbracoContentIndexer.NodeTypeAliasFieldName) + .Where(x => x != BaseUmbracoIndexer.IndexPathFieldName) + .Where(x => x != LuceneIndexer.NodeTypeAliasFieldName) .ToArray(); } - protected override Lucene.Net.Store.Directory GetLuceneDirectory() + protected override Directory GetLuceneDirectory() { - throw new NotImplementedException("Fix local storage stuff"); - - ////local temp storage is not enabled, just return the default - //if (_localTempPath == null) return base.GetLuceneDirectory(); - - ////local temp storage is enabled, configure the local directory instance - //if (_localTempDirectory == null) - //{ - // lock (Locker) - // { - // if (_localTempDirectory == null) - // { - // switch (_localStorageType) - // { - // case LocalStorageType.Sync: - // var fsDir = base.GetLuceneDirectory() as FSDirectory; - // if (fsDir != null) - // { - // _localTempDirectory = LocalTempStorageDirectoryTracker.Current.GetDirectory( - // new DirectoryInfo(_localTempPath), - // fsDir); - // } - // else - // { - // return base.GetLuceneDirectory(); - // } - // break; - // case LocalStorageType.LocalOnly: - // _localTempDirectory = DirectoryTracker.Current.GetDirectory(new DirectoryInfo(_localTempPath)); - // break; - // default: - // throw new ArgumentOutOfRangeException(); - // } - - - // } - // } - //} - - //return _localTempDirectory; + //local temp storage is not enabled, just return the default + return _localTempDirectory.IsValueCreated == false + ? base.GetLuceneDirectory() + : _localTempDirectory.Value; } } }