8000 feat: browser prepared-query limit setters (minigraf#872) by adityamukho · Pull Request #22 · project-minigraf/minigraf-wasm · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
include:
- { package: browser, os: ubuntu-24.04 }
- { package: wasi, os: ubuntu-latest }
- { package: wasi, os: macos-14 }
- { package: wasi, os: macos-15 }
- { package: wasi, os: windows-latest }
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/verify-published.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
include:
- { package: browser, os: ubuntu-24.04 }
- { package: wasi, os: ubuntu-latest }
- { package: wasi, os: macos-14 }
- { package: wasi, os: macos-15 }
- { package: wasi, os: windows-latest }
steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ crate-type = ["cdylib", "rlib"]

[dependencies]
# Unreleased v3 core (format v8). Switches to version = "=3.0.0" at the v3.0.0 release.
minigraf = { git = "https://github.com/project-minigraf/minigraf", rev = "02243f1656eedd987611a9babdfee4a8c6e22b6b", features = ["browser"] }
minigraf = { git = "https://github.com/project-minigraf/minigraf", rev = "d9755c02d19a1538ca03fd0b80c56a752bbc6504", features = ["browser"] }

[features]
browser = ["minigraf/browser"]
Expand Down
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,21 @@ const pq = db.prepare('(query [:find ?n :where [?e :age $age] [?e :name ?n]])')
JSON.parse(pq.execute({ age: 30 })).results
```

`withMaxDerivedFacts(n)`, `withQueryMemoryLimit(bytes)` and `withMaxIterations(n)` each
return a new `BrowserPreparedQuery` with that limit; the one they are called on is
unchanged, and the new one keeps the database open as it does. An allowed keyword in
the query text still overrides them. The argument is a non-negative integer `number`; at
or above 2^32 − 1 (the browser's largest `usize`) it means no limit, and a negative,
fractional or non-finite number throws `[API-017]`. These setters are the browser's way
to set a query memory limit: `BrowserDb` takes no options, so the default override list
applies and `:query-memory-limit` in query text throws `[API-025]`.

```js
const reach = db.prepare('(query [:find ?x ?y :where (reach ?x ?y)])')
const small = reach.withMaxDerivedFacts(10000).withQueryMemoryLimit(16 * 1024 * 1024)
small.execute({}) // [INT-020] or [QRY-012] past a limit; reach is unchanged
```

### User functions

`db.registerPredicate(name, fn)` adds a predicate, called in a query as `[(name ?v)]`.
Expand Down Expand Up @@ -107,9 +122,11 @@ await db.execute('(query [:find (product ?a) :where [?e :age ?a]])')

`BrowserDb` keeps its pages in memory and writes them to IndexedDB; it has no `.graph`
file, WAL or file lock. So the file-backed options of the other bindings
(`read_only`, `allow_unlocked`, `page_cache_size`, the WAL options) do not apply, and
the raw log writer (`LogWriter`) is not built for wasm32. The fact log is not exposed
in the browser. To move data between a browser and a native database, use
(`read_only`, `allow_unlocked`, `page_cache_size`, the WAL options) do not apply.
`BrowserDb` takes no open options at all: the override list (`allow_overrides`), the
query limits and `transaction_chunk_facts` keep their defaults, and a prepared query's
limits are set with its setters. The raw log writer (`LogWriter`) is not built for
wasm32, and the fact log is not exposed in the browser. To move data between a browser and a native database, use
`exportGraph()` / `importGraph()`.

## WASI (`@minigraf/wasi`)
Expand Down
17 changes: 17 additions & 0 deletions consumer-test/browser/contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ async function run() {
}
check(typeof missing === 'string' && missing.startsWith('[API-022] '), `a missing slot is API-022 (${missing})`)

// Limit setters (minigraf#872): each returns a new prepared query and
// leaves the receiver unchanged.
await mem.execute('(transact [[:n1 :next :n2] [:n2 :next :n3] [:n3 :next :n4]])')
await mem.execute('(rule [(reach ?x ?y) [?x :next ?y]])')
await mem.execute('(rule [(reach ?x ?y) [?x :next ?z] (reach ?z ?y)])')
const reach = mem.prepare('(query [:find ?x ?y :where (reach ?x ?y)])')
const limited = reach.withMaxIterations(1)
let rounds
try {
limited.execute({})
} catch (e) {
rounds = e
}
check(typeof rounds === 'string' && rounds.startsWith('[INT-020] '), `withMaxIterations(1) limits the new query (${rounds})`)
check(JSON.parse(reach.execute({})).results.length === 6, 'the receiver of withMaxIterations is unchanged')
check(JSON.parse(limited.withMaxIterations(2 ** 32).execute({})).results.length === 6, 'a setter at usize::MAX or more is no limit')

// Sets (minigraf#316): a set value comes back as an array, and binds as a
// JS Set or the typed form.
await mem.execute('(transact [[:alice :tags #{1 "x"}]])')
Expand Down
Loading
0