Skip to content

Commit 5820ce7

Browse files
authored
Merge pull request #224 from poissoncorp/RDBC-848
RDBC-848 Implement session.Advanced.SessionInfo.SetContext
2 parents 2db843c + 6f2f483 commit 5820ce7

File tree

1 file changed

+40
-24
lines changed

1 file changed

+40
-24
lines changed

ravendb/documents/session/misc.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
)
1818
from ravendb.documents import DocumentStore
1919

20-
2120
_T_Key = TypeVar("_T_Key")
2221
_T_Value = TypeVar("_T_Value")
2322

@@ -39,8 +38,8 @@ def __str__(self):
3938

4039

4140
class SessionInfo:
42-
__client_session_id_counter = threading.local()
43-
__client_session_id_counter.counter = 0
41+
_client_session_id_counter = threading.local()
42+
_client_session_id_counter.counter = 0
4443

4544
def __init__(
4645
self, session: InMemoryDocumentSessionOperations, options: SessionOptions, document_store: DocumentStore
@@ -49,11 +48,11 @@ def __init__(
4948
raise ValueError("DocumentStore cannot be None")
5049
if not session:
5150
raise ValueError("Session cannot be None")
52-
self.__session = session
53-
self.__session_id: Union[None, int] = None
54-
self.__session_id_used: Union[None, bool] = None
55-
self.__load_balancer_context_seed = session._request_executor.conventions.load_balancer_context_seed
56-
self.__can_use_load_balance_behavior = (
51+
self._session = session
52+
self._session_id: Union[None, int] = None
53+
self._session_id_used: Union[None, bool] = None
54+
self._load_balancer_context_seed = session.request_executor.conventions.load_balancer_context_seed
55+
self._can_use_load_balance_behavior = (
5756
session.conventions.load_balance_behavior == LoadBalanceBehavior.USE_SESSION_CONTEXT
5857
and session.conventions.load_balancer_per_session_context_selector is not None
5958
)
@@ -64,36 +63,53 @@ def __init__(
6463

6564
@property
6665
def can_use_load_balance_behavior(self) -> bool:
67-
return self.__can_use_load_balance_behavior
66+
return self._can_use_load_balance_behavior
6867

6968
@property
7069
def session_id(self) -> int:
71-
if self.__session_id is None:
70+
if self._session_id is None:
7271
context = None
73-
selector = self.__session.conventions.load_balancer_per_session_context_selector
72+
selector = self._session.conventions.load_balancer_per_session_context_selector
7473
if selector is not None:
75-
context = selector(self.__session.database_name)
76-
self.__set_context_internal(context)
77-
self.__session_id_used = True
78-
return self.__session_id
74+
context = selector(self._session.database_name)
75+
self._set_context_internal(context)
76+
self._session_id_used = True
77+
return self._session_id
78+
79+
@property
80+
def context(self):
81+
# placeholder for convenient setter
82+
return None
83+
84+
@context.setter
85+
def context(self, session_id: str):
86+
if not session_id or session_id.isspace():
87+
raise ValueError("Session key cannot be None or whitespace")
88+
89+
self._set_context_internal(session_id)
90+
91+
self._can_use_load_balance_behavior = (
92+
self._can_use_load_balance_behavior
93+
or self._session.conventions.load_balance_behavior == LoadBalanceBehavior.USE_SESSION_CONTEXT
94+
)
7995

80-
def __set_context_internal(self, session_key: str) -> None:
81-
if self.__session_id_used:
96+
def _set_context_internal(self, session_id: str) -> None:
97+
if self._session_id_used:
8298
raise RuntimeError(
8399
"Unable to set the session context after it has already been used. "
84100
"The session context can only be modified before it is utilized."
85101
)
86102

87-
if session_key is None:
88-
v = self.__client_session_id_counter.counter
89-
self.__session_id = v
103+
if session_id is None:
104+
v = self._client_session_id_counter.counter
105+
self._session_id = v
90106
v += 1
91-
self.__client_session_id_counter = v
107+
self._client_session_id_counter = v
92108
else:
93-
self.__session_id = int(hashlib.md5(bytes(session_key)).digest().decode("utf-8"))
109+
self._session_id = int(hashlib.md5(session_id.encode("utf-8")).hexdigest(), 16)
94110

95111
def increment_request_count(self) -> None:
96-
self.__session.increment_requests_count()
112+
self._session.increment_requests_count()
97113

98114

99115
class SessionOptions:
@@ -193,7 +209,7 @@ def parameters(self) -> Dict[str, object]:
193209

194210
def __get_next_argument_name(self) -> str:
195211
self.__arg_counter += 1
196-
return f"val_{self.__arg_counter-1}_{self.__suffix}"
212+
return f"val_{self.__arg_counter - 1}_{self.__suffix}"
197213

198214
def add(self, *u) -> JavaScriptArray:
199215
def __func(value) -> str:

0 commit comments

Comments
 (0)