When changing the title/author of a book, use hard links instead of copying the file, for a large speedup.

This commit is contained in:
Kovid Goyal 2012-10-22 21:57:44 +05:30
parent 0d532dd522
commit 5d3418f22c
2 changed files with 11 additions and 1 deletions

View File

@ -646,7 +646,7 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
spath = os.path.join(self.library_path, *current_path.split('/')) spath = os.path.join(self.library_path, *current_path.split('/'))
tpath = os.path.join(self.library_path, *path.split('/')) tpath = os.path.join(self.library_path, *path.split('/'))
wam = WindowsAtomicFolderMove(spath) if iswindows else None wam = WindowsAtomicFolderMove(spath) if iswindows and current_path else None
try: try:
if not os.path.exists(tpath): if not os.path.exists(tpath):
os.makedirs(tpath) os.makedirs(tpath)
@ -1375,6 +1375,11 @@ class LibraryDatabase2(LibraryDatabase, SchemaUpgrade, CustomColumns):
if hasattr(dest, 'flush'): if hasattr(dest, 'flush'):
dest.flush() dest.flush()
elif dest and not samefile(dest, path): elif dest and not samefile(dest, path):
try:
os.link(path, dest)
return
except:
pass
with lopen(path, 'rb') as f, lopen(dest, 'wb') as d: with lopen(path, 'rb') as f, lopen(dest, 'wb') as d:
shutil.copyfileobj(f, d) shutil.copyfileobj(f, d)

View File

@ -300,6 +300,11 @@ class WindowsAtomicFolderMove(object):
if handle is None: if handle is None:
raise ValueError(u'The file %r did not exist when this move' raise ValueError(u'The file %r did not exist when this move'
' operation was started'%path) ' operation was started'%path)
try:
win32file.CreateHardLink(dest, path)
return
except:
pass
with lopen(dest, 'wb') as f: with lopen(dest, 'wb') as f:
while True: while True:
hr, raw = win32file.ReadFile(handle, 1024*1024) hr, raw = win32file.ReadFile(handle, 1024*1024)