ensure unique id on per-object add, too

This commit is contained in:
Damien Elmes 2011-08-26 22:51:08 +09:00
parent ebac628187
commit f7b89c9fa1
5 changed files with 25 additions and 6 deletions

View file

@ -3,7 +3,7 @@
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import time import time
from anki.utils import intTime, hexifyID from anki.utils import intTime, hexifyID, timestampID
# Cards # Cards
########################################################################## ##########################################################################
@ -28,7 +28,7 @@ class Card(object):
self.load() self.load()
else: else:
# to flush, set fid, ord, and due # to flush, set fid, ord, and due
self.id = intTime(1000) self.id = timestampID(deck.db, "cards")
self.gid = 1 self.gid = 1
self.crt = intTime() self.crt = intTime()
self.type = 0 self.type = 0

View file

@ -6,7 +6,7 @@ import time
from anki.errors import AnkiError from anki.errors import AnkiError
from anki.utils import fieldChecksum, intTime, \ from anki.utils import fieldChecksum, intTime, \
joinFields, splitFields, ids2str, parseTags, canonifyTags, hasTag, \ joinFields, splitFields, ids2str, parseTags, canonifyTags, hasTag, \
stripHTML stripHTML, timestampID
class Fact(object): class Fact(object):
@ -17,7 +17,7 @@ class Fact(object):
self.id = id self.id = id
self.load() self.load()
else: else:
self.id = intTime(1000) self.id = timestampID(deck.db, "facts")
self._model = model self._model = model
self.gid = deck.defaultGroup(model.conf['gid']) self.gid = deck.defaultGroup(model.conf['gid'])
self.mid = model.id self.mid = model.id

View file

@ -3,7 +3,8 @@
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html # License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
import simplejson import simplejson
from anki.utils import intTime, hexifyID, joinFields, splitFields, ids2str from anki.utils import intTime, hexifyID, joinFields, splitFields, ids2str, \
timestampID
from anki.lang import _ from anki.lang import _
# Models # Models
@ -63,7 +64,7 @@ class Model(object):
self.id = id self.id = id
self.load() self.load()
else: else:
self.id = intTime(1000) self.id = timestampID(deck.db, "models")
self.name = u"" self.name = u""
self.conf = defaultConf.copy() self.conf = defaultConf.copy()
self.css = "" self.css = ""

View file

@ -180,6 +180,15 @@ def ids2str(ids):
"""Given a list of integers, return a string '(int1,int2,...)'.""" """Given a list of integers, return a string '(int1,int2,...)'."""
return "(%s)" % ",".join(str(i) for i in ids) return "(%s)" % ",".join(str(i) for i in ids)
def timestampID(db, table):
"Return a non-conflicting timestamp for table."
# be careful not to create multiple objects without flushing them, or they
# may share an ID.
t = intTime(1000)
while db.scalar("select id from %s where id = ?" % table, t):
t += 1
return t
# Tags # Tags
############################################################################## ##############################################################################

View file

@ -2,6 +2,7 @@
import os, re, datetime import os, re, datetime
from tests.shared import assertException, getEmptyDeck, testDir from tests.shared import assertException, getEmptyDeck, testDir
from anki.stdmodels import BasicModel
from anki import Deck from anki import Deck
@ -189,3 +190,11 @@ def test_addDelTags():
f.load() f.load()
assert f.tags[0] == "aaa" assert f.tags[0] == "aaa"
assert len(f.tags) == 2 assert len(f.tags) == 2
def test_timestamps():
deck = getEmptyDeck()
assert len(deck.models()) == 2
for i in range(100):
deck.addModel(BasicModel(deck))
assert len(deck.models()) == 102