Skip to content

Commit

Permalink
Use weakref.WeakSet rather than transaction.weakset.WeakSet
Browse files Browse the repository at this point in the history
  • Loading branch information
jmuchemb committed Sep 17, 2019
1 parent 79fe473 commit 082af72
Showing 1 changed file with 17 additions and 35 deletions.
52 changes: 17 additions & 35 deletions src/ZODB/DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import datetime
import time
import warnings
import weakref
from itertools import chain

from . import utils

Expand All @@ -28,8 +30,6 @@
from ZODB._compat import Pickler, _protocol, BytesIO
import ZODB.serialize

import transaction.weakset

from zope.interface import implementer
from ZODB.interfaces import IDatabase
from ZODB.interfaces import IMVCCStorage
Expand Down Expand Up @@ -80,7 +80,7 @@ def __init__(self, size, timeout):
# A weak set of all connections we've seen. A connection vanishes
# from this set if pop() hands it out, it's not reregistered via
# repush(), and it becomes unreachable.
self.all = transaction.weakset.WeakSet()
self.all = weakref.WeakSet()

def setSize(self, size):
"""Change our belief about the expected maximum # of live connections.
Expand Down Expand Up @@ -123,6 +123,9 @@ def __init__(self, size, timeout=1<<31):
# in this stack.
self.available = []

def __iter__(self):
return iter(self.all)

def _append(self, c):
available = self.available
cactive = c._cache.cache_non_ghost_count
Expand Down Expand Up @@ -208,10 +211,6 @@ def pop(self):
assert result in self.all
return result

def map(self, f):
"""For every live connection c, invoke f(c)."""
self.all.map(f)

def availableGC(self):
"""Perform garbage collection on available connections.
Expand Down Expand Up @@ -251,6 +250,9 @@ def __init__(self, size, timeout=1<<31):
super(KeyedConnectionPool, self).__init__(size, timeout)
self.pools = {}

def __iter__(self):
return chain(*self.pools.values())

def setSize(self, v):
self._size = v
for pool in self.pools.values():
Expand Down Expand Up @@ -284,10 +286,6 @@ def pop(self, key):
if pool is not None:
return pool.pop()

def map(self, f):
for pool in six.itervalues(self.pools):
pool.map(f)

def availableGC(self):
for key, pool in list(self.pools.items()):
pool.availableGC()
Expand All @@ -299,20 +297,6 @@ def clear(self):
pool.clear()
self.pools.clear()

@property
def test_all(self):
result = set()
for pool in six.itervalues(self.pools):
result.update(pool.all)
return frozenset(result)

@property
def test_available(self):
result = []
for pool in six.itervalues(self.pools):
result.extend(pool.available)
return tuple(result)


def toTimeStamp(dt):
utc_struct = dt.utctimetuple()
Expand Down Expand Up @@ -515,8 +499,10 @@ def _connectionMap(self, f):
"""Call f(c) for all connections c in all pools, live and historical.
"""
with self._lock:
self.pool.map(f)
self.historical_pool.map(f)
for c in self.pool:
f(c)
for c in self.historical_pool:
f(c)

def cacheDetail(self):
"""Return object counts by class accross all connections.
Expand Down Expand Up @@ -868,36 +854,32 @@ def setCacheSize(self, size):
"""
with self._lock:
self._cache_size = size
def setsize(c):
for c in self.pool:
c._cache.cache_size = size
self.pool.map(setsize)

def setCacheSizeBytes(self, size):
"""Reconfigure the cache total size in bytes
"""
with self._lock:
self._cache_size_bytes = size
def setsize(c):
for c in self.pool:
c._cache.cache_size_bytes = size
self.pool.map(setsize)

def setHistoricalCacheSize(self, size):
"""Reconfigure the historical cache size (non-ghost object count)
"""
with self._lock:
self._historical_cache_size = size
def setsize(c):
for c in self.historical_pool:
c._cache.cache_size = size
self.historical_pool.map(setsize)

def setHistoricalCacheSizeBytes(self, size):
"""Reconfigure the historical cache total size in bytes
"""
with self._lock:
self._historical_cache_size_bytes = size
def setsize(c):
for c in self.historical_pool:
c._cache.cache_size_bytes = size
self.historical_pool.map(setsize)

def setPoolSize(self, size):
"""Reconfigure the connection pool size
Expand Down

0 comments on commit 082af72

Please sign in to comment.