Skip to content

Commit 6698bff

Browse files
committed
Fix only formatting in policy and tablets related code
1 parent fab07e1 commit 6698bff

File tree

2 files changed

+50
-46
lines changed

2 files changed

+50
-46
lines changed

cassandra/policies.py

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -373,37 +373,38 @@ def make_query_plan(self, working_keyspace=None, query=None):
373373
if query is None:
374374
for host in child.make_query_plan(keyspace, query):
375375
yield host
376-
else:
377-
routing_key = query.routing_key
378-
if routing_key is None or keyspace is None:
379-
for host in child.make_query_plan(keyspace, query):
380-
yield host
381-
else:
382-
replicas = []
383-
if self._tablets_routing_v1:
384-
tablet = self._cluster_metadata._tablets.get_tablet_for_key(keyspace, query.table, self._cluster_metadata.token_map.token_class.from_key(routing_key))
385-
386-
if tablet is not None:
387-
replicas_mapped = set(map(lambda r: r[0], tablet.replicas))
388-
child_plan = child.make_query_plan(keyspace, query)
389-
390-
replicas = [host for host in child_plan if host.host_id in replicas_mapped]
391-
392-
if replicas == []:
393-
replicas = self._cluster_metadata.get_replicas(keyspace, routing_key)
394-
395-
if self.shuffle_replicas:
396-
shuffle(replicas)
397-
for replica in replicas:
398-
if replica.is_up and \
399-
child.distance(replica) == HostDistance.LOCAL:
400-
yield replica
401-
402-
for host in child.make_query_plan(keyspace, query):
403-
# skip if we've already listed this host
404-
if host not in replicas or \
405-
child.distance(host) == HostDistance.REMOTE:
406-
yield host
376+
return
377+
routing_key = query.routing_key
378+
if routing_key is None or keyspace is None:
379+
for host in child.make_query_plan(keyspace, query):
380+
yield host
381+
return
382+
383+
replicas = []
384+
if self._tablets_routing_v1:
385+
tablet = self._cluster_metadata._tablets.get_tablet_for_key(
386+
keyspace, query.table, self._cluster_metadata.token_map.token_class.from_key(routing_key))
387+
388+
if tablet is not None:
389+
replicas_mapped = set(map(lambda r: r[0], tablet.replicas))
390+
child_plan = child.make_query_plan(keyspace, query)
391+
392+
replicas = [host for host in child_plan if host.host_id in replicas_mapped]
393+
394+
if not replicas:
395+
replicas = self._cluster_metadata.get_replicas(keyspace, routing_key)
396+
397+
if self.shuffle_replicas:
398+
shuffle(replicas)
399+
400+
for replica in replicas:
401+
if replica.is_up and child.distance(replica) == HostDistance.LOCAL:
402+
yield replica
403+
404+
for host in child.make_query_plan(keyspace, query):
405+
# skip if we've already listed this host
406+
if host not in replicas or child.distance(host) == HostDistance.REMOTE:
407+
yield host
407408

408409
def on_up(self, *args, **kwargs):
409410
return self._child_policy.on_up(*args, **kwargs)

cassandra/tablets.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Experimental, this interface and use may change
22
from threading import Lock
33

4+
45
class Tablet(object):
56
"""
67
Represents a single ScyllaDB tablet.
@@ -11,7 +12,7 @@ class Tablet(object):
1112
last_token = 0
1213
replicas = None
1314

14-
def __init__(self, first_token = 0, last_token = 0, replicas = None):
15+
def __init__(self, first_token=0, last_token=0, replicas=None):
1516
self.first_token = first_token
1617
self.last_token = last_token
1718
self.replicas = replicas
@@ -28,10 +29,11 @@ def _is_valid_tablet(replicas):
2829
@staticmethod
2930
def from_row(first_token, last_token, replicas):
3031
if Tablet._is_valid_tablet(replicas):
31-
tablet = Tablet(first_token, last_token,replicas)
32+
tablet = Tablet(first_token, last_token, replicas)
3233
return tablet
3334
return None
3435

36+
3537
# Experimental, this interface and use may change
3638
class Tablets(object):
3739
_lock = None
@@ -43,10 +45,10 @@ def __init__(self, tablets):
4345

4446
def get_tablet_for_key(self, keyspace, table, t):
4547
tablet = self._tablets.get((keyspace, table), [])
46-
if tablet == []:
48+
if not tablet:
4749
return None
4850

49-
id = bisect_left(tablet, t.value, key = lambda tablet: tablet.last_token)
51+
id = bisect_left(tablet, t.value, key=lambda tablet: tablet.last_token)
5052
if id < len(tablet) and t.value > tablet[id].first_token:
5153
return tablet[id]
5254
return None
@@ -55,13 +57,13 @@ def add_tablet(self, keyspace, table, tablet):
5557
with self._lock:
5658
tablets_for_table = self._tablets.setdefault((keyspace, table), [])
5759

58-
# find first overlaping range
59-
start = bisect_left(tablets_for_table, tablet.first_token, key = lambda t: t.first_token)
60+
# find first overlapping range
61+
start = bisect_left(tablets_for_table, tablet.first_token, key=lambda t: t.first_token)
6062
if start > 0 and tablets_for_table[start - 1].last_token > tablet.first_token:
6163
start = start - 1
6264

63-
# find last overlaping range
64-
end = bisect_left(tablets_for_table, tablet.last_token, key = lambda t: t.last_token)
65+
# find last overlapping range
66+
end = bisect_left(tablets_for_table, tablet.last_token, key=lambda t: t.last_token)
6567
if end < len(tablets_for_table) and tablets_for_table[end].first_token >= tablet.last_token:
6668
end = end - 1
6769

@@ -70,6 +72,7 @@ def add_tablet(self, keyspace, table, tablet):
7072

7173
tablets_for_table.insert(start, tablet)
7274

75+
7376
# bisect.bisect_left implementation from Python 3.11, needed untill support for
7477
# Python < 3.10 is dropped, it is needed to use `key` to extract last_token from
7578
# Tablet list - better solution performance-wise than materialize list of last_tokens
@@ -97,11 +100,11 @@ def bisect_left(a, x, lo=0, hi=None, *, key=None):
97100
lo = mid + 1
98101
else:
99102
hi = mid
100-
else:
101-
while lo < hi:
102-
mid = (lo + hi) // 2
103-
if key(a[mid]) < x:
104-
lo = mid + 1
105-
else:
106-
hi = mid
103+
return
104+
while lo < hi:
105+
mid = (lo + hi) // 2
106+
if key(a[mid]) < x:
107+
lo = mid + 1
108+
else:
109+
hi = mid
107110
return lo

0 commit comments

Comments
 (0)