-
Notifications
You must be signed in to change notification settings - Fork 96
feat(zero-client): expo-sqlite storage #4669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
08f94ae
@austinm911's changes to add first-class expo integrations
austinm911 9b3e643
fix: only have zero-expo
0xcadams 3c3c2ba
feat: added expo sqlite
0xcadams 0c8bbfb
fix: remove unused prepared statements
0xcadams e0c0bee
fix: entrypoint
0xcadams 0b78396
fix: cleanup
0xcadams 5e867f8
Merge branch 'main' into 0xcadams/expo
0xcadams 0e0e73c
fix: add back WITHOUT ROWID
0xcadams 5fc9311
Merge remote-tracking branch 'origin/0xcadams/expo' into 0xcadams/expo
0xcadams edaca04
chore: fix global types
0xcadams 59e6b7c
chore: update peer dep version
0xcadams de0b1a9
fix: move to use tx/savepoint for reads
0xcadams 99c12e8
test: simplify bench
0xcadams e0b779e
fix: moved defaults to be config and fixed test config
0xcadams c726a5c
Merge branch 'main' into 0xcadams/expo
0xcadams caf8fc1
fix: test config
0xcadams f9ff100
Merge branch 'main' into 0xcadams/expo
0xcadams 1a0de26
fix: move to use read pool and single write conn
0xcadams fd47385
Merge remote-tracking branch 'origin/0xcadams/expo' into 0xcadams/expo
0xcadams 1320e5c
Merge remote-tracking branch 'origin/main' into 0xcadams/expo
0xcadams 891d60a
fix: remove readonly flag
0xcadams 3d536a4
Merge branch 'main' into 0xcadams/expo
0xcadams b430406
Merge branch 'main' into 0xcadams/expo
0xcadams 0710a6d
fix: update based on @arv feedback
0xcadams 5eada28
fix: rename
0xcadams baae8b6
Merge branch 'main' into 0xcadams/expo
0xcadams 57d26d3
Merge branch 'main' into 0xcadams/expo
0xcadams c9b14ce
Merge branch 'main' into 0xcadams/expo
0xcadams 589d170
Merge branch 'main' into 0xcadams/expo
0xcadams File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,3 +8,4 @@ perf/index.js.map | |
perf/index.js | ||
tool/bump/bump | ||
.vscode/launch.json | ||
src/kv/*.db* |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* eslint-disable no-console */ | ||
import sqlite3 from '@rocicorp/zero-sqlite3'; | ||
import fs from 'fs'; | ||
import path from 'path'; | ||
import {SQLiteDatabaseManager} from './sqlite-store.ts'; | ||
|
||
export const getTestSQLiteDatabaseManager = (logging: boolean = false) => | ||
new SQLiteDatabaseManager({ | ||
open: name => { | ||
const filename = path.resolve(__dirname, `${name}.db`); | ||
// this cannot be :memory: because multiple read connections must access | ||
// the same database | ||
const db = sqlite3(filename); | ||
return { | ||
close: () => { | ||
db.close(); | ||
}, | ||
destroy: () => { | ||
db.close(); | ||
if (fs.existsSync(filename)) { | ||
fs.unlinkSync(filename); | ||
} | ||
}, | ||
prepare: (sql: string) => { | ||
const stmt = db.prepare(sql); | ||
if (logging) { | ||
console.log('prepare', sql); | ||
} | ||
return { | ||
all: <T>(...params: unknown[]): T[] => { | ||
const result = params.length ? stmt.all(...params) : stmt.all(); | ||
if (logging) { | ||
console.log('all', sql, params, result); | ||
} | ||
return result as T[]; | ||
}, | ||
run: (...params: unknown[]): void => { | ||
if (logging) { | ||
console.log('run', sql, params); | ||
} | ||
if (params.length) { | ||
stmt.run(...params); | ||
} else { | ||
stmt.run(); | ||
} | ||
}, | ||
finalize: () => { | ||
// no-op | ||
}, | ||
}; | ||
}, | ||
}; | ||
}, | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
import {afterAll, bench, describe, expect} from 'vitest'; | ||
import {withRead, withWrite} from '../with-transactions.ts'; | ||
import {getTestSQLiteDatabaseManager} from './sqlite-store-test-util.ts'; | ||
import {createSQLiteStore} from './sqlite-store.ts'; | ||
|
||
const walSQLite3DatabaseManager = getTestSQLiteDatabaseManager(); | ||
const createWalStore = createSQLiteStore(walSQLite3DatabaseManager); | ||
const walStore = createWalStore('bench-wal', { | ||
readPoolSize: 2, | ||
journalMode: 'WAL', | ||
synchronous: 'NORMAL', | ||
readUncommitted: false, | ||
}); | ||
|
||
const defaultSQLite3DatabaseManager = getTestSQLiteDatabaseManager(); | ||
const createDefaultStore = createSQLiteStore(defaultSQLite3DatabaseManager); | ||
const defaultStore = createDefaultStore('bench-default', { | ||
readPoolSize: 2, | ||
synchronous: 'NORMAL', | ||
readUncommitted: false, | ||
}); | ||
|
||
afterAll(() => { | ||
walSQLite3DatabaseManager.clearAllStoresForTesting(); | ||
defaultSQLite3DatabaseManager.clearAllStoresForTesting(); | ||
}); | ||
|
||
describe('sqlite tx', () => { | ||
bench( | ||
`default journal mode`, | ||
async () => { | ||
await withWrite(defaultStore, async wt => { | ||
expect(await wt.get('bar')).equal(undefined); | ||
await wt.put('bar', 'baz'); | ||
expect(await wt.get('bar')).equal('baz'); | ||
await wt.del('bar'); | ||
expect(await wt.get('bar')).equal(undefined); | ||
}); | ||
}, | ||
{ | ||
throws: true, | ||
}, | ||
); | ||
|
||
bench( | ||
`WAL journal mode`, | ||
async () => { | ||
await withWrite(walStore, async wt => { | ||
expect(await wt.get('bar')).equal(undefined); | ||
await wt.put('bar', 'baz'); | ||
expect(await wt.get('bar')).equal('baz'); | ||
await wt.del('bar'); | ||
expect(await wt.get('bar')).equal(undefined); | ||
}); | ||
}, | ||
{ | ||
throws: true, | ||
}, | ||
); | ||
}); | ||
|
||
describe('sqlite write contention', () => { | ||
bench( | ||
`default journal mode`, | ||
async () => { | ||
await withWrite(defaultStore, async wt => { | ||
await wt.put('foo', 'bar'); | ||
}); | ||
|
||
const readP1 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP2 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP3 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP4 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP5 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const writeP = withWrite(defaultStore, async wt => { | ||
await wt.put('foo', 'bar2'); | ||
}); | ||
|
||
await Promise.all([readP1, readP2, readP3, readP4, readP5, writeP]); | ||
}, | ||
{ | ||
throws: true, | ||
teardown: async () => { | ||
await withWrite(defaultStore, async wt => { | ||
await wt.del('foo'); | ||
}); | ||
}, | ||
}, | ||
); | ||
|
||
bench( | ||
`WAL journal mode`, | ||
async () => { | ||
await withWrite(walStore, async wt => { | ||
await wt.put('foo', 'bar'); | ||
}); | ||
|
||
const readP1 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP2 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP3 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP4 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP5 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const writeP = withWrite(walStore, async wt => { | ||
await wt.put('foo', 'bar2'); | ||
}); | ||
|
||
await Promise.all([readP1, readP2, readP3, readP4, readP5, writeP]); | ||
}, | ||
{ | ||
throws: true, | ||
setup: async () => { | ||
await withWrite(walStore, async wt => { | ||
await wt.del('foo'); | ||
}); | ||
}, | ||
}, | ||
); | ||
}); | ||
|
||
describe('plain read', () => { | ||
bench( | ||
`default journal mode`, | ||
async () => { | ||
const readP1 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP2 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP3 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP4 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP5 = withRead(defaultStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
|
||
await Promise.all([readP1, readP2, readP3, readP4, readP5]); | ||
}, | ||
{ | ||
throws: true, | ||
setup: async () => { | ||
await withWrite(defaultStore, async wt => { | ||
await wt.put('foo', 'bar'); | ||
}); | ||
}, | ||
}, | ||
); | ||
|
||
bench( | ||
`WAL journal mode`, | ||
async () => { | ||
const readP1 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP2 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP3 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP4 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
const readP5 = withRead(walStore, async rt => { | ||
expect(await rt.get('foo')).equal('bar'); | ||
}); | ||
|
||
await Promise.all([readP1, readP2, readP3, readP4, readP5]); | ||
}, | ||
{ | ||
throws: true, | ||
setup: async () => { | ||
await withWrite(walStore, async wt => { | ||
await wt.put('foo', 'bar'); | ||
}); | ||
}, | ||
}, | ||
); | ||
}); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it's technically doable via memory with some fancy options. But yeah, better to just keep it file based.