Hi,
On 2015-02-13, 11:34 PM, "OC" wrote:
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,
To speed up lastValidPriceOffer or for some other reason? I am assuming this
is a large list. You could sort and cache on the EO and invalidate the cache
if the relationship changes. Sorting at the database is usually faster,
especially if there is a usable index.
and it is a must to speed up lastValidPriceOffer -- very considerably,
preferrably to O(1) if possible.
I'd model a to-one and update this when the priceOffers relationship changes.
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.
Except that, as you note below, relationships in EOF are unordered sets. EOF
makes no guarantees about maintain an ordering in the relationship.
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?
Don't fight EOF. Un-ordered set are un-ordered sets.
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.
Why not just model it? It is a real thing.
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,
Not sure that I helped much. :-)
Chuck
_______________________________________________
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]