All files / json-crdt/codec/structural/binary Encoder.ts

100% Statements 140/140
100% Branches 29/29
100% Functions 19/19
100% Lines 120/120

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 189174x 174x 174x 174x 174x   174x     174x 475x 475x       475x       6535x 6535x 6535x 6535x 6158x 6535x       6158x 6158x 6158x 6158x 6158x 6158x 6158x 6158x 6158x       377x 377x 377x 377x 377x       6158x 6158x 6158x 6158x 6158x 6158x 6158x 6158x     475x 98424x 98424x 98424x 98424x     475x 756170x 756170x     475x 22512x     475x     6535x 6535x 6455x       144832x 144832x   2245x 2245x           244944x 150399x 144832x 96921x 26165x 25583x 12712x       94545x 94545x 94545x 1137x 1137x   93408x 93408x         5567x 5567x 5567x       70756x 70756x 70756x 70756x     475x 206529x 206529x       582x 582x 582x 582x 582x 582x 2795x 2795x 2793x         47911x 47911x 47911x 47911x 347780x 347780x 161788x         12712x 12712x 12712x 12712x 12712x 166418x 166418x 166418x 166418x 166418x 45535x         12871x 12871x 12871x 12871x 12871x 12871x 18403x 18403x 18403x 18403x 18403x 15792x 23600x        
import * as nodes from '../../../nodes';
import {ClockEncoder} from '../../../../json-crdt-patch/codec/clock/ClockEncoder';
import {CrdtWriter} from '../../../../json-crdt-patch/util/binary/CrdtWriter';
import {type ITimestampStruct, Timestamp} from '../../../../json-crdt-patch/clock';
import {CborEncoder} from '@jsonjoy.com/json-pack/lib/cbor/CborEncoder';
import {SESSION} from '../../../../json-crdt-patch/constants';
import {CRDT_MAJOR_OVERLAY} from './constants';
import type {Model} from '../../../model';
 
export class Encoder extends CborEncoder<CrdtWriter> {
  protected clockEncoder: ClockEncoder = new ClockEncoder();
  protected time: number = 0;
  protected doc!: Model;
 
  constructor(writer?: CrdtWriter) {
    super(writer || new CrdtWriter());
  }
 
  public encode(doc: Model<any>): Uint8Array {
    this.doc = doc;
    const writer = this.writer;
    writer.reset();
    if (doc.clock.sid === SESSION.SERVER) this.encodeServer(doc);
    else this.encodeLogical(doc);
    return writer.flush();
  }
 
  public encodeLogical(model: Model): void {
    const writer = this.writer;
    this.ts = this.tsLogical;
    this.clockEncoder.reset(model.clock);
    writer.ensureCapacity(4);
    const x0 = writer.x0;
    const x = writer.x;
    writer.x += 4;
    this.cRoot(model.root);
    this.encodeClockTable(x0, x);
  }
 
  public encodeServer(model: Model): void {
    this.ts = this.tsServer;
    const writer = this.writer;
    writer.u8(0b10000000);
    writer.vu57((this.time = model.clock.time));
    this.cRoot(model.root);
  }
 
  protected encodeClockTable(x0: number, x: number) {
    const writer = this.writer;
    const shift = writer.x0 - x0;
    writer.view.setUint32(writer.x0 + (x - x0), writer.x - x - shift - 4);
    const clockEncoder = this.clockEncoder!;
    const table = clockEncoder.table;
    const length = table.size;
    writer.vu57(length);
    table.forEach(this.cTableEntry);
  }
 
  protected readonly cTableEntry = (entry: {clock: ITimestampStruct}) => {
    const clock = entry.clock;
    const writer = this.writer;
    writer.vu57(clock.sid);
    writer.vu57(clock.time);
  };
 
  protected readonly tsLogical = (ts: ITimestampStruct): void => {
    const relativeId = this.clockEncoder!.append(ts);
    this.writer.id(relativeId.sessionIndex, relativeId.timeDiff);
  };
 
  protected readonly tsServer = (ts: ITimestampStruct): void => {
    this.writer.vu57(ts.time);
  };
 
  protected ts: (ts: ITimestampStruct) => void = this.tsLogical;
 
  protected cRoot(root: nodes.RootNode): void {
    const val = root.val;
    if (val.sid === SESSION.SYSTEM) this.writer.u8(0);
    else this.cNode(root.node());
  }
 
  protected writeTL(majorOverlay: CRDT_MAJOR_OVERLAY, length: number): void {
    const writer = this.writer;
    if (length < 0b11111) writer.u8(majorOverlay | length);
    else {
      writer.u8(majorOverlay | 0b11111);
      writer.vu57(length);
    }
  }
 
  protected cNode(node: nodes.JsonNode): void {
    // TODO: PERF: use a switch?
    if (node instanceof nodes.ConNode) this.cCon(node);
    else if (node instanceof nodes.ValNode) this.cVal(node);
    else if (node instanceof nodes.StrNode) this.cStr(node);
    else if (node instanceof nodes.ObjNode) this.cObj(node);
    else if (node instanceof nodes.VecNode) this.cVec(node);
    else if (node instanceof nodes.ArrNode) this.cArr(node);
    else if (node instanceof nodes.BinNode) this.cBin(node);
  }
 
  protected cCon(node: nodes.ConNode): void {
    const val = node.val;
    this.ts(node.id);
    if (val instanceof Timestamp) {
      this.writer.u8(1); // this.writeTL(CRDT_MAJOR_OVERLAY.CON, 1);
      this.ts(val as Timestamp);
    } else {
      this.writer.u8(0); // this.writeTL(CRDT_MAJOR_OVERLAY.CON, 0);
      this.writeAny(val);
    }
  }
 
  protected cVal(node: nodes.ValNode): void {
    this.ts(node.id);
    this.writer.u8(0b00100000); // this.writeTL(CRDT_MAJOR_OVERLAY.VAL, 0);
    this.cNode(node.node());
  }
 
  protected cObj(node: nodes.ObjNode): void {
    this.ts(node.id);
    const keys = node.keys;
    this.writeTL(CRDT_MAJOR_OVERLAY.OBJ, keys.size);
    keys.forEach(this.cKey);
  }
 
  protected readonly cKey = (val: ITimestampStruct, key: string) => {
    this.writeStr(key);
    this.cNode(this.doc.index.get(val)!);
  };
 
  protected cVec(node: nodes.VecNode): void {
    const elements = node.elements;
    const length = elements.length;
    this.ts(node.id);
    this.writeTL(CRDT_MAJOR_OVERLAY.VEC, length);
    const index = this.doc.index;
    for (let i = 0; i < length; i++) {
      const elementId = elements[i];
      if (!elementId) this.writer.u8(0);
      else this.cNode(index.get(elementId)!);
    }
  }
 
  protected cStr(node: nodes.StrNode): void {
    const ts = this.ts;
    ts(node.id);
    this.writeTL(CRDT_MAJOR_OVERLAY.STR, node.count);
    for (let chunk = node.first(); chunk; chunk = node.next(chunk)) {
      ts(chunk.id);
      if (chunk.del) this.writeUInteger(chunk.span);
      else this.writeStr(chunk.data!);
    }
  }
 
  protected cBin(node: nodes.BinNode): void {
    const ts = this.ts;
    const writer = this.writer;
    ts(node.id);
    this.writeTL(CRDT_MAJOR_OVERLAY.BIN, node.count);
    for (let chunk = node.first(); chunk; chunk = node.next(chunk)) {
      ts(chunk.id);
      const length = chunk.span;
      const deleted = chunk.del;
      writer.b1vu56(~~deleted as 0 | 1, length);
      if (deleted) continue;
      writer.buf(chunk.data!, length);
    }
  }
 
  protected cArr(node: nodes.ArrNode): void {
    const ts = this.ts;
    const writer = this.writer;
    ts(node.id);
    this.writeTL(CRDT_MAJOR_OVERLAY.ARR, node.count);
    const index = this.doc.index;
    for (let chunk = node.first(); chunk; chunk = node.next(chunk)) {
      ts(chunk.id);
      const span = chunk.span;
      const deleted = chunk.del;
      writer.b1vu56(~~deleted as 0 | 1, span);
      if (deleted) continue;
      const nodes = chunk.data!;
      for (let i = 0; i < span; i++) this.cNode(index.get(nodes[i])!);
    }
  }
}