MXL : Media eXchange Layer
MXL
Loading...
Searching...
No Matches
mxl::lib::FlowManager Class Reference

#include <FlowManager.hpp>

Public Member Functions

std::pair< bool, std::unique_ptr< ContinuousFlowData > > createOrOpenContinuousFlow (uuids::uuid const &flowId, std::string const &flowDef, mxlDataFormat flowFormat, mxlRational const &sampleRate, std::size_t channelCount, std::size_t sampleWordSize, std::size_t bufferLength, std::uint32_t maxSyncBatchSizeHintOpt=1, std::uint32_t maxCommitBatchSizeHintOpt=1)
std::pair< bool, std::unique_ptr< DiscreteFlowData > > createOrOpenDiscreteFlow (uuids::uuid const &flowId, std::string const &flowDef, mxlDataFormat flowFormat, std::size_t grainCount, mxlRational const &grainRate, std::size_t grainPayloadSize, std::size_t grainNumOfSlices, std::array< std::uint32_t, MXL_MAX_PLANES_PER_GRAIN > grainSliceLengths, std::uint32_t maxSyncBatchSizeHintOpt=1, std::uint32_t maxCommitBatchSizeHintOpt=1)
std::pair< bool, std::unique_ptr< EventFlowData > > createOrOpenEventFlow (uuids::uuid const &flowId, std::string const &flowDef, std::size_t eventCount, mxlRational const &grainRate, std::size_t eventPayloadSize)
 Create an event flow atomically, or open the existing flow with the same ID.
bool deleteFlow (std::unique_ptr< FlowData > &&flowData)
bool deleteFlow (uuids::uuid const &flowId)
 FlowManager (std::filesystem::path const &in_mxlDomain)
std::filesystem::path const & getDomain () const
std::string getFlowDef (uuids::uuid const &flowId) const
std::vector< uuids::uuid > listFlows () const
std::unique_ptr< FlowData > openFlow (uuids::uuid const &flowId, AccessMode mode)

Private Member Functions

std::unique_ptr< ContinuousFlowData > openContinuousFlow (std::filesystem::path const &flowDir, SharedMemoryInstance< Flow > &&sharedFlowInstance)
std::unique_ptr< DiscreteFlowData > openDiscreteFlow (std::filesystem::path const &flowDir, SharedMemoryInstance< Flow > &&sharedFlowInstance)

Private Attributes

std::filesystem::path _mxlDomain

Detailed Description

Performs Flow CRUD operations

CREATE Creates the flow resources in the base path (the MXL 'domain'). Flow resource contain

  • <mxl_domain>/<flow_id>.mxl-flow : The toplevel for all files belonging to MXLs representation of a the flow
  • <mxl_domain>/<flow_id>.mxl-flow/flow_def.json : A file containing the flow definition (NMOS Flow resource json)
  • <mxl_domain>/<flow_id>.mxl-flow/access : A file used for access notifications by consumers of the flow
  • <mxl_domain>/<flow_id>.mxl-flow/data : The shared memory segment containing the Flow
  • <mxl_domain>/<flow_id>.mxl-flow/grains/ : A directory containing the per grain shared memory segments.

After creation, the FlowData associated with the new flow is stored in an internal cache.

GET Return a cached FlowData for a flowId. The flow must opened first.

DELETE Delete a flow by flowId. If the flow is opened it is first closed than all physical resources (see above) are deleted

LIST List all the flows found in the domain.

Definition at line 42 of file FlowManager.hpp.

Constructor & Destructor Documentation

◆ FlowManager()

mxl::lib::FlowManager::FlowManager ( std::filesystem::path const & in_mxlDomain)

Creates a FlowManager. Ideally there should only be on instance of the manager per instance of the SDK.

\in_mxlDomain : The directory where the flows are stored.

Exceptions
std::filesystem::filesystem_errorif the directory does not exist or is not accessible.

Definition at line 155 of file FlowManager.cpp.

156 : _mxlDomain{std::filesystem::canonical(in_mxlDomain)}
157 {
158 if (!exists(in_mxlDomain) || !is_directory(in_mxlDomain))
159 {
160 throw std::filesystem::filesystem_error{
161 "Path does not exist or is not a directory.", in_mxlDomain, std::make_error_code(std::errc::no_such_file_or_directory)};
162 }
163 }
std::filesystem::path _mxlDomain

Member Function Documentation

◆ createOrOpenContinuousFlow()

std::pair< bool, std::unique_ptr< ContinuousFlowData > > mxl::lib::FlowManager::createOrOpenContinuousFlow ( uuids::uuid const & flowId,
std::string const & flowDef,
mxlDataFormat flowFormat,
mxlRational const & sampleRate,
std::size_t channelCount,
std::size_t sampleWordSize,
std::size_t bufferLength,
std::uint32_t maxSyncBatchSizeHintOpt = 1,
std::uint32_t maxCommitBatchSizeHintOpt = 1 )

Create a new continuous flow together with its associated channel store and open it in read-write mode.

Parameters
[in]flowIdThe id of the flow.
[in]flowDefThe json definition of the flow (NMOS Resource format). The flow is not parsed or validated. It is written as is.
[in]flowFormatThe flow data format. Must be one of the continuous formats.
[in]sampleRateThe sample rate.
[in]channelCountThe number of channels in the flow.
[in]sampleWordSizeThe size of one sample in bytes.
[in]bufferLengthThe length of each channel buffer in samples.
[in]maxSyncBatchSizeHintOptOptional max sync batch size hint.
[in]maxCommitBatchSizeHintOptOptional max commit batch size hint
Returns
(created, flowData) If the flow was created, the first returnd value is true. If the flow was opened instead, false will be returned. The second returned value is the flow data of the opened or created flow.

Definition at line 327 of file FlowManager.cpp.

330 {
331 auto const uuidString = uuids::to_string(flowId);
332 MXL_DEBUG("Create continuous flow. id: {}, channel count: {}, word size: {}, buffer length: {}",
333 uuidString,
334 channelCount,
335 sampleWordSize,
336 bufferLength);
337
338 flowFormat = sanitizeFlowFormat(flowFormat);
339 if (!mxlIsContinuousDataFormat(flowFormat))
340 {
341 throw std::runtime_error{"Attempt to create continuous flow with unsupported or non matching format."};
342 }
343
344 auto const tempDirectory = createTemporaryFlowDirectory(_mxlDomain);
345 auto _ = defer(
346 [&]() noexcept
347 {
348 auto ec = std::error_code{};
349 std::filesystem::remove_all(tempDirectory, ec);
350 if (ec)
351 {
352 MXL_WARN("Failed to remove temporary flow directory: {}", ec.message());
353 }
354 });
355
356 // Write the json file to disk.
357 writeFlowDescriptor(tempDirectory, flowDef);
358
359 auto const flowDataPath = makeFlowDataFilePath(tempDirectory);
360 auto flowData = std::make_unique<ContinuousFlowData>(flowDataPath.string().c_str(), AccessMode::CREATE_READ_WRITE, LockMode::Shared);
361
362 auto& info = *flowData->flowInfo();
363 info.version = FLOW_DATA_VERSION;
364 info.size = sizeof info;
365 info.config.common = initCommonFlowConfigInfo(flowId, flowFormat, sampleRate, maxSyncBatchSizeHintOpt, maxCommitBatchSizeHintOpt);
366 info.config.continuous = {};
367 info.config.continuous.channelCount = channelCount;
368 info.config.continuous.bufferLength = bufferLength;
369
370 info.runtime = initFlowRuntimeInfo();
371
372 auto& state = *flowData->flowState();
373 state = initFlowState(flowDataPath);
374
375 flowData->openChannelBuffers(makeChannelDataFilePath(tempDirectory).string().c_str(), sampleWordSize);
376
377 auto const finalDir = makeFlowDirectoryName(_mxlDomain, uuidString);
378 if (publishFlowDirectory(tempDirectory, finalDir))
379 {
380 return {true, std::move(flowData)};
381 }
382 else
383 {
385 if (!existingFlowData)
386 {
387 throw std::runtime_error{"Could not open existing flow because it is of a different format"};
388 }
389
390 return {false, std::move(existingFlowData)};
391 }
392 }
#define MXL_WARN(...)
Definition Logging.hpp:56
#define MXL_DEBUG(...)
Definition Logging.hpp:34
std::unique_ptr< FlowData > openFlow(uuids::uuid const &flowId, AccessMode mode)
int mxlIsContinuousDataFormat(int format)
Definition dataformat.h:106
std::filesystem::path makeChannelDataFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:47
std::filesystem::path makeFlowDataFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:17
constexpr Deferred< F > defer(F &&f) noexcept
Definition Deferred.hpp:48
std::unique_ptr< To > dynamic_pointer_cast(std::unique_ptr< From > &&source) noexcept
std::filesystem::path makeFlowDirectoryName(std::filesystem::path const &domain, std::string const &uuid)
Definition PathUtils.cpp:11
constexpr auto FLOW_DATA_VERSION
The version of the flow data structs in shared memory that we expect and support.
Definition Flow.hpp:21
auto const flowId
auto flowDef

