mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 00:36:38 -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.
154 lines
4.3 KiB
Python
154 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
|
|
|
|
import time
|
|
from anki.utils import intTime, hexifyID
|
|
|
|
MAX_TIMER = 60
|
|
|
|
# Cards
|
|
##########################################################################
|
|
|
|
# Type: 0=learning, 1=due, 2=new
|
|
# Queue: 0=learning, 1=due, 2=new
|
|
# -1=suspended, -2=user buried, -3=sched buried
|
|
# Group: scheduling group
|
|
# Ordinal: card template # for fact
|
|
# Flags: unused; reserved for future use
|
|
|
|
# Due is used differently for different queues.
|
|
# - new queue: fact.id
|
|
# - rev queue: integer day
|
|
# - lrn queue: integer timestamp
|
|
|
|
class Card(object):
|
|
|
|
def __init__(self, deck, id=None):
|
|
self.deck = deck
|
|
self.timerStarted = None
|
|
self._qa = None
|
|
if id:
|
|
self.id = id
|
|
self.load()
|
|
else:
|
|
# to flush, set fid, tid, due and ord
|
|
self.id = None
|
|
self.gid = 1
|
|
self.crt = intTime()
|
|
self.type = 2
|
|
self.queue = 2
|
|
self.ivl = 0
|
|
self.factor = 0
|
|
self.reps = 0
|
|
self.streak = 0
|
|
self.lapses = 0
|
|
self.grade = 0
|
|
self.cycles = 0
|
|
self.data = ""
|
|
|
|
def load(self):
|
|
(self.id,
|
|
self.fid,
|
|
self.tid,
|
|
self.gid,
|
|
self.ord,
|
|
self.crt,
|
|
self.mod,
|
|
self.type,
|
|
self.queue,
|
|
self.due,
|
|
self.ivl,
|
|
self.factor,
|
|
self.reps,
|
|
self.streak,
|
|
self.lapses,
|
|
self.grade,
|
|
self.cycles,
|
|
self.data) = self.deck.db.first(
|
|
"select * from cards where id = ?", self.id)
|
|
|
|
def flush(self):
|
|
self.mod = intTime()
|
|
self.deck.db.execute(
|
|
"""
|
|
insert or replace into cards values
|
|
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
self.id,
|
|
self.fid,
|
|
self.tid,
|
|
self.gid,
|
|
self.ord,
|
|
self.crt,
|
|
self.mod,
|
|
self.type,
|
|
self.queue,
|
|
self.due,
|
|
self.ivl,
|
|
self.factor,
|
|
self.reps,
|
|
self.streak,
|
|
self.lapses,
|
|
self.grade,
|
|
self.cycles,
|
|
self.data)
|
|
|
|
def flushSched(self):
|
|
self.mod = intTime()
|
|
self.deck.db.execute(
|
|
"""update cards set
|
|
mod=?, type=?, queue=?, due=?, ivl=?, factor=?, reps=?,
|
|
streak=?, lapses=?, grade=?, cycles=? where id = ?""",
|
|
self.mod, self.type, self.queue, self.due, self.ivl,
|
|
self.factor, self.reps, self.streak, self.lapses,
|
|
self.grade, self.cycles, self.id)
|
|
|
|
def q(self):
|
|
return self._getQA()['q']
|
|
|
|
def a(self):
|
|
return self._getQA()['a']
|
|
|
|
def _getQA(self, reload=False):
|
|
# this is a hack at the moment
|
|
if not self._qa or reload:
|
|
self._qa = self.deck.formatQA(
|
|
self.id,
|
|
self.deck._cacheFacts([self.fid])[self.fid],
|
|
self.deck._cacheMeta("and c.id = %d" % self.id)[2][self.id])
|
|
return self._qa
|
|
|
|
def fact(self):
|
|
return self.deck.getFact(self.fid)
|
|
|
|
def template(self):
|
|
return self.deck.getTemplate(self.tid)
|
|
|
|
def startTimer(self):
|
|
self.timerStarted = time.time()
|
|
|
|
def timeTaken(self):
|
|
return min(time.time() - self.timerStarted, MAX_TIMER)
|
|
|
|
# Questions and answers
|
|
##########################################################################
|
|
|
|
def htmlQuestion(self, type="question", align=True):
|
|
div = '''<div class="card%s" id="cm%s%s">%s</div>''' % (
|
|
type[0], type[0], hexifyID(self.tid),
|
|
getattr(self, type))
|
|
# add outer div & alignment (with tables due to qt's html handling)
|
|
if not align:
|
|
return div
|
|
attr = type + 'Align'
|
|
if getattr(self.cardModel, attr) == 0:
|
|
align = "center"
|
|
elif getattr(self.cardModel, attr) == 1:
|
|
align = "left"
|
|
else:
|
|
align = "right"
|
|
return (("<center><table width=95%%><tr><td align=%s>" % align) +
|
|
div + "</td></tr></table></center>")
|
|
|
|
def htmlAnswer(self, align=True):
|
|
return self.htmlQuestion(type="answer", align=align)
|