Skip to content

Commit 40b1baf

Browse files
authored
Merge pull request #210 from poissoncorp/RDBC-809
RDBC-811, RDBC-809, RDBC-807, RDBC-750 - Breaking changes - new version 5.2.6
2 parents 68ba497 + f82c69e commit 40b1baf

File tree

17 files changed

+356
-155
lines changed

17 files changed

+356
-155
lines changed

ravendb/documents/commands/results.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import annotations
2-
from typing import Union, Optional, List, Dict
2+
from typing import Union, Optional, List, Dict, Any
33

44

55
class GetDocumentResult:
@@ -39,3 +39,13 @@ def from_json(cls, json_dict: dict) -> GetDocumentsResult:
3939
json_dict.get("CompareExchangeValueIncludes", None),
4040
json_dict.get("NextPageStart", None),
4141
)
42+
43+
def to_json(self) -> Dict[str, Any]:
44+
return {
45+
"Includes": self.includes,
46+
"Results": self.results,
47+
"CounterIncludes": self.counter_includes,
48+
"TimeSeriesIncludes": self.time_series_includes,
49+
"CompareExchangeValueIncludes": self.compare_exchange_includes,
50+
"NextPageStart": self.next_page_start,
51+
}

ravendb/documents/session/cluster_transaction_operation.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,14 @@ def lazily(self) -> ILazyClusterTransactionOperations:
5050
pass
5151

5252
@abc.abstractmethod
53-
def get_compare_exchange_value(self, key: str, object_type: Type[_T] = None) -> Optional[CompareExchangeValue[_T]]:
53+
def get_compare_exchange_value(
54+
self, key: str, object_type: Optional[Type[_T]] = None
55+
) -> Optional[CompareExchangeValue[_T]]:
5456
pass
5557

5658
@abc.abstractmethod
5759
def get_compare_exchange_values(
58-
self, keys: List[str], object_type: Type[_T]
60+
self, keys: List[str], object_type: Optional[Type[_T]] = None
5961
) -> Dict[str, CompareExchangeValue[_T]]:
6062
pass
6163

@@ -289,11 +291,13 @@ def __init__(self, session: "DocumentSession"):
289291
def lazily(self) -> ILazyClusterTransactionOperations:
290292
return LazyClusterTransactionOperations(self.session)
291293

292-
def get_compare_exchange_value(self, key: str, object_type: Type[_T] = None) -> Optional[CompareExchangeValue[_T]]:
294+
def get_compare_exchange_value(
295+
self, key: str, object_type: Optional[Type[_T]] = None
296+
) -> Optional[CompareExchangeValue[_T]]:
293297
return self._get_compare_exchange_value_internal(key, object_type)
294298

295299
def get_compare_exchange_values(
296-
self, keys: List[str], object_type: Type[_T]
300+
self, keys: List[str], object_type: Optional[Type[_T]] = None
297301
) -> Dict[str, CompareExchangeValue[_T]]:
298302
return super()._get_compare_exchange_values_internal(keys, object_type)
299303

ravendb/documents/session/document_session.py

