add flag to skip downgrade on collection close

Disabled for now; when enabled it will allow faster collection
open and close in the normal case, while continuing to downgrade
when exporting or doing a full sync.

Also, when downgrading is disabled, the journal mode is no longer
changed back to delete.
This commit is contained in:
Damien Elmes 2020-04-04 17:21:45 +10:00
parent 45ea0ddefd
commit 7375a0389a
11 changed files with 28 additions and 18 deletions

View file

@ -245,16 +245,14 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""",
return True return True
return None return None
def close(self, save: bool = True) -> None: def close(self, save: bool = True, downgrade: bool = False) -> None:
"Disconnect from DB." "Disconnect from DB."
if self.db: if self.db:
if save: if save:
self.save(trx=False) self.save(trx=False)
else: else:
self.db.rollback() self.db.rollback()
if not self.server: self.backend.close_collection(downgrade=downgrade)
self.db.execute("pragma journal_mode = delete")
self.backend.close_collection()
self.db = None self.db = None
self.media.close() self.media.close()
self._closeLog() self._closeLog()
@ -296,7 +294,7 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""",
self.save(trx=False) self.save(trx=False)
self.db.execute("vacuum") self.db.execute("vacuum")
self.db.execute("analyze") self.db.execute("analyze")
self.close(save=False) self.close(save=False, downgrade=True)
# Object creation helpers # Object creation helpers
########################################################################## ##########################################################################

View file

@ -280,7 +280,7 @@ class AnkiExporter(Exporter):
self.count = self.dst.cardCount() self.count = self.dst.cardCount()
self.dst.setMod() self.dst.setMod()
self.postExport() self.postExport()
self.dst.close() self.dst.close(downgrade=True)
def postExport(self) -> None: def postExport(self) -> None:
# overwrite to apply customizations to the deck before it's closed, # overwrite to apply customizations to the deck before it's closed,
@ -402,7 +402,7 @@ class AnkiCollectionPackageExporter(AnkiPackageExporter):
self.count = self.col.cardCount() self.count = self.col.cardCount()
v2 = self.col.schedVer() != 1 v2 = self.col.schedVer() != 1
mdir = self.col.media.dir() mdir = self.col.media.dir()
self.col.close() self.col.close(downgrade=True)
if not v2: if not v2:
z.write(self.col.path, "collection.anki2") z.write(self.col.path, "collection.anki2")
else: else:

View file

@ -40,7 +40,7 @@ class Anki2Importer(Importer):
try: try:
self._import() self._import()
finally: finally:
self.src.close(save=False) self.src.close(save=False, downgrade=False)
def _prepareFiles(self) -> None: def _prepareFiles(self) -> None:
importingV2 = self.file.endswith(".anki21") importingV2 = self.file.endswith(".anki21")

View file

@ -638,7 +638,7 @@ class FullSyncer(HttpSyncer):
def download(self) -> Optional[str]: def download(self) -> Optional[str]:
hooks.sync_stage_did_change("download") hooks.sync_stage_did_change("download")
localNotEmpty = self.col.db.scalar("select 1 from cards") localNotEmpty = self.col.db.scalar("select 1 from cards")
self.col.close() self.col.close(downgrade=False)
cont = self.req("download") cont = self.req("download")
tpath = self.col.path + ".tmp" tpath = self.col.path + ".tmp"
if cont == "upgradeRequired": if cont == "upgradeRequired":

View file

@ -35,7 +35,7 @@ def getEmptyCol():
os.close(fd) os.close(fd)
os.unlink(nam) os.unlink(nam)
col = aopen(nam) col = aopen(nam)
col.close() col.close(downgrade=False)
getEmptyCol.master = nam getEmptyCol.master = nam
(fd, nam) = tempfile.mkstemp(suffix=".anki2") (fd, nam) = tempfile.mkstemp(suffix=".anki2")
shutil.copy(getEmptyCol.master, nam) shutil.copy(getEmptyCol.master, nam)

View file

@ -440,6 +440,8 @@ close the profile or restart Anki."""
# Collection load/unload # Collection load/unload
########################################################################## ##########################################################################
downgrade_on_close = True
def loadCollection(self) -> bool: def loadCollection(self) -> bool:
try: try:
self._loadCollection() self._loadCollection()
@ -450,7 +452,7 @@ close the profile or restart Anki."""
# clean up open collection if possible # clean up open collection if possible
if self.col: if self.col:
try: try:
self.col.close(save=False) self.col.close(save=False, downgrade=self.downgrade_on_close)
except: except:
pass pass
self.col = None self.col = None
@ -506,7 +508,7 @@ close the profile or restart Anki."""
except: except:
corrupt = True corrupt = True
try: try:
self.col.close() self.col.close(downgrade=self.downgrade_on_close)
except: except:
corrupt = True corrupt = True
finally: finally:

View file

@ -400,7 +400,7 @@ class SyncThread(QThread):
self.fireEvent("error", err) self.fireEvent("error", err)
finally: finally:
# don't bump mod time unless we explicitly save # don't bump mod time unless we explicitly save
self.col.close(save=False) self.col.close(save=False, downgrade=False)
hooks.sync_stage_did_change.remove(syncEvent) hooks.sync_stage_did_change.remove(syncEvent)
hooks.sync_progress_did_change.remove(syncMsg) hooks.sync_progress_did_change.remove(syncMsg)

View file

@ -336,7 +336,7 @@ impl Backend {
let col_inner = col.take().unwrap(); let col_inner = col.take().unwrap();
if downgrade { if downgrade {
let log = log::terminal(); let log = log::terminal();
if let Err(e) = col_inner.downgrade_and_close() { if let Err(e) = col_inner.close(downgrade) {
error!(log, " failed: {:?}", e); error!(log, " failed: {:?}", e);
} }
} }

View file

@ -130,8 +130,8 @@ impl Collection {
self.state.task_state == CollectionTaskState::Normal self.state.task_state == CollectionTaskState::Normal
} }
pub(crate) fn downgrade_and_close(self) -> Result<()> { pub(crate) fn close(self, downgrade: bool) -> Result<()> {
self.storage.downgrade_to_schema_11() self.storage.close(downgrade)
} }
// fixme: invalidate when config changes // fixme: invalidate when config changes

View file

@ -204,6 +204,14 @@ impl SqliteStorage {
Ok(storage) Ok(storage)
} }
pub(crate) fn close(self, downgrade: bool) -> Result<()> {
if downgrade {
self.downgrade_to_schema_11()?;
self.db.pragma_update(None, "journal_mode", &"delete")?;
}
Ok(())
}
// Standard transaction start/stop // Standard transaction start/stop
////////////////////////////////////// //////////////////////////////////////

View file

@ -19,7 +19,7 @@ impl SqliteStorage {
Ok(()) Ok(())
} }
pub(crate) fn downgrade_to_schema_11(self) -> Result<()> { pub(super) fn downgrade_to_schema_11(&self) -> Result<()> {
self.begin_trx()?; self.begin_trx()?;
self.downgrade_tags_from_schema13()?; self.downgrade_tags_from_schema13()?;
@ -30,6 +30,8 @@ impl SqliteStorage {
self.db self.db
.execute_batch(include_str!("schema12_downgrade.sql"))?; .execute_batch(include_str!("schema12_downgrade.sql"))?;
self.commit_trx() self.commit_trx()?;
Ok(())
} }
} }