Crdt.Patch_builder
crdt · API reference
Builder API for constructing patches
Builder API for constructing patches.
Provides an ergonomic interface for creating patches with automatic ID assignment from a logical clock.
Usage:
let builder = Patch_builder.create clock in
Patch_builder.new_str builder;
Patch_builder.ins_str builder ~obj:ts1 ~after:ts2 ~value:"hello";
let patch = Patch_builder.flush builderTypes
type t = {
clock : Clock.clock_vector; (* Clock for ID assignment *)
mutable ops : Op.op_data list; (* Operations (in reverse order) *)
mutable start_id : Clock.timestamp option; (* Starting ID of current patch *)
}A patch builder with accumulating operations
Creation
val create : Clock.clock_vector -> tCreate a new builder with the given clock vector
val reset : t -> unitReset the builder, discarding any pending operations
Building Patches
val ensure_start_id : t -> Clock.timestampInternal: ensure we have a start ID, tick the clock if needed
val add_op : t -> Op.op_data -> unitInternal: add an operation and advance clock by its span
val flush : t -> Patch.t optionFlush the builder and return the completed patch. Returns None if no operations were added.
val flush_or_empty : t -> Patch.tFlush the builder, returning an empty patch if no operations. The empty patch will have the current clock time as its ID.
Creation Operations
val new_con : t -> Value.t -> unitCreate a constant node with the given value
val new_val : t -> unitCreate a mutable value node
val new_obj : t -> unitCreate an object node
val new_vec : t -> unitCreate a vector node
val new_str : t -> unitCreate a string node
val new_bin : t -> unitCreate a binary node
val new_arr : t -> unitCreate an array node
Edit Operations
val ins_val : t -> obj:Op.timestamp -> value:Op.timestamp -> unitSet a value node's reference
val ins_obj : t -> obj:Op.timestamp -> entries:Op.obj_entry list -> unitInsert/update object entries
val ins_vec : t -> obj:Op.timestamp -> idx:int -> value:Op.timestamp -> unitSet a vector slot
val ins_str :
t ->
obj:Op.timestamp ->
after:Op.timestamp ->
value:string ->
unitInsert text into a string node
val ins_bin :
t ->
obj:Op.timestamp ->
after:Op.timestamp ->
value:bytes ->
unitInsert bytes into a binary node
val ins_arr :
t ->
obj:Op.timestamp ->
after:Op.timestamp ->
value:Op.timestamp ->
unitInsert element into an array node
val upd_arr :
t ->
obj:Op.timestamp ->
pos:Op.timestamp ->
value:Op.timestamp ->
unitUpdate an array element
val del : t -> obj:Op.timestamp -> what:Op.timespan list -> unitDelete ranges from an RGA
val nop : t -> int -> unitAdd a no-op (for padding/alignment)
Utilities
val pending_count : t -> intGet the number of pending operations
val has_pending : t -> boolCheck if builder has pending operations
val current_time : t -> intGet the current clock time (for reference)
val session_id : t -> intGet the session ID
val next_op_id : t -> Clock.timestampGet the ID that will be assigned to the next operation added. This is computed as start_id + cumulative_span, where cumulative_span is the sum of spans of all operations added so far. If no operations have been added yet, returns the current clock time (which will become the start_id after the first tick).