mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 06:38:20 +00:00
Move cast_handle from storage-async.bif to BackendHandleVal static method
This commit is contained in:
parent
135acc7c6d
commit
cbc41e298d
4 changed files with 40 additions and 39 deletions
|
@ -164,4 +164,26 @@ bool detail::BackendHandleVal::DoUnserializeData(BrokerDataView) {
|
|||
return false;
|
||||
}
|
||||
|
||||
namespace detail {
|
||||
|
||||
zeek::expected<storage::detail::BackendHandleVal*, OperationResult> BackendHandleVal::CastFromAny(Val* handle) {
|
||||
// Quick exit by checking the type tag. This should be faster than doing the dynamic
|
||||
// cast below.
|
||||
if ( handle->GetType() != detail::backend_opaque )
|
||||
return zeek::unexpected<OperationResult>(
|
||||
OperationResult{ReturnCode::OPERATION_FAILED, "Invalid storage handle type"});
|
||||
|
||||
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(handle);
|
||||
|
||||
if ( ! b )
|
||||
return zeek::unexpected<OperationResult>(
|
||||
OperationResult{ReturnCode::OPERATION_FAILED, "Invalid storage handle type"});
|
||||
else if ( ! b->backend->IsOpen() )
|
||||
return zeek::unexpected<OperationResult>(OperationResult{ReturnCode::NOT_CONNECTED, "Backend is closed"});
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
} // namespace zeek::storage
|
||||
|
|
|
@ -309,6 +309,16 @@ public:
|
|||
BackendHandleVal(BackendPtr backend) : OpaqueVal(detail::backend_opaque), backend(std::move(backend)) {}
|
||||
~BackendHandleVal() override = default;
|
||||
|
||||
/**
|
||||
* Attempts to cast a handle passed from script-land into a BackendHandleVal. Used by
|
||||
* various BIF methods.
|
||||
*
|
||||
* @param handle The handle passed from script-land.
|
||||
* @return A zeek::expected with either the correctly-casted handle, or an OperationResult
|
||||
* containing error information.
|
||||
*/
|
||||
static zeek::expected<storage::detail::BackendHandleVal*, OperationResult> CastFromAny(Val*);
|
||||
|
||||
BackendPtr backend;
|
||||
|
||||
protected:
|
||||
|
|
|
@ -33,20 +33,6 @@ static zeek::detail::trigger::TriggerPtr init_trigger(zeek::detail::Frame* frame
|
|||
return {NewRef{}, trigger};
|
||||
}
|
||||
|
||||
// Utility method to cast the handle val passed into BIF methods into a form that can
|
||||
// be used to start storage operations. The method is also used by the BIFs in sync.bif.
|
||||
static zeek::expected<storage::detail::BackendHandleVal*, OperationResult> cast_handle(Val* handle) {
|
||||
auto b = static_cast<storage::detail::BackendHandleVal*>(handle);
|
||||
|
||||
if ( ! b )
|
||||
return zeek::unexpected<OperationResult>(
|
||||
OperationResult{ReturnCode::OPERATION_FAILED, "Invalid storage handlle"});
|
||||
else if ( ! b->backend->IsOpen() )
|
||||
return zeek::unexpected<OperationResult>(OperationResult{ReturnCode::NOT_CONNECTED, "Backend is closed"});
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
static void handle_async_result(const IntrusivePtr<Backend>& backend, ResultCallback* cb,
|
||||
const OperationResult& op_result) {
|
||||
if ( op_result.code != ReturnCode::IN_PROGRESS || ! backend->SupportsAsync() ) {
|
||||
|
@ -112,7 +98,7 @@ function Storage::Async::__close_backend%(backend: opaque of Storage::BackendHan
|
|||
return nullptr;
|
||||
|
||||
auto cb = new ResultCallback(trigger, frame->GetTriggerAssoc());
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b ) {
|
||||
cb->Complete(b.error());
|
||||
delete cb;
|
||||
|
@ -133,7 +119,7 @@ function Storage::Async::__put%(backend: opaque of Storage::BackendHandle, key:
|
|||
return nullptr;
|
||||
|
||||
auto cb = new ResultCallback(trigger, frame->GetTriggerAssoc());
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b ) {
|
||||
cb->Complete(b.error());
|
||||
delete cb;
|
||||
|
@ -158,7 +144,7 @@ function Storage::Async::__get%(backend: opaque of Storage::BackendHandle, key:
|
|||
return nullptr;
|
||||
|
||||
auto cb = new ResultCallback(trigger, frame->GetTriggerAssoc());
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b ) {
|
||||
cb->Complete(b.error());
|
||||
delete cb;
|
||||
|
@ -179,7 +165,7 @@ function Storage::Async::__erase%(backend: opaque of Storage::BackendHandle, key
|
|||
return nullptr;
|
||||
|
||||
auto cb = new ResultCallback(trigger, frame->GetTriggerAssoc());
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b ) {
|
||||
cb->Complete(b.error());
|
||||
delete cb;
|
||||
|
|
|
@ -7,23 +7,6 @@
|
|||
|
||||
using namespace zeek;
|
||||
using namespace zeek::storage;
|
||||
|
||||
// Utility method to cast the handle val passed into BIF methods into a form that can
|
||||
// be used to start storage operations. This is a duplicate of the method in sync.bif
|
||||
// due to how utility methods are built by bifcl.
|
||||
/*
|
||||
static zeek::expected<storage::detail::BackendHandleVal*, OperationResult> cast_handle(Val* handle) {
|
||||
auto b = static_cast<storage::detail::BackendHandleVal*>(handle);
|
||||
|
||||
if ( ! b )
|
||||
return zeek::unexpected<OperationResult>(
|
||||
OperationResult{ReturnCode::OPERATION_FAILED, "Invalid storage handlle"});
|
||||
else if ( ! b->backend->IsOpen() )
|
||||
return zeek::unexpected<OperationResult>(OperationResult{ReturnCode::NOT_CONNECTED, "Backend is closed"});
|
||||
|
||||
return b;
|
||||
}
|
||||
*/
|
||||
%%}
|
||||
|
||||
module Storage::Sync;
|
||||
|
@ -64,7 +47,7 @@ function Storage::Sync::__close_backend%(backend: opaque of Storage::BackendHand
|
|||
%{
|
||||
OperationResult op_result;
|
||||
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b )
|
||||
op_result = b.error();
|
||||
else {
|
||||
|
@ -89,7 +72,7 @@ function Storage::Sync::__put%(backend: opaque of Storage::BackendHandle, key: a
|
|||
%{
|
||||
OperationResult op_result;
|
||||
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b )
|
||||
op_result = b.error();
|
||||
else {
|
||||
|
@ -118,7 +101,7 @@ function Storage::Sync::__get%(backend: opaque of Storage::BackendHandle, key: a
|
|||
%{
|
||||
OperationResult op_result;
|
||||
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b )
|
||||
op_result = b.error();
|
||||
else {
|
||||
|
@ -143,7 +126,7 @@ function Storage::Sync::__erase%(backend: opaque of Storage::BackendHandle, key:
|
|||
%{
|
||||
OperationResult op_result;
|
||||
|
||||
auto b = cast_handle(backend);
|
||||
auto b = storage::detail::BackendHandleVal::CastFromAny(backend);
|
||||
if ( ! b )
|
||||
op_result = b.error();
|
||||
else {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue