mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 22:42:25 -04:00

Anki used random 64bit IDs for cards, facts and fields. This had some nice properties: - merging data in syncs and imports was simply a matter of copying each way, as conflicts were astronomically unlikely - it made it easy to identify identical cards and prevent them from being reimported But there were some negatives too: - they're more expensive to store - javascript can't handle numbers > 2**53, which means AnkiMobile, iAnki and so on have to treat the ids as strings, which is slow - simply copying data in a sync or import can lead to corruption, as while a duplicate id indicates the data was originally the same, it may have diverged. A more intelligent approach is necessary. - sqlite was sorting the fields table based on the id, which meant the fields were spread across the table, and costly to fetch So instead, we'll move to incremental ids. In the case of model changes we'll declare that a schema change and force a full sync to avoid having to deal with conflicts, and in the case of cards and facts, we'll need to update the ids on one end to merge. Identical cards can be detected by checking to see if their id is the same and their creation time is the same. Creation time has been added back to cards and facts because it's necessary for sync conflict merging. That means facts.pos is not required. The graves table has been removed. It's not necessary for schema related changes, and dead cards/facts can be represented as a card with queue=-4 and created=0. Because we will record schema modification time and can ensure a full sync propagates to all endpoints, it means we can remove the dead cards/facts on schema change. Tags have been removed from the facts table and are represented as a field with ord=-1 and fmid=0. Combined with the locality improvement for fields, it means that fetching fields is not much more expensive than using the q/a cache. Because of the above, removing the q/a cache is a possibility now. The q and a columns on cards has been dropped. It will still be necessary to render the q/a on fact add/edit, since we need to record media references. It would be nice to avoid this in the future. Perhaps one way would be the ability to assign a type to fields, like "image", "audio", or "latex". LaTeX needs special consider anyway, as it was being rendered into the q/a cache.
126 lines
3.5 KiB
Python
126 lines
3.5 KiB
Python
# coding: utf-8
|
|
|
|
import os, re
|
|
from tests.shared import assertException, getEmptyDeck, testDir
|
|
|
|
from anki import Deck
|
|
|
|
newPath = None
|
|
newMod = None
|
|
|
|
def test_create():
|
|
global newPath, newMod
|
|
path = "/tmp/test_attachNew.anki"
|
|
try:
|
|
os.unlink(path)
|
|
except OSError:
|
|
pass
|
|
deck = Deck(path)
|
|
# for open()
|
|
newPath = deck.path
|
|
deck.save()
|
|
newMod = deck.mod
|
|
deck.close()
|
|
del deck
|
|
|
|
def test_open():
|
|
deck = Deck(newPath)
|
|
assert deck.mod == newMod
|
|
deck.close()
|
|
|
|
def test_openReadOnly():
|
|
# non-writeable dir
|
|
assertException(Exception,
|
|
lambda: Deck("/attachroot"))
|
|
# reuse tmp file from before, test non-writeable file
|
|
os.chmod(newPath, 0)
|
|
assertException(Exception,
|
|
lambda: Deck(newPath))
|
|
os.chmod(newPath, 0666)
|
|
os.unlink(newPath)
|
|
|
|
def test_factAddDelete():
|
|
deck = getEmptyDeck()
|
|
# add a fact
|
|
f = deck.newFact()
|
|
f['Front'] = u"one"; f['Back'] = u"two"
|
|
n = deck.addFact(f)
|
|
assert n == 1
|
|
deck.rollback()
|
|
assert deck.cardCount() == 0
|
|
# try with two cards
|
|
f = deck.newFact()
|
|
f['Front'] = u"one"; f['Back'] = u"two"
|
|
m = f.model
|
|
m.templates[1].active = True
|
|
m.flush()
|
|
n = deck.addFact(f)
|
|
assert n == 2
|
|
# check q/a generation
|
|
c0 = f.cards()[0]
|
|
assert re.sub("</?.+?>", "", c0.q()) == u"one"
|
|
# it should not be a duplicate
|
|
for p in f.problems():
|
|
assert not p
|
|
# now let's make a duplicate and test uniqueness
|
|
f2 = deck.newFact()
|
|
f2.model.fields[1].conf['required'] = True
|
|
f2['Front'] = u"one"; f2['Back'] = u""
|
|
p = f2.problems()
|
|
assert p[0] == "unique"
|
|
assert p[1] == "required"
|
|
# try delete the first card
|
|
cards = f.cards()
|
|
id1 = cards[0].id; id2 = cards[1].id
|
|
assert deck.cardCount() == 2
|
|
assert deck.factCount() == 1
|
|
deck.deleteCard(id1)
|
|
assert deck.cardCount() == 1
|
|
assert deck.factCount() == 1
|
|
# and the second should clear the fact
|
|
deck.deleteCard(id2)
|
|
assert deck.cardCount() == 0
|
|
assert deck.factCount() == 0
|
|
|
|
def test_fieldChecksum():
|
|
deck = getEmptyDeck()
|
|
f = deck.newFact()
|
|
f['Front'] = u"new"; f['Back'] = u"new2"
|
|
deck.addFact(f)
|
|
assert deck.db.scalar(
|
|
"select csum from fdata where ord = 0") == "22af645d"
|
|
# empty field should have no checksum
|
|
f['Front'] = u""
|
|
f.flush()
|
|
assert deck.db.scalar(
|
|
"select csum from fdata where ord = 0") == ""
|
|
# changing the val should change the checksum
|
|
f['Front'] = u"newx"
|
|
f.flush()
|
|
assert deck.db.scalar(
|
|
"select csum from fdata where ord = 0") == "4b0e5a4c"
|
|
# back should have no checksum, because it's not set to be unique
|
|
assert deck.db.scalar(
|
|
"select csum from fdata where ord = 1") == ""
|
|
# if we turn on unique, it should get a checksum
|
|
f.model.fields[1].conf['unique'] = True
|
|
f.model.flush()
|
|
f.model.updateCache()
|
|
print deck.db.scalar(
|
|
"select csum from fdata where ord = 1")
|
|
assert deck.db.scalar(
|
|
"select csum from fdata where ord = 1") == "82f2ec5f"
|
|
# turning it off doesn't currently zero the checksum for efficiency reasons
|
|
# f.model.fields[1].conf['unique'] = False
|
|
# f.model.flush()
|
|
# f.model.updateCache()
|
|
# assert deck.db.scalar(
|
|
# "select csum from fdata where ord = 1") == ""
|
|
|
|
def test_upgrade():
|
|
import tempfile, shutil
|
|
src = os.path.join(testDir, "support", "anki12.anki")
|
|
(fd, dst) = tempfile.mkstemp(suffix=".anki")
|
|
print "upgrade to", dst
|
|
shutil.copy(src, dst)
|
|
deck = Deck(dst)
|