Skip to content

Commit cc43c15

Browse files
authored
Merge pull request #166 from poissoncorp/RDBC-614
RDBC-614 Refactor GetCompareExchangeValuesOperation
2 parents 95f82c5 + a43c5ea commit cc43c15

File tree

5 files changed

+56
-54
lines changed

5 files changed

+56
-54
lines changed

ravendb/documents/operations/compare_exchange/compare_exchange.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ def get_value(
8989
raise RavenException(
9090
f"Unable to read compare exchange value: {self.__original_value.value}", ex
9191
)
92-
else:
92+
elif isinstance(self.__original_value.value, dict):
9393
entity = Utils.convert_json_dict_to_object(self.__original_value.value, object_type)
94+
else:
95+
entity = self.__original_value.value
9496

9597
value = CompareExchangeValue(self._key, self._index, entity)
9698
self.__value = value

ravendb/documents/operations/compare_exchange/operations.py

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -190,31 +190,30 @@ def get_raft_unique_request_id(self) -> str:
190190
class GetCompareExchangeValuesOperation(IOperation[Dict[str, CompareExchangeValue[_T]]], Generic[_T]):
191191
def __init__(
192192
self,
193-
keys_or_start_with: Union[Collection[str], StartingWithOptions],
194-
object_type: Optional[Type[_T]],
193+
keys: Optional[Collection[str]] = None,
194+
object_type: Optional[Type[_T]] = None,
195195
materialize_metadata: Optional[bool] = True,
196-
): # todo: starting with
197-
start_with = isinstance(keys_or_start_with, StartingWithOptions)
198-
if not materialize_metadata and start_with:
199-
raise ValueError(
200-
f"Cannot set materialize_metadata to False while "
201-
f"collecting cmpxchg values starting with '{keys_or_start_with.starts_with}'"
202-
)
203-
196+
):
197+
self._materialize_metadata = materialize_metadata
198+
self._keys = keys
204199
self._object_type = object_type
200+
self._start = None
201+
self._page_size = None
202+
self._start_with = None
205203

206-
if start_with:
207-
self._keys = None
208-
self._materialize_metadata = True # todo: documentation - tell the user that it'll be set to True anyway
209-
self._start = keys_or_start_with.start
210-
self._page_size = keys_or_start_with.page_size
211-
self._start_with = keys_or_start_with.starts_with
212-
else:
213-
self._materialize_metadata = materialize_metadata
214-
self._keys = keys_or_start_with
215-
self._start = None
216-
self._page_size = None
217-
self._start_with = None
204+
@classmethod
205+
def create_for_start_with(
206+
cls,
207+
start_with: str,
208+
start: Optional[int] = None,
209+
page_size: Optional[int] = None,
210+
object_type: Optional[Type[_T]] = None,
211+
) -> GetCompareExchangeValuesOperation:
212+
operation = cls(None, object_type, True)
213+
operation._starts_with = start_with
214+
operation._start = start
215+
operation._page_size = page_size
216+
return operation
218217

219218
def get_command(self, store: DocumentStore, conventions: DocumentConventions, cache: HttpCache) -> RavenCommand[_T]:
220219
return self.GetCompareExchangeValuesCommand(self, self._materialize_metadata, conventions)

