Completely rework return values from storage operations

This commit is contained in:
Tim Wojtulewicz 2025-02-24 14:37:11 -07:00
parent 8ddda016ff
commit 9ed3e33f97
50 changed files with 859 additions and 586 deletions

View file

@ -22,17 +22,19 @@ export {
## 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.
## Returns: A record containing the status of the operation, and either an error
## string on failure or a value on success. The value returned here will
## be an ``opaque of BackendHandle``.
global open_backend: function(btype: Storage::Backend, options: Storage::BackendOptions, key_type: any,
val_type: any): opaque of Storage::BackendHandle;
val_type: any): Storage::OperationResult;
## 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;
## Returns: A record containing the status of the operation and an optional error
## string for failures.
global close_backend: function(backend: opaque of Storage::BackendHandle): Storage::OperationResult;
## Inserts a new entry into a backend asynchronously.
##
@ -41,11 +43,9 @@ export {
## 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;
## Returns: A record containing the status of the operation and an optional error
## string for failures.
global put: function(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): Storage::OperationResult;
## Gets an entry from the backend asynchronously.
##
@ -53,13 +53,11 @@ export {
##
## 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;
## Returns: A record containing the status of the operation, an optional error
## string for failures, and an optional value for success. The value
## returned here will be of the type passed into
## :zeek:see:`Storage::open_backend`.
global get: function(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult;
## Erases an entry from the backend asynchronously.
##
@ -67,35 +65,33 @@ export {
##
## 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;
## Returns: A record containing the status of the operation and an optional error
## string for failures.
global erase: function(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult;
}
function open_backend(btype: Storage::Backend, options: Storage::BackendOptions, key_type: any,
val_type: any): opaque of Storage::BackendHandle
val_type: any): Storage::OperationResult
{
return Storage::Async::__open_backend(btype, options, key_type, val_type);
}
function close_backend(backend: opaque of Storage::BackendHandle): bool
function close_backend(backend: opaque of Storage::BackendHandle): Storage::OperationResult
{
return Storage::Async::__close_backend(backend);
}
function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool
function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): Storage::OperationResult
{
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
function get(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult
{
return Storage::Async::__get(backend, key);
}
function erase(backend: opaque of Storage::BackendHandle, key: any): bool
function erase(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult
{
return Storage::Async::__erase(backend, key);
}

View file

@ -5,9 +5,9 @@
module Storage;
export {
## Base record for backend options. Backend plugins can redef this record to add
## relevant fields to it.
type BackendOptions: record {};
## Base record for backend options. Backend plugins can redef this record to add
## relevant fields to it.
type BackendOptions: record { };
## Record for passing arguments to :zeek:see:`Storage::Async::put` and
## :zeek:see:`Storage::Sync::put`.

View file

@ -20,17 +20,19 @@ export {
## 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.
## Returns: A record containing the status of the operation, and either an error
## string on failure or a value on success. The value returned here will
## be an ``opaque of BackendHandle``.
global open_backend: function(btype: Storage::Backend, options: Storage::BackendOptions, key_type: any,
val_type: any): opaque of Storage::BackendHandle;
val_type: any): Storage::OperationResult;
## 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;
## Returns: A record containing the status of the operation and an optional error
## string for failures.
global close_backend: function(backend: opaque of Storage::BackendHandle): Storage::OperationResult;
## Inserts a new entry into a backend.
##
@ -39,11 +41,9 @@ export {
## 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;
## Returns: A record containing the status of the operation and an optional error
## string for failures.
global put: function(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): Storage::OperationResult;
## Gets an entry from the backend.
##
@ -51,13 +51,11 @@ export {
##
## 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;
## Returns: A record containing the status of the operation, an optional error
## string for failures, and an optional value for success. The value
## returned here will be of the type passed into
## :zeek:see:`Storage::open_backend`.
global get: function(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult;
## Erases an entry from the backend.
##
@ -65,35 +63,33 @@ export {
##
## 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;
## Returns: A record containing the status of the operation and an optional error
## string for failures.
global erase: function(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult;
}
function open_backend(btype: Storage::Backend, options: Storage::BackendOptions, key_type: any,
val_type: any): opaque of Storage::BackendHandle
val_type: any): Storage::OperationResult
{
return Storage::Sync::__open_backend(btype, options, key_type, val_type);
}
function close_backend(backend: opaque of Storage::BackendHandle): bool
function close_backend(backend: opaque of Storage::BackendHandle): Storage::OperationResult
{
return Storage::Sync::__close_backend(backend);
}
function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): bool
function put(backend: opaque of Storage::BackendHandle, args: Storage::PutArgs): Storage::OperationResult
{
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
function get(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult
{
return Storage::Sync::__get(backend, key);
}
function erase(backend: opaque of Storage::BackendHandle, key: any): bool
function erase(backend: opaque of Storage::BackendHandle, key: any): Storage::OperationResult
{
return Storage::Sync::__erase(backend, key);
}

View file

@ -356,14 +356,6 @@ type ftp_port: record {
valid: bool; ##< True if format was right. Only then are *h* and *p* valid.
};
## A generic type for returning either a value or an error string from a
## function or a BIF method. This is sort of equivalent to std::expected
## in C++.
type val_result: record {
val: any &optional;
error: string &optional;
};
## Statistics about what a TCP endpoint sent.
##
## .. zeek:see:: conn_stats
@ -6224,7 +6216,50 @@ export {
## The interval used by the storage framework for automatic expiration
## of elements in all backends that don't support it natively, or if
## using expiration while reading pcap files.
const expire_interval = 15.0 secs &redef;
const expire_interval = 15.0secs &redef;
## Common set of statuses that can be returned by storage operations. Backend plugins
## can add to this enum if custom values are needed.
type ReturnCode: enum {
## Operation succeeded.
SUCCESS,
## Type of value passed to operation does not match type of
## value passed when opening backend.
VAL_TYPE_MISMATCH,
## Type of key passed to operation does not match type of
## key passed when opening backend.
KEY_TYPE_MISMATCH,
## Backend is not connected.
NOT_CONNECTED,
## Operation timed out.
TIMEOUT,
## Connection to backed was lost.
CONNECTION_LOST,
## Generic operation failed.
OPERATION_FAILED,
## Key requested was not found in backend.
KEY_NOT_FOUND,
## Key requested for overwrite already exists.
KEY_EXISTS,
## Generic connection failure.
CONNECTION_FAILED,
## Generic disconnection failure.
DISCONNECTION_FAILED,
## Generic initialization failure.
INITIALIZATION_FAILED
} &redef;
## Returned as the result of the various storage operations.
type OperationResult: record {
## One of a set of backend-redefinable return codes.
code: ReturnCode;
## An optional error string. This should be set when the
## ``code`` field is not set ``SUCCESS``.
error_str: string &optional;
## An optional value returned by ``get`` operations when a match
## was found the key requested.
value: any &optional;
};
}
module GLOBAL;