|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS Indexes Indexed LINQ
Optimizing the performance of LINQ queries using in-memory indexes
By: Aaron Erickson
Aug. 1, 2007 06:30 PM
Build the Index Dictionary<int, List<T>> In our actual implementation, we'll look up by int rather than string for the index. We use int because int is what the GetHashCode() method returns from all objects in .NET. One of the keys to making this work is that all objects support GetHashCode() - which lets us, while not uniquely identify an object, effectively partition it into very small groups. Of course, there may be more than one index in our collection. So in actuality, our implementation will use a Dictionary of indexes, which gives us the following structure in the end:
protected Dictionary<string, Dictionary<int, List<T>>> _indexes Building the indexes on the construction of the collection is the next step. When we build a new IndexableCollection<T>, we need to reflect what we're collecting, find out which properties are going to be indexed (by looking for the presence of the IndexableAttribute), and create the index accordingly. The following routine, called from the constructor, accomplishes that goal:
protected void BuildIndexes()
Intercept Collection Adds
public new void Add(T newItem) The mechanics of Add are to iterate through the known indexes, and using reflection, retrieve the hash code of the appropriate property value for each field on the object we're going to index. Once we have that value (hashCode above), we look in the index to see if we have an entry yet for that hash code. If we do, we simple add the item to the list of entries that have the given hash code. If not, we create a new entry in that index based on the hash code. Once we're done, we call the Add routine in the base class so that we have the normal behavior of add implemented. We can now go ahead and add a couple of helper methods that would help anyone trying to implement an index:
public bool PropertyHasIndex(string propName) We need to make these public since anything that might want to implement an index on this is going to need to be able to see if a property has an index and retrieve the index if a given index exists. MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||