All files / json-crdt-extensions/peritext/slice Slices.ts

94.91% Statements 112/118
81.81% Branches 18/22
82.6% Functions 19/23
96.26% Lines 103/107

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 23661x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x 61x                   61x 10578x           10578x   10578x   10578x                             14351x 14351x 14351x 14351x 14351x 14351x 14351x 14351x   14351x     14351x 14351x 14351x 14351x 14351x           14351x 14351x 14351x   14351x 14351x 14351x   14351x 14351x 14351x       1032x                   1004x 1004x 1004x         1004x   1004x 1004x 1004x 1004x 1004x       192x       788x       58x       567x 567x 567x 567x 567x 567x 567x 567x 140x 567x       576x       2497x 2497x 2497x 2497x   2485x 2485x       7021x 7021x 7021x 7021x 7021x 19873x 19873x 19873x 6625x     7021x 2341x 2341x 2341x       18536x             21365x 40532x       66x         53071x         10578x 10578x     115600x 115600x 16932x   16932x 201791x 201791x 150214x   51577x       115600x   115600x 147637x 147637x   115600x                                              
import {AvlMap} from 'sonic-forest/lib/avl/AvlMap';
import {printTree} from 'tree-dump/lib/printTree';
import {PersistedSlice} from './PersistedSlice';
import {Timespan, compare, tss} from '../../../json-crdt-patch/clock';
import {updateRga} from '../../../json-crdt/hash';
import {CONST, updateNum} from '../../../json-hash/hash';
import {SliceHeaderShift, SliceStacking, SliceTupleIndex} from './constants';
import {MarkerSlice} from './MarkerSlice';
import {VecNode} from '../../../json-crdt/nodes';
import {Chars} from '../constants';
import {Anchor} from '../rga/constants';
import {UndEndIterator, type UndEndNext} from '../../../util/iterator';
import type {Range} from '../rga/Range';
import type {Slice, SliceType} from './types';
import type {ITimespanStruct, ITimestampStruct} from '../../../json-crdt-patch/clock';
import type {Stateful} from '../types';
import type {Printable} from 'tree-dump/lib/types';
import type {ArrChunk, ArrNode} from '../../../json-crdt/nodes';
import type {AbstractRga} from '../../../json-crdt/nodes/rga';
import type {Peritext} from '../Peritext';
 
export class Slices<T = string> implements Stateful, Printable {
  private list = new AvlMap<ITimestampStruct, PersistedSlice<T>>(compare);
 
  protected readonly rga: AbstractRga<T>;
 
  constructor(
    /** The text RGA. */
    protected readonly txt: Peritext<T>,
    /** The `arr` node, used as a set, where slices are stored. */
    public readonly set: ArrNode,
  ) {
    this.rga = txt.str as unknown as AbstractRga<T>;
  }
 
  public ins<
    S extends PersistedSlice<T>,
    K extends new (
      ...args: ConstructorParameters<typeof PersistedSlice<T>>
    ) => S,
  >(
    range: Range<T>,
    stacking: SliceStacking,
    type: SliceType,
    data?: unknown,
    Klass: K = stacking === SliceStacking.Marker ? <any>MarkerSlice : PersistedSlice,
  ): S {
    const slicesModel = this.set.doc;
    const set = this.set;
    const api = slicesModel.api;
    const builder = api.builder;
    const tupleId = builder.vec();
    const start = range.start.clone();
    const end = range.end.clone();
    stacking = stacking & 0b111;
    const header =
      (stacking << SliceHeaderShift.Stacking) +
      ((start.anchor & 0b1) << SliceHeaderShift.X1Anchor) +
      ((end.anchor & 0b1) << SliceHeaderShift.X2Anchor);
    const headerId = builder.con(header);
    const x1Id = builder.con(start.id);
    const x2Id = builder.con(compare(start.id, end.id) === 0 ? 0 : end.id);
    const subtypeId = builder.con(type);
    const tupleKeysUpdate: [key: number, value: ITimestampStruct][] = [
      [SliceTupleIndex.Header, headerId],
      [SliceTupleIndex.X1, x1Id],
      [SliceTupleIndex.X2, x2Id],
      [SliceTupleIndex.Type, subtypeId],
    ];
    if (data !== undefined) tupleKeysUpdate.push([SliceTupleIndex.Data, builder.json(data)]);
    builder.insVec(tupleId, tupleKeysUpdate);
    const chunkId = builder.insArr(set.id, set.id, [tupleId]);
    // TODO: Consider using `s` schema here.
    api.apply();
    const tuple = slicesModel.index.get(tupleId) as VecNode;
    const chunk = set.findById(chunkId)!;
    // TODO: Need to check if split slice text was deleted
    const slice = new Klass(slicesModel, this.txt, chunk, tuple, stacking, type, start, end);
    this.list.set(chunk.id, slice);
    return slice;
  }
 
  public insMarker(range: Range<T>, type: SliceType, data?: unknown | ITimestampStruct): MarkerSlice<T> {
    return this.ins(range, SliceStacking.Marker, type, data) as MarkerSlice<T>;
  }
 
