File size: 11,371 Bytes
90cbf22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import { xxHash32 } from '../util/xxhash';
import { compressSigned, uncompressSigned } from '../util/FastIntegerCompression';
import {
  runLengthEncode,
  deltaEncode,
  quantize,
  deltaDecode,
  runLengthDecode,
  unquantize,
} from '../util/compression';

// `HistoricalObject`s require the developer to pass in the
// field names that'll be tracked and sent down to the client.
//
// By default, the historical tracking will round each floating point
// value to an integer. The developer can specify more or less precision
// via the `precision` parameter: the table's quantization will maintain
// less than `1 / 2^precision` error. Note that higher precision values
// imply less error.
export type FieldConfig = Array<string | { name: string; precision: number }>;

// `HistoricalObject`s support at most 16 fields.
const MAX_FIELDS = 16;

const PACKED_VERSION = 1;

type NormalizedFieldConfig = Array<{
  name: string;
  precision: number;
}>;

// The `History` structure represents the history of a continuous
// value over all bounded time. Each sample represents a line
// segment that's extends to the previous sample's time inclusively
// and to the sample's time non-inclusively. We track an `initialValue`
// that goes to `-\infty` up until the first sample, and the final
// sample persists out to `+\infty`.
// ```
//                    ^
//                 position
//                    |
// samples[0].value - |         x---------------o
//                    |
// samples[1].value - |                         x-------->
//                    |
// initialValue     - <---------o
//                    |
//                     ------------------------------> time
//                              |               |
//                      samples[0].time  samples[1].time
// ```
export type History = {
  initialValue: number;
  samples: Sample[];
};

export type Sample = {
  time: number;
  value: number;
};

// `HistoricalObject` tracks a set of numeric fields over time and
// supports compressing the fields' histories into a binary buffer.
// This can be useful for continuous properties like position, where
// we'd want to smoothly replay their tick-by-tick progress at a high
// frame rate on the client.
//
// `HistoricalObject`s have a few limitations:
// - Documents in a historical can only have up to 16 fields.
// - The historical tracking only applies to a specified list of fields,
//   and these fields must match between the client and server.
export class HistoricalObject<T extends Record<string, number>> {
  startTs?: number;

  fieldConfig: NormalizedFieldConfig;

  data: T;
  history: Record<string, History> = {};

  constructor(fields: FieldConfig, initialValue: T) {
    if (fields.length >= MAX_FIELDS) {
      throw new Error(`HistoricalObject can have at most ${MAX_FIELDS} fields.`);
    }
    this.fieldConfig = normalizeFieldConfig(fields);
    this.checkShape(initialValue);
    this.data = initialValue;
  }

  historyLength() {
    return Object.values(this.history)
      .map((h) => h.samples.length)
      .reduce((a, b) => a + b, 0);
  }

  checkShape(data: any) {
    for (const [key, value] of Object.entries(data)) {
      if (!this.fieldConfig.find((f) => f.name === key)) {
        throw new Error(`Cannot set undeclared field '${key}'`);
      }
      if (typeof value !== 'number') {
        throw new Error(
          `HistoricalObject only supports numeric values, found: ${JSON.stringify(value)}`,
        );
      }
    }
  }

  update(now: number, data: T) {
    this.checkShape(data);
    for (const [key, value] of Object.entries(data)) {
      const currentValue = this.data[key];
      if (currentValue !== value) {
        let history = this.history[key];
        if (!history) {
          this.history[key] = history = { initialValue: currentValue, samples: [] };
        }
        const { samples } = history;
        let inserted = false;
        if (samples.length > 0) {
          const last = samples[samples.length - 1];
          if (now < last.time) {
            throw new Error(`Server time moving backwards: ${now} < ${last.time}`);
          }
          if (now === last.time) {
            last.value = value;
            inserted = true;
          }
        }
        if (!inserted) {
          samples.push({ time: now, value });
        }
      }
    }
    this.data = data;
  }

  pack(): ArrayBuffer | null {
    if (this.historyLength() === 0) {
      return null;
    }
    return packSampleRecord(this.fieldConfig, this.history);
  }
}

// Pack (normalized) field configuration into a binary buffer.
//
// Format:
// ```
// [ u8 version ]
// for each field config:
//   [ u8 field name length ]
//   [ UTF8 encoded field name ]
//   [ u8 precision ]
// ```
function packFieldConfig(fields: NormalizedFieldConfig) {
  const out = new ArrayBuffer(1024);
  const outView = new DataView(out);
  let pos = 0;

  outView.setUint8(pos, PACKED_VERSION);
  pos += 1;

  const encoder = new TextEncoder();
  for (const fieldConfig of fields) {
    const name = encoder.encode(fieldConfig.name);

    outView.setUint8(pos, name.length);
    pos += 1;

    new Uint8Array(out, pos, name.length).set(name);
    pos += name.length;

    outView.setUint8(pos, fieldConfig.precision);
    pos += 1;
  }
  return out.slice(0, pos);
}

