-
Notifications
You must be signed in to change notification settings - Fork 135
Data visitor #95
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
Merged
Merged
Data visitor #95
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
672b7cb
DataVisitor - a wrapper for obx_data_visitor
vaind 989cfd4
expose obx_supports_bytes_array internally
vaind 7559acf
query.find() - large array support on 32-bit systems
vaind 2d948f7
box getAll/getMany - large array support on 32-bit systems
vaind f44023a
hide data visitor internals
vaind 3cf705d
restore box getMany allowMissing behaviour when not using data-visitors
vaind 713d59a
DataVisitor docs and minor formatting changes
vaind File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import 'dart:ffi'; | ||
import 'signatures.dart'; | ||
import "package:ffi/ffi.dart" show allocate, free; | ||
|
||
/// This file implements C call forwarding using a trampoline approach. | ||
/// | ||
/// When you want to pass a dart callback to a C function you cannot use lambdas and instead the callback must be | ||
/// a static function, otherwise `Pointer.fromFunction()` called with your function won't compile. | ||
/// Since static functions don't have any state, you must either rely on a global state or use a "userData" pointer | ||
/// pass-through functionality provided by a C function. | ||
/// | ||
/// The DataVisitor class tries to alleviate the burden of managing this and instead allows using lambdas from | ||
/// user-code, internally mapping the C calls to the appropriate lambda. | ||
/// | ||
/// Sample usage: | ||
/// final results = <T>[]; | ||
/// final visitor = DataVisitor((Pointer<Uint8> dataPtr, int length) { | ||
/// final bytes = dataPtr.asTypedList(length); | ||
/// results.add(_fbManager.unmarshal(bytes)); | ||
/// return true; // return value usually indicates to the C function whether it should continue. | ||
/// }); | ||
/// | ||
/// final err = bindings.obx_query_visit(_cQuery, visitor.fn, visitor.userData, offset, limit); | ||
/// visitor.close(); // make sure to close the visitor, unregistering the callback it from the forwarder | ||
/// checkObx(err); | ||
|
||
int _lastId = 0; | ||
final _callbacks = <int, bool Function(Pointer<Uint8> dataPtr, int length)>{}; | ||
|
||
// called from C, forwards calls to the actual callback registered at the given ID | ||
int _forwarder(Pointer<Void> callbackId, Pointer<Uint8> dataPtr, int size) { | ||
if (callbackId == null || callbackId.address == 0) { | ||
throw Exception("Data-visitor callback issued with NULL user_data (callback ID)"); | ||
} | ||
|
||
return _callbacks[callbackId.cast<Int64>().value](dataPtr, size) ? 1 : 0; | ||
} | ||
|
||
/// A data visitor wrapper/forwarder to be used where obx_data_visitor is expected. | ||
class DataVisitor { | ||
int _id; | ||
Pointer<Int64> _idPtr; | ||
|
||
Pointer<NativeFunction<obx_data_visitor_native_t>> get fn => Pointer.fromFunction(_forwarder, 0); | ||
|
||
Pointer<Void> get userData => _idPtr.cast<Void>(); | ||
|
||
DataVisitor(bool Function(Pointer<Uint8> dataPtr, int length) callback) { | ||
// cycle through ids until we find an empty slot | ||
_lastId++; | ||
var initialId = _lastId; | ||
while (_callbacks.containsKey(_lastId)) { | ||
_lastId++; | ||
|
||
if (initialId == _lastId) { | ||
throw Exception("Data-visitor callbacks queue full - can't allocate another"); | ||
} | ||
} | ||
// register the visitor | ||
_id = _lastId; | ||
_callbacks[_id] = callback; | ||
|
||
_idPtr = allocate<Int64>(); | ||
_idPtr.value = _id; | ||
} | ||
|
||
void close() { | ||
// unregister the visitor | ||
_callbacks.remove(_id); | ||
free(_idPtr); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do we need that for? E.g. this is one of the visitor C APIs:
obx_err obx_query_visit(OBX_query* query, obx_data_visitor* visitor, void* user_data, uint64_t offset, uint64_t limit);
I assume you want to pass "id" as
user_data
? Is this more "convenient" that passing in a lambda-like thingy asvisitor
? The latter would already be thread-safe, which the current map-based is not, I think?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lambdas can't be passed as a C callback, FFI requires static functions. I've tried that (lambda) approach but had to resolve to this trampoline one instead (basically the same thing we do in Go)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, please make sure to document that in the code so that this explanation is not lost
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was not addressed yet. Does that require additional thread synchronization?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no concurrent memory access in Dart. All code in a single isolate executes on a single event-loop and isolates don't share memory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how so? I don't see a problem with the code in this PR. You mean something unrelated, e.g. transactions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To my limited understanding, I imagine this to happen:
E.g. Isolate 2 registers a callback for ID 1, but isolate 1 gets to consume it because it also has a pending callback for ID 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They don't with objectbox-c visitors - all calls are synchronous, executed on the same thread. This approach wouldn't work for other kinds of callbacks, where you register the callback and it may happen in the future (e.g. some kind of event listeners). Not sure how that could be handled with dart, if at all.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's an open issue with some workarounds available dart-lang/sdk#37022
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, that one is strictly synchronous and if FFI guarantees synchronous callbacks to execute in the same isolate (I assume that is defined somewhere?), we should be fine.