Re: sort attribute for referenced relation

From: Dirk Olmes (dirk.olme..mx.de)
Date: Thu Nov 14 2002 - 23:52:57 EST

  • Next message: martin ruff: "getting object by primary key"

    Andrus wrote:
    >> Cayenne uses org.objectstyle.cayenne.access.ToManyList for
    >> relationships. Without looking at the source I'd guess that is so
    >> because then you'll be able to map adds to the List to inserts to the
    >> database.
    >
    > Custom implementation was mostly due to the fact that we wanted lazy
    > resolution of to many relationships. Anyway, a few ideas though.

    Well, I just did a quick check and adding directly to the ToManyList
    works, too (well, at least for my simple test case).

    > As of now users shouldn't add objects to the list directly. The right
    > way is to use methods in DataObjects like "addToRelname(Someclass obj)"
    > and "removeFromRelname(Someclass obj)" (kind of like
    > "addObjectToBothSidesOfRelationshipWithKey" in EOF).

    Hmm, just because I'm used to that API I was happy with it. Now that I
    think a little bit more about it I'd love to see both ways working, too.

    > Now, Java has a notion of "unmodifiable" lists (via
    > java.util.Collections wrappers), though this is dumb and not obvious
    > from the interface. So we can take advantage of that and make sure that
    > relationship list is "unmodifiable", and safely implement the sorting.

    Ouch, just had a look at that code ...
    The implementation of making a list unmodifyable is to throw an
    UnsupportedOperationException :-(

    Holger and I thought about this way already but we both agreed that this
    solution is total crap. What you'd really need would be a split of the
    List interface into a mutable and an immutable part.

    > 2. Now that I wrote (1), I almost regret about it :-). Your idea of
    > modifying to-many relationships via simple "add/remove" list API is very
    > appealing... So, why don't we concentrate on making add/remove work via
    > ToManyList (clean behind the scenes way).

    YES!

    > Sorting will be done initially
    > on fetch (configurable from the DataMap and Modeler). And if we follow
    > your logic, a user should be in full control of the list. So once she
    > inserts/removes something, she shouldn't expect it to be ordered (since
    > she just modified it). Instead she could use Collections API to order it
    > any way she wants.

    Well, I'm not really sure if this is what I would want. As of today, we
    don't have sorted collections. What I would normally do is to override
    the get accessor method for the relationship and sort the List before
    returning it. I could even cache that sorted List and invalidate that
    cache when adding/removing from it. This would give me sorted
    relationships minus the ability to directly modify the List and have
    Cayenne do the right thing(tm).

    What I'd really like to have is a relationship that's always sorted.
    Sure, if the list was sorted on fetch we could have the db server sort
    it -> better performance.
    Otherwise, if I have to care (in user code) about whether the
    relationship is sorted or not that's overhead again and I'd almost be
    happier with the first approach I described above.

    What about the following approach: In order to keep the ToManyList
    always sorted we'd document that the add methods will not guarantee that
    you'll get the same object after inserting at a certain index. This is
    due to the nature of a sorted list. Browsing through the javadoc of List
    I even found the following for the add(index, object) method:

    "Inserts the specified element at the specified position in this list
    (optional operation)."
    What does the "optional operation" apply to? Insert the object? Does
    this mean that the List doesn't have to implement it? Or is it "at the
    specified position"? If it was the last then I'd say we implement the
    ToManyList in a way that it's always sorted ...

    -dirk



    This archive was generated by hypermail 2.0.0 : Thu Nov 14 2002 - 23:54:55 EST