Skip to content

Commit

Permalink
fix: fixes #858
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleriva committed Dec 11, 2024
1 parent fc68724 commit 3af29ed
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
11 changes: 4 additions & 7 deletions packages/orama/src/methods/insert.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AnyOrama, PartialSchemaDeep, SortValue, TypedDocument } from '../types.js'
import { isArrayType, isGeoPointType, isVectorType } from '../components.js'
import { isAsyncFunction } from '../utils.js'
import { isAsyncFunction, sleep } from '../utils.js'
import { runMultipleHook, runSingleHook } from '../components/hooks.js'
import { trackInsertion } from '../components/sync-blocking-checker.js'
import { createError } from '../errors.js'
Expand Down Expand Up @@ -316,8 +316,6 @@ async function innerInsertMultipleAsync<T extends AnyOrama>(

const processAllBatches = async (): Promise<void> => {
let currentIndex = 0
const sab = new SharedArrayBuffer(4)
const ia = new Int32Array(sab)

while (currentIndex < docs.length) {
const startTime = Date.now()
Expand All @@ -327,7 +325,7 @@ async function innerInsertMultipleAsync<T extends AnyOrama>(
const elapsedTime = Date.now() - startTime
const waitTime = timeout - elapsedTime
if (waitTime > 0) {
Atomics.wait(ia, 0, 0, waitTime)
sleep(waitTime)
}
}
}
Expand Down Expand Up @@ -369,8 +367,6 @@ function innerInsertMultipleSync<T extends AnyOrama>(

function processAllBatches() {
const startTime = Date.now()
const sab = new SharedArrayBuffer(4)
const ia = new Int32Array(sab)

// eslint-disable-next-line no-constant-condition
while (true) {
Expand All @@ -382,7 +378,7 @@ function innerInsertMultipleSync<T extends AnyOrama>(
if (elapsedTime >= timeout) {
const remainingTime = timeout - (elapsedTime % timeout)
if (remainingTime > 0) {
Atomics.wait(ia, 0, 0, remainingTime)
sleep(remainingTime)
}
}
}
Expand All @@ -398,6 +394,7 @@ function innerInsertMultipleSync<T extends AnyOrama>(
return ids
}


export function innerInsertMultiple<T extends AnyOrama>(
orama: T,
docs: PartialSchemaDeep<TypedDocument<T>>[],
Expand Down
28 changes: 28 additions & 0 deletions packages/orama/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,3 +442,31 @@ export function setUnion<V>(set1: Set<V> | undefined, set2: Set<V>) {
}
return new Set([...set1, ...set2]);
}

// This code is taken from https://github.com/davidmarkclements/atomic-sleep, MIT licensed at the time of commit b8149d3ca276c84a54fa8fa1478f9cc79aabc15a.
// All credits go to the original author (David Mark Clements, https://github.com/davidmarkclements).
export function sleep(ms: number) {
if (typeof SharedArrayBuffer !== 'undefined' && typeof Atomics !== 'undefined') {
const nil = new Int32Array(new SharedArrayBuffer(4))
const valid = ms > 0 && ms < Infinity
if (valid === false) {
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
throw TypeError('sleep: ms must be a number')
}
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
}

Atomics.wait(nil, 0, 0, Number(ms))

} else {
const valid = ms > 0 && ms < Infinity
if (valid === false) {
if (typeof ms !== 'number' && typeof ms !== 'bigint') {
throw TypeError('sleep: ms must be a number')
}
throw RangeError('sleep: ms must be a number that is greater than 0 but less than Infinity')
}
const target = Date.now() + Number(ms)
while (target > Date.now()){ /* empty */ }
}
}

0 comments on commit 3af29ed

Please sign in to comment.