- if we store it inside the media folder, we inadvertently bump the folder mod
   time every time sqlite creates a journal file

- close/reopen the media db as the deck is closed/opened
This commit is contained in:
Damien Elmes 2011-09-12 05:03:31 +09:00
parent c59dd854fb
commit 87bfb38e2b
3 changed files with 19 additions and 8 deletions

View file

@ -125,12 +125,14 @@ crt=?, mod=?, scm=?, dty=?, lastSync=?, conf=?""",
self.rollback() self.rollback()
self.db.close() self.db.close()
self.db = None self.db = None
self.media.close()
def reopen(self): def reopen(self):
"Reconnect to DB (after changing threads, etc). Doesn't reload." "Reconnect to DB (after changing threads, etc). Doesn't reload."
import anki.db import anki.db
if not self.db: if not self.db:
self.db = anki.db.DB(self.path) self.db = anki.db.DB(self.path)
self.media.connect()
def rollback(self): def rollback(self):
self.db.rollback() self.db.rollback()

View file

@ -22,12 +22,19 @@ class MediaManager(object):
os.makedirs(self._dir) os.makedirs(self._dir)
os.chdir(self._dir) os.chdir(self._dir)
# change database # change database
path = os.path.join(self.dir(), "media.db") self.connect()
def connect(self):
path = self.dir()+"db"
create = not os.path.exists(path) create = not os.path.exists(path)
self.db = DB(path) self.db = DB(path)
if create: if create:
self._initDB() self._initDB()
def close(self):
self.db.close()
self.db = None
def dir(self): def dir(self):
return self._dir return self._dir
@ -124,7 +131,7 @@ If the same name exists, compare checksums."""
# loop through directory and find unused & missing media # loop through directory and find unused & missing media
unused = [] unused = []
for file in os.listdir(mdir): for file in os.listdir(mdir):
if file.startswith("latex-") or file.startswith("media.db"): if file.startswith("latex-"):
continue continue
path = os.path.join(mdir, file) path = os.path.join(mdir, file)
if not os.path.isfile(path): if not os.path.isfile(path):
@ -159,8 +166,8 @@ If the same name exists, compare checksums."""
# in the log, a mod time of zero indicates a delete # in the log, a mod time of zero indicates a delete
self.db.executescript(""" self.db.executescript("""
create table media (fname text primary key, csum text, mod int); create table media (fname text primary key, csum text, mod int);
create table meta (dirMod int); create table meta (dirMod int, inSync int);
insert into meta values (0); insert into meta values (0, 0);
create table log (id int, fname text, mod int); create table log (id int, fname text, mod int);
create index ix_log_id on log (id); create index ix_log_id on log (id);
""") """)
@ -209,8 +216,8 @@ create index ix_log_id on log (id);
changed = [] changed = []
# loop through on-disk files # loop through on-disk files
for f in os.listdir(self.dir()): for f in os.listdir(self.dir()):
# ignore our db and folders # ignore folders
if f.startswith("media.db") or os.path.isdir(f): if os.path.isdir(f):
continue continue
# newly added? # newly added?
if f not in self.cache: if f not in self.cache:
@ -255,3 +262,5 @@ create index ix_log_id on log (id);
self.db.executemany("delete from media where fname = ?", self.db.executemany("delete from media where fname = ?",
mediaRem) mediaRem)
self.db.execute("update meta set dirMod = ?", self._mtime(self.dir())) self.db.execute("update meta set dirMod = ?", self._mtime(self.dir()))
self.db.commit()

View file

@ -14,8 +14,8 @@ def test_latex():
f = d.newFact() f = d.newFact()
f['Front'] = u"[latex]hello[/latex]" f['Front'] = u"[latex]hello[/latex]"
d.addFact(f) d.addFact(f)
# but since latex couldn't run, it will only have the media.db # but since latex couldn't run, there's nothing there
assert len(os.listdir(d.media.dir())) == 1 assert len(os.listdir(d.media.dir())) == 0
# check the error message # check the error message
msg = f.cards()[0].q() msg = f.cards()[0].q()
assert "executing latex" in msg assert "executing latex" in msg