Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
self.__data.update(other)
def rawData(self):
"""Return internal dict - use it carefully"""
return self.__data
def __len__(self):
"""Return size of set"""
return len(self.__data)
def __contains__(self, item):
"""True if item exists"""
return item in self.__data
class ReplQueue(SyncObjConsumer):
def __init__(self, maxsize=0):
"""
Replicated FIFO queue. Based on collections.deque.
Has an interface similar to Queue.
:param maxsize: Max queue size.
:type maxsize: int
"""
super(ReplQueue, self).__init__()
self.__maxsize = maxsize
self.__data = collections.deque()
def qsize(self):
"""Return size of queue"""
return len(self.__data)
False - if queue is full and item can not be placed."""
if self.__maxsize and len(self.__data) >= self.__maxsize:
return False
heapq.heappush(self.__data, item)
return True
@replicated
def get(self, default=None):
"""Extract the smallest item from queue.
Return default if queue is empty."""
if not self.__data:
return default
return heapq.heappop(self.__data)
class _ReplLockManagerImpl(SyncObjConsumer):
def __init__(self, autoUnlockTime):
super(_ReplLockManagerImpl, self).__init__()
self.__locks = {}
self.__autoUnlockTime = autoUnlockTime
@replicated
def acquire(self, lockID, clientID, currentTime):
existingLock = self.__locks.get(lockID, None)
# Auto-unlock old lock
if existingLock is not None:
if currentTime - existingLock[1] > self.__autoUnlockTime:
existingLock = None
# Acquire lock if possible
if existingLock is None or existingLock[0] == clientID:
self.__locks[lockID] = (clientID, currentTime)
return True
import threading
import weakref
import time
import socket
import os
import collections
import heapq
from .syncobj import SyncObjConsumer, replicated
class ReplCounter(SyncObjConsumer):
def __init__(self):
"""
Simple distributed counter. You can set, add, sub and inc counter value.
"""
super(ReplCounter, self).__init__()
self.__counter = int()
@replicated
def set(self, newValue):
"""
Set new value to a counter.
:param newValue: new value
:return: new counter value
"""
self.__counter = newValue
def newFunc(self, *args, **kwargs):
if kwargs.pop('_doApply', False):
return func(self, *args, **kwargs)
else:
if isinstance(self, SyncObj):
applier = self._applyCommand
funcName = self._getFuncName(func.__name__)
funcID = self._methodToID[funcName]
elif isinstance(self, SyncObjConsumer):
consumerId = id(self)
funcName = self._syncObj._getFuncName((consumerId, func.__name__))
funcID = self._syncObj._methodToID[(consumerId, funcName)]
applier = self._syncObj._applyCommand
else:
raise SyncObjException("Class should be inherited from SyncObj or SyncObjConsumer")
callback = kwargs.pop('callback', None)
if kwargs:
cmd = (funcID, args, kwargs)
elif args and not kwargs:
cmd = (funcID, args)
else:
cmd = funcID
sync = kwargs.pop('sync', False)
if callback is not None:
"""
Increments counter value by one.
:return: new counter value
"""
self.__counter += 1
return self.__counter
def get(self):
"""
:return: current counter value
"""
return self.__counter
class ReplList(SyncObjConsumer):
def __init__(self):
"""
Distributed list - it has an interface similar to a regular list.
"""
super(ReplList, self).__init__()
self.__data = []
@replicated
def reset(self, newData):
"""Replace list with a new one"""
assert isinstance(newData, list)
self.__data = newData
@replicated
def set(self, position, newValue):
"""Update value at given position."""
@replicated(ver=1)
def __setitem__(self, position, element):
"""Update value at given position."""
self.__data[position] = element
def __len__(self):
"""Return the number of items of a sequence or collection."""
return len(self.__data)
def rawData(self):
"""Return internal list - use it carefully"""
return self.__data
class ReplDict(SyncObjConsumer):
def __init__(self):
"""
Distributed dict - it has an interface similar to a regular dict.
"""
super(ReplDict, self).__init__()
self.__data = {}
@replicated
def reset(self, newData):
"""Replace dict with a new one"""
assert isinstance(newData, dict)
self.__data = newData
@replicated
def __setitem__(self, key, value):
"""Set value for specified key"""
else:
self.__conf = conf
self.__conf.validate()
if self.__conf.password is not None:
if not HAS_CRYPTO:
raise ImportError("Please install 'cryptography' module")
self.__encryptor = getEncryptor(self.__conf.password)
else:
self.__encryptor = None
consumers = consumers or []
newConsumers = []
for c in consumers:
if not isinstance(c, SyncObjConsumer) and getattr(c, '_consumer', None):
c = c._consumer()
if not isinstance(c, SyncObjConsumer):
raise SyncObjException('Consumers must be inherited from SyncObjConsumer')
newConsumers.append(c)
consumers = newConsumers
self.__consumers = consumers
if not isinstance(selfNode, Node) and selfNode is not None:
selfNode = nodeClass(selfNode)
self.__selfNode = selfNode
self.__otherNodes = set() # set of Node
for otherNode in otherNodes:
if not isinstance(otherNode, Node):
otherNode = nodeClass(otherNode)
self.__otherNodes.add(otherNode)
return self.__data.keys()
def values(self):
"""Return all values"""
return self.__data.values()
def items(self):
"""Return all items"""
return self.__data.items()
def rawData(self):
"""Return internal dict - use it carefully"""
return self.__data
class ReplSet(SyncObjConsumer):
def __init__(self):
"""
Distributed set - it has an interface similar to a regular set.
"""
super(ReplSet, self).__init__()
self.__data = set()
@replicated
def reset(self, newData):
"""Replace set with a new one"""
assert isinstance(newData, set)
self.__data = newData
@replicated
def add(self, item):
"""Add an element to a set"""
if self.__maxsize and len(self.__data) >= self.__maxsize:
return False
self.__data.append(item)
return True
@replicated
def get(self, default=None):
"""Extract item from queue.
Return default if queue is empty."""
try:
return self.__data.popleft()
except:
return default
class ReplPriorityQueue(SyncObjConsumer):
def __init__(self, maxsize=0):
"""
Replicated priority queue. Based on heapq.
Has an interface similar to Queue.
:param maxsize: Max queue size.
:type maxsize: int
"""
super(ReplPriorityQueue, self).__init__()
self.__maxsize = maxsize
self.__data = []
def qsize(self):
"""Return size of queue"""
return len(self.__data)