Using collection indexes to find items
January 24th, 2006 by Andi VajdaDuring last week’s sprints I was asked to see how easy it was to import mail into Chandler from a local mailbox. Thanks to Python’s mailbox and email packages, the mailbox parsing was trivial. Similarly, Chandler’s domain model can represent email items and has a number of APIs that make creating such items from a raw email string very easy.
Unexpectedly, importing mail messages was very slow however, and was getting worse as more messages were added to the repository. The first cause of slowness was easily resolved by turning off ‘bz2′ compression of all the text LOBs being created.
The second cause of slowness was more surprising though. It had to do with looking up existing email address items from the email addresses occurring in the headers of the messages being imported. The EmailAddress item class defined here
has a class method called getEmailAddress() that will linearly iterate all EmailAddress instances every time such an item is sought. The mailbox I was trying to import contains about 6,500 mails with 800 unique email addresses. Linearly searching them won’t scale.
In the early days of the repository we had planned on having a query system and Ted even implemented a query language not unlike one you’d encounter in an SQL database. Last year, while working on Chandler 0.6, it occurred to us that a formal query language in a Python-based system was somewhat redundant and that we should be using computed collections instead of queries altogether to accomplish the same goals . Hence abstract sets and their item wrappers were implemented.
By combining abstract sets, collections and indexes - something I had added to bi-directional reference collections during the Chandler 0.5 release - we realized we could get very decent item look-up performance as well as cache the computed aspect of abstract sets making membership tests and iteration also considerably faster.
This technique can be illustrated with the example below taken from the EmailAddress index I added during last week’s sprints.
- First, a collection needs to be setup. This can be as simple as a bi-directional reference collection, an abstract collection of all items of a given kind or a more complicated computed collection combining or filtering other collections. In the EmailAddress example, I just created a KindCollection instance based on the EmailAddress kind. In the app parcel I added yet another collection called
emailAddressCollection.emailAddressCollection = \ KindCollection.update(parcel, 'emailAddressCollection', kind=pim.mail.EmailAddress.getKind(view), recursive=True) - Then, a sorted index needs be added to the collection. I wanted to be able to find existing email addresses such that no two email address items in the collection have the same lowercase internet email address string. For this purpose, I added the
_compareAddr()method to the EmailAddress class in the mail parcel:def _compareAddr(self, other): return cmp(self.emailAddress.lower(), other.emailAddress.lower())and the index creation call after the code creating the collection:
emailAddressCollection.rep.addIndex('emailAddress', 'compare', compare='_compareAddr')which creates a ‘compare’ index called ‘emailAddress’, an index calling a method called ‘_compareAddr’
on the items it is comparing. - And finally to use this collection and the index to find items I added a
findEmailAddress()class method to the EmailAddress class:@classmethod def findEmailAddress(cls, view, emailAddress): collection = schema.ns("osaf.app", view).emailAddressCollection.rep emailAddress = emailAddress.lower() def compareAddr(key): return cmp(emailAddress, view[key].emailAddress.lower()) uuid = collection.findInIndex('emailAddress', 'exact', compareAddr) if uuid is None: return None return view[uuid]The
findInIndex()call looks for an exact match as per thecompareAddrlocal function which compares lowercase versions of internet address strings.
The technique above replaces the linear search with a binary search yielding a very noticeable performance improvement in the overall importing of email messages into the repository.
free viagra
buy viagra online
generic viagra
how does viagra work
cheap viagra
buy viagra
buy viagra online inurl
viagra 6 free samples
viagra online
viagra for women
viagra side effects
female viagra
natural viagra
online viagra
cheapest viagra prices
herbal viagra
alternative to viagra
buy generic viagra
purchase viagra online
free viagra without prescription
viagra attorneys
free viagra samples before buying
buy generic viagra cheap
viagra uk
generic viagra online
try viagra for free
generic viagra from india
fda approves viagra
free viagra sample
what is better viagra or levitra
discount generic viagra online
viagra cialis levitra
viagra dosage
viagra cheap
viagra on line
best price for viagra
free sample pack of viagra
viagra generic
viagra without prescription
discount viagra
gay viagra
mail order viagra
viagra inurl
generic viagra online paypal
generic viagra overnight
generic viagra online pharmacy
generic viagra uk
buy cheap viagra online uk
suppliers of viagra
how long does viagra last
viagra sex
generic viagra soft tabs
generic viagra 100mg
buy viagra onli
generic viagra online without prescription
viagra energy drink
cheapest uk supplier viagra
viagra cialis
generic viagra safe
viagra professional
viagra sales
viagra free trial pack
viagra lawyers
over the counter viagra
best price for generic viagra
viagra jokes
buying viagra
viagra samples
viagra sample
cialis
generic cialis
cheapest cialis
buy cialis online
buying generic cialis
cialis for order
what are the side effects of cialis
buy generic cialis
what is the generic name for cialis
cheap cialis
cialis online
buy cialis
cialis side effects
how long does cialis last
cialis forum
cialis lawyer ohio
cialis attorneys
cialis attorney columbus
cialis injury lawyer ohio
cialis injury attorney ohio
cialis injury lawyer columbus
prices cialis
cialis lawyers
viagra cialis levitra
cialis lawyer columbus
online generic cialis
daily cialis
cialis injury attorney columbus
cialis attorney ohio
cialis cost
cialis professional
cialis super active
how does cialis work
what does cialis look like
cialis drug
viagra cialis
cialis to buy new zealand
cialis without prescription
free cialis
cialis soft tabs
discount cialis
cialis generic
generic cialis from india
cheap cialis sale online
cialis daily
cialis reviews
cialis generico
how can i take cialis
cheap cialis si
cialis vs viagra
levitra
generic levitra
levitra attorneys
what is better viagra or levitra
viagra cialis levitra
levitra side effects
buy levitra
levitra online
levitra dangers
how does levitra work
levitra lawyers
what is the difference between levitra and viagra
levitra versus viagra
which works better viagra or levitra
buy levitra and overnight shipping
levitra vs viagra
canidan pharmacies levitra
how long does levitra last
viagra cialis levitra
levitra acheter
comprare levitra
levitra ohne rezept
levitra 20mg
levitra senza ricetta
cheapest generic levitra
levitra compra
cheap levitra
levitra overnight
levitra generika
levitra kaufen

