Skip to content

Commit 9f7c5ba

Browse files
authored
GG-30304 Fix partition awareness.
1 parent f17cd62 commit 9f7c5ba

3 files changed

Lines changed: 42 additions & 6 deletions

File tree

pygridgain/api/affinity.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from typing import Iterable, Union
1717

1818
from pygridgain.datatypes import Bool, Int, Long, UUIDObject
19-
from pygridgain.datatypes.internal import StructArray
19+
from pygridgain.datatypes.internal import StructArray, Conditional, Struct
2020
from pygridgain.queries import Query
2121
from pygridgain.queries.op_codes import OP_CACHE_PARTITIONS
2222
from pygridgain.utils import is_iterable
@@ -46,10 +46,22 @@
4646
('cache_config', cache_config),
4747
])
4848

49+
empty_cache_mapping = StructArray([
50+
('cache_id', Int)
51+
])
52+
53+
empty_node_mapping = Struct([])
54+
4955
partition_mapping = StructArray([
5056
('is_applicable', Bool),
51-
('cache_mapping', cache_mapping),
52-
('node_mapping', node_mapping),
57+
58+
('cache_mapping', Conditional(lambda ctx: ctx['is_applicable'] == b'\x01',
59+
lambda ctx: ctx['is_applicable'] is True,
60+
cache_mapping, empty_cache_mapping)),
61+
62+
('node_mapping', Conditional(lambda ctx: ctx['is_applicable'] == b'\x01',
63+
lambda ctx: ctx['is_applicable'] is True,
64+
node_mapping, empty_node_mapping)),
5365
])
5466

5567

pygridgain/cache.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ def get_best_node(
282282
# calculate the number of partitions
283283
parts = sum(
284284
[len(p) for _, p in self.affinity['node_mapping'].items()]
285-
)
285+
) if 'node_mapping' in self.affinity else 0
286+
286287
self.affinity['number_of_partitions'] = parts
287288
else:
288289
# get number of partitions

pygridgain/datatypes/internal.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import ctypes
1818
import decimal
1919
from datetime import date, datetime, timedelta
20-
from typing import Any, Tuple, Union
20+
from typing import Any, Tuple, Union, Callable
2121
import uuid
2222

2323
import attr
@@ -111,6 +111,20 @@ def tc_map(key: bytes, _memo_map: dict = {}):
111111
return _memo_map[key]
112112

113113

114+
class Conditional:
115+
116+
def __init__(self, predicate1: Callable[[any], bool], predicate2: Callable[[any], bool], var1, var2):
117+
self.predicate1 = predicate1
118+
self.predicate2 = predicate2
119+
self.var1 = var1
120+
self.var2 = var2
121+
122+
def parse(self, client: 'Client', context):
123+
return self.var1.parse(client) if self.predicate1(context) else self.var2.parse(client)
124+
125+
def to_python(self, ctype_object, context, *args, **kwargs):
126+
return self.var1.to_python(ctype_object, *args, **kwargs) if self.predicate2(context) else self.var2.to_python(ctype_object, *args, **kwargs)
127+
114128
@attr.s
115129
class StructArray:
116130
""" `counter_type` counter, followed by count*following structure. """
@@ -193,13 +207,17 @@ def parse(
193207
) -> Tuple[ctypes.LittleEndianStructure, bytes]:
194208
buffer = b''
195209
fields = []
210+
values = {}
196211

197212
for name, c_type in self.fields:
198-
c_type, buffer_fragment = c_type.parse(client)
213+
is_cond = isinstance(c_type, Conditional)
214+
c_type, buffer_fragment = c_type.parse(client, values) if is_cond else c_type.parse(client)
199215
buffer += buffer_fragment
200216

201217
fields.append((name, c_type))
202218

219+
values[name] = buffer_fragment
220+
203221
data_class = type(
204222
'StructLE',
205223
(ctypes.LittleEndianStructure,),
@@ -216,7 +234,12 @@ def to_python(
216234
) -> Union[dict, OrderedDict]:
217235
result = self.dict_type()
218236
for name, c_type in self.fields:
237+
is_cond = isinstance(c_type, Conditional)
219238
result[name] = c_type.to_python(
239+
getattr(ctype_object, name),
240+
result,
241+
*args, **kwargs
242+
) if is_cond else c_type.to_python(
220243
getattr(ctype_object, name),
221244
*args, **kwargs
222245
)

0 commit comments

Comments
 (0)