View Single Post
Old 03-09-2022, 11:23 AM   #1153
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,509
Karma: 8065348
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by JimmXinu View Post
I don't see any obvious inefficiencies, unless db.set_custom() has started started doing commits too.
The legacy method db.set_custom() ignores the 'commit' parameter. Every call ends up calling db.new_api.set_field(), which does commit the operation. This isn't new -- I think it was changed in calibre V4.

To avoid the commits you should use the book_id_to_val_map parameter of set_field() (in calibre.db.cache). Using your description of the processing you would:
  1. map = dict()
  2. First remove the series name from all books that have it. Search for all books with that series. For each book found, do
    Code:
    map[book_id] = ''
  3. Set the series for all the books in the list along with the series index. For each book in the list do
    Code:
    map[book_id] = '{} [{}]'.format(series_lookup_name, series_index)
    Note that series_index can't be empty. If it is then use
    Code:
    map[book_id] = series_lookup_name
  4. Call db.new_api.set_field(series_name, map). This will do all the operations in one transaction.
(NB: there might be syntax errors in the code. I didn't try to run it.)

The operation order is important. The second operation will add back any series that was deleted in the first operation.

Last edited by chaley; 03-09-2022 at 11:29 AM. Reason: Changed series_name to series_lookup_name
chaley is offline   Reply With Quote