From 41cd14a5d7c15a107e1c5d5b98c059fbd0bebbfe Mon Sep 17 00:00:00 2001 From: Andrew Noyes Date: Wed, 17 Jan 2024 10:46:55 -0800 Subject: [PATCH] Add ConflictSet.h --- ConflictSet.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 ConflictSet.h diff --git a/ConflictSet.h b/ConflictSet.h new file mode 100644 index 0000000..b132c37 --- /dev/null +++ b/ConflictSet.h @@ -0,0 +1,60 @@ +#pragma once + +#include + +struct ConflictSet { + enum Result { + /// The result of a check which does not intersect any conflicting writes + Commit, + /// The result of a check which intersects a write at a version > + /// readVersion + Conflict, + /// The result of a check with a readVersion older than the highest call to + /// `setOldestVersion` + TooOld, + }; + /// Bytes ordered lexicographically + struct Key { + const uint8_t *p; + int len; + }; + /// Denotes a set of keys to be checked for conflicts + struct ReadRange { + Key begin; + /// `end` having length 0 denotes that this range is the single key {begin}. + /// Otherwise this denotes the range [begin, end) + Key end; + int64_t readVersion; + }; + /// Denotes a set of keys to be considered written at `writeVersion` + struct WriteRange { + Key begin; + /// `end` having length 0 denotes that this range is the single key {begin}. + /// Otherwise this denotes the range [begin, end) + Key end; + int64_t writeVersion; + }; + /// `reads` must be sorted ascending, and must not have adjacent or + /// overlapping ranges. The result of checking reads[i] is written in + /// results[i]. + void check(const ReadRange *reads, Result *results, int count) const; + /// `writes` must be sorted ascending, and must not have adjacent or + /// overlapping ranges. Reads intersecting writes where readVersion < + /// writeVersion will result in `Conflict` (or `TooOld`, eventually) + void addWrites(const WriteRange *writes, int count); + /// Reads where readVersion < oldestVersion will result in `TooOld` + void setOldestVersion(int64_t oldestVersion); + + /// Reads where readVersion < oldestVersion will result in `TooOld`. There are + /// no writes initially. + explicit ConflictSet(int64_t oldestVersion); + ~ConflictSet(); + + ConflictSet(ConflictSet &&) noexcept; + ConflictSet &operator=(ConflictSet &&) noexcept; + ConflictSet(const ConflictSet &) = delete; + ConflictSet &operator=(const ConflictSet &) = delete; + +private: + struct Impl *impl; +}; \ No newline at end of file