public UriPrefixTable(bool includePortInComparison, bool useWeakReferences) { this.includePortInComparison = includePortInComparison; this.useWeakReferences = useWeakReferences; root = new SegmentHierarchyNode <TItem>(null, useWeakReferences); lookupCache = new HopperCache(HopperSize, useWeakReferences); }
public bool TryLookupUri(Uri uri, HostNameComparisonMode hostNameComparisonMode, out TItem item) { BaseUriWithWildcard key = new BaseUriWithWildcard(uri, hostNameComparisonMode); if (TryCacheLookup(key, out item)) { return(item != null); } using (AsyncLock.TakeLock()) { // exact match failed, perform the full lookup (which will also // catch case-insensitive variations that aren't yet in our cache) bool dummy; SegmentHierarchyNode <TItem> node = FindDataNode( UriSegmenter.ToPath(key.BaseAddress, hostNameComparisonMode, includePortInComparison), out dummy); if (node != null) { item = node.Data; } // We want to cache both positive AND negative results AddToCache(key, item); return(item != null); } }
private SegmentHierarchyNode <TItem> FindOrCreateNode(BaseUriWithWildcard baseUri) { Fx.Assert(baseUri != null, "FindOrCreateNode: baseUri is null"); string[] path = UriSegmenter.ToPath(baseUri.BaseAddress, baseUri.HostNameComparisonMode, _includePortInComparison); SegmentHierarchyNode <TItem> current = _root; for (int i = 0; i < path.Length; ++i) { if (!current.TryGetChild(path[i], out SegmentHierarchyNode <TItem> next)) { next = new SegmentHierarchyNode <TItem>(path[i], _useWeakReferences); current.SetChildNode(path[i], next); } current = next; } return(current); }
public void RegisterUri(Uri uri, HostNameComparisonMode hostNameComparisonMode, TItem item) { Fx.Assert(HostNameComparisonModeHelper.IsDefined(hostNameComparisonMode), "RegisterUri: Invalid HostNameComparisonMode value passed in."); using (AsyncLock.TakeLock()) { // Since every newly registered Uri could alter what Prefixes should have matched, we // should clear the cache of any existing results and start over ClearCache(); BaseUriWithWildcard key = new BaseUriWithWildcard(uri, hostNameComparisonMode); SegmentHierarchyNode <TItem> node = FindOrCreateNode(key); if (node.Data != null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format( SR.DuplicateRegistration, uri))); } node.SetData(item, key); count++; } }
private SegmentHierarchyNode <TItem> FindDataNode(string[] path, out bool exactMatch) { Fx.Assert(path != null, "FindDataNode: path is null"); exactMatch = false; SegmentHierarchyNode <TItem> current = _root; SegmentHierarchyNode <TItem> result = null; for (int i = 0; i < path.Length; ++i) { if (!current.TryGetChild(path[i], out SegmentHierarchyNode <TItem> next)) { break; } else if (next.Data != null) { result = next; exactMatch = (i == path.Length - 1); } current = next; } return(result); }
public bool TryGetChild(string segment, out SegmentHierarchyNode <TData> value) { return(children.TryGetValue(segment, out value)); }
public void SetChildNode(string name, SegmentHierarchyNode <TData> node) { children[name] = node; }