Lines changed: 47 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -375,16 +375,30 @@ def load_starting_with(
375375
start_after: Optional[str] = None,
376376
) -> List[_T]:
377377
load_starting_with_operation = LoadStartingWithOperation(self)
378-
self.__load_starting_with_internal(
379-
id_prefix, load_starting_with_operation, None, matches, start, page_size, exclude, start_after
378+
self._load_starting_with_internal(
379+
id_prefix, load_starting_with_operation, matches, start, page_size, exclude, start_after
380380
)
381381
return load_starting_with_operation.get_documents(object_type)
382382

383-
def __load_starting_with_internal(
383+
def load_starting_with_into_stream(
384+
self,
385+
id_prefix: str,
386+
matches: str = None,
387+
start: int = 0,
388+
page_size: int = 25,
389+
exclude: str = None,
390+
start_after: str = None,
391+
) -> bytes:
392+
if id_prefix is None:
393+
raise ValueError("Arg 'id_prefix' is cannot be None.")
394+
return self._load_starting_with_into_stream_internal(
395+
id_prefix, LoadStartingWithOperation(self), matches, start, page_size, exclude, start_after
396+
)
397+
398+
def _load_starting_with_internal(
384399
self,
385400
id_prefix: str,
386401
operation: LoadStartingWithOperation,
387-
stream,
388402
matches: str,
389403
start: int,
390404
page_size: int,
@@ -395,12 +409,31 @@ def __load_starting_with_internal(
395409
command = operation.create_request()
396410
if command:
397411
self._request_executor.execute_command(command, self.session_info)
398-
if stream:
399-
pass # todo: stream
400-
else:
401-
operation.set_result(command.result)
412+
operation.set_result(command.result)
402413
return command
403414

415+
def _load_starting_with_into_stream_internal(
416+
self,
417+
id_prefix: str,
418+
operation: LoadStartingWithOperation,
419+
matches: str,
420+
start: int,
421+
page_size: int,
422+
exclude: str,
423+
start_after: str,
424+
) -> bytes:
425+
operation.with_start_with(id_prefix, matches, start, page_size, exclude, start_after)
426+
command = operation.create_request()
427+
bytes_result = None
428+
if command:
429+
self.request_executor.execute_command(command, self.session_info)
430+
try:
431+
result = command.result
432+
bytes_result = json.dumps(result.to_json()).encode("utf-8")
433+
except Exception as e:
434+
raise RuntimeError("Unable sto serialize returned value into stream") from e
435+
return bytes_result
436+
404437
def document_query_from_index_type(self, index_type: Type[_TIndex], object_type: Type[_T]) -> DocumentQuery[_T]:
405438
try:
406439
index = Utils.try_get_new_instance(index_type)
@@ -456,6 +489,8 @@ def counters_for_entity(self, entity: object) -> SessionDocumentCounters:
456489
return SessionDocumentCounters(self, entity)
457490

458491
def time_series_for(self, document_id: str, name: str = None) -> SessionDocumentTimeSeries:
492+
if not isinstance(document_id, str):
493+
raise TypeError("Method time_series_for expects a string. Did you want to call time_series_for_entity?")
459494
return SessionDocumentTimeSeries(self, document_id, name)
460495

461496
def time_series_for_entity(self, entity: object, name: str = None) -> SessionDocumentTimeSeries:
@@ -723,7 +758,7 @@ def lazily(self) -> LazySessionOperations:
723758
return self._session._lazily
724759

725760
def graph_query(self, object_type: type, query: str): # -> GraphDocumentQuery:
726-
pass
761+
raise NotImplementedError("Dropped support for graph queries")
727762

728763
def what_changed(self) -> Dict[str, List[DocumentsChanges]]:
729764
return self._session._what_changed()
@@ -781,32 +816,6 @@ def wait_for_indexes_after_save_changes(
781816

782817
index_options.wait_for_indexes = True
783818

784-
def __load_starting_with_internal(
785-
self,
786-
id_prefix: str,
787-
operation: LoadStartingWithOperation,
788-
stream: Union[None, bytes],
789-
matches: str,
790-
start: int,
791-
page_size: int,
792-
exclude: str,
793-
start_after: str,
794-
) -> GetDocumentsCommand:
795-
operation.with_start_with(id_prefix, matches, start, page_size, exclude, start_after)
796-
command = operation.create_request()
797-
if command is not None:
798-
self._session._request_executor.execute(command, self._session.session_info)
799-
if stream:
800-
try:
801-
result = command.result
802-
stream_to_dict = json.loads(stream.decode("utf-8"))
803-
result.__dict__.update(stream_to_dict)
804-
except IOError as e:
805-
raise RuntimeError(f"Unable to serialize returned value into stream {e.args[0]}", e)
806-
else:
807-
operation.set_result(command.result)
808-
return command
809-
810819
def load_starting_with(
811820
self,
812821
id_prefix: str,
@@ -824,26 +833,14 @@ def load_starting_with(
824833
def load_starting_with_into_stream(
825834
self,
826835
id_prefix: str,
827-
output: bytes,
828836
matches: str = None,
829837
start: int = 0,
830838
page_size: int = 25,
831839
exclude: str = None,
832840
start_after: str = None,
833-
):
834-
if not output:
835-
raise ValueError("Output cannot be None")
836-
if not id_prefix:
837-
raise ValueError("Id prefix cannot be None")
838-
self.__load_starting_with_internal(
839-
id_prefix,
840-
LoadStartingWithOperation(self._session),
841-
output,
842-
matches,
843-
start,
844-
page_size,
845-
exclude,
846-
start_after,
841+
) -> bytes:
842+
return self._session.load_starting_with_into_stream(
843+
id_prefix, matches, start, page_size, exclude, start_after
847844
)
848845

849846
def load_into_stream(self, keys: List[str], output: bytes) -> None:

ravendb/documents/session/loaders/include.py

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -358,23 +358,29 @@ def include_all_counters(self) -> SubscriptionIncludeBuilder:
358358
self._include_all_counters("")
359359
return self
360360

361+
def include_time_series_by_range_type_and_time(
362+
self, name: str, ts_type: TimeSeriesRangeType, time: TimeValue
363+
) -> SubscriptionIncludeBuilder:
364+
self._include_time_series_by_range_type_and_time("", name, ts_type, time)
365+
return self
366+
367+
def include_time_series_by_range_type_and_count(
368+
self, name: str, ts_type: TimeSeriesRangeType, count: int
369+
) -> SubscriptionIncludeBuilder:
370+
self._include_time_series_by_range_type_and_count("", name, ts_type, count)
371+
return self
372+
373+
def include_all_time_series_by_range_type_and_count(
374+
self, ts_type: TimeSeriesRangeType, count: int
375+
) -> SubscriptionIncludeBuilder:
376+
self._include_time_series_by_range_type_and_count("", constants.TimeSeries.ALL, ts_type, count)
377+
return self
361378

362-
# def include_time_series(
363-
# self,
364-
# name:str,
365-
# ts_type: TimeSeriesRangeType,
366-
# time: TimeValue
367-
# ) -> SubscriptionIncludeBuilder:
368-
# self._include_time_series_by_range_type_and_time("", name, ts_type, time)
369-
# return self
370-
#
371-
# def include_time_series_by_range_type_and_count(
372-
# self,
373-
# name:str,
374-
# ts_type: TimeSeriesRangeType,
375-
# time: TimeValue
376-
# ) -> SubscriptionIncludeBuilder:
377-
# self._include_time_series_by_range_type_and_count("", name, type, count)
379+
def include_all_time_series_by_range_type_and_time(
380+
self, ts_type: TimeSeriesRangeType, time: TimeValue
381+
) -> SubscriptionIncludeBuilder:
382+
self._include_time_series_by_range_type_and_time("", constants.TimeSeries.ALL, ts_type, time)
383+
return self
378384

379385

380386
class TimeSeriesIncludeBuilder(IncludeBuilderBase):

ravendb/documents/session/loaders/loaders.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from __future__ import annotations
22
from abc import abstractmethod
3-
from typing import TypeVar, TYPE_CHECKING, Dict, List, Type
3+
from typing import TypeVar, TYPE_CHECKING, Dict, List, Type, Optional, Set, Union
44

55
from ravendb.documents.store.lazy import Lazy
66

@@ -16,21 +16,24 @@ def include(self, path: str) -> LoaderWithInclude:
1616
pass
1717

1818
@abstractmethod
19-
def load(self, object_type: _T, *ids: str) -> _T:
19+
def load(self, id_: str, object_type: Optional[Type[_T]] = None) -> _T:
2020
pass
2121

2222

2323
class MultiLoaderWithInclude(LoaderWithInclude):
2424
def __init__(self, session: DocumentSession):
2525
self.__session = session
26-
self.__includes: List[str] = []
26+
self.__includes: Set[str] = set()
2727

2828
def include(self, path: str) -> LoaderWithInclude:
29-
self.__includes.append(path)
29+
self.__includes.add(path)
3030
return self
3131

32-
def load(self, object_type: Type[_T], *ids: str) -> Dict[str, _T]:
33-
return self.__session._load_internal(object_type, list(ids), self.__includes)
32+
def load(self, id_or_ids: Union[List[str], str], object_type: Optional[Type[_T]] = None) -> _T:
33+
if not isinstance(id_or_ids, (str, list)):
34+
raise TypeError(f"Expected str or list of str, got '{type(id_or_ids)}'")
35+
ids = [id_or_ids] if isinstance(id_or_ids, str) else id_or_ids
36+
return self.__session._load_internal(object_type, ids, self.__includes)
3437

3538

3639
class LazyMultiLoaderWithInclude(LoaderWithInclude):
@@ -42,5 +45,8 @@ def include(self, path: str) -> LazyMultiLoaderWithInclude:
4245
self.__includes.append(path)
4346
return self
4447

45-
def load(self, object_type: Type[_T], *ids: str) -> Lazy[Dict[str, _T]]:
46-
return self.__session.lazy_load_internal(object_type, list(ids), self.__includes, None)
48+
def load(self, id_or_ids: Union[List[str], str], object_type: Optional[Type[_T]] = None) -> Lazy[Dict[str, _T]]:
49+
if not isinstance(id_or_ids, (str, list)):
50+
raise TypeError(f"Expected str or list of str, got '{type(id_or_ids)}'")
51+
ids = [id_or_ids] if isinstance(id_or_ids, str) else id_or_ids
52+
return self.__session.lazy_load_internal(object_type, ids, self.__includes, None)

ravendb/documents/session/operations/lazy.py

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,62 +183,62 @@ def handle_response(self, response: "GetResponse") -> None:
183183

184184
class LazySessionOperations:
185185
def __init__(self, delegate: DocumentSession):
186-
self._delegate = delegate
186+
self._session = delegate
187187

188188
def include(self, path: str) -> LazyMultiLoaderWithInclude:
189-
return LazyMultiLoaderWithInclude(self._delegate).include(path)
189+
return LazyMultiLoaderWithInclude(self._session).include(path)
190190

191191
def load(
192-
self, object_type: Type[_T], ids: Union[List[str], str], on_eval: Callable = None
192+
self, ids: Union[List[str], str], object_type: Optional[Type[_T]] = None, on_eval: Callable = None
193193
) -> Optional[Lazy[Union[Dict[str, object], object]]]:
194194
if not ids:
195195
return None
196196

197197
if isinstance(ids, str):
198198
key = ids
199-
if self._delegate.advanced.is_loaded(key):
200-
return Lazy(lambda: self._delegate.load(key, object_type))
199+
if self._session.advanced.is_loaded(key):
200+
return Lazy(lambda: self._session.load(key, object_type))
201201

202202
lazy_load_operation = LazyLoadOperation(
203-
object_type, self._delegate, LoadOperation(self._delegate).by_key(key)
203+
object_type, self._session, LoadOperation(self._session).by_key(key)
204204
).by_key(key)
205205

206-
return self._delegate.add_lazy_operation(object_type, lazy_load_operation, on_eval)
206+
return self._session.add_lazy_operation(object_type, lazy_load_operation, on_eval)
207207

208208
elif isinstance(ids, list):
209-
return self._delegate.lazy_load_internal(object_type, ids, [], on_eval)
209+
return self._session.lazy_load_internal(object_type, ids, [], on_eval)
210210

211-
raise TypeError("Expected 'ids' as 'str' or 'list[str]'")
211+
raise TypeError(f"Expected a 'str' or 'list' of 'str', the document ids. Got '{type(ids).__name__}'.")
212212

213213
def load_starting_with(
214214
self,
215215
id_prefix: str,
216-
object_type: Optional[Type[_T]],
216+
object_type: Optional[Type[_T]] = None,
217217
matches: str = None,
218218
start: int = 0,
219219
page_size: int = 25,
220220
exclude: str = None,
221221
start_after: str = None,
222222
) -> Lazy[Dict[str, _T]]:
223223
operation = LazyStartsWithOperation(
224-
object_type, id_prefix, matches, exclude, start, page_size, self._delegate, start_after
224+
object_type, id_prefix, matches, exclude, start, page_size, self._session, start_after
225225
)
226226

227-
return self._delegate.add_lazy_operation(dict, operation, None)
227+
return self._session.add_lazy_operation(dict, operation, None)
228228

229229
def conditional_load(
230-
self, key: str, change_vector: str, object_type: Type[_T] = None
230+
self, key: str, change_vector: str, object_type: Optional[Type[_T]] = None
231231
) -> Lazy[ConditionalLoadResult[_T]]:
232232
if not key or key.isspace():
233233
raise ValueError("key cannot be None or whitespace")
234234

235-
if self._delegate.is_loaded(key):
235+
if self._session.is_loaded(key):
236236

237237
def __lazy_factory():
238-
entity = self._delegate.load(key, object_type)
238+
entity = self._session.load(key, object_type)
239239
if entity is None:
240240
return ConditionalLoadResult.create(None, None)
241-
cv = self._delegate.advanced.get_change_vector_for(entity)
241+
cv = self._session.advanced.get_change_vector_for(entity)
242242
return ConditionalLoadResult.create(entity, cv)
243243

244244
return Lazy(__lazy_factory)
@@ -249,8 +249,8 @@ def __lazy_factory():
249249
f"conditional load when change_vector is None or empty"
250250
)
251251

252-
lazy_load_operation = LazyConditionalLoadOperation(object_type, key, change_vector, self._delegate)
253-
return self._delegate.add_lazy_operation(ConditionalLoadResult, lazy_load_operation, None)
252+
lazy_load_operation = LazyConditionalLoadOperation(object_type, key, change_vector, self._session)
253+
return self._session.add_lazy_operation(ConditionalLoadResult, lazy_load_operation, None)
254254

255255

256256
class LazyLoadOperation(LazyOperation):

0 commit comments

Comments
 (0)