sdks/unreal/src/SpacetimeDbSdk/Source/SpacetimeDbSdk/Public/DBCache/README.md
Utilities used to maintain a client side cache of database tables. These classes store rows received from the server and provide helpers for fast lookups.
BTreeUniqueIndex.h – B-tree implementation of IUniqueIndex supporting range queries.ClientCache.h – Owns FTableCache objects and applies insert/delete diffs sent over the network.IUniqueIndex.h – Interface that unique index implementations conform to.RowEntry.h – Wrapper storing a row value with a reference count used by overlapping subscriptions.TableAppliedDiff.h – Describes the inserts, deletes and updates detected when applying a diff.TableCache.h – In-memory representation of a table and its unique indices.TableHandle.h – Lightweight helper exposing read only access to a cached table.UniqueConstraintHandle.h – Helper that allows typed lookups against a unique constraint.UniqueIndex.h – Hash map based implementation of a unique index.WithBsatn.h – Pairs a row with its serialized BSATN bytes for diff processing.This module implements a Client-Side Table Cache for storing and querying rows efficiently using Unique Constraints and B-Tree Multi-Key Indices.
Designed for Unreal Engine 5, it supports point queries, multi-key lookups, and is optimized for high-performance client caching.
TMap<TArray<uint8>, FRowEntry<RowType>> for serialized keys.TFunction-based for easy integration with UE types.template<typename ColType>
void AddUniqueConstraint(
const FString& Name,
TFunction<ColType(const RowType&)> ExtractColumn);
Entries.template<typename KeyType>
void AddMultiKeyBTreeIndex(
const FString& Name,
TFunction<KeyType(const RowType&)> ExtractKey);
template<typename KeyType>
const RowType* FindByUniqueIndex(
const FString& Name,
const KeyType& Key) const;
nullptr if not found.template<typename KeyType>
void FindByMultiKeyBTreeIndex(
TArray<RowType>& OutResults,
const FString& Name,
const KeyType& Key) const;
void GetValues(TArray<RowType>& AllRows) const;
AllRows with copies of every row in the cache.FTableCache<FMessage> Table;
// Add indices before inserting data
Table->AddUniqueConstraint<FSpacetimeDBIdentity>("sender", [](const FMessageType& Row) -> FSpacetimeDBIdentity {
return Row.Sender;
});
using FSenderTextKey = TTuple<FSpacetimeDBIdentity, FString>;
Table->AddMultiKeyBTreeIndex<FSenderTextKey>(
TEXT("sender_text"),
[](const FMessageType& Msg) { return MakeTuple(Msg.Sender, Msg.Text); }
);
// Query examples
const FMessage* Found = Table.FindByUniqueIndex(TEXT("MessageID"), TEXT("abc-123"));
using FSenderTextKey = TTuple<FSpacetimeDBIdentity, FString>;
TArray<FMessageType> Results;
Table->FindByMultiKeyBTreeIndex<FSenderTextKey>(
Results,
TEXT("sender_text"),
MakeTuple(Sender, Message)
);
// Retrieve all rows
TArray<FMessage> AllRows;
Table.GetValues(AllRows);
TArray<uint8> keys allow serialized identifiers (network-friendly).