You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix: compare Uint8Arrays by content for proper binary ID equality
Fixes `eq` function and hash indexing to compare Uint8Arrays/Buffers by content
instead of reference, enabling proper ULID comparisons in WHERE clauses.
Changes:
- Hash small Uint8Arrays (≤128 bytes) by content in db-ivm for better indexing
- Compare Uint8Arrays by content in eq operator via areValuesEqual() function
- Add comprehensive tests for Uint8Array equality comparison
Fix Uint8Array/Buffer comparison to work by content instead of reference. This enables proper equality checks for binary IDs like ULIDs in WHERE clauses using the `eq` function.
Copy file name to clipboardExpand all lines: packages/db-ivm/tests/utils.test.ts
+40-3Lines changed: 40 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -299,7 +299,8 @@ describe(`hash`, () => {
299
299
expect(hash4).not.toBe(hash6)// Different Symbol content should have different hash
300
300
})
301
301
302
-
it(`should hash Buffers, Uint8Arrays and File objects by reference`,()=>{
302
+
it(`should hash small Buffers and Uint8Arrays by content`,()=>{
303
+
// Small buffers (≤128 bytes) are hashed by content for proper equality comparisons
303
304
constbuffer1=Buffer.from([1,2,3])
304
305
constbuffer2=Buffer.from([1,2,3])
305
306
constbuffer3=Buffer.from([1,2,3,4])
@@ -309,7 +310,7 @@ describe(`hash`, () => {
309
310
consthash3=hash(buffer3)
310
311
311
312
expect(typeofhash1).toBe(hashType)
312
-
expect(hash1).not.toBe(hash2)// Same content but different buffer instances have a different hash because it would be too costly to deeply hash buffers
313
+
expect(hash1).toBe(hash2)// Same content = same hash for small buffers
313
314
expect(hash1).not.toBe(hash3)// Different Buffer content should have different hash
314
315
expect(hash1).toBe(hash(buffer1))// Hashing same buffer should return same hash
315
316
@@ -322,10 +323,46 @@ describe(`hash`, () => {
322
323
consthash6=hash(uint8Array3)
323
324
324
325
expect(typeofhash4).toBe(hashType)
325
-
expect(hash4).not.toBe(hash5)// Same content but different uint8Array instances have a different hash because it would be too costly to deeply hash uint8Arrays
326
+
expect(hash4).toBe(hash5)// Same content = same hash for small Uint8Arrays
326
327
expect(hash4).not.toBe(hash6)// Different uint8Array content should have different hash
327
328
expect(hash4).toBe(hash(uint8Array1))// Hashing same uint8Array should return same hash
329
+
})
330
+
331
+
it(`should hash large Buffers, Uint8Arrays and File objects by reference`,()=>{
332
+
// Large buffers (>128 bytes) are hashed by reference to avoid performance costs
333
+
constlargeBuffer1=Buffer.alloc(300)
334
+
constlargeBuffer2=Buffer.alloc(300)
335
+
336
+
// Fill with same content
337
+
for(leti=0;i<300;i++){
338
+
largeBuffer1[i]=i%256
339
+
largeBuffer2[i]=i%256
340
+
}
341
+
342
+
consthash1=hash(largeBuffer1)
343
+
consthash2=hash(largeBuffer2)
344
+
345
+
expect(typeofhash1).toBe(hashType)
346
+
expect(hash1).not.toBe(hash2)// Same content but different instances = different hash for large buffers
347
+
expect(hash1).toBe(hash(largeBuffer1))// Hashing same buffer should return same hash
348
+
349
+
constlargeUint8Array1=newUint8Array(300)
350
+
constlargeUint8Array2=newUint8Array(300)
351
+
352
+
// Fill with same content
353
+
for(leti=0;i<300;i++){
354
+
largeUint8Array1[i]=i%256
355
+
largeUint8Array2[i]=i%256
356
+
}
357
+
358
+
consthash3=hash(largeUint8Array1)
359
+
consthash4=hash(largeUint8Array2)
360
+
361
+
expect(typeofhash3).toBe(hashType)
362
+
expect(hash3).not.toBe(hash4)// Same content but different instances = different hash for large Uint8Arrays
363
+
expect(hash3).toBe(hash(largeUint8Array1))// Hashing same uint8Array should return same hash
328
364
365
+
// Files are always hashed by reference regardless of size
0 commit comments