Hello there,

I have bumped into a bottleneck in some pretty ancient parts of my code, and am 
seeking an advice how to fix it. The bottleneck is caused by 
sorting-and-searching objects of 1:N relationships:

=== my EO code ===
    public NSArray orderedPriceOffers {
        NSArray offers=priceOffers() // this is a modelled relationship
        if (offers==null || offers.count<2) return offers
        try {
            offers=offers.sortedArrayUsingComparator(new OCSDateComparator())
        } catch (Exception exc) {
            ... never happens anyway, no need to show ...
        }
        return offers
    }
    public DBPriceOffer lastValidPriceOffer {
        NSArray a=orderedPriceOffers()
        if (a==null || a.count==0) return null
        for (int n=a.count-1;n>=0;n--) {
            DBPriceOffer po=(DBPriceOffer)a.objectAtIndex(n)
            if (po.validOffer()) // this is a modelled boolean attribute
               return po
        }
        return null
    }
...
@OCStandard class OCSDateComparator extends NSComparator {
    public int compare(Object left,Object right) {
        DBPriceOffer l=(DBPriceOffer)left,r=(DBPriceOffer)right
        return l.creationDate().compare(r.creationDate()) // modelled timestamp 
attribute, always set creation-time
    }
}
===

It would be very beneficial to speed up orderedPriceOffers, and it is a must to 
speed up lastValidPriceOffer -- very considerably, preferrably to O(1) if 
possible.

What's the best approach here?

Note that the price offers are always sorted by their creation date -- never 
otherwise --, which effectively means new objects are always added to the end 
of the sorted array, never ever inserted. Also, price offers are not editable; 
once stored, they never change, both validOffer and creationDate attributes 
stay unchanged forever (other ones too, which is irrelevant here).

It seems to me, given this business logic, it would be best
- to fetch the fault using a sort ordering, to ensure it fetches appropriately 
ordered
- whenever a new object is added to an already fetched relationship, to add it 
to the end of the array the EO stack maintains.

I don't see how to do that, though? I went through lists and found it is a big 
no-no to attempt something like that (e.g., 
http://lists.apple.com/archives/webobjects-dev/2007/Aug/msg00432.html), but I 
think the reasons why not do not apply in my case. Anyway... what's the best 
way to solve this?

As for lastValidPriceOffer, I tried to cache the object's permanentGlobalID 
when found or added new valid one (caching the object itself failed with 
different ECs), but it still does not work well, and is terribly ugly and 
error-prone.

I suppose such problems must be WebObjects-101, though I can't recall I've 
bumped into this kind of problem before -- seems I has been lucky that none of 
similar constructs before happened to be a bottleneck.

Will be grateful for any advice,
OC


 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to