Skip to content
Source
Sign inSign up
TanStack/db
Open History to see the last commit

murmur-stream-reference-oracle.ts

109 lines3.0 KB

1import { declareOracle } from '../../db/tests/oracle-declaration.js'
2/**
3 * Reference model for the murmur stream oracle: the stream as it was before
4 * it wrote whole words, with one `writeByte` call per byte. Load it after
5 * stubbing `Math.random`, so its seed and markers equal the current module's.
6 */
7​
8const randomHash = () => (Math.random() * (2 ** 31 - 1)) >>> 0
9​
10const RANDOM_SEED = randomHash()
11const STRING_MARKER = randomHash()
12const BIG_INT_MARKER = randomHash()
13const NEG_BIG_INT_MARKER = randomHash()
14​
15const buf = new ArrayBuffer(8)
16const dv = new DataView(buf)
17const u8 = new Uint8Array(buf)
18​
19export class ReferenceByteStream {
20 private hash: number = RANDOM_SEED
21 private length = 0
22 private carry = 0
23 private carryBytes = 0
24​
25 private _mix(k1: number): void {
26 k1 = Math.imul(k1, 0xcc9e2d51)
27 k1 = (k1 << 15) | (k1 >>> 17)
28 k1 = Math.imul(k1, 0x1b873593)
29 this.hash ^= k1
30 this.hash = (this.hash << 13) | (this.hash >>> 19)
31 this.hash = Math.imul(this.hash, 5) + 0xe6546b64
32 }
33​
34 writeByte(byte: number): void {
35 this.carry |= (byte & 0xff) << (8 * this.carryBytes)
36 this.carryBytes++
37 this.length++
38​
39 if (this.carryBytes === 4) {
40 this._mix(this.carry >>> 0)
41 this.carry = 0
42 this.carryBytes = 0
43 }
44 }
45​
46 update(chunk: symbol | string | number | bigint): void {
47 switch (typeof chunk) {
48 case `symbol`:
49 throw new TypeError(`The reference stream takes no symbols`)
50 case `string`:
51 this.update(STRING_MARKER)
52 for (let i = 0; i < chunk.length; i++) {
53 const code = chunk.charCodeAt(i)
54 this.writeByte(code & 0xff)
55 this.writeByte((code >>> 8) & 0xff)
56 }
57 return
58 case `number`:
59 dv.setFloat64(0, chunk, true) // fixed little-endian
60 this.writeByte(u8[0]!)
61 this.writeByte(u8[1]!)
62 this.writeByte(u8[2]!)
63 this.writeByte(u8[3]!)
64 this.writeByte(u8[4]!)
65 this.writeByte(u8[5]!)
66 this.writeByte(u8[6]!)
67 this.writeByte(u8[7]!)
68 return
69 case `bigint`: {
70 let value = chunk
71 if (value < 0n) {
72 value = -value
73 this.update(NEG_BIG_INT_MARKER)
74 } else {
75 this.update(BIG_INT_MARKER)
76 }
77 while (value > 0n) {
78 this.writeByte(Number(value & 0xffn))
79 value >>= 8n
80 }
81 if (chunk === 0n) this.writeByte(0)
82 return
83 }
84 default:
85 throw new TypeError(`Unsupported input type: ${typeof chunk}`)
86 }
87 }
88​
89 digest(): number {
90 if (this.carryBytes > 0) {
91 let k1 = this.carry >>> 0
92 k1 = Math.imul(k1, 0xcc9e2d51)
93 k1 = (k1 << 15) | (k1 >>> 17)
94 k1 = Math.imul(k1, 0x1b873593)
95 this.hash ^= k1
96 }
97​
98 this.hash ^= this.length
99 this.hash ^= this.hash >>> 16
100 this.hash = Math.imul(this.hash, 0x85ebca6b)
101 this.hash ^= this.hash >>> 13
102 this.hash = Math.imul(this.hash, 0xc2b2ae35)
103 this.hash ^= this.hash >>> 16
104​
105 return this.hash >>> 0
106 }
108​
109declareOracle({ model: [ReferenceByteStream] })