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.
This commit is contained in:
Damien Elmes 2011-03-12 23:23:01 +09:00
parent 7d52805b41
commit 66dcd45599
2 changed files with 17 additions and 14 deletions

View file

@ -36,7 +36,9 @@ defaultConf = {
'sessionTimeLimit': 600, 'sessionTimeLimit': 600,
'currentModelId': None, 'currentModelId': None,
'currentGroupId': 1, 'currentGroupId': 1,
'nextFactPos': 1, 'nextFid': 1,
'nextCid': 1,
'nextGid': 2,
'mediaURL': "", 'mediaURL': "",
'latexPre': """\ 'latexPre': """\
\\documentclass[12pt]{article} \\documentclass[12pt]{article}
@ -162,6 +164,7 @@ qconf=?, conf=?, data=?""",
########################################################################## ##########################################################################
def nextID(self, type): def nextID(self, type):
type = "next"+type.capitalize()
id = self.conf.get(type, 1) id = self.conf.get(type, 1)
self.conf[type] = id+1 self.conf[type] = id+1
return id return id
@ -389,29 +392,29 @@ due > :now and due < :now""", now=time.time())
cms = self.findTemplates(fact) cms = self.findTemplates(fact)
if not cms: if not cms:
return None return None
# set pos # flush the fact
fact.pos = self.conf['nextFactPos'] fact.id = self.nextID("fid")
self.conf['nextFactPos'] += 1 fact.flush()
ncards = 0 # notice any new tags
self.registerTags(fact.tags)
# if random mode, determine insertion point
isRandom = self.qconf['newCardOrder'] == NEW_CARDS_RANDOM isRandom = self.qconf['newCardOrder'] == NEW_CARDS_RANDOM
if isRandom: if isRandom:
due = random.randrange(0, 1000000) due = random.randrange(0, 1000000)
# flush the fact so we get its id # add cards
fact.flush() ncards = 0
for template in cms: for template in cms:
card = anki.cards.Card(self) card = anki.cards.Card(self)
card.id = self.nextID("cid")
card.fid = fact.id card.fid = fact.id
card.ord = template['ord'] card.ord = template['ord']
card.gid = template['gid'] or gid card.gid = template['gid'] or gid
if isRandom: if isRandom:
card.due = due card.due = due
else: else:
card.due = fact.pos card.due = fact.id
card.flush() card.flush()
ncards += 1 ncards += 1
# save fact last, which will update caches too
fact.flush()
self.registerTags(fact.tags)
return ncards return ncards
def findTemplates(self, fact, checkActive=True): def findTemplates(self, fact, checkActive=True):

View file

@ -494,9 +494,9 @@ update cards set due = cast(
(case when due < :stamp then 0 else 1 end) + (case when due < :stamp then 0 else 1 end) +
((due-:stamp)/86400) as int)+:today where type ((due-:stamp)/86400) as int)+:today where type
between 0 and 1""", stamp=deck.sched.dayCutoff, today=deck.sched.today) between 0 and 1""", stamp=deck.sched.dayCutoff, today=deck.sched.today)
# track ids # update insertion id
#deck.conf['nextFact'] = deck.db.scalar("select max(id) from facts")+1 deck.conf['nextFid'] = deck.db.scalar("select max(id) from facts")+1
#deck.conf['nextCard'] = deck.db.scalar("select max(id) from cards")+1 deck.conf['nextCid'] = deck.db.scalar("select max(id) from cards")+1
deck.save() deck.save()
# optimize and finish # optimize and finish