Re: ordered and filtered fault efficiency
Re: ordered and filtered fault efficiency
- Subject: Re: ordered and filtered fault efficiency
- From: OC <email@hidden>
- Date: Sun, 15 Feb 2015 09:19:43 +0100
Chuck,
On 15. 2. 2015, at 1:56, Chuck Hill <email@hidden> wrote:
> === 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?
It is used elsewhere too, and although it is not that pressing as lastValidPriceOffer (which is used at more places), it would help to speed it up, too.
> I am assuming this is a large list.
Alas, it is. When written and tested years ago, there used be tens of items max; today they use the code with thousands of items, and that's a big problem which I have to solve ASAP :)
> 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.
This is precisely what I don't know howto, can you help? I found no way to force the SELECT caused by firing a relationship fault to use any ORDER BY.
Or do you suggest that I don't use a modelled relationship at all, implementing something like
=== [*]
private cache
def orderedPriceOffers {
if (cache) return cache
cache=this.editingContext.objectsWithFetchSpecification( yadda yadda incl sort orderings )
}
def addPriceOffer {
cache=nil
...
}
===
I thought caching objects this way was an extremely big no-no? Besides even at the first look I can see lots of possible catches and gotchas, with different ECs, with objects inserted but not-yet-saved, etc...
>> 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.
Hmmm... actually, does this differ anyhow from caching the GID? (Save for the first access, for cached GID is not stored in the DB, and thus forces search once.)
A to-one is simply cached PK, stored in DB, or am I overlooking something?
>> 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.
Well there must be _some_ reason they used NSArrays instead of NSSets I guess.
> Don’t fight EOF. Un-ordered set are un-ordered sets.
Anyway, I don't really want to fight EOF; rather I would co-operate with it to get the best result.
In this case the best result would be
(a) to fetch sorted with ORDER BY into an array
(b) to add newly created objects to the end of the array
Seems to me somewhat too far at the ubiquitous side for EOF/Wonder not to support anything remotely similar and leaving me completely on me own with something like [*]? But perhaps it's just the way the things are :)
>> 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.
Well I sort of never thought of that :)
That aside, for this app it is somewhat inconvenient to change the DB schema. For the next release I will do that.
Thanks a lot,
OC
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden