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

SQLAlchemy is a great tool, but it wasn't a great fit for Anki: - We often had to drop down to raw SQL for performance reasons. - The DB cursors and results were wrapped, which incurred a sizable performance hit due to introspection. Operations like fetching 50k records from a hot cache were taking more than twice as long to complete. - We take advantage of sqlite-specific features, so SQL language abstraction is useless to us. - The anki schema is quite small, so manually saving and loading objects is not a big burden. In the process of porting to DBAPI, I've refactored the database schema: - App configuration data that we don't need in joins or bulk updates has been moved into JSON objects. This simplifies serializing, and means we won't need DB schema changes to store extra options in the future. This change obsoletes the deckVars table. - Renamed tables: -- fieldModels -> fields -- cardModels -> templates -- fields -> fdata - a number of attribute names have been shortened Classes like Card, Fact & Model remain. They maintain a reference to the deck. To write their state to the DB, call .flush(). Objects no longer have their modification time manually updated. Instead, the modification time is updated when they are flushed. This also applies to the deck. Decks will now save on close, because various operations that were done at deck load will be moved into deck close instead. Operations like undoing buried card are cheap on a hot cache, but expensive on startup. Programmatically you can call .close(save=False) to avoid a save and a modification bump. This will be useful for generating due counts. Because of the new saving behaviour, the save and save as options will be removed from the GUI in the future. The q/a cache and field cache generating has been centralized. Facts will automatically rebuild the cache on flush; models can do so with model.updateCache(). Media handling has also been reworked. It has moved into a MediaRegistry object, which the deck holds. Refcounting has been dropped - it meant we had to compare old and new value every time facts or models were changed, and existed for the sole purpose of not showing errors on a missing media download. Instead we just media.registerText(q+a) when it's updated. The download function will be expanded to ask the user if they want to continue after a certain number of files have failed to download, which should be an adequate alternative. And we now add the file into the media DB when it's copied to th emedia directory, not when the card is commited. This fixes duplicates a user would get if they added the same media to a card twice without adding the card. The old DeckStorage object had its upgrade code split in a previous commit; the opening and upgrading code has been merged back together, and put in a separate storage.py file. The correct way to open a deck now is import anki; d = anki.Deck(path). deck.getCard() -> deck.sched.getCard() same with answerCard deck.getCard(id) returns a Card object now. And the DB wrapper has had a few changes: - sql statements are a more standard DBAPI: - statement() -> execute() - statements() -> executemany() - called like execute(sql, 1, 2, 3) or execute(sql, a=1, b=2, c=3) - column0 -> list
65 lines
1.7 KiB
Python
65 lines
1.7 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
|
|
|
|
MATURE_THRESHOLD = 21
|
|
|
|
# whether new cards should be mixed with reviews, or shown first or last
|
|
NEW_CARDS_DISTRIBUTE = 0
|
|
NEW_CARDS_LAST = 1
|
|
NEW_CARDS_FIRST = 2
|
|
|
|
# new card insertion order
|
|
NEW_CARDS_RANDOM = 0
|
|
NEW_CARDS_DUE = 1
|
|
|
|
# sort order for day's new cards
|
|
NEW_TODAY_ORD = 0
|
|
NEW_TODAY_DUE = 1
|
|
|
|
# review card sort order
|
|
REV_CARDS_OLD_FIRST = 0
|
|
REV_CARDS_NEW_FIRST = 1
|
|
REV_CARDS_RANDOM = 2
|
|
|
|
# Labels
|
|
##########################################################################
|
|
|
|
def newCardOrderLabels():
|
|
return {
|
|
0: _("Add new cards in random order"),
|
|
1: _("Add new cards to end of queue"),
|
|
}
|
|
|
|
def newCardSchedulingLabels():
|
|
return {
|
|
0: _("Spread new cards out through reviews"),
|
|
1: _("Show new cards after all other cards"),
|
|
2: _("Show new cards before reviews"),
|
|
}
|
|
|
|
# FIXME: order due is not very useful anymore
|
|
def revCardOrderLabels():
|
|
return {
|
|
0: _("Review cards from largest interval"),
|
|
1: _("Review cards from smallest interval"),
|
|
2: _("Review cards in order due"),
|
|
3: _("Review cards in random order"),
|
|
}
|
|
|
|
def failedCardOptionLabels():
|
|
return {
|
|
0: _("Show failed cards soon"),
|
|
1: _("Show failed cards at end"),
|
|
2: _("Show failed cards in 10 minutes"),
|
|
3: _("Show failed cards in 8 hours"),
|
|
4: _("Show failed cards in 3 days"),
|
|
5: _("Custom failed cards handling"),
|
|
}
|
|
|
|
def alignmentLabels():
|
|
return {
|
|
0: _("Center"),
|
|
1: _("Left"),
|
|
2: _("Right"),
|
|
}
|