From b24493d7e8f65b6a1bde9981701cb9a3d7a9af14 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 1 Jun 2013 16:25:06 +0530 Subject: [PATCH] Add UUID cache to new db backend --- src/calibre/db/backend.py | 4 ++-- src/calibre/db/tables.py | 11 +++++++++++ src/calibre/db/write.py | 19 +++++++++++++++---- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/calibre/db/backend.py b/src/calibre/db/backend.py index 917178ab18..76c5841180 100644 --- a/src/calibre/db/backend.py +++ b/src/calibre/db/backend.py @@ -29,7 +29,7 @@ from calibre.utils.magick.draw import save_cover_data_to from calibre.utils.recycle_bin import delete_tree from calibre.db.tables import (OneToOneTable, ManyToOneTable, ManyToManyTable, SizeTable, FormatsTable, AuthorsTable, IdentifiersTable, PathTable, - CompositeTable, LanguagesTable) + CompositeTable, LanguagesTable, UUIDTable) # }}} ''' @@ -701,7 +701,7 @@ class DB(object): if col == 'cover' else col) if not metadata['column']: metadata['column'] = col - tables[col] = (PathTable if col == 'path' else OneToOneTable)(col, metadata) + tables[col] = (PathTable if col == 'path' else UUIDTable if col == 'uuid' else OneToOneTable)(col, metadata) for col in ('series', 'publisher', 'rating'): tables[col] = ManyToOneTable(col, self.field_metadata[col].copy()) diff --git a/src/calibre/db/tables.py b/src/calibre/db/tables.py index fc62fbe951..83d4b23712 100644 --- a/src/calibre/db/tables.py +++ b/src/calibre/db/tables.py @@ -98,6 +98,17 @@ class SizeTable(OneToOneTable): 'WHERE data.book=books.id) FROM books'): self.book_col_map[row[0]] = self.unserialize(row[1]) +class UUIDTable(OneToOneTable): + + def read(self, db): + OneToOneTable.read(self, db) + self.uuid_to_id_map = {v:k for k, v in self.book_col_map.iteritems()} + + def update_uuid_cache(self, book_id_val_map): + for book_id, uuid in book_id_val_map.iteritems(): + self.uuid_to_id_map.pop(self.book_col_map[book_id], None) # discard old uuid + self.uuid_to_id_map[uuid] = book_id + class CompositeTable(OneToOneTable): def read(self, db): diff --git a/src/calibre/db/write.py b/src/calibre/db/write.py index 87e7179661..1bdbabf082 100644 --- a/src/calibre/db/write.py +++ b/src/calibre/db/write.py @@ -174,12 +174,19 @@ def one_one_in_books(book_id_val_map, db, field, *args): db.conn.executemany( 'UPDATE books SET %s=? WHERE id=?'%field.metadata['column'], sequence) field.table.book_col_map.update(book_id_val_map) - if field.name == 'title': - # Set the title sort field - field.title_sort_field.writer.set_books( - {k:title_sort(v) for k, v in book_id_val_map.iteritems()}, db) return set(book_id_val_map) +def set_uuid(book_id_val_map, db, field, *args): + field.table.update_uuid_cache(book_id_val_map) + return one_one_in_books(book_id_val_map, db, field, *args) + +def set_title(book_id_val_map, db, field, *args): + ans = one_one_in_books(book_id_val_map, db, field, *args) + # Set the title sort field + field.title_sort_field.writer.set_books( + {k:title_sort(v) for k, v in book_id_val_map.iteritems()}, db) + return ans + def one_one_in_other(book_id_val_map, db, field, *args): 'Set a one-one field in the non-books table, like comments' deleted = tuple((k,) for k, v in book_id_val_map.iteritems() if v is None) @@ -460,6 +467,10 @@ class Writer(object): self.set_books_func = custom_series_index elif self.name == 'identifiers': self.set_books_func = identifiers + elif self.name == 'uuid': + self.set_books_func = set_uuid + elif self.name == 'title': + self.set_books_func = set_title elif field.is_many_many: self.set_books_func = many_many elif field.is_many: