Split sync and async into separate script-land namespaces

This commit is contained in:
Tim Wojtulewicz 2025-03-01 15:16:57 -07:00
parent e8074c40d4
commit 28951dccf1
24 changed files with 465 additions and 303 deletions

View file

@ -1 +1,3 @@
@load ./main @load ./async
@load ./main
@load ./sync

View file

@ -0,0 +1,100 @@
##! Asynchronous operation methods for the storage framework. These methods must
##! be called as part of a :zeek:see:`when` statement. An error will be returned
##! otherwise.
@load ./main
module Storage::Async;
export {
## Opens a new backend connection based on a configuration object asynchronously.
##
## btype: A tag indicating what type of backend should be opened. These are
## defined by the backend plugins loaded.
##
## options: A record containing the configuration for the connection.
##
## key_type: The script-level type of keys stored in the backend. Used for
## validation of keys passed to other framework methods.
##
## val_type: The script-level type of keys stored in the backend. Used for
## validation of values passed to :zeek:see:`Storage::Async::put` as
## well as for type conversions for return values from
## :zeek:see:`Storage::Async::get`.
##
## Returns: A handle to the new backend connection, or ``F`` if the connection
## failed.
global open_backend: function(btype: Storage::Backend, options: any, key_type: any,
val_type: any): opaque of Storage::BackendHandle;
## Closes an existing backend connection asynchronously.
##
## backend: A handle to a backend connection.
##
## Returns: A boolean indicating success or failure of the operation.
global close_backend: function(backend: opaque of Storage::BackendHandle): bool;
## Inserts a new entry into a backend asynchronously.
##
## backend: A handle to a backend connection.
##
## args: A :zeek:see:`Storage::PutArgs` record containing the arguments for the
## operation.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned.
global put: function(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool;
## Gets an entry from the backend asynchronously.
##
## backend: A handle to a backend connection.
##
## key: The key to look up.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned. The caller should check the validity of the value before
## attempting to use it. If the value is unset, an error string may be
## available to describe the failure.
global get: function(backend: opaque of Storage::BackendHandle, key: any): val_result;
## Erases an entry from the backend asynchronously.
##
## backend: A handle to a backend connection.
##
## key: The key to erase.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned.
global erase: function(backend: opaque of Storage::BackendHandle, key: any): bool;
}
function open_backend(btype: Storage::Backend, options: any, key_type: any, val_type: any): opaque of Storage::BackendHandle
{
return Storage::Async::__open_backend(btype, options, key_type, val_type);
}
function close_backend(backend: opaque of Storage::BackendHandle): bool
{
return Storage::Async::__close_backend(backend);
}
function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool
{
return Storage::Async::__put(backend, args$key, args$value, args$overwrite, args$expire_time);
}
function get(backend: opaque of Storage::BackendHandle, key: any): val_result
{
return Storage::Async::__get(backend, key);
}
function erase(backend: opaque of Storage::BackendHandle, key: any): bool
{
return Storage::Async::__erase(backend, key);
}

View file

@ -20,128 +20,5 @@ export {
# An interval of time until the entry is automatically removed from the # An interval of time until the entry is automatically removed from the
# backend. # backend.
expire_time: interval &default=0sec; expire_time: interval &default=0sec;
# Indicates whether this operation should happen asynchronously. If this
# is true, the call to put must happen as part of a :zeek:see:`when`
# statement. This flag is overridden and set to F when reading pcaps,
# since time won't move forward the same as when caputring live traffic.
async_mode: bool &default=T;
}; };
## Opens a new backend connection based on a configuration object.
##
## btype: A tag indicating what type of backend should be opened. These are
## defined by the backend plugins loaded.
##
## options: A record containing the configuration for the connection.
##
## key_type: The script-level type of keys stored in the backend. Used for
## validation of keys passed to other framework methods.
##
## val_type: The script-level type of keys stored in the backend. Used for
## validation of values passed to :zeek:see:`Storage::put` as well as
## for type conversions for return values from :zeek:see:`Storage::get`.
##
## async_mode: Indicates whether this operation should happen asynchronously. If
## this is T, the call must happen as part of a :zeek:see:`when`
## statement. This flag is overridden and set to F when reading pcaps,
## since time won't move forward the same as when caputring live
## traffic.
##
## Returns: A handle to the new backend connection, or ``F`` if the connection
## failed.
global open_backend: function(btype: Storage::Backend, options: any, key_type: any,
val_type: any, async_mode: bool &default=F): opaque of Storage::BackendHandle;
## Closes an existing backend connection.
##
## backend: A handle to a backend connection.
##
## async_mode: Indicates whether this operation should happen asynchronously. If
## this is T, the call must happen as part of a :zeek:see:`when`
## statement. This flag is overridden and set to F when reading pcaps,
## since time won't move forward the same as when caputring live
## traffic.
##
## Returns: A boolean indicating success or failure of the operation.
global close_backend: function(backend: opaque of Storage::BackendHandle, async_mode: bool &default=F): bool;
## Inserts a new entry into a backend.
##
##
## backend: A handle to a backend connection.
##
## args: A :zeek:see:`Storage::PutArgs` record containing the arguments for the
## operation.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned.
global put: function(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool;
## Gets an entry from the backend.
##
## backend: A handle to a backend connection.
##
## key: The key to look up.
##
## async_mode: Indicates whether this operation should happen asynchronously. If
## this is T, the call must happen as part of a :zeek:see:`when`
## statement. This flag is overridden and set to F when reading pcaps,
## since time won't move forward the same as when caputring live
## traffic.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned. The caller should check the validity of the value before
## attempting to use it. If the value is unset, an error string may be
## available to describe the failure.
global get: function(backend: opaque of Storage::BackendHandle, key: any,
async_mode: bool &default=T): val_result;
## Erases an entry from the backend.
##
## backend: A handle to a backend connection.
##
## key: The key to erase.
##
## async_mode: Indicates whether this operation should happen asynchronously. If
## this is T, the call must happen as part of a :zeek:see:`when`
## statement. This flag is overridden and set to F when reading pcaps,
## since time won't move forward the same as when caputring live
## traffic.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned.
global erase: function(backend: opaque of Storage::BackendHandle, key: any,
async_mode: bool &default=T): bool;
}
function open_backend(btype: Storage::Backend, options: any, key_type: any, val_type: any, async_mode: bool &default=F): opaque of Storage::BackendHandle
{
return Storage::__open_backend(btype, options, key_type, val_type, async_mode);
}
function close_backend(backend: opaque of Storage::BackendHandle, async_mode: bool &default=F): bool
{
return Storage::__close_backend(backend, async_mode);
}
function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool
{
return Storage::__put(backend, args$key, args$value, args$overwrite, args$expire_time, args$async_mode);
}
function get(backend: opaque of Storage::BackendHandle, key: any, async_mode: bool &default=T): val_result
{
return Storage::__get(backend, key, async_mode);
}
function erase(backend: opaque of Storage::BackendHandle, key: any, async_mode: bool &default=T): bool
{
return Storage::__erase(backend, key, async_mode);
} }

View file

@ -0,0 +1,98 @@
##! Synchronous operation methods for the storage framework.
@load ./main
module Storage::Sync;
export {
## Opens a new backend connection based on a configuration object.
##
## btype: A tag indicating what type of backend should be opened. These are
## defined by the backend plugins loaded.
##
## options: A record containing the configuration for the connection.
##
## key_type: The script-level type of keys stored in the backend. Used for
## validation of keys passed to other framework methods.
##
## val_type: The script-level type of keys stored in the backend. Used for
## validation of values passed to :zeek:see:`Storage::Sync::put` as well
## as for type conversions for return values from
## :zeek:see:`Storage::Sync::get`.
##
## Returns: A handle to the new backend connection, or ``F`` if the connection
## failed.
global open_backend: function(btype: Storage::Backend, options: any, key_type: any,
val_type: any): opaque of Storage::BackendHandle;
## Closes an existing backend connection.
##
## backend: A handle to a backend connection.
##
## Returns: A boolean indicating success or failure of the operation.
global close_backend: function(backend: opaque of Storage::BackendHandle): bool;
## Inserts a new entry into a backend.
##
## backend: A handle to a backend connection.
##
## args: A :zeek:see:`Storage::PutArgs` record containing the arguments for the
## operation.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned.
global put: function(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool;
## Gets an entry from the backend.
##
## backend: A handle to a backend connection.
##
## key: The key to look up.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned. The caller should check the validity of the value before
## attempting to use it. If the value is unset, an error string may be
## available to describe the failure.
global get: function(backend: opaque of Storage::BackendHandle, key: any): val_result;
## Erases an entry from the backend.
##
## backend: A handle to a backend connection.
##
## key: The key to erase.
##
## Returns: A boolean indicating success or failure of the operation. Type
## comparison failures against the types passed to
## :zeek:see:`Storage::open_backend` for the backend will cause ``F`` to
## be returned.
global erase: function(backend: opaque of Storage::BackendHandle, key: any): bool;
}
function open_backend(btype: Storage::Backend, options: any, key_type: any, val_type: any): opaque of Storage::BackendHandle
{
return Storage::Sync::__open_backend(btype, options, key_type, val_type);
}
function close_backend(backend: opaque of Storage::BackendHandle): bool
{
return Storage::Sync::__close_backend(backend);
}
function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool
{
return Storage::Sync::__put(backend, args$key, args$value, args$overwrite, args$expire_time);
}
function get(backend: opaque of Storage::BackendHandle, key: any): val_result
{
return Storage::Sync::__get(backend, key);
}
function erase(backend: opaque of Storage::BackendHandle, key: any): bool
{
return Storage::Sync::__erase(backend, key);
}

View file

@ -148,11 +148,16 @@ static std::unordered_map<std::string, unsigned int> func_attrs = {
{"Reporter::warning", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Reporter::warning", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Spicy::__resource_usage", ATTR_NO_ZEEK_SIDE_EFFECTS}, {"Spicy::__resource_usage", ATTR_NO_ZEEK_SIDE_EFFECTS},
{"Spicy::__toggle_analyzer", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Spicy::__toggle_analyzer", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::__close_backend", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Storage::Async::__close_backend", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::__erase", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Storage::Async::__erase", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::__get", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Storage::Async::__get", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::__open_backend", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Storage::Async::__open_backend", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::__put", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Storage::Async::__put", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::Sync::__close_backend", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::Sync::__erase", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::Sync::__get", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::Sync::__open_backend", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Storage::Sync::__put", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Supervisor::__create", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Supervisor::__create", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Supervisor::__destroy", ATTR_NO_SCRIPT_SIDE_EFFECTS}, {"Supervisor::__destroy", ATTR_NO_SCRIPT_SIDE_EFFECTS},
{"Supervisor::__is_supervised", ATTR_IDEMPOTENT}, {"Supervisor::__is_supervised", ATTR_IDEMPOTENT},

View file

@ -7,13 +7,13 @@
using namespace zeek; using namespace zeek;
using namespace zeek::storage; using namespace zeek::storage;
static zeek::detail::trigger::TriggerPtr init_trigger(zeek::detail::Frame* frame, const BackendPtr& b) { static zeek::detail::trigger::TriggerPtr init_trigger(zeek::detail::Frame* frame) {
auto trigger = frame->GetTrigger(); auto trigger = frame->GetTrigger();
if ( ! trigger ) { if ( ! trigger ) {
emit_builtin_error("Async storage operations can only be called inside when-conditions"); emit_builtin_error("Asynchronous storage operations must be called via a when-condition");
return nullptr; return nullptr;
} }
if ( auto timeout = trigger->TimeoutValue(); timeout < 0 ) { if ( auto timeout = trigger->TimeoutValue(); timeout < 0 ) {
emit_builtin_error("Async Storage operations must specify a timeout block"); emit_builtin_error("Async Storage operations must specify a timeout block");
@ -25,6 +25,21 @@ static zeek::detail::trigger::TriggerPtr init_trigger(zeek::detail::Frame* frame
return {NewRef{}, trigger}; return {NewRef{}, trigger};
} }
static IntrusivePtr<storage::detail::BackendHandleVal> make_backend_handle(Val* btype) {
auto btype_val = IntrusivePtr<EnumVal>{NewRef{}, btype->AsEnumVal()};
Tag tag{btype_val};
auto b = storage_mgr->Instantiate(tag);
if ( ! b.has_value() ) {
emit_builtin_error(b.error().c_str());
return nullptr;
}
return make_intrusive<storage::detail::BackendHandleVal>(b.value());
}
%%} %%}
module Storage; module Storage;
@ -35,7 +50,130 @@ event Storage::backend_opened%(%);
# Generated when a backend connection is lost # Generated when a backend connection is lost
event Storage::backend_lost%(%); event Storage::backend_lost%(%);
function Storage::__open_backend%(btype: Storage::Backend, options: any, key_type: any, val_type: any, async_mode: bool%): opaque of Storage::BackendHandle module Storage::Async;
function Storage::Async::__open_backend%(btype: Storage::Backend, options: any, key_type: any, val_type: any%): opaque of Storage::BackendHandle
%{
auto trigger = init_trigger(frame);
if ( ! trigger )
return val_mgr->Bool(false);
auto btype_val = IntrusivePtr<EnumVal>{NewRef{}, btype->AsEnumVal()};
Tag tag{btype_val};
auto b = storage_mgr->Instantiate(tag);
if ( ! b.has_value() ) {
emit_builtin_error(b.error().c_str());
return nullptr;
}
auto bh = make_intrusive<storage::detail::BackendHandleVal>(b.value());
auto cb = new OpenResultCallback(trigger, frame->GetTriggerAssoc(), bh.release());
auto kt = key_type->AsTypeVal()->GetType()->AsTypeType()->GetType();
auto vt = val_type->AsTypeVal()->GetType()->AsTypeType()->GetType();
auto options_val = IntrusivePtr<RecordVal>{NewRef{}, options->AsRecordVal()};
storage_mgr->OpenBackend(b.value(), options_val, kt, vt, cb);
return nullptr;
%}
function Storage::Async::__close_backend%(backend: opaque of Storage::BackendHandle%) : bool
%{
auto trigger = init_trigger(frame);
if ( ! trigger )
return val_mgr->Bool(false);
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend);
if ( ! b ) {
emit_builtin_error("Invalid storage handle", backend);
return val_mgr->Bool(false);
}
else if ( ! b->backend->IsOpen() )
return val_mgr->Bool(true);
auto cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc());
storage_mgr->CloseBackend(b->backend, cb);
return nullptr;
%}
function Storage::Async::__put%(backend: opaque of Storage::BackendHandle, key: any, value: any,
overwrite: bool, expire_time: interval%): bool
%{
auto trigger = init_trigger(frame);
if ( ! trigger )
return val_mgr->Bool(false);
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend);
if ( ! b ) {
emit_builtin_error("Invalid storage handle", backend);
return val_mgr->Bool(false);
}
else if ( ! b->backend->IsOpen() )
return val_mgr->Bool(false);
auto cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc());
auto key_v = IntrusivePtr<Val>{NewRef{}, key};
auto val_v = IntrusivePtr<Val>{NewRef{}, value};
b->backend->Put(key_v, val_v, overwrite, expire_time, cb);
return nullptr;
%}
function Storage::Async::__get%(backend: opaque of Storage::BackendHandle, key: any%): val_result
%{
static auto val_result_type = id::find_type<RecordType>("val_result");
auto val_result = make_intrusive<RecordVal>(val_result_type);
auto trigger = init_trigger(frame);
if ( ! trigger ) {
val_result->Assign(1, make_intrusive<StringVal>("Failed to create trigger"));
return val_result;
}
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend);
if ( ! b ) {
val_result->Assign(1, make_intrusive<StringVal>("Invalid storage handlle"));
return val_result;
}
else if ( ! b->backend->IsOpen() ) {
val_result->Assign(1, make_intrusive<StringVal>("Backend is closed"));
return val_result;
}
auto cb = new ValResultCallback(trigger, frame->GetTriggerAssoc());
auto key_v = IntrusivePtr<Val>{NewRef{}, key};
auto result = b->backend->Get(key_v, cb);
return nullptr;
%}
function Storage::Async::__erase%(backend: opaque of Storage::BackendHandle, key: any%): bool
%{
auto trigger = init_trigger(frame);
if ( ! trigger )
return val_mgr->Bool(false);
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend);
if ( ! b ) {
emit_builtin_error("Invalid storage handle", backend);
return val_mgr->Bool(false);
}
else if ( ! b->backend->IsOpen() )
return val_mgr->Bool(false);
auto cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc());
auto key_v = IntrusivePtr<Val>{NewRef{}, key};
b->backend->Erase(key_v, cb);
return nullptr;
%}
module Storage::Sync;
function Storage::Sync::__open_backend%(btype: Storage::Backend, options: any, key_type: any, val_type: any%): opaque of Storage::BackendHandle
%{ %{
auto btype_val = IntrusivePtr<EnumVal>{NewRef{}, btype->AsEnumVal()}; auto btype_val = IntrusivePtr<EnumVal>{NewRef{}, btype->AsEnumVal()};
Tag tag{btype_val}; Tag tag{btype_val};
@ -47,34 +185,20 @@ function Storage::__open_backend%(btype: Storage::Backend, options: any, key_typ
return val_mgr->Bool(false); return val_mgr->Bool(false);
} }
auto bo = make_intrusive<storage::detail::BackendHandleVal>(b.value());
OpenResultCallback* cb = nullptr;
if ( async_mode ) {
auto trigger = init_trigger(frame, b.value());
if ( ! trigger )
return val_mgr->Bool(false);
cb = new OpenResultCallback(trigger, frame->GetTriggerAssoc(), bo.release());
}
auto kt = key_type->AsTypeVal()->GetType()->AsTypeType()->GetType(); auto kt = key_type->AsTypeVal()->GetType()->AsTypeType()->GetType();
auto vt = val_type->AsTypeVal()->GetType()->AsTypeType()->GetType(); auto vt = val_type->AsTypeVal()->GetType()->AsTypeType()->GetType();
auto options_val = IntrusivePtr<RecordVal>{NewRef{}, options->AsRecordVal()}; auto options_val = IntrusivePtr<RecordVal>{NewRef{}, options->AsRecordVal()};
auto open_res = storage_mgr->OpenBackend(b.value(), options_val, kt, vt, cb); auto open_res = storage_mgr->OpenBackend(b.value(), options_val, kt, vt, nullptr);
if ( async_mode )
return nullptr;
if ( open_res.has_value() ) { if ( open_res.has_value() ) {
emit_builtin_error(open_res.value().c_str()); emit_builtin_error(open_res.value().c_str());
return val_mgr->Bool(false); return val_mgr->Bool(false);
} }
return bo; return make_intrusive<storage::detail::BackendHandleVal>(b.value());
%} %}
function Storage::__close_backend%(backend: opaque of Storage::BackendHandle, async_mode: bool%) : bool function Storage::Sync::__close_backend%(backend: opaque of Storage::BackendHandle%) : bool
%{ %{
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend); auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend);
if ( ! b ) { if ( ! b ) {
@ -85,20 +209,7 @@ function Storage::__close_backend%(backend: opaque of Storage::BackendHandle, as
// Return true here since the backend is already closed // Return true here since the backend is already closed
return val_mgr->Bool(true); return val_mgr->Bool(true);
ErrorResultCallback* cb = nullptr; auto result = storage_mgr->CloseBackend(b->backend, nullptr);
if ( async_mode ) {
auto trigger = init_trigger(frame, b->backend);
if ( ! trigger )
return val_mgr->Bool(false);
cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc());
}
auto result = storage_mgr->CloseBackend(b->backend, cb);
if ( async_mode )
return nullptr;
if ( result.has_value() ) { if ( result.has_value() ) {
emit_builtin_error(result.value().c_str()); emit_builtin_error(result.value().c_str());
@ -108,8 +219,8 @@ function Storage::__close_backend%(backend: opaque of Storage::BackendHandle, as
return val_mgr->Bool(true); return val_mgr->Bool(true);
%} %}
function Storage::__put%(backend: opaque of Storage::BackendHandle, key: any, value: any, function Storage::Sync::__put%(backend: opaque of Storage::BackendHandle, key: any, value: any,
overwrite: bool, expire_time: interval, async_mode: bool &default=T%): bool overwrite: bool, expire_time: interval%): bool
%{ %{
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend); auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend);
if ( ! b ) { if ( ! b ) {
@ -119,22 +230,9 @@ function Storage::__put%(backend: opaque of Storage::BackendHandle, key: any, va
else if ( ! b->backend->IsOpen() ) else if ( ! b->backend->IsOpen() )
return val_mgr->Bool(false); return val_mgr->Bool(false);
ErrorResultCallback* cb = nullptr;
if ( async_mode ) {
auto trigger = init_trigger(frame, b->backend);
if ( ! trigger )
return val_mgr->Bool(false);
cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc());
}
auto key_v = IntrusivePtr<Val>{NewRef{}, key}; auto key_v = IntrusivePtr<Val>{NewRef{}, key};
auto val_v = IntrusivePtr<Val>{NewRef{}, value}; auto val_v = IntrusivePtr<Val>{NewRef{}, value};
auto result = b->backend->Put(key_v, val_v, overwrite, expire_time, cb); auto result = b->backend->Put(key_v, val_v, overwrite, expire_time, nullptr);
if ( async_mode )
return nullptr;
if ( result.has_value() ) { if ( result.has_value() ) {
emit_builtin_error(util::fmt("Failed to store data: %s", result.value().c_str())); emit_builtin_error(util::fmt("Failed to store data: %s", result.value().c_str()));
@ -144,7 +242,7 @@ function Storage::__put%(backend: opaque of Storage::BackendHandle, key: any, va
return val_mgr->Bool(true); return val_mgr->Bool(true);
%} %}
function Storage::__get%(backend: opaque of Storage::BackendHandle, key: any, async_mode: bool &default=T%): val_result function Storage::Sync::__get%(backend: opaque of Storage::BackendHandle, key: any%): val_result
%{ %{
static auto val_result_type = id::find_type<RecordType>("val_result"); static auto val_result_type = id::find_type<RecordType>("val_result");
auto val_result = make_intrusive<RecordVal>(val_result_type); auto val_result = make_intrusive<RecordVal>(val_result_type);
@ -159,27 +257,12 @@ function Storage::__get%(backend: opaque of Storage::BackendHandle, key: any, as
return val_result; return val_result;
} }
ValResultCallback* cb = nullptr;
if ( async_mode ) {
auto trigger = init_trigger(frame, b->backend);
if ( ! trigger ) {
val_result->Assign(1, make_intrusive<StringVal>("Failed to create trigger"));
return val_result;
}
cb = new ValResultCallback(trigger, frame->GetTriggerAssoc());
}
auto key_v = IntrusivePtr<Val>{NewRef{}, key}; auto key_v = IntrusivePtr<Val>{NewRef{}, key};
auto result = b->backend->Get(key_v, cb); auto result = b->backend->Get(key_v, nullptr);
if ( async_mode )
return nullptr;
if ( ! result.has_value() ) { if ( ! result.has_value() ) {
val_result->Assign(1, make_intrusive<StringVal>( val_result->Assign(1, make_intrusive<StringVal>(
util::fmt("Failed to retrieve data: %s", result.error().c_str()))); util::fmt("Failed to retrieve data: %s", result.error().c_str())));
return val_result; return val_result;
} }
@ -187,7 +270,7 @@ function Storage::__get%(backend: opaque of Storage::BackendHandle, key: any, as
return val_result; return val_result;
%} %}
function Storage::__erase%(backend: opaque of Storage::BackendHandle, key: any, async_mode: bool &default=T%): bool function Storage::Sync::__erase%(backend: opaque of Storage::BackendHandle, key: any%): bool
%{ %{
auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend); auto b = dynamic_cast<storage::detail::BackendHandleVal*>(backend);
if ( ! b ) { if ( ! b ) {
@ -197,21 +280,8 @@ function Storage::__erase%(backend: opaque of Storage::BackendHandle, key: any,
else if ( ! b->backend->IsOpen() ) else if ( ! b->backend->IsOpen() )
return val_mgr->Bool(false); return val_mgr->Bool(false);
ErrorResultCallback* cb = nullptr;
if ( async_mode ) {
auto trigger = init_trigger(frame, b->backend);
if ( ! trigger )
return val_mgr->Bool(false);
cb = new ErrorResultCallback(trigger, frame->GetTriggerAssoc());
}
auto key_v = IntrusivePtr<Val>{NewRef{}, key}; auto key_v = IntrusivePtr<Val>{NewRef{}, key};
auto result = b->backend->Erase(key_v, cb); auto result = b->backend->Erase(key_v, nullptr);
if ( async_mode )
return nullptr;
if ( result.has_value() ) { if ( result.has_value() ) {
emit_builtin_error(util::fmt("Failed to erase data for key: %s", result.value().c_str())); emit_builtin_error(util::fmt("Failed to erase data for key: %s", result.value().c_str()));

View file

@ -369,7 +369,9 @@ scripts/base/init-default.zeek
scripts/base/frameworks/telemetry/main.zeek scripts/base/frameworks/telemetry/main.zeek
scripts/base/misc/version.zeek scripts/base/misc/version.zeek
scripts/base/frameworks/storage/__load__.zeek scripts/base/frameworks/storage/__load__.zeek
scripts/base/frameworks/storage/main.zeek scripts/base/frameworks/storage/async.zeek
scripts/base/frameworks/storage/main.zeek
scripts/base/frameworks/storage/sync.zeek
scripts/base/frameworks/spicy/__load__.zeek scripts/base/frameworks/spicy/__load__.zeek
scripts/base/frameworks/spicy/main.zeek scripts/base/frameworks/spicy/main.zeek
scripts/base/protocols/conn/__load__.zeek scripts/base/protocols/conn/__load__.zeek

View file

@ -1,2 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
551 seen BiFs, 0 unseen BiFs (), 0 new BiFs () 556 seen BiFs, 0 unseen BiFs (), 0 new BiFs ()

View file

@ -1,4 +1,4 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/storage.zeek, line 41: Failed to retrieve data: Failed to find key error in <...>/storage.zeek, line 41: Failed to retrieve data: Failed to find key
error in <...>/storage.zeek, line 53: Failed to open backend Storage::STORAGEDUMMY: open_fail was set to true, returning error (Storage::open_backend(Storage::STORAGEDUMMY, to_any_coerce opts, to_any_coerce str, to_any_coerce str, F)) error in <...>/sync.zeek, line 74: Failed to open backend Storage::STORAGEDUMMY: open_fail was set to true, returning error (Storage::Sync::__open_backend(Storage::Sync::btype, Storage::Sync::options, Storage::Sync::key_type, Storage::Sync::val_type))
error in <...>/storage.zeek, line 54: Invalid storage handle (Storage::close_backend(b2, F) and F) error in <...>/sync.zeek, line 79: Invalid storage handle (Storage::Sync::__close_backend(Storage::Sync::backend) and F)

View file

@ -1,3 +1,3 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. ### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <...>/sqlite-error-handling.zeek, line 20: Failed to open backend Storage::SQLITE: SQLite call failed: unable to open database file (Storage::open_backend(Storage::SQLITE, to_any_coerce opts, to_any_coerce str, to_any_coerce str, F)) error in <...>/sync.zeek, line 74: Failed to open backend Storage::SQLITE: SQLite call failed: unable to open database file (Storage::Sync::__open_backend(Storage::Sync::btype, Storage::Sync::options, Storage::Sync::key_type, Storage::Sync::val_type))
error in <...>/sqlite-error-handling.zeek, line 28: Failed to store data: type of key passed (count) does not match backend's key type (str) (Storage::put(b, (coerce [$key=bad_key, $value=value, $async_mode=F] to Storage::PutArgs))) error in <...>/sync.zeek, line 84: Failed to store data: type of key passed (count) does not match backend's key type (str) (Storage::Sync::__put(Storage::Sync::backend, Storage::Sync::args$key, Storage::Sync::args$value, Storage::Sync::args$overwrite, Storage::Sync::args$expire_time))

View file

@ -177,11 +177,16 @@ global known_BiFs = set(
"Reporter::warning", "Reporter::warning",
"Spicy::__resource_usage", "Spicy::__resource_usage",
"Spicy::__toggle_analyzer", "Spicy::__toggle_analyzer",
"Storage::__close_backend", "Storage::Async::__close_backend",
"Storage::__erase", "Storage::Async::__erase",
"Storage::__get", "Storage::Async::__get",
"Storage::__open_backend", "Storage::Async::__open_backend",
"Storage::__put", "Storage::Async::__put",
"Storage::Sync::__close_backend",
"Storage::Sync::__erase",
"Storage::Sync::__get",
"Storage::Sync::__open_backend",
"Storage::Sync::__put",
"Supervisor::__create", "Supervisor::__create",
"Supervisor::__destroy", "Supervisor::__destroy",
"Supervisor::__is_supervised", "Supervisor::__is_supervised",

View file

@ -8,7 +8,7 @@
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff output
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff zeek-stderr # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff zeek-stderr
@load base/frameworks/storage @load base/frameworks/storage/sync
# Create a typename here that can be passed down into get(). # Create a typename here that can be passed down into get().
type str: string; type str: string;
@ -26,30 +26,30 @@ event zeek_init() {
# Test basic operation. The second get() should return an error # Test basic operation. The second get() should return an error
# as the key should have been erased. # as the key should have been erased.
local b = Storage::open_backend(Storage::STORAGEDUMMY, opts, str, str); local b = Storage::Sync::open_backend(Storage::STORAGEDUMMY, opts, str, str);
local put_res = Storage::put(b, [$key=key, $value=value, $overwrite=F, $async_mode=F]); local put_res = Storage::Sync::put(b, [$key=key, $value=value, $overwrite=F]);
local get_res = Storage::get(b, key, F); local get_res = Storage::Sync::get(b, key);
if ( get_res is bool ) { if ( get_res is bool ) {
print("Got an invalid value in response!"); print("Got an invalid value in response!");
} }
local erase_res = Storage::erase(b, key, F); local erase_res = Storage::Sync::erase(b, key);
get_res = Storage::get(b, key, F); get_res = Storage::Sync::get(b, key);
Storage::close_backend(b); Storage::Sync::close_backend(b);
if ( get_res?$error ) if ( get_res?$error )
Reporter::error(get_res$error); Reporter::error(get_res$error);
# Test attempting to use the closed handle. # Test attempting to use the closed handle.
put_res = Storage::put(b, [$key="a", $value="b", $overwrite=F]); put_res = Storage::Sync::put(b, [$key="a", $value="b", $overwrite=F]);
get_res = Storage::get(b, "a"); get_res = Storage::Sync::get(b, "a");
erase_res = Storage::erase(b, "a"); erase_res = Storage::Sync::erase(b, "a");
print(fmt("results of trying to use closed handle: get: %d, put: %d, erase: %d", print(fmt("results of trying to use closed handle: get: %d, put: %d, erase: %d",
get_res?$val, put_res, erase_res)); get_res?$val, put_res, erase_res));
# Test failing to open the handle and test closing an invalid handle. # Test failing to open the handle and test closing an invalid handle.
opts$open_fail = T; opts$open_fail = T;
local b2 = Storage::open_backend(Storage::STORAGEDUMMY, opts, str, str); local b2 = Storage::Sync::open_backend(Storage::STORAGEDUMMY, opts, str, str);
Storage::close_backend(b2); Storage::Sync::close_backend(b2);
} }

View file

@ -3,7 +3,7 @@
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr # @TEST-EXEC: btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
type Color: enum { type Color: enum {
@ -64,11 +64,11 @@ event zeek_init() {
value[2] = "b"; value[2] = "b";
value[3] = "c"; value[3] = "c";
local b = Storage::open_backend(Storage::SQLITE, opts, Rec, tbl); local b = Storage::Sync::open_backend(Storage::SQLITE, opts, Rec, tbl);
local res = Storage::put(b, [$key=key, $value=value, $async_mode=F]); local res = Storage::Sync::put(b, [$key=key, $value=value]);
print "put result", res; print "put result", res;
local res2 = Storage::get(b, key, F); local res2 = Storage::Sync::get(b, key);
print "get result", res2; print "get result", res2;
} }

View file

@ -4,7 +4,7 @@
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
# Create a typename here that can be passed down into get(). # Create a typename here that can be passed down into get().
@ -20,14 +20,14 @@ event zeek_init() {
# Test inserting/retrieving a key/value pair that we know won't be in # Test inserting/retrieving a key/value pair that we know won't be in
# the backend yet. # the backend yet.
local b = Storage::open_backend(Storage::SQLITE, opts, str, str); local b = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str);
local res = Storage::erase(b, key, F); local res = Storage::Sync::erase(b, key);
print "erase result", res; print "erase result", res;
local res2 = Storage::get(b, key, F); local res2 = Storage::Sync::get(b, key);
if ( res2?$error ) if ( res2?$error )
print "get result", res2$error; print "get result", res2$error;
Storage::close_backend(b); Storage::Sync::close_backend(b);
} }

View file

@ -3,7 +3,7 @@
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
redef Storage::expire_interval = 2 secs; redef Storage::expire_interval = 2 secs;
@ -19,11 +19,11 @@ global value: string = "value7890";
event check_removed() { event check_removed() {
# This should return an error from the sqlite backend that there aren't any more # This should return an error from the sqlite backend that there aren't any more
# rows available. # rows available.
local res2 = Storage::get(backend, key, F); local res2 = Storage::Sync::get(backend, key);
if ( res2?$error ) if ( res2?$error )
print "get result", res2$error; print "get result", res2$error;
Storage::close_backend(backend); Storage::Sync::close_backend(backend);
terminate(); terminate();
} }
@ -32,12 +32,12 @@ event setup_test() {
opts$database_path = "storage-test.sqlite"; opts$database_path = "storage-test.sqlite";
opts$table_name = "testing"; opts$table_name = "testing";
backend = Storage::open_backend(Storage::SQLITE, opts, str, str); backend = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str);
local res = Storage::put(backend, [$key=key, $value=value, $expire_time=2 secs, $async_mode=F]); local res = Storage::Sync::put(backend, [$key=key, $value=value, $expire_time=2 secs]);
print "put result", res; print "put result", res;
local res2 = Storage::get(backend, key, F); local res2 = Storage::Sync::get(backend, key);
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value == (res2$val as string); print "get result same as inserted", value == (res2$val as string);

View file

@ -4,7 +4,7 @@
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr # @TEST-EXEC: btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
# Create a typename here that can be passed down into get(). # Create a typename here that can be passed down into get().
@ -18,15 +18,15 @@ event zeek_init() {
local key = "key1234"; local key = "key1234";
local value = "value7890"; local value = "value7890";
local b = Storage::open_backend(Storage::SQLITE, opts, str, str); local b = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str);
local res = Storage::put(b, [$key=key, $value=value, $async_mode=F]); local res = Storage::Sync::put(b, [$key=key, $value=value]);
print "put result", res; print "put result", res;
local res2 = Storage::get(b, key, F); local res2 = Storage::Sync::get(b, key);
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value == (res2$val as string); print "get result same as inserted", value == (res2$val as string);
Storage::close_backend(b); Storage::Sync::close_backend(b);
} }

View file

@ -12,7 +12,8 @@
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
@load base/frameworks/storage @load base/frameworks/storage/sync
@load base/frameworks/storage/async
@load policy/frameworks/storage/backend/redis @load policy/frameworks/storage/backend/redis
# Create a typename here that can be passed down into open_backend() # Create a typename here that can be passed down into open_backend()
@ -28,17 +29,17 @@ event zeek_init() {
local key = "key1234"; local key = "key1234";
local value = "value5678"; local value = "value5678";
local b = Storage::open_backend(Storage::REDIS, opts, str, str); local b = Storage::Sync::open_backend(Storage::REDIS, opts, str, str);
when [b, key, value] ( local res = Storage::put(b, [$key=key, $value=value]) ) { when [b, key, value] ( local res = Storage::Async::put(b, [$key=key, $value=value]) ) {
print "put result", res; print "put result", res;
when [b, key, value] ( local res2 = Storage::get(b, key) ) { when [b, key, value] ( local res2 = Storage::Async::get(b, key) ) {
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value == (res2$val as string); print "get result same as inserted", value == (res2$val as string);
Storage::close_backend(b); Storage::Sync::close_backend(b);
} }
timeout 5 sec { timeout 5 sec {
print "get requeest timed out"; print "get requeest timed out";

View file

@ -12,7 +12,8 @@
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
@load base/frameworks/storage @load base/frameworks/storage/async
@load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/redis @load policy/frameworks/storage/backend/redis
redef exit_only_after_terminate = T; redef exit_only_after_terminate = T;
@ -30,17 +31,17 @@ event zeek_init() {
local key = "key1234"; local key = "key1234";
local value = "value5678"; local value = "value5678";
local b = Storage::open_backend(Storage::REDIS, opts, str, str); local b = Storage::Sync::open_backend(Storage::REDIS, opts, str, str);
when [b, key, value] ( local res = Storage::put(b, [$key=key, $value=value]) ) { when [b, key, value] ( local res = Storage::Async::put(b, [$key=key, $value=value]) ) {
print "put result", res; print "put result", res;
when [b, key, value] ( local res2 = Storage::get(b, key) ) { when [b, key, value] ( local res2 = Storage::Async::get(b, key) ) {
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value == (res2$val as string); print "get result same as inserted", value == (res2$val as string);
Storage::close_backend(b); Storage::Sync::close_backend(b);
terminate(); terminate();
} }

View file

@ -19,7 +19,7 @@
# @TEST-EXEC: btest-diff worker-2/.stdout # @TEST-EXEC: btest-diff worker-2/.stdout
# @TEST-EXEC: # @TEST-EXEC:
@load base/frameworks/storage @load base/frameworks/storage/sync
@load base/frameworks/cluster @load base/frameworks/cluster
@load policy/frameworks/storage/backend/redis @load policy/frameworks/storage/backend/redis
@load policy/frameworks/cluster/experimental @load policy/frameworks/cluster/experimental
@ -46,14 +46,14 @@ event zeek_init() {
opts$key_prefix = "testing"; opts$key_prefix = "testing";
opts$async_mode = F; opts$async_mode = F;
backend = Storage::open_backend(Storage::REDIS, opts, str, str); backend = Storage::Sync::open_backend(Storage::REDIS, opts, str, str);
} }
event redis_data_written() { event redis_data_written() {
print "redis_data_written"; print "redis_data_written";
local res = Storage::get(backend, "1234", F); local res = Storage::Sync::get(backend, "1234");
print Cluster::node, res; print Cluster::node, res;
Storage::close_backend(backend); Storage::Sync::close_backend(backend);
terminate(); terminate();
} }
@ -77,7 +77,7 @@ event redis_data_written() {
@if ( Cluster::node == "worker-1" ) @if ( Cluster::node == "worker-1" )
event Cluster::Experimental::cluster_started() { event Cluster::Experimental::cluster_started() {
local res = Storage::put(backend, [$key="1234", $value="5678", $async_mode=F]); local res = Storage::Sync::put(backend, [$key="1234", $value="5678"]);
print Cluster::node, "put result", res; print Cluster::node, "put result", res;
local e = Cluster::make_event(redis_data_written); local e = Cluster::make_event(redis_data_written);

View file

@ -12,7 +12,7 @@
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
@load base/frameworks/storage @load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/redis @load policy/frameworks/storage/backend/redis
redef Storage::expire_interval = 2 secs; redef Storage::expire_interval = 2 secs;
@ -26,10 +26,10 @@ global key: string = "key1234";
global value: string = "value7890"; global value: string = "value7890";
event check_removed() { event check_removed() {
local res2 = Storage::get(b, key, F); local res2 = Storage::Sync::get(b, key);
print "get result after expiration", res2; print "get result after expiration", res2;
Storage::close_backend(b); Storage::Sync::close_backend(b);
terminate(); terminate();
} }
@ -40,12 +40,12 @@ event setup_test() {
opts$key_prefix = "testing"; opts$key_prefix = "testing";
opts$async_mode = F; opts$async_mode = F;
b = Storage::open_backend(Storage::REDIS, opts, str, str); b = Storage::Sync::open_backend(Storage::REDIS, opts, str, str);
local res = Storage::put(b, [$key=key, $value=value, $async_mode=F, $expire_time=2 secs]); local res = Storage::Sync::put(b, [$key=key, $value=value, $expire_time=2 secs]);
print "put result", res; print "put result", res;
local res2 = Storage::get(b, key, F); local res2 = Storage::Sync::get(b, key);
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value == (res2$val as string); print "get result same as inserted", value == (res2$val as string);

View file

@ -12,7 +12,7 @@
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
@load base/frameworks/storage @load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/redis @load policy/frameworks/storage/backend/redis
# Create a typename here that can be passed down into open_backend() # Create a typename here that can be passed down into open_backend()
@ -28,24 +28,24 @@ event zeek_init() {
local key = "key1234"; local key = "key1234";
local value = "value1234"; local value = "value1234";
local b = Storage::open_backend(Storage::REDIS, opts, str, str); local b = Storage::Sync::open_backend(Storage::REDIS, opts, str, str);
local res = Storage::put(b, [$key=key, $value=value, $async_mode=F]); local res = Storage::Sync::put(b, [$key=key, $value=value]);
print "put result", res; print "put result", res;
local res2 = Storage::get(b, key, F); local res2 = Storage::Sync::get(b, key);
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value == (res2$val as string); print "get result same as inserted", value == (res2$val as string);
local value2 = "value5678"; local value2 = "value5678";
res = Storage::put(b, [$key=key, $value=value2, $overwrite=T, $async_mode=F]); res = Storage::Sync::put(b, [$key=key, $value=value2, $overwrite=T]);
print "overwrite put result", res; print "overwrite put result", res;
res2 = Storage::get(b, key, F); res2 = Storage::Sync::get(b, key);
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value2 == (res2$val as string); print "get result same as inserted", value2 == (res2$val as string);
Storage::close_backend(b); Storage::Sync::close_backend(b);
} }

View file

@ -3,7 +3,8 @@
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage/async
@load base/frameworks/storage/sync
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
redef exit_only_after_terminate = T; redef exit_only_after_terminate = T;
@ -22,17 +23,17 @@ event zeek_init() {
# Test inserting/retrieving a key/value pair that we know won't be in # Test inserting/retrieving a key/value pair that we know won't be in
# the backend yet. # the backend yet.
local b = Storage::open_backend(Storage::SQLITE, opts, str, str); local b = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str);
when [b, key, value] ( local res = Storage::put(b, [$key=key, $value=value]) ) { when [b, key, value] ( local res = Storage::Async::put(b, [$key=key, $value=value]) ) {
print "put result", res; print "put result", res;
when [b, key, value] ( local res2 = Storage::get(b, key) ) { when [b, key, value] ( local res2 = Storage::Async::get(b, key) ) {
print "get result", res2; print "get result", res2;
if ( res2?$val ) if ( res2?$val )
print "get result same as inserted", value == (res2$val as string); print "get result same as inserted", value == (res2$val as string);
Storage::close_backend(b); Storage::Sync::close_backend(b);
terminate(); terminate();
} }

View file

@ -3,7 +3,7 @@
# @TEST-EXEC: btest-diff out # @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr # @TEST-EXEC: btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage/async
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
redef exit_only_after_terminate = T; redef exit_only_after_terminate = T;
@ -22,18 +22,18 @@ event zeek_init() {
# Test inserting/retrieving a key/value pair that we know won't be in # Test inserting/retrieving a key/value pair that we know won't be in
# the backend yet. # the backend yet.
when [opts, key, value] ( local b = Storage::open_backend(Storage::SQLITE, opts, str, str, T) ) { when [opts, key, value] ( local b = Storage::Async::open_backend(Storage::SQLITE, opts, str, str) ) {
print "open successful"; print "open successful";
when [b, key, value] ( local put_res = Storage::put(b, [$key=key, $value=value]) ) { when [b, key, value] ( local put_res = Storage::Async::put(b, [$key=key, $value=value]) ) {
print "put result", put_res; print "put result", put_res;
when [b, key, value] ( local get_res = Storage::get(b, key) ) { when [b, key, value] ( local get_res = Storage::Async::get(b, key) ) {
print "get result", get_res; print "get result", get_res;
if ( get_res?$val ) if ( get_res?$val )
print "get result same as inserted", value == (get_res$val as string); print "get result same as inserted", value == (get_res$val as string);
when [b] ( local close_res = Storage::close_backend(b, T) ) { when [b] ( local close_res = Storage::Async::close_backend(b) ) {
print "closed succesfully"; print "closed succesfully";
terminate(); terminate();
} timeout 5 sec { } timeout 5 sec {

View file

@ -3,7 +3,7 @@
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff out
# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr # @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff .stderr
@load base/frameworks/storage @load base/frameworks/storage/sync
@load base/frameworks/reporter @load base/frameworks/reporter
@load policy/frameworks/storage/backend/sqlite @load policy/frameworks/storage/backend/sqlite
@ -17,18 +17,18 @@ event zeek_init() {
opts$table_name = "testing"; opts$table_name = "testing";
# This should report an error in .stderr and reporter.log # This should report an error in .stderr and reporter.log
local b = Storage::open_backend(Storage::SQLITE, opts, str, str); local b = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str);
# Open a valid database file # Open a valid database file
opts$database_path = "test.sqlite"; opts$database_path = "test.sqlite";
b = Storage::open_backend(Storage::SQLITE, opts, str, str); b = Storage::Sync::open_backend(Storage::SQLITE, opts, str, str);
local bad_key: count = 12345; local bad_key: count = 12345;
local value = "abcde"; local value = "abcde";
Storage::put(b, [$key=bad_key, $value=value, $async_mode=F]); Storage::Sync::put(b, [$key=bad_key, $value=value]);
# Close the backend and then attempt to use the closed handle # Close the backend and then attempt to use the closed handle
Storage::close_backend(b); Storage::Sync::close_backend(b);
local res = Storage::put(b, [$key="a", $value="b", $async_mode=F]); local res = Storage::Sync::put(b, [$key="a", $value="b"]);
print fmt("Put result on closed handle: %d", res); print fmt("Put result on closed handle: %d", res);
} }