public static void Setup() { s_sortedIndex = new SimpleCacheIndex(IdentityExtractor.Instance, true, null, true); s_unsortedIndex = new SimpleCacheIndex(IdentityExtractor.Instance, false, null, true); s_setKeys = new HashSet(); s_setKeysTenToTwenty = new HashSet(); ICacheEntry entry; for (var key = 50; key < 250; key++) { var value = key % 30; s_setKeys.Add(key); if (value >= 10 && value <= 20) { s_setKeysTenToTwenty.Add(key); } entry = new CacheEntry(key, value); s_sortedIndex.Insert(entry); s_unsortedIndex.Insert(entry); } // Add null value to indexes entry = new CacheEntry(-1000, null); s_sortedIndex.Insert(entry); s_unsortedIndex.Insert(entry); }
/// <summary> /// Add an index to the given dictionary of indexes, keyed by the given /// extractor. Also add the index as a listener to the given cache. /// </summary> /// <param name="extractor"> /// The IValueExtractor object that is used to extract an indexable /// property value from a cache entry. /// </param> /// <param name="ordered"> /// True if the contents of the indexed information should be ordered; /// false otherwise /// </param> /// <param name="comparator"> /// The IComparer object which imposes an ordering on entries in the /// indexed cache or <c>null</c> if the entries' values natural /// ordering should be used. /// </param> /// <param name="cache"> /// The cache that the newly created ICacheIndex will use for /// initialization and listen to for changes. /// </param> /// <param name="dictIndex"> /// The dictionary of indexes that the newly created ICacheIndex will /// be added to. /// </param> public static void AddIndex(IValueExtractor extractor, bool ordered, IComparer comparator, IObservableCache cache, IDictionary dictIndex) { var index = (ICacheIndex)dictIndex[extractor]; if (index == null) { for (int cAttempts = 4; ;) { if (extractor is IIndexAwareExtractor) { index = ((IIndexAwareExtractor)extractor). CreateIndex(ordered, comparator, dictIndex); if (index == null) { return; } } else { index = new SimpleCacheIndex(extractor, ordered, comparator); dictIndex[extractor] = index; } ICacheListener listener = EnsureListener(index); cache.AddCacheListener(listener, null, false); try { // build the index foreach (ICacheEntry entry in cache) { index.Insert(entry); } break; } catch (InvalidOperationException ioe) //collection was modified { cache.RemoveCacheListener(listener); if (--cAttempts == 0) { RemoveIndex(extractor, cache, dictIndex); CacheFactory.Log("Exception occured during index rebuild: " + ioe, CacheFactory.LogLevel.Error); throw; } } } } else if (!(ordered == index.IsOrdered && Equals(comparator, index.Comparer))) { throw new InvalidOperationException("Index for " + extractor + " already exists;" + " remove the index and add it with the new settings"); } }