Skip to content
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

Feat: Full text search and update wa-sqlite #7

Merged
merged 34 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
8ea0c3a
Work around stack overflow in debug build.
shoestringresearch Dec 8, 2023
4897e2d
Handle 64-bit arguments with Emscripten legalization.
shoestringresearch Dec 8, 2023
83b377e
Merge pull request #135 from rhashimoto/int64
rhashimoto Dec 8, 2023
253d5e6
Export all SQLite3 API functions, plus Emscripten utilities.
shoestringresearch Dec 8, 2023
4aa5a32
Merge pull request #136 from rhashimoto/augment-exports
rhashimoto Dec 9, 2023
1928aef
Update issue templates
rhashimoto Jan 15, 2024
675294b
Update follow-redirects per Dependabot.
shoestringresearch Jan 16, 2024
c1f7e35
Merge pull request #144 from rhashimoto/dependabot
rhashimoto Jan 16, 2024
60ec4a2
Fix #143. Handle detached buffers in IDBBatchAtomicVFS.
shoestringresearch Jan 18, 2024
dbd6a6d
Bump package version.
shoestringresearch Jan 18, 2024
0477ca5
Merge pull request #146 from rhashimoto/bug-143
rhashimoto Jan 18, 2024
60b6317
Fix #143 for Safari (no ArrayBuffer.prototype.detached).
shoestringresearch Jan 19, 2024
be1b8f2
Bump package version.
shoestringresearch Jan 19, 2024
390744d
Merge pull request #148 from rhashimoto/bug-143-again
rhashimoto Jan 19, 2024
e014f2f
Update package per Dependabot.
shoestringresearch Feb 29, 2024
cb83046
Remove prepack script.
shoestringresearch Feb 29, 2024
93d7498
Bump version.
shoestringresearch Feb 29, 2024
f1f8550
Merge pull request #160 from rhashimoto/remove-prepack-build
rhashimoto Feb 29, 2024
f847e6a
Rebuild on changes to Javascript libraries
joeyates Mar 23, 2024
1d81695
Bugfix: Avoid dot notation in minified Javascript
joeyates Mar 24, 2024
cb44d0d
Merge pull request #162 from joeyates/feature/rebuild-on-js-changes
rhashimoto Mar 25, 2024
d2a2f1c
Merge pull request #163 from joeyates/fix/avoid-dot-notation-with-man…
rhashimoto Mar 25, 2024
c4c2a46
Dependency update for dependabot.
shoestringresearch Mar 31, 2024
e1ebcdc
Bump package version.
shoestringresearch Mar 31, 2024
ca2084c
Merge pull request #166 from rhashimoto/dependabot
rhashimoto Mar 31, 2024
bb0293b
Build fts targets
mugikhan Apr 16, 2024
0d0c35b
Revert stack size flag
mugikhan Apr 16, 2024
53be4d6
Revert formatting changes
mugikhan Apr 16, 2024
153621e
Remove unused intermediate files
mugikhan Apr 16, 2024
9be5d78
Add fts flag to standard build.
mugikhan Apr 17, 2024
ccbe0f6
Clean up unused code
mugikhan Apr 17, 2024
10dd935
Merge remote-tracking branch 'upstream/master' into chore/update-wa-s…
mugikhan Apr 17, 2024
87fbd0c
Lock sinon version
mugikhan Apr 17, 2024
0077436
Bump minor version
mugikhan Apr 17, 2024
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
6 changes: 6 additions & 0 deletions .changeset/stupid-jokes-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@journeyapps/wa-sqlite": minor
---

Added full text search (FTS5) support out of the box by default. There should be no configuration required.
Updated from upstream changes
65 changes: 61 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ CFILES = \
libvfs.c \
$(CFILES_EXTRA)

JSFILES = \
src/libauthorizer.js \
src/libfunction.js \
src/libmodule.js \
src/libprogress.js \
src/libvfs.js

vpath %.c src
vpath %.c deps
vpath %.c deps/$(SQLITE_VERSION)
Expand Down Expand Up @@ -56,7 +63,7 @@ EMFLAGS_COMMON = \

EMFLAGS_DEBUG = \
-s ASSERTIONS=1 \
-g -Oz \
-g -Oz -Oz \
$(EMFLAGS_COMMON)

