Temporal Engine Architecture¶
The temporal engine manages record versioning, point-in-time indexing, historical diffing, and state restoration.
Source Location¶
- Index:
src/index/history_index.h,src/index/history_index.cpp - Implementations:
src/temporal/as_of.cpp— Single-row and key-levelAS OFresolution.src/temporal/between.cpp— Range-based version extraction.src/temporal/compare.cpp— Net endpoint comparison and pairwise difference algorithms.src/temporal/evolution.cpp— Step-by-step changelog generation.src/temporal/history.cpp— Complete version timeline queries.src/temporal/rollback.cpp— Non-destructive historical restoration.src/temporal/snapshot.cpp— Table-wide point-in-time reconstruction.
The In-Memory HistoryIndex¶
The HistoryIndex serves as the temporal index for MemoraDB:
class HistoryIndex {
private:
std::unordered_map<std::string, std::vector<RecordVersion>> history;
public:
bool contains(const std::string& pk);
void addVersion(const std::string& pk, const RecordVersion& version);
const std::vector<RecordVersion>& getHistory(const std::string& pk);
const RecordVersion& latest(const std::string& pk);
const RecordVersion* latestBefore(const std::string& pk, uint64_t timestamp);
};
Binary Search via latestBefore¶
Because versions are strictly appended in ascending chronological order, point-in-time queries run in O(log V) time using binary search:
const RecordVersion* HistoryIndex::latestBefore(const std::string& pk, uint64_t timestamp) {
auto it = history.find(pk);
if (it == history.end()) return nullptr;
const std::vector<RecordVersion>& hist = it->second;
const RecordVersion* ans = nullptr;
std::ptrdiff_t l = 0, r = static_cast<std::ptrdiff_t>(hist.size()) - 1;
while (l <= r) {
std::ptrdiff_t mid = l + (r - l) / 2;
if (hist[mid].timestamp <= timestamp) {
ans = &hist[mid];
l = mid + 1; // Look for a later version that is still <= timestamp
} else {
r = mid - 1;
}
}
return ans;
}
Temporal Operation Algorithms¶
1. Snapshot Reconstruction (Table::snapshot)¶
- Iterates over all known primary keys in
HistoryIndex. - For each key, calls
latestBefore(pk, timestamp). - If the anchor version has
deleted == 0, reads the record from disk and includes it.
2. Net Difference (Table::compare)¶
- Calls
selectBetween(pk, t1, t2)to gather all versions in the time window. - If empty, returns no differences.
- Calls
compareRecords(recs.front(), recs.back(), meta). - Returns only the net attribute differences between the start and end of the window.
3. Step-by-Step Changelog (Table::evolution)¶
- Locates the anchor version at
t1. - Loops through every consecutive version pair
(hist[i], hist[i+1])up tot2. - Calls
compareRecords(r1, r2, meta)on each pair. - Stamps each difference with the exact timestamp when that specific change was committed (
diff.timestamp = r2.timestamp).
4. Non-Destructive Rollback (Table::rollback)¶
- Fetches the historical record via
selectAsOf(pk, timestamp). - Calls
appendRecord(record), creating a brand new record at the end ofdata.dbwithtimestamp = now(). - The historical state is restored as the current state without destroying subsequent history.