  public insMarkerAfter(
    after: ITimestampStruct,
    type: SliceType,
    data?: unknown,
    separator: string = Chars.BlockSplitSentinel,
  ): MarkerSlice<T> {
    // TODO: test condition when cursors is at absolute or relative starts
    const txt = this.txt;
    const api = txt.model.api;
    const builder = api.builder;
    /**
     * We skip one clock cycle to prevent Block-wise RGA from merging adjacent
     * characters. We want the marker chunk to always be its own distinct chunk.
     */
    builder.nop(1);
    // TODO: Handle case when marker is inserted at the abs start, prevent abs start/end inserts.
    const textId = builder.insStr(txt.str.id, after, separator);
    api.apply();
    const point = txt.point(textId, Anchor.Before);
    const range = txt.range(point, point.clone());
    return this.insMarker(range, type, data);
  }
 
  public insStack(range: Range<T>, type: SliceType, data?: unknown | ITimestampStruct): PersistedSlice<T> {
    return this.ins(range, SliceStacking.Many, type, data);
  }
 
  public insOne(range: Range<T>, type: SliceType, data?: unknown | ITimestampStruct): PersistedSlice<T> {
    return this.ins(range, SliceStacking.One, type, data);
  }
 
  public insErase(range: Range<T>, type: SliceType, data?: unknown | ITimestampStruct): PersistedSlice<T> {
    return this.ins(range, SliceStacking.Erase, type, data);
  }
 
  protected unpack(chunk: ArrChunk): PersistedSlice<T> {
    const txt = this.txt;
    const model = this.set.doc;
    const tupleId = chunk.data ? chunk.data[0] : undefined;
    Iif (!tupleId) throw new Error('SLICE_NOT_FOUND');
    const tuple = model.index.get(tupleId);
    Iif (!(tuple instanceof VecNode)) throw new Error('NOT_TUPLE');
    let slice = PersistedSlice.deserialize<T>(model, txt, chunk, tuple);
    if (slice.isSplit())
      slice = new MarkerSlice<T>(model, txt, chunk, tuple, slice.stacking, slice.type, slice.start, slice.end);
    return slice;
  }
 
  public get(id: ITimestampStruct): PersistedSlice<T> | undefined {
    return this.list.get(id);
  }
 
  public del(id: ITimestampStruct): void {
    this.list.del(id);
    const set = this.set;
    const api = set.doc.api;
    if (!set.findById(id)) return;
    // TODO: Is it worth checking if the slice is already deleted?
    api.builder.del(set.id, [tss(id.sid, id.time, 1)]);
    api.apply();
  }
 
  public delSlices(slices: Iterable<Slice<T>>): boolean {
    const set = this.set;
    const doc = set.doc;
    const api = doc.api;
    const spans: ITimespanStruct[] = [];
    for (const slice of slices) {
      if (slice instanceof PersistedSlice) {
        const id = slice.id;
        if (!set.findById(id)) continue;
        spans.push(new Timespan(id.sid, id.time, 1));
      }
    }
    if (!spans.length) return false;
    api.builder.del(this.set.id, spans);
    api.apply();
    return true;
  }
 
  public size(): number {
    return this.list._size;
  }
 
  /**
   * @todo Rename to `each0`.
   */
  public iterator0(): UndEndNext<PersistedSlice<T>> {
    const iterator = this.list.iterator0();
    return () => iterator()?.v;
  }
 
  public each(): UndEndIterator<PersistedSlice<T>> {
    return new UndEndIterator<PersistedSlice<T>>(this.iterator0());
  }
 
  public forEach(callback: (item: PersistedSlice<T>) => void): void {
    // biome-ignore lint: list is not iterable
    this.list.forEach((node) => callback(node.v));
  }
 
  // ----------------------------------------------------------------- Stateful
 
  private _topologyHash: number = 0;
  public hash: number = 0;
 
  public refresh(): number {
    const topologyHash = updateRga(CONST.START_STATE, this.set);
    if (topologyHash !== this._topologyHash) {
      this._topologyHash = topologyHash;
      let chunk: ArrChunk | undefined;
      for (const iterator = this.set.iterator(); (chunk = iterator()); ) {
        const item = this.list.get(chunk.id);
        if (chunk.del) {
          if (item) this.list.del(chunk.id);
        } else {
          if (!item) this.list.set(chunk.id, this.unpack(chunk));
        }
      }
    }
    let hash: number = topologyHash;
    // biome-ignore lint: slices is not iterable
    this.list.forEach(({v: item}) => {
      item.refresh();
      hash = updateNum(hash, item.hash);
    });
    return (this.hash = hash);
  }
 
  // ---------------------------------------------------------------- Printable
 
  public toStringName(): string {
    return 'Slices';
  }
 
  public toString(tab: string = ''): string {
    return (
      this.toStringName() +
      printTree(
        tab,
        [...this.list.entries()].map(
          ({v}) =>
            (tab) =>
              v.toString(tab),
        ),
      )
    );
  }
}