]> git.phdru.name Git - m_librarian.git/blobdiff - m_librarian/db.py
Speedup inp import by caching a set of files in the archive
[m_librarian.git] / m_librarian / db.py
index 6e08204acfcdac30da1ec9790b2132d9e7d8df14..99a2843aa42f3ad5f347a86efaecde0b30b6a1d5 100755 (executable)
@@ -2,7 +2,7 @@
 
 __all__ = ['Author', 'Book', 'Extension', 'Genre', 'Language',
            'AuthorBook', 'BookGenre',
-           'init_db', 'insert_name', 'update_counters',
+           'init_db', 'insert_name', 'insert_author', 'update_counters',
            ]
 
 import os
@@ -95,6 +95,7 @@ class Book(SQLObject):
     title_idx = DatabaseIndex(title)
     series_idx = DatabaseIndex(series)
     ser_no_idx = DatabaseIndex(ser_no)
+    archive_idx = DatabaseIndex(archive)
     archive_file_idx = DatabaseIndex(archive, file, unique=True)
     file_idx = DatabaseIndex(file)
     size_idx = DatabaseIndex(size)
@@ -157,15 +158,23 @@ def insert_name(table, name, **kw):
         return table(name=name, count=0, **kw)
 
 
+def insert_author(surname, name, misc_name):
+    try:
+        return Author.full_name_idx.get(
+            surname=surname, name=name, misc_name=misc_name)
+    except SQLObjectNotFound:
+        return Author(surname=surname, name=name, misc_name=misc_name, count=0)
+
+
 def update_counters():
     for author in Author.select():
-        author.count = len(author.books)
+        author.count = AuthorBook.select(AuthorBook.q.author == author).count()
 
     for ext in Extension.select():
         ext.count = Book.select(Book.q.extension == ext.name).count()
 
     for genre in Genre.select():
-        genre.count = len(genre.books)
+        genre.count = BookGenre.select(BookGenre.q.genre == genre).count()
 
     for language in Language.select():
         language.count = Book.select(Book.q.language == language.name).count()