◆ createOrOpenDiscreteFlow()

std::pair< bool, std::unique_ptr< DiscreteFlowData > > mxl::lib::FlowManager::createOrOpenDiscreteFlow ( uuids::uuid const & flowId,
std::string const & flowDef,
mxlDataFormat flowFormat,
std::size_t grainCount,
mxlRational const & grainRate,
std::size_t grainPayloadSize,
std::size_t grainNumOfSlices,
std::array< std::uint32_t, MXL_MAX_PLANES_PER_GRAIN > grainSliceLengths,
std::uint32_t maxSyncBatchSizeHintOpt = 1,
std::uint32_t maxCommitBatchSizeHintOpt = 1 )

Create a new discrete flow together with its associated grains and open it in read-write mode.

Parameters
[in]flowIdThe id of the flow.
[in]flowDefThe json definition of the flow (NMOS Resource format). The flow is not parsed or validated. It is written as is.
[in]flowFormatThe flow data format. Must be one of the discrete formats.
[in]grainCountHow many individual grains to create.
[in]grainRateThe grain rate.
[in]grainPayloadSizeSize of the grain in host memory. 0 if the grain payload lives in device memory.
[in]grainNumOfSlicesNumber of slices per grain.
[in]grainSliceLengthsLength of each slice in bytes.
[in]maxSyncBatchSizeHintOptOptional max sync batch size hint.
[in]maxCommitBatchSizeHintOptOptional max commit batch size hint
Returns
(created, flowData) If the flow was created, the first returnd value is true. If the flow was opened instead, false will be returned. The second returned value is the flow data of the opened or created flow.

Definition at line 165 of file FlowManager.cpp.