// Pack a document's sample record into a binary buffer.
//
// We encode each field's history with a few layered forms of
// compression:
// 1. Quantization: Turn each floating point number into an integer
//    by multiplying by 2^precision and then `Math.floor()`.
// 2. Delta encoding: Assume that values are continuous and don't
//    abruptly change over time, so their differences will be small.
//    This step turns the large integers from (1) into small ones.
// 3. Run length encoding (optional): Assume that some quantities
//    in the system will have constant velocity, so encode `k`
//    repetitions of `n` as `[k, n]`. If run length encoding doesn't
//    make (2) smaller, we skip it.
// 4. Varint encoding: Using FastIntegerCompression.js, we use a
//    variable length integer encoding that uses fewer bytes for
//    smaller numbers.
//
// Format:
// ```
// [ 4 byte xxhash of packed field config ]
//
// for each set field:
//   [ 0 0 0 useRLE? ]
//   [ u4 field number ]
//
//   Sample timestamps:
//   [ u64le initial timestamp ]
//   [ u16le timestamp buffer length ]
//   [ vint(RLE(delta(remaining timestamps)))]
//
//   Sample values:
//   [ u16le value buffer length ]
//   [ vint(RLE?(delta([initialValue, ...values])))]
// ```
export function packSampleRecord(
  fields: NormalizedFieldConfig,
  sampleRecord: Record<string, History>,
): ArrayBuffer {
  const out = new ArrayBuffer(65536);
  const outView = new DataView(out);
  let pos = 0;

  const configHash = xxHash32(new Uint8Array(packFieldConfig(fields)));
  outView.setUint32(pos, configHash, true);
  pos += 4;

  for (let fieldNumber = 0; fieldNumber < fields.length; fieldNumber += 1) {
    const { name, precision } = fields[fieldNumber];
    const history = sampleRecord[name];
    if (!history || history.samples.length === 0) {
      continue;
    }

    const timestamps = history.samples.map((s) => Math.floor(s.time));
    const initialTimestamp = timestamps[0];
    const encodedTimestamps = runLengthEncode(deltaEncode(timestamps.slice(1), initialTimestamp));
    const compressedTimestamps = compressSigned(encodedTimestamps);
    if (compressedTimestamps.byteLength >= 1 << 16) {
      throw new Error(`Compressed buffer too long: ${compressedTimestamps.byteLength}`);
    }

    const values = [history.initialValue, ...history.samples.map((s) => s.value)];
    const quantized = quantize(values, precision);
    const deltaEncoded = deltaEncode(quantized);
    const runLengthEncoded = runLengthEncode(deltaEncoded);

    // Decide if we're going to run length encode the values based on whether
    // it actually made the encoded buffer smaller.
    const useRLE = runLengthEncoded.length < deltaEncoded.length;
    let fieldHeader = fieldNumber;
    if (useRLE) {
      fieldHeader |= 1 << 4;
    }

    const encoded = useRLE ? runLengthEncoded : deltaEncoded;
    const compressed = compressSigned(encoded);
    if (compressed.byteLength >= 1 << 16) {
      throw new Error(`Compressed buffer too long: ${compressed.byteLength}`);
    }

    outView.setUint8(pos, fieldHeader);
    pos += 1;

    outView.setBigUint64(pos, BigInt(initialTimestamp), true);
    pos += 8;

    outView.setUint16(pos, compressedTimestamps.byteLength, true);
    pos += 2;

    new Uint8Array(out, pos, compressedTimestamps.byteLength).set(
      new Uint8Array(compressedTimestamps),
    );
    pos += compressedTimestamps.byteLength;

    outView.setUint16(pos, compressed.byteLength, true);
    pos += 2;

    new Uint8Array(out, pos, compressed.byteLength).set(new Uint8Array(compressed));
    pos += compressed.byteLength;
  }

  return out.slice(0, pos);
}

export function unpackSampleRecord(fields: FieldConfig, buffer: ArrayBuffer) {
  const view = new DataView(buffer);
  let pos = 0;

  const normalizedFields = normalizeFieldConfig(fields);
  const expectedConfigHash = xxHash32(new Uint8Array(packFieldConfig(normalizedFields)));

  const configHash = view.getUint32(pos, true);
  pos += 4;

  if (configHash !== expectedConfigHash) {
    throw new Error(`Config hash mismatch: ${configHash} !== ${expectedConfigHash}`);
  }

  const out = {} as Record<string, History>;
  while (pos < buffer.byteLength) {
    const fieldHeader = view.getUint8(pos);
    pos += 1;

    const fieldNumber = fieldHeader & 0b00001111;
    const useRLE = (fieldHeader & (1 << 4)) !== 0;
    const fieldConfig = normalizedFields[fieldNumber];
    if (!fieldConfig) {
      throw new Error(`Invalid field number: ${fieldNumber}`);
    }

    const initialTimestamp = Number(view.getBigUint64(pos, true));
    pos += 8;

    const compressedTimestampLength = view.getUint16(pos, true);
    pos += 2;

    const compressedTimestampBuffer = buffer.slice(pos, pos + compressedTimestampLength);
    pos += compressedTimestampLength;

    const timestamps = [
      initialTimestamp,
      ...deltaDecode(
        runLengthDecode(uncompressSigned(compressedTimestampBuffer)),
        initialTimestamp,
      ),
    ];

    const compressedLength = view.getUint16(pos, true);
    pos += 2;

    const compressedBuffer = buffer.slice(pos, pos + compressedLength);
    pos += compressedLength;

    const encoded = uncompressSigned(compressedBuffer);
    const deltaEncoded = useRLE ? runLengthDecode(encoded) : encoded;
    const quantized = deltaDecode(deltaEncoded);
    const values = unquantize(quantized, fieldConfig.precision);

    if (timestamps.length + 1 !== values.length) {
      throw new Error(`Invalid sample record: ${timestamps.length} + 1 !== ${values.length}`);
    }
    const initialValue = values[0];
    const samples = [];
    for (let i = 0; i < timestamps.length; i++) {
      const time = timestamps[i];
      const value = values[i + 1];
      samples.push({ value, time });
    }
    const history = { initialValue, samples };
    out[fieldConfig.name] = history;
  }
  return out;
}

function normalizeFieldConfig(fields: FieldConfig): NormalizedFieldConfig {
  return fields.map((f) => (typeof f === 'string' ? { name: f, precision: 0 } : f));
}