Skip to content

Commit c2d37ed

Browse files
committed
fixed issue with 10k keys limit
1 parent 4461bf6 commit c2d37ed

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

splitapiclient/resources/segment_definition.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,23 @@ def import_keys_from_json(self, replace_keys, json_data, apiclient=None):
103103
:rtype: boolean
104104
'''
105105
imc = require_client('SegmentDefinition', self._client, apiclient)
106-
return imc.import_keys_from_json(self._name, self._environment['id'], replace_keys, json_data)
106+
keys = json_data['keys']
107+
if(len(keys) > 10000):
108+
# Split keys into batches of 10,000
109+
key_batches = [keys[i:i + 10000] for i in range(0, len(keys), 10000)]
110+
success = True
111+
# Process each batch
112+
for key_batch in key_batches:
113+
# Make a copy of the json_data to avoid modifying the original
114+
batch_data = json_data.copy()
115+
batch_data['keys'] = key_batch
116+
# If any batch fails, mark the entire operation as failed
117+
batch_result = imc.import_keys_from_json(self._name, self._environment['id'], replace_keys, batch_data)
118+
if not batch_result:
119+
success = False
120+
return success
121+
else:
122+
return imc.import_keys_from_json(self._name, self._environment['id'], replace_keys, json_data)
107123

108124
def remove_keys(self, json_data, apiclient=None):
109125
'''

splitapiclient/tests/resources/test_segment_definition.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,46 @@ def test_import_keys_from_json(self, mocker):
108108
)
109109
assert attr == True
110110

111+
def test_import_keys_from_json_large_batch(self, mocker):
112+
"""Test importing more than 10,000 keys to verify batch processing"""
113+
# Create a large list of keys (e.g., 25,000)
114+
large_key_list = [f"id{i}" for i in range(25000)]
115+
data = {"keys": large_key_list, "comment": "large batch test"}
116+
117+
# Mock the microclient
118+
mock_imc = mocker.Mock()
119+
mock_imc.import_keys_from_json.return_value = True
120+
121+
# Mock the require_client function to return our mock
122+
mocker.patch('splitapiclient.resources.segment_definition.require_client',
123+
return_value=mock_imc)
124+
125+
seg = SegmentDefinition(
126+
{
127+
'name': 'name',
128+
'environment': {
129+
'id': '1',
130+
'name': 'env'
131+
},
132+
'trafficType': {},
133+
}
134+
)
135+
136+
# Call the method
137+
result = seg.import_keys_from_json(False, data)
138+
139+
# Verify the method returns True when all batches succeed
140+
assert result is True
141+
142+
# Verify the microclient was called 3 times (for 25,000 keys)
143+
assert mock_imc.import_keys_from_json.call_count == 3
144+
145+
# Verify each batch had the correct number of keys
146+
calls = mock_imc.import_keys_from_json.call_args_list
147+
assert len(calls[0][0][3]['keys']) == 10000 # First batch
148+
assert len(calls[1][0][3]['keys']) == 10000 # Second batch
149+
assert len(calls[2][0][3]['keys']) == 5000 # Last batch
150+
111151
def test_remove_keys(self, mocker):
112152
'''
113153
'''

0 commit comments

Comments
 (0)