chunk deletions to reduce load on AnkiWeb

This commit is contained in:
Damien Elmes 2018-07-26 21:34:53 +10:00
parent d6874de8c8
commit eb44584b29

View file

@ -75,12 +75,19 @@ class Syncer:
if not self.col.basicCheck(): if not self.col.basicCheck():
self.col.log("basic check") self.col.log("basic check")
return "basicCheckFailed" return "basicCheckFailed"
# step 2: deletions # step 2: startup and deletions
runHook("sync", "meta") runHook("sync", "meta")
lrem = self.removed() rrem = self.server.start(minUsn=self.minUsn, lnewer=self.lnewer)
rrem = self.server.start(
minUsn=self.minUsn, lnewer=self.lnewer, graves=lrem) # apply deletions to server
lgraves = self.removed()
while lgraves:
gchunk, lgraves = self._gravesChunk(lgraves)
self.server.applyGraves(chunk=gchunk)
# then apply server deletions here
self.remove(rrem) self.remove(rrem)
# ...and small objects # ...and small objects
lchg = self.changes() lchg = self.changes()
rchg = self.server.applyChanges(changes=lchg) rchg = self.server.applyChanges(changes=lchg)
@ -119,6 +126,20 @@ class Syncer:
self.finish(mod) self.finish(mod)
return "success" return "success"
def _gravesChunk(self, graves):
lim = 250
chunk = dict(notes=[], cards=[], decks=[])
for cat in "notes", "cards", "decks":
if lim and graves[cat]:
chunk[cat] = graves[cat][:lim]
graves[cat] = graves[cat][lim:]
lim -= len(chunk[cat])
# anything remaining?
if graves['notes'] or graves['cards'] or graves['decks']:
return chunk, graves
return chunk, None
def meta(self): def meta(self):
return dict( return dict(
mod=self.col.mod, mod=self.col.mod,
@ -565,6 +586,9 @@ class RemoteServer(HttpSyncer):
return return
return json.loads(ret.decode("utf8")) return json.loads(ret.decode("utf8"))
def applyGraves(self, **kw):
return self._run("applyGraves", kw)
def applyChanges(self, **kw): def applyChanges(self, **kw):
return self._run("applyChanges", kw) return self._run("applyChanges", kw)