From 66dcd4559913615308192bbaace7a53632baa6dc Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 12 Mar 2011 23:23:01 +0900 Subject: [PATCH] use nextID() when adding facts/cards We want to ensure that we never recycle ids from deleted cards. We could do this with an autoincrement column in sqlite, but it's cheaper for us to handle the ids ourselves, as the deck object is always in memory. --- anki/deck.py | 25 ++++++++++++++----------- anki/storage.py | 6 +++--- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/anki/deck.py b/anki/deck.py index 25b4a3c8a..6aa9babe2 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -36,7 +36,9 @@ defaultConf = { 'sessionTimeLimit': 600, 'currentModelId': None, 'currentGroupId': 1, - 'nextFactPos': 1, + 'nextFid': 1, + 'nextCid': 1, + 'nextGid': 2, 'mediaURL': "", 'latexPre': """\ \\documentclass[12pt]{article} @@ -162,6 +164,7 @@ qconf=?, conf=?, data=?""", ########################################################################## def nextID(self, type): + type = "next"+type.capitalize() id = self.conf.get(type, 1) self.conf[type] = id+1 return id @@ -389,29 +392,29 @@ due > :now and due < :now""", now=time.time()) cms = self.findTemplates(fact) if not cms: return None - # set pos - fact.pos = self.conf['nextFactPos'] - self.conf['nextFactPos'] += 1 - ncards = 0 + # flush the fact + fact.id = self.nextID("fid") + fact.flush() + # notice any new tags + self.registerTags(fact.tags) + # if random mode, determine insertion point isRandom = self.qconf['newCardOrder'] == NEW_CARDS_RANDOM if isRandom: due = random.randrange(0, 1000000) - # flush the fact so we get its id - fact.flush() + # add cards + ncards = 0 for template in cms: card = anki.cards.Card(self) + card.id = self.nextID("cid") card.fid = fact.id card.ord = template['ord'] card.gid = template['gid'] or gid if isRandom: card.due = due else: - card.due = fact.pos + card.due = fact.id card.flush() ncards += 1 - # save fact last, which will update caches too - fact.flush() - self.registerTags(fact.tags) return ncards def findTemplates(self, fact, checkActive=True): diff --git a/anki/storage.py b/anki/storage.py index dbc515e2a..f9bc17a9e 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -494,9 +494,9 @@ update cards set due = cast( (case when due < :stamp then 0 else 1 end) + ((due-:stamp)/86400) as int)+:today where type between 0 and 1""", stamp=deck.sched.dayCutoff, today=deck.sched.today) - # track ids - #deck.conf['nextFact'] = deck.db.scalar("select max(id) from facts")+1 - #deck.conf['nextCard'] = deck.db.scalar("select max(id) from cards")+1 + # update insertion id + deck.conf['nextFid'] = deck.db.scalar("select max(id) from facts")+1 + deck.conf['nextCid'] = deck.db.scalar("select max(id) from cards")+1 deck.save() # optimize and finish