ravendb/documents/session/cluster_transaction_operation.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -144,31 +144,9 @@ def _get_compare_exchange_value_internal(
144144
return None
145145

146146
def _get_compare_exchange_values_internal(
147-
self, keys_or_start_with_options: Union[List[str], StartingWithOptions], object_type: Optional[Type[_T]]
147+
self, keys: List[str], object_type: Optional[Type[_T]]
148148
) -> Dict[str, Optional[CompareExchangeValue[_T]]]:
149-
start_with = isinstance(keys_or_start_with_options, StartingWithOptions)
150-
if start_with:
151-
self._session.increment_requests_count()
152-
values = self._session.operations.send(
153-
GetCompareExchangeValuesOperation(keys_or_start_with_options, dict), self.session.session_info
154-
)
155-
156-
results = {}
157-
158-
for key, value in values.items():
159-
if value is None:
160-
self.register_missing_compare_exchange_value(key)
161-
results[key] = None
162-
continue
163-
164-
session_value = self.register_compare_exchange_value(value)
165-
results[key] = session_value.get_value(object_type, self.session.conventions)
166-
167-
return results
168-
169-
results, not_tracked_keys = self.get_compare_exchange_values_from_session_internal(
170-
keys_or_start_with_options, object_type
171-
)
149+
results, not_tracked_keys = self.get_compare_exchange_values_from_session_internal(keys, object_type)
172150

173151
if not not_tracked_keys:
174152
return results
@@ -191,6 +169,32 @@ def _get_compare_exchange_values_internal(
191169

192170
return results
193171

172+
def _get_compare_exchange_values_starting_with_internal(
173+
self,
174+
start_with: str,
175+
start: Optional[int] = None,
176+
page_size: Optional[int] = None,
177+
object_type: Optional[Type[_T]] = None,
178+
) -> Dict[str, Optional[CompareExchangeValue[_T]]]:
179+
self._session.increment_requests_count()
180+
values = self._session.operations.send(
181+
GetCompareExchangeValuesOperation.create_for_start_with(start_with, start, page_size, object_type),
182+
self.session.session_info,
183+
)
184+
185+
results = {}
186+
187+
for key, value in values.items():
188+
if value is None:
189+
self.register_missing_compare_exchange_value(key)
190+
results[key] = None
191+
continue
192+
193+
session_value = self.register_compare_exchange_value(value)
194+
results[key] = session_value.get_value(object_type, self.session.conventions)
195+
196+
return results
197+
194198
def get_compare_exchange_value_from_session_internal(
195199
self, key: str, object_type: Optional[Type[_T]] = None
196200
) -> Tuple[Union[None, CompareExchangeValue[_T]], bool]:
@@ -299,9 +303,7 @@ def get_compare_exchange_values_starting_with(
299303
page_size: Optional[int] = None,
300304
object_type: Optional[Type[_T]] = None,
301305
):
302-
return self._get_compare_exchange_values_internal(
303-
StartingWithOptions(starts_with, start, page_size), object_type
304-
)
306+
return self._get_compare_exchange_values_starting_with_internal(starts_with, start, page_size, object_type)
305307

306308

307309
# this class helps to expose better typehints without tons of methods and fields from ClusterTransactionOperationsBase

ravendb/tests/jvm_migrated_tests/issues_tests/test_ravenDB_14006.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_compare_exchange_value_tracking_in_session_starts_with(self):
3232
session.save_changes()
3333

3434
with self.store.open_session(session_options=session_options) as session:
35-
results = session.advanced.cluster_transaction.get_compare_exchange_values(
36-
StartingWithOptions("comp"), Company
35+
results = session.advanced.cluster_transaction.get_compare_exchange_values_starting_with(
36+
"comp", object_type=Company
3737
)
3838
self.assertEqual(10, len(results))
3939
self.assertTrue(all(map(lambda x: x is not None, results)))

ravendb/tests/jvm_migrated_tests/test_unique_values.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from ravendb.documents.operations.statistics import GetDetailedStatisticsOperation, DetailedDatabaseStatistics
1313
from ravendb.documents.session.misc import TransactionMode
1414
from ravendb.tests.test_base import TestBase, User
15-
from ravendb.util.util import StartingWithOptions
1615

1716

1817
class TestUniqueValues(TestBase):
@@ -101,7 +100,7 @@ def test_can_list_compare_exchange(self):
101100
self.assertTrue(res2.successful)
102101

103102
values: Dict[str, CompareExchangeValue[User]] = self.store.operations.send(
104-
GetCompareExchangeValuesOperation(StartingWithOptions("test"), User)
103+
GetCompareExchangeValuesOperation.create_for_start_with("test", object_type=User)
105104
)
106105
self.assertEqual(2, len(values))
107106

0 commit comments

Comments
 (0)