35 lines
976 B
C++
35 lines
976 B
C++
#pragma once
|
|||
|
|
|
||
|
|
#include <cstdint>
|
||
|
|
#include <map>
|
||
|
|
#include <mutex>
|
||
|
|
#include <string>
|
||
|
|
|
||
|
|
namespace molo::replication {
|
||
|
|
|
||
|
|
enum class VectorOrder {
|
||
|
|
Equal,
|
||
|
|
Before, // this <= other with at least one strict <
|
||
|
|
After, // this >= other with at least one strict >
|
||
|
|
Concurrent, // neither dominates: a real conflict happened
|
||
|
|
};
|
||
|
|
|
||
|
|
// Version vector over per-instance oplog sequences, exchanged at sync so
|
||
|
|
// each side fetches only what the other has not seen (S7-7).
|
||
|
|
class VersionVector {
|
||
|
|
public:
|
||
|
|
void update(const std::string& instance, std::uint64_t seq);
|
||
|
|
std::uint64_t get(const std::string& instance) const;
|
||
|
|
void merge(const VersionVector& other);
|
||
|
|
VectorOrder compare(const VersionVector& other) const;
|
||
|
|
|
||
|
|
std::string encode() const; // JSON object {instance: seq}
|
||
|
|
static bool decode(const std::string& text, VersionVector& out);
|
||
|
|
|
||
|
|
private:
|
||
|
|
mutable std::mutex mutex_;
|
||
|
|
std::map<std::string, std::uint64_t> versions_;
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace molo::replication
|