![]() |
#1 |
Junior Member
![]() Posts: 2
Karma: 10
Join Date: Dec 2010
Device: Blackberry, Nook
|
Development Question: author_sort in books table?
I've just started working on calibre and I'm still feeling my way around the source, but I had a question.
Why does the db.author_sort() lookup the author sort in the books table rather than the authors table and authors link table? As far as I can tell the only places db.author_sort() is used are in the initialization of the metadata_single dialog and in db.get_metadata(), which also uses authors_with_sort_strings(). I'm probably missing something, but it looks like a one way street, if authors->author_sort is updated using set_sort_field_for_author then books is also updated with the new author_sort, but if books->author_sort is updated with set_author_sort then just books is updated. I'm specifically working on the Ticket #847 and noticed that when I built author_sort using db.get_metadata().author_sort_map it came out differently than db.get_metadata().author_sort. After looking around I saw it was because they are drawing from two different tables, and in my particular case those tables had different data. For example my books table has "Oath of Swords" author_sort listed as Weber, David J. but looking up that book through book_authors_link and then to authors it shows as Weber, David. I'm not really sure how they got out of sink, but it would make sense that if the author_sort column was necessary in both tables that there would only be one function for changing both tables rather than ones for updating one table or the other. To summarize that wall of ramble.
I'm still learning developing non-trivial programs and would appreciate your thoughts. Also is there a better place to post questions like this? |
![]() |
![]() |
![]() |
#2 |
Wizard
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 4,553
Karma: 950151
Join Date: Nov 2008
Device: Sony PRS-950, iphone/ipad (Marvin/iBooks/QuickReader)
|
I think that historically there was just the value on the books table, and the author_sort on the authors table is relatively recent. I believe that the reason both are kept is that at times one can want books to be sorted differently to the authors table - particularly in the case of multiple authors.
|
![]() |
![]() |
Advert | |
|
![]() |
#3 | ||||||||
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,342
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Quote:
Quote:
The choice to update the books when the sort string for an author is changed was not made lightly, and it could be argued that it was wrong. In the end, I decided that if you are playing with an author's sort, then you *probably* want to change the author's books. No one has complained yet. ![]() Quote:
As regards the enhancement request, the custom columns already do much of what the ticket is asking for, at least for fields with single values (although I seem to have forgotten series, which is probably a bug). Multi-value (is_multiple) fields are more complicated: what does equals mean, exactly? The authors field is even more complicated, because order matters, which isn't the case with tags. It isn't at all clear to me what should be displayed for is_multiple fields. Should it display only if all are identical? Should it display the ones that are identical (which would open up some interesting UI issues)? For authors, should it respect order (I think yes)? Quote:
Quote:
The link table question is different. All is_multiple or single-field tag-like information is normalized in the database, which is good practice and generally useful. Quote:
Quote:
Including the author_sort_map isn't strictly necessary because it doesn't appear in the user-visible metadata for a book. It is included for completeness, making it easier to compute on authors without needing to split the authors string and so that the OPF contains all the information for a book. Final note: as itimpi said, there are some legacy aspects involved. I added the sort string for authors in the 0.7 releases (don't remember exactly when), something that permitted fixing some bugs but also made explicit the difference between the author_sort and authors' sort strings. If I were designing it today, I would need to reflect on whether the two values are strictly necessary. I think yes, for the same reason that there is title and title_sort. Second question: does author_sort need to be editable? Good question. I think yes, because users have all sorts of needs that we can't anticipate. That raises the title/title_sort functionality discrepancy; title_sort cannot be changed by the user. I have wondered about that decision, because I have had instances where I wanted to manually edit title_sort. |
||||||||
![]() |
![]() |
![]() |
#4 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,214
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
@chaley: title_sort was actually introduced for performance reasons so that the title_sort function would not have to be called for each entry every time a sort on the title field was requested. That's why it isn't editable
![]() |
![]() |
![]() |
![]() |
#5 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,342
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Instead of the trigger, I would rather title_sort operate like author_sort, giving me a default value but letting me change it in edit metadata. I haven't proposed/built this up to now because it could be that I am just weird and no one else cares. |
|
![]() |
![]() |
Advert | |
|
![]() |
#6 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,214
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
I certainly dont, but I wont object to the change
![]() |
![]() |
![]() |
![]() |
#7 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,214
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Thinking about it a little, why is the trigger a problem. Surely if the title is changed, the sort value also needs to be changed?
|
![]() |
![]() |
![]() |
#8 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,342
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
I am not up enough on triggers to know if one can easily check the old and new values of title and not do the update of title_sort if the title hasn't changed. Then, even if the trigger is changed, one would need to be careful with transaction order. For example, assume that I can change title_sort in edit metadata. Assume further that I change the title and the title sort. I want my title sort to win, so I would need to ensure that the write of the title happened before the write of title sort, so that the trigger doesn't overwrite my change. I think it would probably be easier to add a parameter to set_title, specifying the title_sort to use. If it is None, then generate a new one. If not, then use it. However, I haven't thought a lot about it up to now. |
|
![]() |
![]() |
![]() |
#9 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,214
Karma: 27110894
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
IIRC in a sqlite update trigger you have access to OLD.title and NEW.title
|
![]() |
![]() |
![]() |
#10 | ||||
Junior Member
![]() Posts: 2
Karma: 10
Join Date: Dec 2010
Device: Blackberry, Nook
|
Quote:
Quote:
Quote:
Quote:
The main goal is a quick check to make sure that the metadata agrees when selecting a group of books, which in my mind would be that they match exactly. It would only be for testing if all are identical. If it only showed the ones that matched and forgave any extras then that would lead to more erroneous assumptions. Programmatically I'm saying if there is only one unique item in a set of that particular field then they all match and there is no need to overwrite that field unless the user changes it. I agree that tags would be different, though you could just test for set equality rather than string equality. |
||||
![]() |
![]() |
![]() |
#11 | |
Grand Sorcerer
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 12,342
Karma: 8012652
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
|
Quote:
Look at library.cache.py, and in particular refresh(), to see how the view is used to build the cache. You will see that it populates an array '_data'. There are some related pointer arrays that are used for sorting and searching. Next look at library.database2, line 306 in current source. You see that the variable 'data' is assigned to the cache object. If data is subscripted (data[12]), because of python magic the ResultCache method __getitem__ is called, which will return the row from _data, thus the values returned by querying the meta2 view. There are zillions of examples of using 'data' in caches.py and gui2.library.models.py. In particular, look at 'data' in models.py, where you will see how column names are translated to the cell in a line in data. |
|
![]() |
![]() |
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
What am I missing? (author_sort) | megachirops | Calibre | 12 | 09-06-2010 11:15 AM |
Table Of Contents Question | Humble | Calibre | 2 | 07-26-2010 09:10 PM |
Question Regarding Table of Contents | Guns4Hire | Sigil | 2 | 01-12-2010 11:15 PM |
Forget coffee table books-- how about a kitchen table book? | ardeegee | Lounge | 10 | 12-02-2009 12:00 PM |
Table of contents and time question | lizzielou | Sony Reader | 2 | 11-29-2009 04:48 AM |