Skip to content

Commit 3f5d779

Browse files
isapegoivandasch
andauthored
GG-32949 [IGNITE-14429] Fix cache.get_size with non-default PeekModes (#34)
(cherry picked from commit a7392fc) Co-authored-by: Ivan Dashchinskiy <ivandasch@gmail.com>
1 parent 33ff593 commit 3f5d779

6 files changed

Lines changed: 95 additions & 31 deletions

File tree

pygridgain/aio_cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,13 +573,13 @@ async def replace_if_equals(self, key, sample, value, key_hint=None, sample_hint
573573
return result
574574

575575
@status_to_exception(CacheError)
576-
async def get_size(self, peek_modes=0):
576+
async def get_size(self, peek_modes=None):
577577
"""
578578
Gets the number of entries in cache.
579579
580580
:param peek_modes: (optional) limit count to near cache partition
581581
(PeekModes.NEAR), primary cache (PeekModes.PRIMARY), or backup cache
582-
(PeekModes.BACKUP). Defaults to all cache partitions (PeekModes.ALL),
582+
(PeekModes.BACKUP). Defaults to primary cache partitions (PeekModes.PRIMARY),
583583
:return: integer number of cache entries.
584584
"""
585585
conn = await self.get_best_node()

pygridgain/api/key_value.py

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@
2323
OP_CACHE_CLEAR_KEYS, OP_CACHE_REMOVE_KEY, OP_CACHE_REMOVE_IF_EQUALS, OP_CACHE_REMOVE_KEYS, OP_CACHE_REMOVE_ALL,
2424
OP_CACHE_GET_SIZE, OP_CACHE_LOCAL_PEEK
2525
)
26-
from pygridgain.datatypes import Map, Bool, Byte, Int, Long, AnyDataArray, AnyDataObject
26+
from pygridgain.datatypes import Map, Bool, Byte, Int, Long, AnyDataArray, AnyDataObject, ByteArray
2727
from pygridgain.datatypes.base import GridGainDataType
28-
from pygridgain.datatypes.key_value import PeekModes
2928
from pygridgain.queries import Query, query_perform
3029
from pygridgain.utils import cache_id
3130

@@ -1128,7 +1127,7 @@ def __cache_remove_all(connection, cache, binary, query_id):
11281127
)
11291128

11301129

