Crdt.Node
crdt · API reference
CRDT node types
CRDT node types.
This module defines all 7 CRDT node types from json-joy:
- con: constant/immutable value
- val: mutable reference (LWW register)
- obj: mutable string-keyed map (LWW-Map)
- vec: mutable indexed tuple (fixed slots)
- arr: mutable ordered list (RGA)
- str: mutable text string (RGA)
- bin: mutable binary data (RGA) Each node has a unique ID (timestamp) assigned when it is created.
Node Type Enumeration
type node_type =
| Con (* Constant/immutable value *)
| Val (* Mutable reference (LWW register) *)
| Obj (* Mutable string-keyed map *)
| Vec (* Mutable indexed tuple *)
| Arr (* Mutable ordered list (RGA) *)
| Str (* Mutable text string (RGA) *)
| Bin (* Mutable binary data (RGA) *)Node types as an enumeration
val type_name : node_type -> stringGet the node type name
RGA Chunk (re-export from Rga module)
type 'a rga_chunk = 'a Rga.chunkA chunk in an RGA - contiguous elements with sequential IDs. For str: each ID represents one UTF-16 code unit. For bin: each ID represents one byte. For arr: each ID represents one element reference.
Node Data Structures
type con_node = {
con_id : Clock.timestamp;
con_value : Value.t; (* The immutable value or Timestamp_ref *)
}Constant node - holds an immutable value. The value can be a primitive (null, bool, number, string) or a reference to another node (timestamp).
type val_node = {
val_id : Clock.timestamp;
mutable val_ref : Clock.timestamp option; (* Reference to the current value node *)
}Value node - LWW register holding a reference to another node. Initially the value is None (not set).
type obj_entry = {
obj_key : string;
obj_value : Clock.timestamp; (* Reference to value node *)
obj_write_ts : Clock.timestamp; (* When this key was written (for LWW) *)
}Object node - LWW-Map of string keys to node references. Each key maps to a (timestamp, value) pair where timestamp is when the key was last written.
type obj_node = {
obj_id : Clock.timestamp;
obj_entries : (string, obj_entry) Hashtbl.t; (* Key-value pairs *)
}type vec_slot = {
vec_idx : int; (* Slot index 0-255 *)
vec_value : Clock.timestamp; (* Reference to value node *)
vec_write_ts : Clock.timestamp; (* When this slot was written *)
}Vector node - fixed-size tuple with up to 256 slots. Each slot is a LWW register.
type vec_node = {
vec_id : Clock.timestamp;
mutable vec_slots : vec_slot list; (* Slots that have been set *)
}type arr_chunk = Clock.timestamp listtype arr_node = {
arr_id : Clock.timestamp;
arr_rga : arr_chunk Rga.t; (* RGA chunks of node references *)
}Array node - RGA of node references
type str_node = {
str_id : Clock.timestamp;
str_rga : str_chunk Rga.t; (* RGA of UTF-16 text slices *)
mutable str_view_cache : string option;
}String node - RGA of UTF-16 text
and str_chunk = {
str_buffer : string;
str_offset : int;
str_length : int;
str_utf16_length : int;
}type bin_chunk = {
buffer : bytes;
offset : int;
length : int;
}type bin_node = {
bin_id : Clock.timestamp;
bin_rga : bin_chunk Rga.t; (* RGA of byte slices *)
mutable bin_view_cache : bytes option;
}Binary node - RGA of bytes
Node Sum Type
type t =
| Node_con of con_node
| Node_val of val_node
| Node_obj of obj_node
| Node_vec of vec_node
| Node_arr of arr_node
| Node_str of str_node
| Node_bin of bin_nodeA CRDT node - one of the 7 types
Node Properties
val id : t -> Clock.timestampGet the ID of a node
val node_type : t -> node_typeGet the type of a node
val name : t -> stringGet the type name of a node
Node Constructors
val make_con : id:Clock.timestamp -> value:Value.t -> tCreate a constant node
val make_val : id:Clock.timestamp -> tCreate a value node (initially empty)
val make_obj : id:Clock.timestamp -> tCreate an object node (initially empty)
val ordered_obj_entries : ('a, obj_entry) Hashtbl.t -> obj_entry listval obj_entries : t -> obj_entry listval find_obj_key : t -> string -> obj_entry optionval make_vec : id:Clock.timestamp -> tCreate a vector node (initially empty)
val make_arr : id:Clock.timestamp -> tCreate an array node (initially empty)
val arr_chunks : t -> Clock.timestamp Rga.chunk listval arr_values : t -> Clock.timestamp listval arr_last_id : t -> Clock.timestamp optionval arr_find_position : t -> int -> Clock.timestamp optionval arr_find_spans : t -> pos:int -> len:int -> Clock.timespan listval arr_length : t -> intval make_str : id:Clock.timestamp -> tCreate a string node (initially empty)
val make_bin : id:Clock.timestamp -> tCreate a binary node (initially empty)
val str_chunk_of_string : string -> str_chunkval materialize_str_chunk : str_chunk -> stringval split_str_chunk :
str_chunk ->
byte_offset:int ->
utf16_offset:int ->
str_chunk * str_chunkval str_chunk_span : str_chunk -> intval utf16_offset_to_str_chunk_byte : str_chunk -> int -> intval split_str_chunk_utf16 : str_chunk -> int -> str_chunk * str_chunkval str_chunks : t -> string Rga.chunk listval str_last_id : t -> Clock.timestamp optionval str_find_position : t -> int -> Clock.timestamp optionval str_find_spans : t -> pos:int -> len:int -> Clock.timespan listval str_length : t -> intval bin_chunk_of_bytes : bytes -> bin_chunkval empty_bin_chunk : bin_chunkval materialize_bin_chunk : bin_chunk -> bytesval split_bin_chunk : bin_chunk -> int -> bin_chunk * bin_chunkval bin_chunk_span : bin_chunk -> intval make_str_with_rga : id:Clock.timestamp -> string Rga.t -> tval make_bin_with_rga : id:Clock.timestamp -> bytes Rga.t -> tval bin_chunks : t -> bytes Rga.chunk listval bin_last_id : t -> Clock.timestamp optionval bin_find_position : t -> int -> Clock.timestamp optionval bin_find_spans : t -> pos:int -> len:int -> Clock.timespan listval bin_length : t -> intval string_view : t -> stringval bytes_view : t -> bytesView Functions
val view : t -> Value.tGet the view (current state as Value.t) of a node. Note: For nodes with references (val, obj, vec, arr), this returns a partial view. Use Model.view for the full resolved view.
Mutation Operations
val set_val : t -> value:Clock.timestamp -> unitSet the reference in a value node
val set_obj_key :
t ->
key:string ->
value:Clock.timestamp ->
write_ts:Clock.timestamp ->
unitSet a key in an object node (LWW semantics)
val set_vec_slot :
t ->
idx:int ->
value:Clock.timestamp ->
write_ts:Clock.timestamp ->
unitSet a slot in a vector node (LWW semantics)
RGA Operations
val insert_str :
t ->
after:Clock.timestamp option ->
chunk_id:Clock.timestamp ->
text:string ->
unitInsert text into a string node after a given position.
parameter after The timestamp to insert after (None for head) parameter chunk_id The timestamp for this insert operation parameter text The text to insert
val insert_bin :
t ->
after:Clock.timestamp option ->
chunk_id:Clock.timestamp ->
data:bytes ->
unitInsert bytes into a binary node.
parameter after The timestamp to insert after (None for head) parameter chunk_id The timestamp for this insert operation parameter data The bytes to insert
val insert_arr :
t ->
after:Clock.timestamp option ->
chunk_id:Clock.timestamp ->
value:Clock.timestamp ->
unitInsert element into an array node.
parameter after The timestamp to insert after (None for head) parameter chunk_id The timestamp for this insert operation parameter value The element reference to insert
val insert_arr_values :
t ->
after:Clock.timestamp option ->
chunk_id:Clock.timestamp ->
values:arr_chunk ->
unitval seq_contains : t -> Clock.timestamp -> boolWhether a sequence node already contains the element ts (visible or tombstoned). Used to make op re-delivery idempotent: an RGA insert whose first element is present was already applied.
val delete_range : t -> spans:Clock.timespan list -> unitDelete a range in an RGA node by marking elements as deleted (tombstones).
parameter spans List of timespans to delete
Pretty Printing
val pp : Format.formatter -> t -> unitPretty print a node
val to_string : t -> stringConvert node to string for debugging