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,
'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):

View file

@ -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