-
Notifications
You must be signed in to change notification settings - Fork 13
Moving BookmarkManager configuration to driver.session
#509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
07cd587
eb6a7bc
eb180be
b9bd74b
dd32b8b
c978f7d
798a66a
6f583d7
87d8708
da40310
5db8305
c403cd5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from .config import Neo4jBookmarkManagerConfig | ||
from .bookmark_manager import ( | ||
BookmarkManager, | ||
Neo4jBookmarkManagerConfig, | ||
) | ||
from .driver import Driver | ||
from .exceptions import ApplicationCodeError |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from __future__ import annotations | ||
|
||
from dataclasses import dataclass | ||
from typing import ( | ||
Any, | ||
Callable, | ||
ClassVar, | ||
Dict, | ||
List, | ||
Optional, | ||
) | ||
|
||
from nutkit.backend import Backend | ||
|
||
from .. import protocol | ||
|
||
|
||
@dataclass | ||
class Neo4jBookmarkManagerConfig: | ||
initial_bookmarks: Optional[Dict[str, List[str]]] = None | ||
bookmarks_supplier: Optional[Callable[[str], List[str]]] = None | ||
bookmarks_consumer: Optional[Callable[[str, List[str]], None]] = None | ||
|
||
|
||
@dataclass | ||
class BookmarkManager: | ||
_registry: ClassVar[Dict[Any, BookmarkManager]] = {} | ||
|
||
def __init__(self, backend: Backend, config: Neo4jBookmarkManagerConfig): | ||
self._backend = backend | ||
self.config = config | ||
|
||
req = protocol.NewBookmarkManager( | ||
config.initial_bookmarks, | ||
config.bookmarks_supplier is not None, | ||
config.bookmarks_consumer is not None | ||
) | ||
res = backend.send_and_receive(req) | ||
if not isinstance(res, protocol.BookmarkManager): | ||
raise Exception("Should be BookmarkManager but was %s" % res) | ||
|
||
self._bookmark_manager = res | ||
self._registry[self._bookmark_manager.id] = self | ||
|
||
@property | ||
def id(self): | ||
return self._bookmark_manager.id | ||
|
||
@classmethod | ||
def process_callbacks(cls, request): | ||
if isinstance(request, protocol.BookmarksSupplierRequest): | ||
if request.bookmark_manager_id not in cls._registry: | ||
raise Exception( | ||
"Backend provided unknown Bookmark manager id: " | ||
f"{request.bookmark_manager_id} not found" | ||
) | ||
|
||
manager = cls._registry[request.bookmark_manager_id] | ||
supply = manager.config.bookmarks_supplier | ||
if supply is not None: | ||
bookmarks = supply(request.database) | ||
return protocol.BookmarksSupplierCompleted(request.id, | ||
bookmarks) | ||
if isinstance(request, protocol.BookmarksConsumerRequest): | ||
if request.bookmark_manager_id not in cls._registry: | ||
raise ValueError( | ||
"Backend provided unknown Bookmark manager id: " | ||
f"{request.bookmark_manager_id} not found" | ||
) | ||
manager = cls._registry[request.bookmark_manager_id] | ||
consume = manager.config.bookmarks_consumer | ||
if consume is not None: | ||
consume(request.database, request.bookmarks) | ||
return protocol.BookmarksConsumerCompleted(request.id) | ||
|
||
def close(self, hooks=None): | ||
res = self._backend.send_and_receive( | ||
protocol.BookmarkManagerClose(self.id), | ||
hooks=hooks | ||
) | ||
if not isinstance(res, protocol.BookmarkManager): | ||
raise Exception("Should be BookmarkManager but was %s" % res) | ||
del self._registry[self.id] |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -71,8 +71,7 @@ def __init__( | |
fetchSize=None, maxTxRetryTimeMs=None, encrypted=None, | ||
trustedCertificates=None, liveness_check_timeout_ms=None, | ||
max_connection_pool_size=None, | ||
connection_acquisition_timeout_ms=None, | ||
bookmark_manager=None | ||
connection_acquisition_timeout_ms=None | ||
): | ||
# Neo4j URI to connect to | ||
self.uri = uri | ||
|
@@ -88,8 +87,6 @@ def __init__( | |
self.livenessCheckTimeoutMs = liveness_check_timeout_ms | ||
self.maxConnectionPoolSize = max_connection_pool_size | ||
self.connectionAcquisitionTimeoutMs = connection_acquisition_timeout_ms | ||
if bookmark_manager is not None: | ||
self.bookmarkManager = bookmark_manager | ||
# (bool) whether to enable or disable encryption | ||
# field missing in message: use driver default (should be False) | ||
if encrypted is not None: | ||
|
@@ -202,8 +199,8 @@ class BookmarksSupplierCompleted: | |
Pushes bookmarks for a given database to the Bookmark Manager. | ||
""" | ||
|
||
def __init__(self, requestId, bookmarks): | ||
self.requestId = requestId | ||
def __init__(self, request_id, bookmarks): | ||
self.requestId = request_id | ||
self.bookmarks = bookmarks | ||
|
||
|
||
|
@@ -214,8 +211,35 @@ class BookmarksConsumerCompleted: | |
Signal the method call has finished | ||
""" | ||
|
||
def __init__(self, requestId): | ||
self.requestId = requestId | ||
def __init__(self, request_id): | ||
self.requestId = request_id | ||
|
||
|
||
class NewBookmarkManager: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we have a method to close/destroy BMMs to avoid piling up resources in the backend? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We will only have 20 lightweight objects in the backend. The log messages generated for this call in the JS backend concern me more. I already removed some log messages from the backend for resolving issues with the process being killed. |
||
"""Instantiates a bookmark manager by calling the default factory. | ||
|
||
Backend should respond with a BookmarkManager response. | ||
""" | ||
|
||
def __init__(self, initial_bookmarks, | ||
bookmarks_supplier_registered, bookmarks_consumer_registered): | ||
self.initialBookmarks = initial_bookmarks | ||
self.bookmarksSupplierRegistered = bookmarks_supplier_registered | ||
self.bookmarksConsumerRegistered = bookmarks_consumer_registered | ||
|
||
|
||
class BookmarkManagerClose: | ||
"""Destroy the bookmark manager in the backend and free the resources. | ||
|
||
The driver-provided BookmarkManager implementation does not have a close | ||
method. This message is an instruction solely for the backend to be able to | ||
destroy the bookmark manager object when done testing it to free resources. | ||
|
||
Backend should respond with a BookmarkManager response. | ||
""" | ||
|
||
def __init__(self, id): | ||
self.id = id | ||
|
||
|
||
class DomainNameResolutionCompleted: | ||
|
@@ -258,7 +282,7 @@ class NewSession: | |
|
||
def __init__(self, driverId, accessMode, bookmarks=None, | ||
database=None, fetchSize=None, impersonatedUser=None, | ||
ignore_bookmark_manager=None): | ||
bookmark_manager=None): | ||
# Id of driver on backend that session should be created on | ||
self.driverId = driverId | ||
# Session accessmode: 'r' for read access and 'w' for write access. | ||
|
@@ -268,8 +292,8 @@ def __init__(self, driverId, accessMode, bookmarks=None, | |
self.database = database | ||
self.fetchSize = fetchSize | ||
self.impersonatedUser = impersonatedUser | ||
if ignore_bookmark_manager is not None: | ||
self.ignoreBookmarkManager = ignore_bookmark_manager | ||
if bookmark_manager is not None: | ||
self.bookmarkManagerId = bookmark_manager.id | ||
|
||
|
||
class SessionClose: | ||
|
Uh oh!
There was an error while loading. Please reload this page.