2015-10-11 02:22:53 +02:00
|
|
|
import array
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
import time
|
2015-10-11 02:22:53 +02:00
|
|
|
|
|
|
|
|
2015-11-15 11:13:57 +01:00
|
|
|
class PeerHashfield(object):
|
2019-03-16 00:42:21 +01:00
|
|
|
__slots__ = ("storage", "time_changed", "append", "remove", "tobytes", "frombytes", "__len__", "__iter__")
|
2015-10-11 02:22:53 +02:00
|
|
|
def __init__(self):
|
2015-11-15 11:13:57 +01:00
|
|
|
self.storage = self.createStorage()
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
self.time_changed = time.time()
|
2015-10-11 02:22:53 +02:00
|
|
|
|
2015-11-15 11:13:57 +01:00
|
|
|
def createStorage(self):
|
2015-10-11 02:22:53 +02:00
|
|
|
storage = array.array("H")
|
|
|
|
self.append = storage.append
|
|
|
|
self.remove = storage.remove
|
2019-03-16 00:42:21 +01:00
|
|
|
self.tobytes = storage.tobytes
|
|
|
|
self.frombytes = storage.frombytes
|
2015-10-11 02:22:53 +02:00
|
|
|
self.__len__ = storage.__len__
|
|
|
|
self.__iter__ = storage.__iter__
|
|
|
|
return storage
|
|
|
|
|
|
|
|
def appendHash(self, hash):
|
|
|
|
hash_id = int(hash[0:4], 16)
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
if hash_id not in self.storage:
|
|
|
|
self.storage.append(hash_id)
|
|
|
|
self.time_changed = time.time()
|
2015-10-11 02:22:53 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-10-22 11:42:55 +02:00
|
|
|
def appendHashId(self, hash_id):
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
if hash_id not in self.storage:
|
|
|
|
self.storage.append(hash_id)
|
|
|
|
self.time_changed = time.time()
|
2015-10-22 11:42:55 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-10-11 02:22:53 +02:00
|
|
|
def removeHash(self, hash):
|
|
|
|
hash_id = int(hash[0:4], 16)
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
if hash_id in self.storage:
|
|
|
|
self.storage.remove(hash_id)
|
|
|
|
self.time_changed = time.time()
|
2015-10-11 02:22:53 +02:00
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-11-07 22:35:05 +01:00
|
|
|
def removeHashId(self, hash_id):
|
|
|
|
if hash_id in self.storage:
|
|
|
|
self.storage.remove(hash_id)
|
|
|
|
self.time_changed = time.time()
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2015-10-11 02:22:53 +02:00
|
|
|
def getHashId(self, hash):
|
|
|
|
return int(hash[0:4], 16)
|
|
|
|
|
|
|
|
def hasHash(self, hash):
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
return int(hash[0:4], 16) in self.storage
|
2015-10-11 02:22:53 +02:00
|
|
|
|
2019-03-16 00:42:21 +01:00
|
|
|
def replaceFromBytes(self, hashfield_raw):
|
2015-11-15 11:13:57 +01:00
|
|
|
self.storage = self.createStorage()
|
2019-03-16 00:42:21 +01:00
|
|
|
self.storage.frombytes(hashfield_raw)
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
self.time_changed = time.time()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
field = PeerHashfield()
|
|
|
|
s = time.time()
|
|
|
|
for i in range(10000):
|
|
|
|
field.appendHashId(i)
|
2019-03-15 21:06:59 +01:00
|
|
|
print(time.time()-s)
|
Rev536, Fix stats page, Support ranged http requests for better video browser compatibility, setHashfield command, One by one send hashfield to connected peers if changed, Keep count hashfield changetime, PeerHashfield optimalizations, Wait for peers on checkmodification, Give more time to query trackers, Do not count udp trackers as error if udp disabled, Test hashfield push
2015-10-30 02:08:02 +01:00
|
|
|
s = time.time()
|
|
|
|
for i in range(10000):
|
|
|
|
field.hasHash("AABB")
|
2019-03-15 21:06:59 +01:00
|
|
|
print(time.time()-s)
|