Skip to content

impl/TaskKeys.hpp

More...

Namespaces

Name
sgns
sgns::processing

Classes

Name
class sgns::processing::TaskKeys

Detailed Description

Date: 2026-05-19 Henrique A. Klein ([email protected])

Source code

#ifndef TASK_KEYS_HPP
#define TASK_KEYS_HPP

#include <string>
#include <string_view>

#include "base/sgns_version.hpp"
#include "crdt/hierarchical_key.hpp"

namespace sgns::processing
{
    class TaskKeys
    {
    public:
        static std::string ProcessingPrefix()
        {
            return std::string( PROCESSING_PREFIX_BASE ) + std::to_string( sgns::version::ProcessingVersion() );
        }

        static std::string ScopePrefix( const std::string &private_network_id )
        {
            if ( private_network_id.empty() )
            {
                return std::string();
            }
            return crdt::HierarchicalKey( "chain" ).ChildString( private_network_id ).GetKey();
        }

        static std::string ScopedTopic( std::string_view public_topic, const std::string &private_network_id )
        {
            if ( private_network_id.empty() )
            {
                return std::string( public_topic );
            }
            return std::string( public_topic ) + "/" + private_network_id;
        }

        static std::string ScopedKeyPath( const std::string &private_network_id, std::string_view path )
        {
            if ( private_network_id.empty() )
            {
                return std::string( path );
            }
            return crdt::HierarchicalKey( ScopePrefix( private_network_id ) ).ChildString( path ).GetKey();
        }

        static std::string SubTaskResultKey( const std::string &private_network_id, std::string_view subTaskId )
        {
            return ScopedKeyPath( private_network_id, std::string( "results/" ) + std::string( subTaskId ) );
        }

        static std::string TaskListKey()
        {
            return ProcessingPrefix() + std::string( TASK_LIST_SUFFIX );
        }

        static std::string TaskListKey( const std::string &private_network_id )
        {
            return ScopePrefix( private_network_id ) + TaskListKey();
        }

        static std::string SubTaskListKey()
        {
            return ProcessingPrefix() + std::string( SUBTASK_LIST_SUFFIX );
        }

        // NOTE: no single-argument scoped overload of the bare subtask list exists on purpose —
        // it would collide with SubTaskListKey(std::string_view taskId): a std::string argument
        // would bind exactly to const std::string& and silently reinterpret a task id as a scope.
        // Compose the bare scoped list as ScopePrefix(scope) + SubTaskListKey() if ever needed.

        static std::string SubTaskListKey( std::string_view taskId )
        {
            return SubTaskListKey() + "/" + std::string( taskId );
        }

        static std::string SubTaskListKey( const std::string &private_network_id, std::string_view taskId )
        {
            return ScopePrefix( private_network_id ) + SubTaskListKey( taskId );
        }

        static std::string TaskKey( std::string_view taskId )
        {
            return TaskListKey() + "/" + std::string( taskId );
        }

        static std::string TaskKey( const std::string &private_network_id, std::string_view taskId )
        {
            return ScopePrefix( private_network_id ) + TaskKey( taskId );
        }

        static std::string SubTaskKey( std::string_view taskId, std::string_view subTaskId )
        {
            return SubTaskListKey( taskId ) + "/" + std::string( subTaskId );
        }

        static std::string SubTaskKey( const std::string &private_network_id,
                                       std::string_view          taskId,
                                       std::string_view          subTaskId )
        {
            return ScopePrefix( private_network_id ) + SubTaskKey( taskId, subTaskId );
        }

        static std::string ClaimableListKey()
        {
            return ProcessingPrefix() + std::string( CLAIMABLE_LIST_SUFFIX );
        }

        static std::string ClaimableListKey( const std::string &private_network_id )
        {
            return ScopePrefix( private_network_id ) + ClaimableListKey();
        }

        static std::string ClaimableTaskKey( std::string_view taskId )
        {
            return ClaimableListKey() + "/" + std::string( taskId );
        }

        static std::string ClaimableTaskKey( const std::string &private_network_id, std::string_view taskId )
        {
            return ScopePrefix( private_network_id ) + ClaimableTaskKey( taskId );
        }

        static std::string ResultTaskKey( std::string_view taskId )
        {
            return ProcessingPrefix() + std::string( RESULTS_SUFFIX ) + std::string( TASK_LIST_SUFFIX ) +
                   std::string( taskId );
        }

        static std::string ResultTaskKey( const std::string &private_network_id, std::string_view taskId )
        {
            return ScopePrefix( private_network_id ) + ResultTaskKey( taskId );
        }

        static std::string LockKey( std::string_view taskKey )
        {
            return std::string( LOCK_KEY_PREFIX ) + std::string( taskKey );
        }

    private:
        static constexpr std::string_view PROCESSING_PREFIX_BASE = "/processing_";
        static constexpr std::string_view TASK_LIST_SUFFIX       = "/tasks";
        static constexpr std::string_view SUBTASK_LIST_SUFFIX    = "/subtasks";
        static constexpr std::string_view CLAIMABLE_LIST_SUFFIX  = "/claimable";
        static constexpr std::string_view RESULTS_SUFFIX         = "/task_results";
        static constexpr std::string_view LOCK_KEY_PREFIX        = "/lock_";
    };
}

#endif // TASK_KEYS_HPP

Updated on 2026-10-15 at 17:15:13 +0000