Skip to content

sgns::crdt::CrdtHeads

CrdtHeads manages the current Merkle-CRDT heads.

#include <crdt_heads.hpp>

Public Types

Name
using storage::rocksdb DataStore
using base::Buffer Buffer
using std::unordered_map< std::string, std::set< CID > > CRDTHeadList
using std::pair< CRDTHeadList, uint64_t > CRDTListResult

Public Functions

Name
CrdtHeads(std::shared_ptr< DataStore > aDatastore, const HierarchicalKey & aNamespace)
CrdtHeads(const CrdtHeads & aHeads)
virtual ~CrdtHeads() =default
Destroy the Crdt Heads object.
bool operator==(const CrdtHeads & aHeads) const
bool operator!=(const CrdtHeads & aHeads) const
CrdtHeads & operator=(const CrdtHeads & aHeads)
HierarchicalKey GetNamespaceKey() const
outcome::result< HierarchicalKey > GetKey(const std::string & topic, const CID & aCid) const
bool IsHead(const CID & aCid, const std::string & topic) const
outcome::result< uint64_t > GetHeadHeight(const CID & aCid, const std::string & topic) const
outcome::result< size_t > GetLength(const std::string & topic ="") const
outcome::result< void > Add(const CID & aCid, uint64_t aHeight, const std::string & topic)
outcome::result< void > Remove(const CID & aCid, const std::string & topic)
outcome::result< void > Replace(const CID & aCidHead, const CID & aNewHeadCid, uint64_t aHeight, const std::string & topic)
outcome::result< CRDTListResult > GetList(const std::unordered_set< std::string > & topics ={}) const
outcome::result< void > PrimeCache()

Protected Functions

Name
outcome::result< void > Write(storage::BufferBatch & aDataStore, const CID & aCid, uint64_t aHeight, const std::string & topic) const
outcome::result< void > Delete(const std::unique_ptr< storage::BufferBatch > & aDataStore, const CID & aCid, const std::string & topic) const

Public Types Documentation

using DataStore

using sgns::crdt::CrdtHeads::DataStore = storage::rocksdb;

using Buffer

using sgns::crdt::CrdtHeads::Buffer = base::Buffer;

using CRDTHeadList

using sgns::crdt::CrdtHeads::CRDTHeadList = std::unordered_map<std::string, std::set<CID>>;

using CRDTListResult

using sgns::crdt::CrdtHeads::CRDTListResult = std::pair<CRDTHeadList, uint64_t>;

Public Functions Documentation

function CrdtHeads

CrdtHeads(
    std::shared_ptr< DataStore > aDatastore,
    const HierarchicalKey & aNamespace
)

Parameters:

  • aDatastore Pointer to datastore
  • aNamespace Namespace key (e.g "/namespace")

Constructor

function CrdtHeads

CrdtHeads(
    const CrdtHeads & aHeads
)

Copy constructor

function ~CrdtHeads

virtual ~CrdtHeads() =default

Destroy the Crdt Heads object.

function operator==

bool operator==(
    const CrdtHeads & aHeads
) const

Return: true if equal otherwise, it returns false.

Equality operator

function operator!=

bool operator!=(
    const CrdtHeads & aHeads
) const

Return: true if NOT equal otherwise, it returns false.

Equality operator

function operator=

CrdtHeads & operator=(
    const CrdtHeads & aHeads
)

Assignment operator

function GetNamespaceKey

HierarchicalKey GetNamespaceKey() const

Get namespace hierarchical key

function GetKey

outcome::result< HierarchicalKey > GetKey(
    const std::string & topic,
    const CID & aCid
) const

Parameters:

  • topic Topic namespace.
  • aCid Content identifier.

Return: full path to CID key as HierarchicalKey or outcome::failure on error

Get full path to CID key. Path format: /namespace/topic/cid

function IsHead

bool IsHead(
    const CID & aCid,
    const std::string & topic
) const

Parameters:

  • aCid Content identifier
  • topic Topic namespace

Return: true is CID is head, false otherwise

Check if CID is among the current heads.

function GetHeadHeight

outcome::result< uint64_t > GetHeadHeight(
    const CID & aCid,
    const std::string & topic
) const

Parameters:

  • aCid Content identifier
  • topic Topic namespace

Return: Height of head or outcome::failure on error

Check if CID is head and return it height if it is

function GetLength

outcome::result< size_t > GetLength(
    const std::string & topic =""
) const

Return: length, current number of heads or outcome::failure on error

Get current number of heads

function Add

outcome::result< void > Add(
    const CID & aCid,
    uint64_t aHeight,
    const std::string & topic
)

Parameters:

  • aCid Content identifier
  • aHeight height of head
  • topic Topic namespace

Return: outcome::failure on error

Add head CID to datastore with full namespace

function Remove

outcome::result< void > Remove(
    const CID & aCid,
    const std::string & topic
)

function Replace

outcome::result< void > Replace(
    const CID & aCidHead,
    const CID & aNewHeadCid,
    uint64_t aHeight,
    const std::string & topic
)

Parameters:

  • aCidHead Content identifier of head to replace
  • aNewHeadCid Content identifier of new head
  • aHeight height of head
  • topic Topic namespace

Return: outcome::failure on error

Replace a head with a new cid.

function GetList

outcome::result< CRDTListResult > GetList(
    const std::unordered_set< std::string > & topics ={}
) const

Parameters:

  • topics Topic to get list from

Return: outcome::failure on error

Returns the list of current heads plus the max height.

function PrimeCache

outcome::result< void > PrimeCache()

Return: outcome::failure on error

primeCache builds the heads cache based on what's in storage; since it is called from the constructor only we don't bother locking.

Protected Functions Documentation

function Write

outcome::result< void > Write(
    storage::BufferBatch & aDataStore,
    const CID & aCid,
    uint64_t aHeight,
    const std::string & topic
) const

Parameters:

  • aDataStore Pointer to datastore batch
  • aCid Content identifier to add
  • aHeight height of CID head
  • topic Topic namespace

Return: outcome::failure on error

Write data to datastore in batch mode

function Delete

outcome::result< void > Delete(
    const std::unique_ptr< storage::BufferBatch > & aDataStore,
    const CID & aCid,
    const std::string & topic
) const

Parameters:

  • aDataStore Pointer to datastore batch
  • aCid Content identifier to remove
  • topic Topic namespace

Delete data from datastore in batch mode


Updated on 2026-03-04 at 13:10:43 -0800