mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00

This compiles, but besides giving debug messages (and partially performing inserts/updates) it is not really helpful and definitely WIP. This also shows that I might have to re-think the approach that we will take here. So far, we actually insert tables as tables into Brokerstores. This opens up the potential to just have several tables synchronized via a single brokerstore. However, it turns out, that the current store_event API sends the complete table with each update. Which is problematic for obvious reasons - and not really sustainable.
122 lines
2.9 KiB
C++
122 lines
2.9 KiB
C++
#pragma once
|
|
|
|
#include "broker/store.bif.h"
|
|
#include "broker/data.bif.h"
|
|
#include "OpaqueVal.h"
|
|
#include "Trigger.h"
|
|
|
|
#include <broker/store.hh>
|
|
#include <broker/store_event.hh>
|
|
#include <broker/backend.hh>
|
|
#include <broker/backend_options.hh>
|
|
|
|
namespace bro_broker {
|
|
|
|
extern OpaqueType* opaque_of_store_handle;
|
|
|
|
/**
|
|
* Create a Broker::QueryStatus value.
|
|
* @param success whether the query status should be set to success or failure.
|
|
* @return a Broker::QueryStatus value.
|
|
*/
|
|
EnumVal* query_status(bool success);
|
|
|
|
/**
|
|
* @return a Broker::QueryResult value that has a Broker::QueryStatus indicating
|
|
* a failure.
|
|
*/
|
|
inline IntrusivePtr<RecordVal> query_result()
|
|
{
|
|
auto rval = make_intrusive<RecordVal>(BifType::Record::Broker::QueryResult);
|
|
rval->Assign(0, query_status(false));
|
|
rval->Assign(1, make_intrusive<RecordVal>(BifType::Record::Broker::Data));
|
|
return rval;
|
|
}
|
|
|
|
/**
|
|
* @param data the result of the query.
|
|
* @return a Broker::QueryResult value that has a Broker::QueryStatus indicating
|
|
* a success.
|
|
*/
|
|
inline IntrusivePtr<RecordVal> query_result(IntrusivePtr<RecordVal> data)
|
|
{
|
|
auto rval = make_intrusive<RecordVal>(BifType::Record::Broker::QueryResult);
|
|
rval->Assign(0, query_status(true));
|
|
rval->Assign(1, std::move(data));
|
|
return rval;
|
|
}
|
|
|
|
/**
|
|
* Used for asynchronous data store queries which use "when" statements.
|
|
*/
|
|
class StoreQueryCallback {
|
|
public:
|
|
StoreQueryCallback(trigger::Trigger* arg_trigger, const CallExpr* arg_call,
|
|
broker::store store)
|
|
: trigger(arg_trigger), call(arg_call), store(std::move(store))
|
|
{
|
|
Ref(trigger);
|
|
}
|
|
|
|
~StoreQueryCallback()
|
|
{
|
|
Unref(trigger);
|
|
}
|
|
|
|
void Result(const IntrusivePtr<RecordVal>& result)
|
|
{
|
|
trigger->Cache(call, result.get());
|
|
trigger->Release();
|
|
}
|
|
|
|
void Abort()
|
|
{
|
|
auto result = query_result();
|
|
trigger->Cache(call, result.get());
|
|
trigger->Release();
|
|
}
|
|
|
|
bool Disabled() const
|
|
{ return trigger->Disabled(); }
|
|
|
|
const broker::store& Store() const
|
|
{ return store; }
|
|
|
|
private:
|
|
|
|
trigger::Trigger* trigger;
|
|
const CallExpr* call;
|
|
broker::store store;
|
|
};
|
|
|
|
/**
|
|
* An opaque handle which wraps a Broker data store.
|
|
*/
|
|
class StoreHandleVal : public OpaqueVal {
|
|
public:
|
|
StoreHandleVal(broker::store s)
|
|
: OpaqueVal(bro_broker::opaque_of_store_handle), store{s}, proxy{store}, store_pid{store.frontend_id()}
|
|
{ }
|
|
|
|
void ValDescribe(ODesc* d) const override;
|
|
|
|
broker::store store;
|
|
broker::store::proxy proxy;
|
|
broker::publisher_id store_pid;
|
|
// Zeek table that events are forwarded to.
|
|
IntrusivePtr<TableVal> forward_to;
|
|
|
|
protected:
|
|
StoreHandleVal() = default;
|
|
|
|
DECLARE_OPAQUE_VALUE(StoreHandleVal)
|
|
};
|
|
|
|
// Helper function to construct a broker backend type from script land.
|
|
broker::backend to_backend_type(BifEnum::Broker::BackendType type);
|
|
|
|
// Helper function to construct broker backend options from script land.
|
|
broker::backend_options to_backend_options(broker::backend backend,
|
|
RecordVal* options);
|
|
|
|
} // namespace bro_broker
|