public IndexedDocumentSources(CopyFromIndexComponent component, DatabaseIndexedDocumentSource database, MemoryIndexedDocumentSource memory) : base(component) { _memorySource = memory; _databaseSource = database; }
protected override void Dispose(bool disposing) { if (_memorySource != null) { _memorySource.Dispose(); _memorySource = null; } if (_databaseSource != null) { _databaseSource.Dispose(); _databaseSource = null; } }
public MemoryIndexedDocumentCache(int cacheSize, MemoryIndexedDocumentSource source) { BuildComponentExceptions.NotNull(source, "source"); if (cacheSize < 0) { throw new ArgumentOutOfRangeException("cacheSize"); } _cacheSize = cacheSize; _documentSource = source; // set up the cache _cacheStorage = new Dictionary <string, WeakReference>( cacheSize, StringComparer.OrdinalIgnoreCase); _cacheQueue = new Queue <string>(cacheSize); }
public MemoryIndexedDocument(MemoryIndexedDocumentSource cache, string file) : this() { BuildComponentExceptions.NotNull(cache, "cache"); BuildComponentExceptions.NotNull(file, "file"); // remember the file this.file = file; // load the document try { //XPathDocument document = new XPathDocument(file, XmlSpace.Preserve); XPathDocument document = new XPathDocument(file); XPathNavigator docNavigator = document.CreateNavigator(); XPathNavigator redirectNode = docNavigator.SelectSingleNode("doc"); // search for value nodes XPathNodeIterator valueNodes = docNavigator.Select(cache.ValueExpression); if ((valueNodes == null || valueNodes.Count == 0) && redirectNode != null) { string redirectPath = redirectNode.GetAttribute( "redirect", String.Empty); if (!String.IsNullOrEmpty(redirectPath)) { redirectPath = Path.GetFullPath( Environment.ExpandEnvironmentVariables(redirectPath)); if (System.IO.File.Exists(redirectPath)) { document = new XPathDocument(redirectPath); docNavigator = document.CreateNavigator(); // search for value nodes valueNodes = docNavigator.Select(cache.ValueExpression); } } } // get the key string for each value node and record it in the index foreach (XPathNavigator valueNode in valueNodes) { XPathNavigator keyNode = valueNode.SelectSingleNode( cache.KeyExpression); if (keyNode == null) { continue; } string key = keyNode.Value; index[key] = valueNode; //if (!index.ContainsKey(key)) //{ // //index.Add(key, valueNode); //} //else //{ //} } } catch (IOException e) { cache.WriteMessage(MessageLevel.Error, String.Format("An access error occurred while attempting to load the file '{0}'. The error message is: {1}", file, e.Message)); } catch (XmlException e) { cache.WriteMessage(MessageLevel.Error, String.Format("The indexed document '{0}' is not a valid XML document. The error message is: {1}", file, e.Message)); } }
private void ProcessIndex(XPathNavigator indexNode, CustomContext context, IDictionary <string, object> globalData) { MemoryIndexedDocumentSource memorySource = null; DatabaseIndexedDocumentSource databaseSource = null; // get the name of the index string name = indexNode.GetAttribute("name", String.Empty); if (String.IsNullOrEmpty(name)) { throw new BuildComponentException("Each index must have a unique name."); } // get the xpath for value nodes string valueXPath = indexNode.GetAttribute("value", String.Empty); if (String.IsNullOrEmpty(valueXPath)) { WriteMessage(MessageLevel.Error, "Each index element must have a value attribute containing an XPath that describes index entries."); } // get the xpath for keys (relative to value nodes) string keyXPath = indexNode.GetAttribute("key", String.Empty); if (String.IsNullOrEmpty(keyXPath)) { WriteMessage(MessageLevel.Error, "Each index element must have a key attribute containing an XPath (relative to the value XPath) that evaluates to the entry key."); } // get the cache size int cache = 10; string cacheValue = indexNode.GetAttribute("cache", String.Empty); if (!String.IsNullOrEmpty(cacheValue)) { cache = Convert.ToInt32(cacheValue); } // create the index memorySource = new MemoryIndexedDocumentSource(_component, keyXPath, valueXPath, context, cache); // search the data directories for entries XPathNodeIterator dataNodes = indexNode.Select("data"); foreach (XPathNavigator dataNode in dataNodes) { string baseValue = dataNode.GetAttribute("base", String.Empty); if (!String.IsNullOrEmpty(baseValue)) { baseValue = Environment.ExpandEnvironmentVariables(baseValue); } bool recurse = false; string recurseValue = dataNode.GetAttribute("recurse", String.Empty); if (!String.IsNullOrEmpty(recurseValue)) { recurse = Convert.ToBoolean(recurseValue); } bool warnOverride = true; string warningValue = dataNode.GetAttribute("warnOverride", String.Empty); if (!String.IsNullOrEmpty(warningValue)) { warnOverride = Convert.ToBoolean(warningValue); } // get the files string files = dataNode.GetAttribute("files", String.Empty); if (String.IsNullOrEmpty(files)) { WriteMessage(MessageLevel.Error, "Each data element must have a files attribute specifying which files to index."); } // if ((files == null) || (files.Length == 0)) throw new BuildComponentException("When instantiating a CopyFromDirectory component, you must specify a directory path using the files attribute."); files = Environment.ExpandEnvironmentVariables(files); WriteMessage(MessageLevel.Info, String.Format( "Searching for files that match '{0}'.", files)); memorySource.AddDocuments(baseValue, files, recurse, false, warnOverride); } WriteMessage(MessageLevel.Info, String.Format( "Indexed {0} elements in {1} files.", memorySource.Count, memorySource.DocumentCount)); globalData.Add(name, this); _documentSources[name] = new IndexedDocumentSources(_component, databaseSource, memorySource); }
private void ProcessCommentsIndex(XPathNavigator indexNode, CustomContext context, IDictionary <string, object> globalData) { MemoryIndexedDocumentSource memorySource = null; DatabaseIndexedDocumentSource databaseSource = null; // get the name of the index string name = indexNode.GetAttribute("name", String.Empty); if (String.IsNullOrEmpty(name)) { throw new BuildComponentException("Each index must have a unique name."); } // get the xpath for value nodes string valueXPath = indexNode.GetAttribute("value", String.Empty); if (String.IsNullOrEmpty(valueXPath)) { WriteMessage(MessageLevel.Error, "Each index element must have a value attribute containing an XPath that describes index entries."); } // get the xpath for keys (relative to value nodes) string keyXPath = indexNode.GetAttribute("key", String.Empty); if (String.IsNullOrEmpty(keyXPath)) { WriteMessage(MessageLevel.Error, "Each index element must have a key attribute containing an XPath (relative to the value XPath) that evaluates to the entry key."); } // get the cache size int cache = 10; string cacheValue = indexNode.GetAttribute("cache", String.Empty); if (!String.IsNullOrEmpty(cacheValue)) { cache = Convert.ToInt32(cacheValue); } HashSet <string> sourceDirs = new HashSet <string>( StringComparer.OrdinalIgnoreCase); // create the index memorySource = new MemoryIndexedDocumentSource(_component, keyXPath, valueXPath, context, cache); // Search for the persistent data sources for entries... XPathNodeIterator sourcesNodes = indexNode.Select("sources"); foreach (XPathNavigator sourcesNode in sourcesNodes) { DataSources dataSources = new DataSources(false, sourcesNode); if (dataSources.IsValid) { // Currently, database is supported for systems only... if (!dataSources.IsSystem && !dataSources.IsDatabase) { dataSources = null; } } else { dataSources = null; } if (dataSources != null && dataSources.Exists) { if (databaseSource == null) { databaseSource = new DatabaseIndexedDocumentSource( _component, keyXPath, valueXPath, context, cache, true); } if (!databaseSource.IsInitialized) { sourceDirs.UnionWith(dataSources.Sources); databaseSource.Initialize(dataSources.OutputDir, false); } else { DatabaseIndexedDocument document = new DatabaseIndexedDocument(true, false, dataSources.OutputDir); if (document.Exists) { sourceDirs.UnionWith(dataSources.Sources); databaseSource.AddDocument(document); } } } } // search the data directories for entries XPathNodeIterator dataNodes = indexNode.Select("data"); foreach (XPathNavigator dataNode in dataNodes) { string baseValue = dataNode.GetAttribute("base", String.Empty); if (!String.IsNullOrEmpty(baseValue)) { baseValue = Environment.ExpandEnvironmentVariables(baseValue); } bool isSystem = false; string systemValue = dataNode.GetAttribute("system", String.Empty); if (!String.IsNullOrEmpty(systemValue)) { isSystem = Convert.ToBoolean(systemValue); } bool recurse = false; string recurseValue = dataNode.GetAttribute("recurse", String.Empty); if (!String.IsNullOrEmpty(recurseValue)) { recurse = Convert.ToBoolean(recurseValue); } bool warnOverride = true; string warningValue = dataNode.GetAttribute("warnOverride", String.Empty); if (!String.IsNullOrEmpty(warningValue)) { warnOverride = Convert.ToBoolean(warningValue); } // get the files string files = dataNode.GetAttribute("files", String.Empty); if (String.IsNullOrEmpty(files)) { WriteMessage(MessageLevel.Error, "Each data element must have a files attribute specifying which files to index."); } // if ((files == null) || (files.Length == 0)) throw new BuildComponentException("When instantiating a CopyFromDirectory component, you must specify a directory path using the files attribute."); files = Environment.ExpandEnvironmentVariables(files); WriteMessage(MessageLevel.Info, String.Format( "Searching for files that match '{0}'.", files)); if (isSystem && sourceDirs.Count != 0 && Directory.Exists(baseValue)) { // For consistent, we make sure all directory paths // end with backslash... string sourceDir = String.Copy(baseValue); if (!sourceDir.EndsWith("\\")) { sourceDir += "\\"; } // If included already in the persistent sources, we // will not load it into memory... if (sourceDirs.Contains(sourceDir)) { continue; } } memorySource.AddDocuments(baseValue, files, recurse, true, warnOverride); } WriteMessage(MessageLevel.Info, String.Format( "Indexed {0} elements in {1} files.", memorySource.Count, memorySource.DocumentCount)); globalData.Add(name, this); _documentSources[name] = new IndexedDocumentSources(_component, databaseSource, memorySource); }