mirror of
https://github.com/ankitects/anki.git
synced 2025-11-14 00:27:13 -05:00
Ported the sync code to the latest libanki structure. Key points: No summary: The old style got each side to fetch ids+mod times and required the client to diff them and then request or bundle up the appropriate objects. Instead, we now get each side to send all changed objects, and it's the responsibility of the other side to decide what needs to be merged and what needs to be discarded. This allows us to skip a separate summary step, which saves scanning tables twice, and allows us to reduce server requests from 4 to 3. Schema changes: Certain operations that are difficult to merge (such as changing the number of fields in a model, or deleting models or groups) result in a full sync. The user is warned about it in the GUI before such schema-changing operations execute. Sync size: For now, we don't try to deal with large incremental syncs. Because the cards, facts and revlog can be large in memory (hundreds of megabytes in some cases), they would have to be chunked for the benefit of devices with a low amount of memory. Currently findChanges() uses the full fact/card objects which we're planning to send to the server. It could be rewritten to fetch a summary (just the id, mod & rep columns) which would save some memory, and then compare against blocks of a few hundred remote objects at a time. However, it's a bit more complicated than that: - If the local summary is huge it could exceed memory limits. Without a local summary we'd have to query the db for each record, which could be a lot slower. - We currently accumulate a list of remote records we need to add locally. This list also has the potential to get too big. We would need to periodically commit the changes as we accumulate them. - Merging a large amount of changes is also potentially slow on mobile devices. Given the fact that certain schema-changing operations require a full sync anyway, I think it's probably best to concentrate on a chunked full sync for now instead, as provided the user syncs periodically it should not be easy to hit the full sync limits except after bulk editing operations. Chunked partial syncing should be possible to add in the future without any changes to the deck format. Still to do: - deck conf merging - full syncing - new http proxy
172 lines
5.3 KiB
Python
172 lines
5.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Copyright: Damien Elmes <anki@ichi2.net>
|
|
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
import time
|
|
from anki.errors import AnkiError
|
|
from anki.utils import fieldChecksum, intTime, \
|
|
joinFields, splitFields, ids2str, stripHTML, timestampID
|
|
|
|
class Fact(object):
|
|
|
|
def __init__(self, deck, model=None, id=None):
|
|
assert not (model and id)
|
|
self.deck = deck
|
|
if id:
|
|
self.id = id
|
|
self.load()
|
|
else:
|
|
self.id = timestampID(deck.db, "facts")
|
|
self._model = model
|
|
self.gid = model['gid']
|
|
self.mid = model['id']
|
|
self.tags = []
|
|
self.fields = [""] * len(self._model['flds'])
|
|
self.data = ""
|
|
self._fmap = self.deck.models.fieldMap(self._model)
|
|
|
|
def load(self):
|
|
(self.mid,
|
|
self.gid,
|
|
self.mod,
|
|
self.tags,
|
|
self.fields,
|
|
self.data) = self.deck.db.first("""
|
|
select mid, gid, mod, tags, flds, data from facts where id = ?""", self.id)
|
|
self.fields = splitFields(self.fields)
|
|
self.tags = self.deck.tags.split(self.tags)
|
|
self._model = self.deck.models.get(self.mid)
|
|
self._fmap = self.deck.models.fieldMap(self._model)
|
|
|
|
def flush(self, mod=None):
|
|
self.mod = mod if mod else intTime()
|
|
sfld = stripHTML(self.fields[self.deck.models.sortIdx(self._model)])
|
|
tags = self.stringTags()
|
|
res = self.deck.db.execute("""
|
|
insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
self.id, self.mid, self.gid,
|
|
self.mod, tags, self.joinedFields(),
|
|
sfld, self.data)
|
|
self.id = res.lastrowid
|
|
self.updateFieldChecksums()
|
|
self.deck.tags.register(self.tags)
|
|
|
|
def joinedFields(self):
|
|
return joinFields(self.fields)
|
|
|
|
def updateFieldChecksums(self):
|
|
self.deck.db.execute("delete from fsums where fid = ?", self.id)
|
|
d = []
|
|
for (ord, conf) in self._fmap.values():
|
|
if not conf['uniq']:
|
|
continue
|
|
val = self.fields[ord]
|
|
if not val:
|
|
continue
|
|
d.append((self.id, self.mid, fieldChecksum(val)))
|
|
self.deck.db.executemany("insert into fsums values (?, ?, ?)", d)
|
|
|
|
def cards(self):
|
|
return [self.deck.getCard(id) for id in self.deck.db.list(
|
|
"select id from cards where fid = ? order by ord", self.id)]
|
|
|
|
def model(self):
|
|
return self._model
|
|
|
|
def updateCardGids(self):
|
|
for c in self.cards():
|
|
if c.gid != self.gid and not c.template()['gid']:
|
|
c.gid = self.gid
|
|
c.flush()
|
|
|
|
# Dict interface
|
|
##################################################
|
|
|
|
def keys(self):
|
|
return self._fmap.keys()
|
|
|
|
def values(self):
|
|
return self.fields
|
|
|
|
def items(self):
|
|
return [(f['name'], self.fields[ord])
|
|
for ord, f in sorted(self._fmap.values())]
|
|
|
|
def _fieldOrd(self, key):
|
|
try:
|
|
return self._fmap[key][0]
|
|
except:
|
|
raise KeyError(key)
|
|
|
|
def __getitem__(self, key):
|
|
return self.fields[self._fieldOrd(key)]
|
|
|
|
def __setitem__(self, key, value):
|
|
self.fields[self._fieldOrd(key)] = value
|
|
|
|
# Tags
|
|
##################################################
|
|
|
|
def hasTag(self, tag):
|
|
return self.deck.tags.inList(tag, self.tags)
|
|
|
|
def stringTags(self):
|
|
return self.deck.tags.canonify(self.tags)
|
|
|
|
def delTag(self, tag):
|
|
rem = []
|
|
for t in self.tags:
|
|
if t.lower() == tag.lower():
|
|
rem.append(t)
|
|
for r in rem:
|
|
self.tags.remove(r)
|
|
|
|
def addTag(self, tag):
|
|
# duplicates will be stripped on save
|
|
self.tags.append(tag)
|
|
|
|
# Unique/duplicate checks
|
|
##################################################
|
|
|
|
def fieldUnique(self, name):
|
|
(ord, conf) = self._fmap[name]
|
|
if not conf['uniq']:
|
|
return True
|
|
val = self[name]
|
|
if not val:
|
|
return True
|
|
csum = fieldChecksum(val)
|
|
if self.id:
|
|
lim = "and fid != :fid"
|
|
else:
|
|
lim = ""
|
|
fids = self.deck.db.list(
|
|
"select fid from fsums where csum = ? and fid != ? and mid = ?",
|
|
csum, self.id or 0, self.mid)
|
|
if not fids:
|
|
return True
|
|
# grab facts with the same checksums, and see if they're actually
|
|
# duplicates
|
|
for flds in self.deck.db.list("select flds from facts where id in "+
|
|
ids2str(fids)):
|
|
fields = splitFields(flds)
|
|
if fields[ord] == val:
|
|
return False
|
|
return True
|
|
|
|
def fieldComplete(self, name, text=None):
|
|
(ord, conf) = self._fmap[name]
|
|
if not conf['req']:
|
|
return True
|
|
return self[name]
|
|
|
|
def problems(self):
|
|
d = []
|
|
for (k, (ord, conf)) in self._fmap.items():
|
|
if not self.fieldUnique(k):
|
|
d.append((ord, "unique"))
|
|
elif not self.fieldComplete(k):
|
|
d.append((ord, "required"))
|
|
else:
|
|
d.append((ord, None))
|
|
return [x[1] for x in sorted(d)]
|