EMFLAGS_DIST = \
Expand Down Expand Up @@ -104,6 +111,7 @@ WASQLITE_DEFINES = \
-DSQLITE_THREADSAFE=0 \
-DSQLITE_USE_ALLOCA \
-DSQLITE_ENABLE_BATCH_ATOMIC_WRITE \
-DSQLITE_ENABLE_FTS5 \
$(WASQLITE_EXTRA_DEFINES)

# directories
Expand All @@ -112,11 +120,11 @@ all: dist

.PHONY: clean
clean:
rm -rf dist dist-xl debug tmp
rm -rf dist debug tmp

.PHONY: spotless
spotless:
rm -rf dist dist-xl debug tmp deps cache
rm -rf dist debug tmp deps cache

## cache
.PHONY: clean-cache
Expand Down Expand Up @@ -194,6 +202,27 @@ debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG) $(RS_DEBUG_BC) $(EXPORTED_FUNCTION
$(RS_WASM_TGT_DIR)/debug/deps/*.bc \
$(OBJ_FILES_DEBUG) *.o -o $@

## Debug FTS builds
# .PHONY: debug
# debug: debug/wa-sqlite.mjs debug/wa-sqlite-async.mjs

# debug/wa-sqlite.mjs: $(OBJ_FILES_DEBUG_FTS) $(RS_DEBUG_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
# mkdir -p debug
# $(EMCC) $(EMFLAGS_DEBUG) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(RS_WASM_TGT_DIR)/debug/deps/*.bc \
# $(OBJ_FILES_DEBUG_FTS) *.o -o $@

# debug/wa-sqlite-async.mjs: $(OBJ_FILES_DEBUG_FTS) $(RS_DEBUG_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
# mkdir -p debug
# $(EMCC) $(EMFLAGS_DEBUG) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(EMFLAGS_ASYNCIFY_DEBUG) \
# $(RS_WASM_TGT_DIR)/debug/deps/*.bc \
# $(OBJ_FILES_DEBUG_FTS) *.o -o $@

## dist
.PHONY: clean-dist
clean-dist:
Expand All @@ -220,4 +249,32 @@ dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST) $(RS_RELEASE_BC) $(EXPORTED_FUNCTION
$(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
$(OBJ_FILES_DIST) -o $@

FORCE: ;
FORCE: ;

# FTS builds
# .PHONY: clean-dist
# clean-dist:
# rm -rf dist

# .PHONY: dist
# dist: dist/wa-sqlite.mjs dist/wa-sqlite-async.mjs

# dist/wa-sqlite.mjs: $(OBJ_FILES_DIST_FTS) $(RS_RELEASE_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS)
# mkdir -p dist
# $(EMCC) $(EMFLAGS_DIST) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
# $(OBJ_FILES_DIST_FTS) -o $@

# dist/wa-sqlite-async.mjs: $(OBJ_FILES_DIST_FTS) $(RS_RELEASE_BC) $(EXPORTED_FUNCTIONS) $(EXPORTED_RUNTIME_METHODS) $(ASYNCIFY_IMPORTS)
# mkdir -p dist
# $(EMCC) $(EMFLAGS_DIST) \
# $(EMFLAGS_INTERFACES) \
# $(EMFLAGS_LIBRARIES) \
# $(EMFLAGS_ASYNCIFY_DIST) \
# $(CFLAGS_DIST) \
# $(RS_WASM_TGT_DIR)/wasm/deps/*.bc \
# $(OBJ_FILES_DIST_FTS) -o $@

# FORCE: ;
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Here are the build steps:
* `git clone [email protected]:rhashimoto/wa-sqlite.git`
* `cd wa-sqlite`
* `yarn install`
* `make` (or `yarn prepack`)
* `make`

The default build produces ES6 modules + WASM, [synchronous and asynchronous](https://github.com/rhashimoto/wa-sqlite/issues/7) (using Asyncify) in `dist/`.

Expand All @@ -51,7 +51,7 @@ git submodule init
```

```bash
git submodule update --recursive
git submodule update --init --recursive
```

```bash
Expand Down
268 changes: 132 additions & 136 deletions dist/wa-sqlite-async.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite-async.wasm
Binary file not shown.
261 changes: 129 additions & 132 deletions dist/wa-sqlite.mjs

Large diffs are not rendered by default.

Binary file modified dist/wa-sqlite.wasm
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"karma-jasmine": "^5.1.0",
"monaco-editor": "^0.34.1",
"puppeteer": "^19.6.3",
"sinon": "^15.0.1",
"sinon": "15.0.1",
"typedoc": "^0.22.11",
"typescript": "^4.2.4"
},
Expand Down
7 changes: 4 additions & 3 deletions scripts/docker-setup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y
source ~/.bashrc
rustup component add rust-src --toolchain nightly-2023-08-28-aarch64-unknown-linux-gnu -y
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y && \
. $HOME/.cargo/env && \
rustup toolchain install nightly-2023-08-28-aarch64-unknown-linux-gnu && \
rustup component add rust-src --toolchain nightly-2023-08-28-aarch64-unknown-linux-gnu

sudo apt-get update -y
sudo apt-get install -y tclsh
Expand Down
104 changes: 56 additions & 48 deletions src/libvfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,75 @@

// sqlite3_io_methods javascript handlers
// 64-bit integer parameters are passed by pointer.
extern int vfsClose(sqlite3_file* file);
extern int vfsRead(sqlite3_file* file, void* pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsWrite(sqlite3_file* file, const void* pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsTruncate(sqlite3_file* file, sqlite3_int64 size);
extern int vfsSync(sqlite3_file* file, int flags);
extern int vfsFileSize(sqlite3_file* file, sqlite3_int64* pSize);
extern int vfsLock(sqlite3_file* file, int flags);
extern int vfsUnlock(sqlite3_file* file, int flags);
extern int vfsCheckReservedLock(sqlite3_file* file, int* pResOut);
extern int vfsFileControl(sqlite3_file* file, int flags, void* pOut);
extern int vfsSectorSize(sqlite3_file* file);
extern int vfsDeviceCharacteristics(sqlite3_file* file);
extern int vfsClose(sqlite3_file *file);
extern int vfsRead(sqlite3_file *file, void *pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsWrite(sqlite3_file *file, const void *pData, int iAmt, sqlite3_int64 iOffset);
extern int vfsTruncate(sqlite3_file *file, sqlite3_int64 size);
extern int vfsSync(sqlite3_file *file, int flags);
extern int vfsFileSize(sqlite3_file *file, sqlite3_int64 *pSize);
extern int vfsLock(sqlite3_file *file, int flags);
extern int vfsUnlock(sqlite3_file *file, int flags);
extern int vfsCheckReservedLock(sqlite3_file *file, int *pResOut);
extern int vfsFileControl(sqlite3_file *file, int flags, void *pOut);
extern int vfsSectorSize(sqlite3_file *file);
extern int vfsDeviceCharacteristics(sqlite3_file *file);

extern int vfsOpen(sqlite3_vfs* vfs, const char *zName, sqlite3_file* file, int flags, int *pOutFlags);
extern int vfsDelete(sqlite3_vfs* vfs, const char *zName, int syncDir);
extern int vfsAccess(sqlite3_vfs* vfs, const char *zName, int flags, int *pResOut);
extern int vfsOpen(sqlite3_vfs *vfs, const char *zName, sqlite3_file *file, int flags, int *pOutFlags);
extern int vfsDelete(sqlite3_vfs *vfs, const char *zName, int syncDir);
extern int vfsAccess(sqlite3_vfs *vfs, const char *zName, int flags, int *pResOut);

// This is undefined in the WASM linker step if not specified
extern int __rust_no_alloc_shim_is_unstable = 0;
extern int sqlite3_powersync_init(sqlite3 *db, char **pzErrMsg,
const sqlite3_api_routines *pApi);
const sqlite3_api_routines *pApi);

static int xOpen(sqlite3_vfs* vfs, const char* zName, sqlite3_file* file, int flags, int* pOutFlags) {
static int xOpen(sqlite3_vfs *vfs, const char *zName, sqlite3_file *file, int flags, int *pOutFlags)
{
static sqlite3_io_methods io_methods = {
1,
vfsClose,
vfsRead,
vfsWrite,
vfsTruncate,
vfsSync,
vfsFileSize,
vfsLock,
vfsUnlock,
vfsCheckReservedLock,
vfsFileControl,
vfsSectorSize,
vfsDeviceCharacteristics
};
1,
vfsClose,
vfsRead,
vfsWrite,
vfsTruncate,
vfsSync,
vfsFileSize,
vfsLock,
vfsUnlock,
vfsCheckReservedLock,
vfsFileControl,
vfsSectorSize,
vfsDeviceCharacteristics};
file->pMethods = &io_methods;

return vfsOpen(vfs, zName, file, flags, pOutFlags);
}

static int xFullPathname(sqlite3_vfs* vfs, const char* zName, int nOut, char* zOut) {
static int xFullPathname(sqlite3_vfs *vfs, const char *zName, int nOut, char *zOut)
{
strncpy(zOut, zName, nOut);
return SQLITE_OK;
}

static int xCurrentTime(sqlite3_vfs* vfs, double* pJulianDay) {
static int xCurrentTime(sqlite3_vfs *vfs, double *pJulianDay)
{
// UNIX epoch 1/1/1970 is Julian day 2440587.5
static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000;
static const sqlite3_int64 unixEpoch = 24405875 * (sqlite3_int64)8640000;
struct timeval sNow;
gettimeofday(&sNow, 0);
sqlite3_int64 julianMillis = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000;
sqlite3_int64 julianMillis = unixEpoch + 1000 * (sqlite3_int64)sNow.tv_sec + sNow.tv_usec / 1000;
*pJulianDay = julianMillis / 86400000.0;
return SQLITE_OK;
}

const int EMSCRIPTEN_KEEPALIVE register_vfs(
const char* zName,
int mxPathName,
int makeDefault,
sqlite3_vfs** ppVFS) {
sqlite3_vfs* vfs = *ppVFS = (sqlite3_vfs*)sqlite3_malloc(sizeof(sqlite3_vfs));
if (!vfs) {
const char *zName,
int mxPathName,
int makeDefault,
sqlite3_vfs **ppVFS)
{
sqlite3_vfs *vfs = *ppVFS = (sqlite3_vfs *)sqlite3_malloc(sizeof(sqlite3_vfs));
if (!vfs)
{
return SQLITE_NOMEM;
}

Expand All @@ -85,9 +89,9 @@ const int EMSCRIPTEN_KEEPALIVE register_vfs(
vfs->xAccess = vfsAccess;
vfs->xFullPathname = xFullPathname;
vfs->xCurrentTime = xCurrentTime;

// Get remaining functionality from the default VFS.
sqlite3_vfs* defer = sqlite3_vfs_find(0);
sqlite3_vfs *defer = sqlite3_vfs_find(0);
#define COPY_FIELD(NAME) vfs->NAME = defer->NAME
COPY_FIELD(xDlOpen);
COPY_FIELD(xDlError);
Expand All @@ -99,22 +103,26 @@ const int EMSCRIPTEN_KEEPALIVE register_vfs(
#undef COPY_FIELD

const int result = sqlite3_vfs_register(vfs, makeDefault);
if (result != SQLITE_OK) {
if (result != SQLITE_OK)
{
*ppVFS = 0;
sqlite3_free(vfs);
}
return result;
}

void* EMSCRIPTEN_KEEPALIVE getSqliteFree() {
void *EMSCRIPTEN_KEEPALIVE getSqliteFree()
{
return sqlite3_free;
}

int main() {
int main()
{
sqlite3_initialize();
return 0;
}

int setup_powersync() {
return sqlite3_auto_extension((void (*)(void)) &sqlite3_powersync_init);
int setup_powersync()
{
return sqlite3_auto_extension((void (*)(void)) & sqlite3_powersync_init);
}
2 changes: 1 addition & 1 deletion src/libvfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const vfs_methods = {
vfs['handleAsync'] = Asyncify.handleAsync;
}

const mxPathName = vfs.mxPathName ?? 64;
const mxPathName = vfs['mxPathName'] ?? 64;
const out = Module['_malloc'](4);
const result = ccall('register_vfs', 'number', ['string', 'number', 'number', 'number'],
[vfs.name, mxPathName, makeDefault ? 1 : 0, out]);
Expand Down
2 changes: 1 addition & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1692,4 +1692,4 @@ declare module 'wa-sqlite/src/examples/tag.js' {
* @returns {function(TemplateStringsArray, ...any): Promise<object[]>}
*/
export function tag(sqlite3: any, db: number): (arg0: TemplateStringsArray, ...args: any[]) => Promise<object[]>;
}
}
Loading
Loading