Download tracking fix

- don't reset tracking too early
- handle inserts when tracking objects
This commit is contained in:
Peter Šurda 2018-02-01 22:31:45 +01:00
parent 8a5ec29540
commit 451174b566
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87

View File

@ -22,15 +22,6 @@ class RandomTrackingDict(object):
def __getitem__(self, key): def __getitem__(self, key):
return self.dictionary[key][1] return self.dictionary[key][1]
def __setitem__(self, key, value):
with self.lock:
if key in self.dictionary:
self.dictionary[key][1] = value
else:
self.indexDict.append(key)
self.dictionary[key] = [self.len, value]
self.len += 1
def _swap(self, i1, i2): def _swap(self, i1, i2):
with self.lock: with self.lock:
key1 = self.indexDict[i1] key1 = self.indexDict[i1]
@ -42,6 +33,16 @@ class RandomTrackingDict(object):
# for quick reassignment # for quick reassignment
return i2 return i2
def __setitem__(self, key, value):
with self.lock:
if key in self.dictionary:
self.dictionary[key][1] = value
else:
self.indexDict.append(key)
self.dictionary[key] = [self.len, value]
self._swap(self.len, self.len - self.pendingLen)
self.len += 1
def __delitem__(self, key): def __delitem__(self, key):
if not key in self.dictionary: if not key in self.dictionary:
raise KeyError raise KeyError
@ -70,12 +71,13 @@ class RandomTrackingDict(object):
self.pendingTimeout = pendingTimeout self.pendingTimeout = pendingTimeout
def randomKeys(self, count=1): def randomKeys(self, count=1):
if self.lastPoll + self.pendingTimeout < time(): if self.len == 0 or (self.pendingLen >= self.maxPending and
with self.lock: self.lastPoll + self.pendingTimeout > time():
self.pendingLen = 0
if self.len == 0 or self.pendingLen >= self.maxPending:
raise KeyError raise KeyError
# reset if we've requested all
with self.lock: with self.lock:
if self.pendingLen == self.len:
self.pendingLen = 0
available = self.len - self.pendingLen available = self.len - self.pendingLen
if count > available: if count > available:
count = available count = available