1131-
def cache_get_size(connection: 'Connection', cache: Union[str, int], peek_modes: Union[int, list, tuple] = 0,
1130+
def cache_get_size(connection: 'Connection', cache: Union[str, int], peek_modes: Union[int, list, tuple] = None,
11321131
binary: bool = False, query_id: Optional[int] = None) -> 'APIResult':
11331132
"""
11341133
Gets the number of entries in cache.
@@ -1137,7 +1136,7 @@ def cache_get_size(connection: 'Connection', cache: Union[str, int], peek_modes:
11371136
:param cache: name or ID of the cache,
11381137
:param peek_modes: (optional) limit count to near cache partition
11391138
(PeekModes.NEAR), primary cache (PeekModes.PRIMARY), or backup cache
1140-
(PeekModes.BACKUP). Defaults to all cache partitions (PeekModes.ALL),
1139+
(PeekModes.BACKUP). Defaults to pimary cache partitions (PeekModes.PRIMARY),
11411140
:param binary: (optional) pass True to keep the value in binary form.
11421141
False by default,
11431142
:param query_id: (optional) a value generated by client and returned as-is
@@ -1151,21 +1150,23 @@ def cache_get_size(connection: 'Connection', cache: Union[str, int], peek_modes:
11511150

11521151

11531152
async def cache_get_size_async(connection: 'AioConnection', cache: Union[str, int],
1154-
peek_modes: Union[int, list, tuple] = 0, binary: bool = False,
1153+
peek_modes: Union[int, list, tuple] = None, binary: bool = False,
11551154
query_id: Optional[int] = None) -> 'APIResult':
11561155
return await __cache_get_size(connection, cache, peek_modes, binary, query_id)
11571156

11581157

11591158
def __cache_get_size(connection, cache, peek_modes, binary, query_id):
1160-
if not isinstance(peek_modes, (list, tuple)):
1161-
peek_modes = [peek_modes] if peek_modes else []
1159+
if peek_modes is None:
1160+
peek_modes = []
1161+
elif not isinstance(peek_modes, (list, tuple)):
1162+
peek_modes = [peek_modes]
11621163

11631164
query_struct = Query(
11641165
OP_CACHE_GET_SIZE,
11651166
[
11661167
('hash_code', Int),
11671168
('flag', Byte),
1168-
('peek_modes', PeekModes),
1169+
('peek_modes', ByteArray),
11691170
],
11701171
query_id=query_id,
11711172
)
@@ -1184,7 +1185,7 @@ def __cache_get_size(connection, cache, peek_modes, binary, query_id):
11841185

11851186

11861187
def cache_local_peek(conn: 'Connection', cache: Union[str, int], key: Any, key_hint: 'GridGainDataType' = None,
1187-
peek_modes: Union[int, list, tuple] = 0, binary: bool = False,
1188+
peek_modes: Union[int, list, tuple] = None, binary: bool = False,
11881189
query_id: Optional[int] = None) -> 'APIResult':
11891190
"""
11901191
Peeks at in-memory cached value using default optional peek mode.
@@ -1199,7 +1200,7 @@ def cache_local_peek(conn: 'Connection', cache: Union[str, int], key: Any, key_h
11991200
should be converted,
12001201
:param peek_modes: (optional) limit count to near cache partition
12011202
(PeekModes.NEAR), primary cache (PeekModes.PRIMARY), or backup cache
1202-
(PeekModes.BACKUP). Defaults to all cache partitions (PeekModes.ALL),
1203+
(PeekModes.BACKUP). Defaults to primary cache partitions (PeekModes.PRIMARY),
12031204
:param binary: (optional) pass True to keep the value in binary form.
12041205
False by default,
12051206
:param query_id: (optional) a value generated by client and returned as-is
@@ -1213,24 +1214,27 @@ def cache_local_peek(conn: 'Connection', cache: Union[str, int], key: Any, key_h
12131214

12141215
async def cache_local_peek_async(
12151216
conn: 'AioConnection', cache: Union[str, int], key: Any, key_hint: 'GridGainDataType' = None,
1216-
peek_modes: Union[int, list, tuple] = 0, binary: bool = False, query_id: Optional[int] = None) -> 'APIResult':
1217+
peek_modes: Union[int, list, tuple] = None, binary: bool = False,
1218+
query_id: Optional[int] = None) -> 'APIResult':
12171219
"""
12181220
Async version of cache_local_peek.
12191221
"""
12201222
return await __cache_local_peek(conn, cache, key, key_hint, peek_modes, binary, query_id)
12211223

12221224

12231225
def __cache_local_peek(conn, cache, key, key_hint, peek_modes, binary, query_id):
1224-
if not isinstance(peek_modes, (list, tuple)):
1225-
peek_modes = [peek_modes] if peek_modes else []
1226+
if peek_modes is None:
1227+
peek_modes = []
1228+
elif not isinstance(peek_modes, (list, tuple)):
1229+
peek_modes = [peek_modes]
12261230

12271231
query_struct = Query(
12281232
OP_CACHE_LOCAL_PEEK,
12291233
[
12301234
('hash_code', Int),
12311235
('flag', Byte),
12321236
('key', key_hint or AnyDataObject),
1233-
('peek_modes', PeekModes),
1237+
('peek_modes', ByteArray),
12341238
],
12351239
query_id=query_id,
12361240
)

pygridgain/cache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,13 +694,13 @@ def replace_if_equals(
694694
return result
695695

696696
@status_to_exception(CacheError)
697-
def get_size(self, peek_modes=0):
697+
def get_size(self, peek_modes=None):
698698
"""
699699
Gets the number of entries in cache.
700700
701701
:param peek_modes: (optional) limit count to near cache partition
702702
(PeekModes.NEAR), primary cache (PeekModes.PRIMARY), or backup cache
703-
(PeekModes.BACKUP). Defaults to all cache partitions (PeekModes.ALL),
703+
(PeekModes.BACKUP). Defaults to primary cache partitions (PeekModes.PRIMARY),
704704
:return: integer number of cache entries.
705705
"""
706706
return cache_get_size(

pygridgain/datatypes/key_value.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,13 @@
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
1515
#
16-
from .primitive_arrays import ByteArray
16+
from enum import IntEnum
1717

1818

19-
class PeekModes(ByteArray):
20-
21-
ALL = 1
22-
NEAR = 2
23-
PRIMARY = 4
24-
BACKUP = 8
25-
ONHEAP = 16
26-
OFFHEAP = 32
19+
class PeekModes(IntEnum):
20+
ALL = 0
21+
NEAR = 1
22+
PRIMARY = 2
23+
BACKUP = 3
24+
ONHEAP = 4
25+
OFFHEAP = 5

tests/common/test_cache_size.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#
2+
# Copyright 2021 GridGain Systems, Inc. and Contributors.
3+
#
4+
# Licensed under the GridGain Community Edition License (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# https://www.gridgain.com/products/software/community-edition/gridgain-community-edition-license
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
import pytest
18+
19+
from pygridgain.datatypes.key_value import PeekModes
20+
from pygridgain.datatypes.prop_codes import PROP_NAME, PROP_IS_ONHEAP_CACHE_ENABLED, PROP_BACKUPS_NUMBER
21+
from tests.util import get_or_create_cache, get_or_create_cache_async
22+
23+
test_params = [
24+
[
25+
{
26+
PROP_NAME: 'cache_onheap_backups_2',
27+
PROP_IS_ONHEAP_CACHE_ENABLED: True,
28+
PROP_BACKUPS_NUMBER: 2
29+
},
30+
[
31+
[None, 1],
32+
[PeekModes.PRIMARY, 1],
33+
[PeekModes.BACKUP, 2],
34+
[PeekModes.ALL, 3],
35+
[[PeekModes.PRIMARY, PeekModes.BACKUP], 3],
36+
[PeekModes.ONHEAP, 1],
37+
[PeekModes.OFFHEAP, 1]
38+
]
39+
]
40+
]
41+
42+
43+
@pytest.mark.parametrize("cache_settings, cache_sizes", test_params)
44+
def test_cache_size(client, cache_settings, cache_sizes):
45+
with get_or_create_cache(client, cache_settings) as cache:
46+
cache.put(1, 1)
47+
48+
for props, exp_value in cache_sizes:
49+
value = cache.get_size(props)
50+
assert value == exp_value, f"expected {exp_value} for {props}, got {value} instead."
51+
52+
53+
@pytest.mark.asyncio
54+
@pytest.mark.parametrize("cache_settings, cache_sizes", test_params)
55+
async def test_cache_size_async(async_client, cache_settings, cache_sizes):
56+
async with get_or_create_cache_async(async_client, cache_settings) as cache:
57+
await cache.put(1, 1)
58+
59+
for props, exp_value in cache_sizes:
60+
value = await cache.get_size(props)
61+
assert value == exp_value, f"expected {exp_value} for {props}, got {value} instead."

tests/util.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@
3737

3838

3939
@contextlib.contextmanager
40-
def get_or_create_cache(client, cache_name):
41-
cache = client.get_or_create_cache(cache_name)
40+
def get_or_create_cache(client, settings):
41+
cache = client.get_or_create_cache(settings)
4242
try:
4343
yield cache
4444
finally:
4545
cache.destroy()
4646

4747

4848
@asynccontextmanager
49-
async def get_or_create_cache_async(client, cache_name):
50-
cache = await client.get_or_create_cache(cache_name)
49+
async def get_or_create_cache_async(client, settings):
50+
cache = await client.get_or_create_cache(settings)
5151
try:
5252
yield cache
5353
finally:

0 commit comments

Comments
 (0)