169 {
170 auto const uuidString = uuids::to_string(flowId);
171 MXL_DEBUG("Create discrete flow. id: {}, grainCount: {}, grain payload size: {}", uuidString, grainCount, grainPayloadSize);
172
173 flowFormat = sanitizeFlowFormat(flowFormat);
174 if (!mxlIsDiscreteDataFormat(flowFormat))
175 {
176 throw std::runtime_error{"Attempt to create discrete flow with unsupported or non matching format."};
177 }
178
179 auto const tempDirectory = createTemporaryFlowDirectory(_mxlDomain);
180 auto _ = defer(
181 [&]() noexcept
182 {
183 std::error_code ec;
184 std::filesystem::remove_all(tempDirectory, ec);
185 if (ec)
186 {
187 MXL_WARN("Failed to remove temporary flow directory: {}", ec.message());
188 }
189 });
190
191 // Write the json file to disk.
192 writeFlowDescriptor(tempDirectory, flowDef);
193
194 // Create the dummy file.
195 auto readAccessFile = makeFlowAccessFilePath(tempDirectory);
196 if (auto out = std::ofstream{readAccessFile, std::ios::out | std::ios::trunc}; !out)
197 {
198 throw std::filesystem::filesystem_error{
199 "Failed to create flow access file.", readAccessFile, std::make_error_code(std::errc::file_exists)};
200 }
201
202 auto const flowDataPath = makeFlowDataFilePath(tempDirectory);
203 auto flowData = std::make_unique<DiscreteFlowData>(flowDataPath.string().c_str(), AccessMode::CREATE_READ_WRITE, LockMode::Shared);
204
205 auto& info = *flowData->flowInfo();
206 info.version = FLOW_DATA_VERSION;
207 info.size = sizeof info;
208 info.config.common = initCommonFlowConfigInfo(flowId, flowFormat, grainRate, maxSyncBatchSizeHintOpt, maxCommitBatchSizeHintOpt);
209 info.config.discrete = {};
210 info.config.discrete.grainCount = grainCount;
211 std::ranges::copy(grainSliceLengths, info.config.discrete.sliceSizes);
212
213 info.runtime = initFlowRuntimeInfo();
214
215 auto& state = *flowData->flowState();
216 state = initFlowState(flowDataPath);
217
218 auto const grainDir = makeGrainDirectoryName(tempDirectory);
219 if (!create_directory(grainDir))
220 {
221 throw std::filesystem::filesystem_error{"Could not create grain directory.", grainDir, std::make_error_code(std::errc::io_error)};
222 }
223
224 for (auto i = std::size_t{0}; i < grainCount; ++i)
225 {
226 auto const grainPath = makeGrainDataFilePath(grainDir, i);
227 MXL_TRACE("Creating grain: {}", grainPath.string());
228
229 // \todo Handle payload stored device memory
230 auto const grain = flowData->emplaceGrain(grainPath.string().c_str(), grainPayloadSize);
231 auto& gInfo = grain->header.info;
232 gInfo.grainSize = grainPayloadSize;
233 gInfo.totalSlices = grainNumOfSlices;
234 gInfo.validSlices = 0;
236 gInfo.size = sizeof gInfo;
237 }
238
239 auto const finalDir = makeFlowDirectoryName(_mxlDomain, uuidString);
240 if (publishFlowDirectory(tempDirectory, finalDir))
241 {
242 return {true, std::move(flowData)};
243 }
244 else
245 {
247 if (!existingFlowData)
248 {
249 throw std::runtime_error("Could not open existing flow because it is of a different format");
250 }
251
252 return {false, std::move(existingFlowData)};
253 }
254 }
#define MXL_TRACE(...)
Definition Logging.hpp:23
int mxlIsDiscreteDataFormat(int format)
Definition dataformat.h:75
std::filesystem::path makeFlowAccessFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:29
constexpr auto GRAIN_HEADER_VERSION
The version of the grain header structs in shared memory that we expect an support.
Definition Flow.hpp:24
std::filesystem::path makeGrainDirectoryName(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:35
std::filesystem::path makeGrainDataFilePath(std::filesystem::path const &grainDirectory, unsigned int index)
Definition PathUtils.cpp:41
uint32_t grainSize
Size in bytes of the complete payload of a grain.
Definition flow.h:143
uint16_t totalSlices
Definition flow.h:146
uint32_t size
Size of the structure.
Definition flow.h:135
uint16_t validSlices
Definition flow.h:149
uint32_t version
Version of the structure. The only currently supported value is 2.
Definition flow.h:133
mxlGrainInfo gInfo
Open the grain.

◆ createOrOpenEventFlow()

std::pair< bool, std::unique_ptr< EventFlowData > > mxl::lib::FlowManager::createOrOpenEventFlow ( uuids::uuid const & flowId,
std::string const & flowDef,
std::size_t eventCount,
mxlRational const & grainRate,
std::size_t eventPayloadSize )

Create an event flow atomically, or open the existing flow with the same ID.

Parameters
flowIdIdentifier of the flow to create or open.
flowDefFlow descriptor JSON to store when creating the flow.
eventCountNumber of slots to create, in the range 2 through 65536.
grainRateRate recorded in the common configuration.
eventPayloadSizePer-entry payload capacity, in the range 1 through 1048576 bytes.
Returns
Creation flag and owned read/write mappings. Existing flows retain their geometry.
Exceptions
std::invalid_argumentThe requested geometry is invalid.
std::filesystem::filesystem_errorFlow files cannot be created or opened.
std::runtime_errorExisting flow type or stored headers are incompatible.
Note
This creates mappings; the caller must guarantee one event writer per flow.

Definition at line 256 of file FlowManager.cpp.

258 {
259 auto const uuidString = uuids::to_string(flowId);
260 MXL_DEBUG("Create event flow. id: {}, eventCount: {}, event payload size: {}", uuidString, eventCount, eventPayloadSize);
261
262 auto const flowFormat = mxlDataFormat{MXL_DATA_FORMAT_EVENT};
263 if ((eventCount < 2) || (eventCount > 65536) || (eventPayloadSize == 0) || (eventPayloadSize > 1048576))
264 {
265 throw std::invalid_argument{"Invalid event buffer dimensions."};
266 }
267
268 auto const tempDirectory = createTemporaryFlowDirectory(_mxlDomain);
269 auto _ = defer(
270 [&]() noexcept
271 {
272 auto ec = std::error_code{};
273 std::filesystem::remove_all(tempDirectory, ec);
274 if (ec)
275 {
276 MXL_WARN("Failed to remove temporary flow directory: {}", ec.message());
277 }
278 });
279
280 // Write the json file to disk.
281 writeFlowDescriptor(tempDirectory, flowDef);
282
283 // Create the dummy file.
284 auto readAccessFile = makeFlowAccessFilePath(tempDirectory);
285 if (auto out = std::ofstream{readAccessFile, std::ios::out | std::ios::trunc}; !out)
286 {
287 throw std::filesystem::filesystem_error{
288 "Failed to create flow access file.", readAccessFile, std::make_error_code(std::errc::file_exists)};
289 }
290
291 auto const flowDataPath = makeFlowDataFilePath(tempDirectory);
292 auto flowData = std::make_unique<EventFlowData>(flowDataPath.string().c_str(), AccessMode::CREATE_READ_WRITE, LockMode::Shared);
293
294 auto& info = *flowData->flowInfo();
295 info.version = FLOW_DATA_VERSION;
296 info.size = sizeof info;
297 info.config.common = initCommonFlowConfigInfo(flowId, flowFormat, grainRate, 1, 1);
298 info.config.event = {};
299 info.config.event.eventCount = eventCount;
300 info.config.event.eventPayloadSize = eventPayloadSize;
301
302 info.runtime = initFlowRuntimeInfo();
303 info.runtime.headIndex = MXL_UNDEFINED_INDEX;
304
305 auto& state = *flowData->flowState();
306 state = initFlowState(flowDataPath);
307
308 flowData->openEventBuffer(makeEventDataFilePath(tempDirectory).string().c_str());
309
310 auto const finalDir = makeFlowDirectoryName(_mxlDomain, uuidString);
311 if (publishFlowDirectory(tempDirectory, finalDir))
312 {
313 return {true, std::move(flowData)};
314 }
315 else
316 {
318 if (!existingFlowData)
319 {
320 throw std::runtime_error("Could not open existing flow because it is of a different format");
321 }
322
323 return {false, std::move(existingFlowData)};
324 }
325 }
mxlDataFormat
Definition dataformat.h:25
@ MXL_DATA_FORMAT_EVENT
Asynchronous queue of complete events or event fragments.
Definition dataformat.h:30
std::filesystem::path makeEventDataFilePath(std::filesystem::path const &flowDirectory)
Locate an event ring file within its flow directory.
Definition PathUtils.cpp:53
#define MXL_UNDEFINED_INDEX
Definition rational.h:17

◆ deleteFlow() [1/2]

bool mxl::lib::FlowManager::deleteFlow ( std::unique_ptr< FlowData > && flowData)

Delete all resources associated to a flow

Parameters
flowDataThe flowdata resource if the flow was previously opened or created
Returns
success or failure.

Definition at line 480 of file FlowManager.cpp.

481 {
482 if (flowData)
483 {
484 // Extract the ID
485 auto const span = uuids::span<std::uint8_t, sizeof flowData->flowInfo()->config.common.id>{
486 const_cast<std::uint8_t*>(flowData->flowInfo()->config.common.id), sizeof flowData->flowInfo()->config.common.id};
487 auto const id = uuids::uuid(span);
488
489 // Close the flow
490 flowData.reset();
491
492 // Delegate to the other deleteFlow overload
493 return deleteFlow(id);
494 }
495 return false;
496 }
bool deleteFlow(std::unique_ptr< FlowData > &&flowData)

◆ deleteFlow() [2/2]

bool mxl::lib::FlowManager::deleteFlow ( uuids::uuid const & flowId)

Delete all resources associated to a flow

Parameters
flowIdThe ID of the flow to delete.
Returns
success or failure.

Definition at line 498 of file FlowManager.cpp.

499 {
500 auto uuid = uuids::to_string(flowId);
501 MXL_TRACE("Delete flow: {}", uuid);
502
503 try
504 {
505 // Compute the flow directory path
506 auto const flowPath = makeFlowDirectoryName(_mxlDomain, uuid);
507 auto const removed = remove_all(flowPath);
508 if (removed == 0)
509 {
510 MXL_TRACE("Flow not found or already deleted: {}", uuid);
511 return false;
512 }
513 return true;
514 }
515 catch (...)
516 {
517 // Convert any filesystem exception to false return
518 // This makes the method effectively noexcept while indicating failure
519 return false;
520 }
521 }

◆ getDomain()

std::filesystem::path const & mxl::lib::FlowManager::getDomain ( ) const

Accessor for the mxl domain (base path where shared memory will be stored)

Returns
The base path

Definition at line 576 of file FlowManager.cpp.

577 {
578 return _mxlDomain;
579 }

◆ getFlowDef()

std::string mxl::lib::FlowManager::getFlowDef ( uuids::uuid const & flowId) const
Parameters
flowIdThe ID of the flow to get the information about.
Returns
The requested json flow definition.
Exceptions
std::filesystem::filesystem_erroron flow not found
std::runtime_erroron any other error

Definition at line 551 of file FlowManager.cpp.

552 {
553 auto const uuid = uuids::to_string(flowId);
554 auto const flowPath = makeFlowDirectoryName(_mxlDomain, uuid);
555 auto const flowJsonFile = makeFlowDescriptorFilePath(flowPath);
556 if (auto in = std::ifstream{flowJsonFile, std::ios::in}; in)
557 {
558 auto const result = std::string{std::istreambuf_iterator<char>{in}, std::istreambuf_iterator<char>{}};
559 if (!in)
560 {
561 throw std::runtime_error{"Error while reading the flow definition."};
562 }
563 return result;
564 }
565 // Here is a race condition, but plain C++ API does not provide a way to check whether it was not possible to open a file because it does
566 // not exist, or whether the access rights are wrong.
567 if (!exists(flowJsonFile))
568 {
569 throw std::filesystem::filesystem_error{"Failed to open flow resource definition - file not found.",
570 flowJsonFile,
571 std::make_error_code(std::errc::no_such_file_or_directory)};
572 }
573 throw std::runtime_error{"Failed to open flow resource definition."};
574 }
std::filesystem::path makeFlowDescriptorFilePath(std::filesystem::path const &flowDirectory)
Definition PathUtils.cpp:23

◆ listFlows()

std::vector< uuids::uuid > mxl::lib::FlowManager::listFlows ( ) const
Returns
List all flows on disk.

Definition at line 523 of file FlowManager.cpp.

524 {
525 auto base = std::filesystem::path{_mxlDomain};
526
527 auto flowIds = std::vector<uuids::uuid>{};
528 if (exists(base) && is_directory(base))
529 {
530 for (auto const& entry : std::filesystem::directory_iterator{_mxlDomain})
531 {
532 if (is_directory(entry) && (entry.path().extension() == FLOW_DIRECTORY_NAME_SUFFIX))
533 {
534 // this looks like a uuid. try to parse it an confirm it is valid.
535 auto id = uuids::uuid::from_string(entry.path().stem().string());
536 if (id.has_value())
537 {
538 flowIds.push_back(*id);
539 }
540 }
541 }
542 }
543 else
544 {
545 throw std::filesystem::filesystem_error{"Base directory not found.", base, std::make_error_code(std::errc::no_such_file_or_directory)};
546 }
547
548 return flowIds;
549 }
constexpr auto const FLOW_DIRECTORY_NAME_SUFFIX
Definition PathUtils.hpp:14

◆ openContinuousFlow()

std::unique_ptr< ContinuousFlowData > mxl::lib::FlowManager::openContinuousFlow ( std::filesystem::path const & flowDir,
SharedMemoryInstance< Flow > && sharedFlowInstance )
private

Definition at line 470 of file FlowManager.cpp.

472 {
473 auto flowData = std::make_unique<ContinuousFlowData>(std::move(sharedFlowInstance));
474
475 flowData->openChannelBuffers(makeChannelDataFilePath(flowDir).string().c_str(), /*sampleWordSize=*/0U);
476
477 return flowData;
478 }

◆ openDiscreteFlow()

std::unique_ptr< DiscreteFlowData > mxl::lib::FlowManager::openDiscreteFlow ( std::filesystem::path const & flowDir,
SharedMemoryInstance< Flow > && sharedFlowInstance )
private

Definition at line 440 of file FlowManager.cpp.

442 {
443 auto flowData = std::make_unique<DiscreteFlowData>(std::move(sharedFlowInstance));
444
445 auto const grainCount = flowData->flowInfo()->config.discrete.grainCount;
446 if (grainCount > 0U)
447 {
448 auto const grainDir = makeGrainDirectoryName(flowDir);
449 if (exists(grainDir) && is_directory(grainDir))
450 {
451 // Open each grain with per-item error handling
452 for (auto i = 0U; i < grainCount; ++i)
453 {
454 auto const grainPath = makeGrainDataFilePath(grainDir, i).string();
455 MXL_TRACE("Opening grain: {}", grainPath);
456
457 flowData->emplaceGrain(grainPath.c_str(), /*grainPayloadSize=*/0U);
458 }
459 }
460 else
461 {
462 throw std::filesystem::filesystem_error{
463 "Grain directory not found.", grainDir, std::make_error_code(std::errc::no_such_file_or_directory)};
464 }
465 }
466
467 return flowData;
468 }

◆ openFlow()

std::unique_ptr< FlowData > mxl::lib::FlowManager::openFlow ( uuids::uuid const & flowId,
AccessMode mode )

Open an existing flow by id.

Parameters
[in]flowIdThe flow to open
[in]modeThe flow access mode

Definition at line 394 of file FlowManager.cpp.

395 {
396 if (in_mode == AccessMode::CREATE_READ_WRITE)
397 {
398 throw std::invalid_argument{"Attempt to open flow with invalid access mode."};
399 }
400
401 auto uuid = uuids::to_string(in_flowId);
402 auto const base = makeFlowDirectoryName(_mxlDomain, uuid);
403
404 // Verify that the flow file exists.
405 if (auto const flowFile = makeFlowDataFilePath(base); exists(flowFile))
406 {
407 auto flowSegment = SharedMemoryInstance<Flow>{flowFile.string().c_str(), in_mode, 0U, LockMode::Shared};
408 if (flowSegment.get()->info.version != FLOW_DATA_VERSION)
409 {
410 throw std::invalid_argument{
411 fmt::format("Unsupported flow data version: {}, supported is: {}", flowSegment.get()->info.version, FLOW_DATA_VERSION)};
412 }
413
414 if (auto const flowFormat = flowSegment.get()->info.config.common.format; mxlIsDiscreteDataFormat(flowFormat))
415 {
416 return openDiscreteFlow(base, std::move(flowSegment));
417 }
418 else if (flowFormat == MXL_DATA_FORMAT_EVENT)
419 {
420 auto data = std::make_unique<EventFlowData>(std::move(flowSegment));
421 data->openEventBuffer(makeEventDataFilePath(base).string().c_str());
422 return data;
423 }
424 else if (mxlIsContinuousDataFormat(flowFormat))
425 {
426 return openContinuousFlow(base, std::move(flowSegment));
427 }
428 else
429 {
430 // This should never happen for a valid flow.
431 throw std::runtime_error{"Attempt to open flow with unsupported data format."};
432 }
433 }
434 else
435 {
436 throw std::filesystem::filesystem_error{"Flow file not found.", flowFile, std::make_error_code(std::errc::no_such_file_or_directory)};
437 }
438 }
std::unique_ptr< DiscreteFlowData > openDiscreteFlow(std::filesystem::path const &flowDir, SharedMemoryInstance< Flow > &&sharedFlowInstance)
std::unique_ptr< ContinuousFlowData > openContinuousFlow(std::filesystem::path const &flowDir, SharedMemoryInstance< Flow > &&sharedFlowInstance)

Member Data Documentation

◆ _mxlDomain

std::filesystem::path mxl::lib::FlowManager::_mxlDomain
private

Definition at line 153 of file FlowManager.hpp.


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