Skip to content

Commit a7392fc

Browse files
ivandaschisapego
authored andcommitted
IGNITE-14429 Fix cache.get_size with non-default PeekModes
This closes #24
1 parent f00d70f commit a7392fc

File tree

6 files changed

+94
-31
lines changed

6 files changed

+94
-31
lines changed

pyignite/aio_cache.py

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

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

pyignite/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 pyignite.datatypes import Map, Bool, Byte, Int, Long, AnyDataArray, AnyDataObject
26+
from pyignite.datatypes import Map, Bool, Byte, Int, Long, AnyDataArray, AnyDataObject, ByteArray
2727
from pyignite.datatypes.base import IgniteDataType
28-
from pyignite.datatypes.key_value import PeekModes
2928
from pyignite.queries import Query, query_perform
3029
from pyignite.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: 'IgniteDataType' = 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: 'IgniteDataType' = 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
)

pyignite/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(

pyignite/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: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
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+
import pytest
17+
18+
from pyignite.datatypes.key_value import PeekModes
19+
from pyignite.datatypes.prop_codes import PROP_NAME, PROP_IS_ONHEAP_CACHE_ENABLED, PROP_BACKUPS_NUMBER
20+
from tests.util import get_or_create_cache, get_or_create_cache_async
21+
22+
test_params = [
23+
[
24+
{
25+
PROP_NAME: 'cache_onheap_backups_2',
26+
PROP_IS_ONHEAP_CACHE_ENABLED: True,
27+
PROP_BACKUPS_NUMBER: 2
28+
},
29+
[
30+
[None, 1],
31+
[PeekModes.PRIMARY, 1],
32+
[PeekModes.BACKUP, 2],
33+
[PeekModes.ALL, 3],
34+
[[PeekModes.PRIMARY, PeekModes.BACKUP], 3],
35+
[PeekModes.ONHEAP, 1],
36+
[PeekModes.OFFHEAP, 1]
37+
]
38+
]
39+
]
40+
41+
42+
@pytest.mark.parametrize("cache_settings, cache_sizes", test_params)
43+
def test_cache_size(client, cache_settings, cache_sizes):
44+
with get_or_create_cache(client, cache_settings) as cache:
45+
cache.put(1, 1)
46+
47+
for props, exp_value in cache_sizes:
48+
value = cache.get_size(props)
49+
assert value == exp_value, f"expected {exp_value} for {props}, got {value} instead."
50+
51+
52+
@pytest.mark.asyncio
53+
@pytest.mark.parametrize("cache_settings, cache_sizes", test_params)
54+
async def test_cache_size_async(async_client, cache_settings, cache_sizes):
55+
async with get_or_create_cache_async(async_client, cache_settings) as cache:
56+
await cache.put(1, 1)
57+
58+
for props, exp_value in cache_sizes:
59+
value = await cache.get_size(props)
60+
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
@@ -35,17 +35,17 @@
3535

3636

3737
@contextlib.contextmanager
38-
def get_or_create_cache(client, cache_name):
39-
cache = client.get_or_create_cache(cache_name)
38+
def get_or_create_cache(client, settings):
39+
cache = client.get_or_create_cache(settings)
4040
try:
4141
yield cache
4242
finally:
4343
cache.destroy()
4444

4545

4646
@asynccontextmanager
47-
async def get_or_create_cache_async(client, cache_name):
48-
cache = await client.get_or_create_cache(cache_name)
47+
async def get_or_create_cache_async(client, settings):
48+
cache = await client.get_or_create_cache(settings)
4949
try:
5050
yield cache
5151
finally:

0 commit comments

Comments
 (0)