Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
Mads::MongoFetch Class Reference

Replay reader over one or more MongoDB collections. More...

#include <mongo_fetch.hpp>

Public Member Functions

 MongoFetch (const std::string &uri)
 Construct a fetcher bound to the specified MongoDB URI.
 
 ~MongoFetch ()
 Destroy the fetcher instance.
 
 MongoFetch (const MongoFetch &)=delete
 
MongoFetchoperator= (const MongoFetch &)=delete
 
 MongoFetch (MongoFetch &&) noexcept
 
MongoFetchoperator= (MongoFetch &&) noexcept
 
void connect ()
 Open the connection to the MongoDB server.
 
void disconnect ()
 Close the connection to the MongoDB server and clear replay state.
 
void select_database (const std::string &db_name)
 Select the database on which to operate.
 
void select_collections (const std::vector< std::string > &collections)
 Define the list of collections to fetch data from.
 
void select_time_range (const std::string &start, const std::string &end)
 Select the time range of interest.
 
void select_time_range (const std::string &start)
 Select the time range of interest, using the current time as the end of the period.
 
std::size_t fetch_data ()
 Create a replay view over the selected MongoDB data.
 
std::size_t fetch_data (std::string &view_name)
 Create a replay view over the selected MongoDB data.
 
std::size_t fetch_data (const std::string &view_name)
 Create a persistent replay view with the specified name.
 
std::size_t fetch_data_from (const std::string &view_name)
 Reuse an existing persistent replay view.
 
void set_repeat (bool repeat)
 Set whether replay restarts from the beginning after the last record.
 
std::chrono::milliseconds load_next (nlohmann::json &out, std::string &collection_name)
 Load the next record from the replay view.
 
void set_unwrap_original (bool unwrap)
 Set whether to unwrap the original document.
 

Detailed Description

Replay reader over one or more MongoDB collections.

Note
The MongoDB driver types this class is built on are hidden behind a pointer to implementation, so this header pulls in no bsoncxx/mongocxx headers and sizeof(MongoFetch) does not depend on the driver ABI.

Definition at line 31 of file mongo_fetch.hpp.

Constructor & Destructor Documentation

◆ MongoFetch() [1/3]

Mads::MongoFetch::MongoFetch ( const std::string &  uri)
explicit

Construct a fetcher bound to the specified MongoDB URI.

Parameters
uriMongoDB connection URI used when connect() is called.

◆ ~MongoFetch()

Mads::MongoFetch::~MongoFetch ( )

Destroy the fetcher instance.

If still connected, this calls disconnect() to release the connection and clean up any owned temporary replay view.

◆ MongoFetch() [2/3]

Mads::MongoFetch::MongoFetch ( const MongoFetch )
delete

◆ MongoFetch() [3/3]

Mads::MongoFetch::MongoFetch ( MongoFetch &&  )
noexcept

Member Function Documentation

◆ connect()

void Mads::MongoFetch::connect ( )

Open the connection to the MongoDB server.

◆ disconnect()

void Mads::MongoFetch::disconnect ( )

Close the connection to the MongoDB server and clear replay state.

If the active replay view was auto-generated by fetch_data(), it is dropped from the database before the connection is closed.

◆ fetch_data() [1/3]

std::size_t Mads::MongoFetch::fetch_data ( )

Create a replay view over the selected MongoDB data.

The resulting view has a temporary random name and is deleted automatically on disconnect or object destruction. Each record in the view is represented as a JSON object with these fields:

  • timestamp: the record timestamp as a BSON UTC date
  • collection_name: the name of the source collection
  • data: the original BSON document

Records are sorted by timestamp and filtered using the time range selected with select_time_range().

Returns
Number of documents exposed by the created replay view.

◆ fetch_data() [2/3]

std::size_t Mads::MongoFetch::fetch_data ( const std::string &  view_name)

Create a persistent replay view with the specified name.

The view is created on the server and is not deleted automatically on object destruction or disconnect.

Parameters
view_nameName of the server-side replay view to create or replace.
Returns
Number of documents exposed by the created replay view.

◆ fetch_data() [3/3]

std::size_t Mads::MongoFetch::fetch_data ( std::string &  view_name)

Create a replay view over the selected MongoDB data.

The resulting view has a temporary random name, unless view_name already contains one. If a name is provided, the view remains in the database after disconnect or object destruction. Each record in the view is represented as a JSON object with these fields:

  • timestamp: the record timestamp as a BSON UTC date
  • collection_name: the name of the source collection
  • data: the original BSON document

Records are sorted by timestamp and filtered using the time range selected with select_time_range().

Parameters
view_nameOptional caller-provided name for the generated view. If empty, a name is generated and written back.
Returns
Number of documents exposed by the created replay view.

◆ fetch_data_from()

std::size_t Mads::MongoFetch::fetch_data_from ( const std::string &  view_name)

Reuse an existing persistent replay view.

The named server-side object must exist, must be a MongoDB view, and must expose replay rows compatible with load_next().

Parameters
view_nameName of the existing replay view to attach to.
Returns
Number of documents exposed by the reused view.

◆ load_next()

std::chrono::milliseconds Mads::MongoFetch::load_next ( nlohmann::json &  out,
std::string &  collection_name 
)

Load the next record from the replay view.

Only the data field of the current record is loaded into out, while collection_name receives the originating collection name.

Parameters
outJSON object populated with the next record payload.
collection_nameOutput string receiving the source collection name.
Returns
Delay before loading the following record, or a negative duration when no more data are available.

◆ operator=() [1/2]

MongoFetch & Mads::MongoFetch::operator= ( const MongoFetch )
delete

◆ operator=() [2/2]

MongoFetch & Mads::MongoFetch::operator= ( MongoFetch &&  )
noexcept

◆ select_collections()

void Mads::MongoFetch::select_collections ( const std::vector< std::string > &  collections)

Define the list of collections to fetch data from.

Parameters
collectionsCollection names to merge into the replay view.

◆ select_database()

void Mads::MongoFetch::select_database ( const std::string &  db_name)

Select the database on which to operate.

Parameters
db_nameDatabase name to use for subsequent operations.

◆ select_time_range() [1/2]

void Mads::MongoFetch::select_time_range ( const std::string &  start)

Select the time range of interest, using the current time as the end of the period.

Parameters
startInclusive lower bound, expressed as epoch milliseconds or ISO-8601 UTC string.

◆ select_time_range() [2/2]

void Mads::MongoFetch::select_time_range ( const std::string &  start,
const std::string &  end 
)

Select the time range of interest.

Parameters
startInclusive lower bound, expressed as epoch milliseconds or ISO-8601 UTC string.
endInclusive upper bound, expressed as epoch milliseconds or ISO-8601 UTC string.

◆ set_repeat()

void Mads::MongoFetch::set_repeat ( bool  repeat)

Set whether replay restarts from the beginning after the last record.

Parameters
repeatIf true, load_next() loops over the replay view; otherwise it stops at the end.

◆ set_unwrap_original()

void Mads::MongoFetch::set_unwrap_original ( bool  unwrap)

Set whether to unwrap the original document.

Parameters
unwrapIf true, the original document is unwrapped; otherwise, it is kept as a nested object.

The documentation for this class was generated from the following file: