Skip to content

Commit e9163d3

Browse files
committed
Get worker VFS example to work
1 parent e78042b commit e9163d3

File tree

16 files changed

+346
-390
lines changed

16 files changed

+346
-390
lines changed

sqlite3/assets/wasm/bridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import_dart("xFileSize") extern int xFileSize(int, int *pSize);
2727
import_dart("xLock") extern int xLock(int, int);
2828
import_dart("xUnlock") extern int xUnlock(int, int);
2929
import_dart("xCheckReservedLock") extern int xCheckReservedLock(int, int *pResOut);
30+
import_dart("xDeviceCharacteristics") extern int xDeviceCharacteristics(int);
3031

3132
import_dart("function_xFunc") extern void dartXFunc(sqlite3_context *ctx,
3233
int nArgs,

sqlite3/assets/wasm/helpers.c

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,21 @@ int dartvfs_fileControl(sqlite3_file *file, int op, void *pArg) {
7979
return SQLITE_NOTFOUND;
8080
}
8181

82+
int dartvfs_deviceCharacteristics(sqlite3_file *file) {
83+
return xDeviceCharacteristics(DART_FD(file));
84+
}
85+
86+
int dartvfs_sectorSize(sqlite3_file *file) {
87+
// This is also the value of SQLITE_DEFAULT_SECTOR_SIZE, which would be picked if this function didn't exist.
88+
// We need this method because vfstrace does not support null callbacks.
89+
return 4096;
90+
}
91+
8292
static int dartvfs_open(sqlite3_vfs* vfs, sqlite3_filename zName, sqlite3_file* file,
8393
int flags, int *pOutFlags) {
8494
dart_vfs_file* dartFile = (dart_vfs_file*) file;
85-
memset(dartFile, sizeof(dart_vfs_file), 0);
95+
memset(dartFile, 0, sizeof(dart_vfs_file));
96+
dartFile->dart_fd = -1;
8697

8798
static sqlite3_io_methods methods = {
8899
.iVersion = 1,
@@ -95,15 +106,21 @@ static int dartvfs_open(sqlite3_vfs* vfs, sqlite3_filename zName, sqlite3_file*
95106
.xLock = &dartvfs_lock,
96107
.xUnlock = &dartvfs_unlock,
97108
.xCheckReservedLock = &dartvfs_checkReservedLock,
98-
.xFileControl = &dartvfs_fileControl
109+
.xFileControl = &dartvfs_fileControl,
110+
.xDeviceCharacteristics = &dartvfs_deviceCharacteristics,
111+
#ifdef SQLITE_ENABLE_VFSTRACE
112+
.xSectorSize = &dartvfs_sectorSize
113+
#else
114+
.xSectorSize = NULL
115+
#endif
99116
};
100117

101118
int *dartFileId = &dartFile->dart_fd;
102119

103120
// The xOpen call will also set the dart_fd field.
104121
int rc = xOpen((int) vfs->pAppData, zName, dartFileId, flags, pOutFlags);
105122

106-
if (*dartFileId != 0) {
123+
if (*dartFileId != -1) {
107124
// sqlite3 will call xClose() even if this open call returns an error if methods
108125
// are set. So, we only provide the methods if a file has actually been opened.
109126
dartFile->pMethods = &methods;
@@ -144,7 +161,7 @@ static int dartvfs_currentTimeInt64(sqlite3_vfs* vfs, sqlite3_int64* timeOut) {
144161

145162
SQLITE_API sqlite3_vfs* dart_sqlite3_register_vfs(const char* name, int dartId, int makeDefault) {
146163
sqlite3_vfs *vfs = calloc(1, sizeof(sqlite3_vfs));
147-
vfs->iVersion = 3;
164+
vfs->iVersion = 2;
148165
vfs->szOsFile = sizeof(dart_vfs_file);
149166
vfs->mxPathname = 1024;
150167
vfs->zName = name;
@@ -157,7 +174,20 @@ SQLITE_API sqlite3_vfs* dart_sqlite3_register_vfs(const char* name, int dartId,
157174
vfs->xSleep = &dartvfs_sleep;
158175
vfs->xCurrentTimeInt64 = &dartvfs_currentTimeInt64;
159176

177+
#ifdef SQLITE_ENABLE_VFSTRACE
178+
sqlite3_vfs_register(vfs, 0);
179+
180+
static const char* prefix = "trace_";
181+
static const int prefixLength = 6;
182+
char *traceName = malloc(strlen(name) + prefixLength);
183+
strcpy(traceName, prefix);
184+
strcpy(&traceName[prefixLength], name);
185+
186+
vfstrace_register(traceName, name, &dartvfs_trace_log1, NULL, makeDefault);
187+
#else
188+
// Just register the VFS as is.
160189
sqlite3_vfs_register(vfs, makeDefault);
190+
#endif
161191
return vfs;
162192
}
163193

sqlite3/example/web/worker.dart

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import 'dart:html';
2-
import 'dart:typed_data';
32

43
import 'package:js/js.dart';
54
import 'package:js/js_util.dart';
6-
import 'package:sqlite3/src/constants.dart';
7-
import 'package:sqlite3/src/vfs.dart';
85
import 'package:sqlite3/src/wasm/vfs/async_opfs/client.dart';
96
import 'package:sqlite3/src/wasm/vfs/async_opfs/worker.dart';
7+
import 'package:sqlite3/wasm.dart';
108

119
@JS()
1210
external bool get crossOriginIsolated;
@@ -33,28 +31,19 @@ void main() {
3331
// Now, wait for the worker to report that it has been initialized.
3432
await worker.onMessage.first;
3533

36-
final vfs = WasmVfs(workerOptions: options);
37-
final opened =
38-
vfs.xOpen(Sqlite3Filename('/test'), SqlFlag.SQLITE_OPEN_CREATE);
39-
final file = opened.file;
40-
print('opened file $file, outflags ${opened.outFlags}');
34+
final sqlite3 =
35+
await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.debug.wasm'));
36+
sqlite3.registerVirtualFileSystem(WasmVfs(workerOptions: options),
37+
makeDefault: true);
4138

42-
final buffer = Uint8List.fromList([1, 2, 3, 4, 5, 6]);
43-
file.xWrite(buffer, 0);
39+
sqlite3.open('/tmp/foo.db')
40+
..execute('pragma user_version = 1')
41+
..execute('CREATE TABLE foo (bar INTEGER NOT NULL);')
42+
..execute('INSERT INTO foo (bar) VALUES (?)', [3])
43+
..dispose();
4444

45-
buffer.fillRange(0, 6, 0);
46-
file.xRead(buffer, 0);
47-
print('Buffer after read = $buffer');
48-
49-
print('size = ${file.xFileSize()}');
50-
file.xTruncate(1024);
51-
print('file after truncate: ${file.xFileSize()}');
52-
53-
file.xClose();
54-
print('closed file');
55-
56-
vfs.xDelete('/test', 0);
57-
print('deleted file');
45+
final db = sqlite3.open('/tmp/foo.db');
46+
print(db.select('SELECT * FROM foo'));
5847
} else {
5948
final message = data as WorkerOptions;
6049

sqlite3/lib/src/constants.dart

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -487,9 +487,9 @@ class SqlTextEncoding {
487487
class SqlFileLockingLevels {
488488
static const SQLITE_LOCK_NONE = 0;
489489
static const SQLITE_LOCK_SHARED = 1;
490-
static const SQLITE_LOCK_RESERVED = 0;
491-
static const SQLITE_LOCK_PENDING = 0;
492-
static const SQLITE_LOCK_EXCLUSIVE = 0;
490+
static const SQLITE_LOCK_RESERVED = 2;
491+
static const SQLITE_LOCK_PENDING = 3;
492+
static const SQLITE_LOCK_EXCLUSIVE = 4;
493493
}
494494

495495
/// Special destructors, https://www.sqlite.org/c3ref/c_static.html
@@ -518,6 +518,10 @@ class SqlFunctionFlag {
518518
static const SQLITE_INNOCUOUS = 0x000200000;
519519
}
520520

521+
class SqlDeviceCharacteristics {
522+
static const SQLITE_IOCAP_UNDELETABLE_WHEN_OPEN = 0x00000800;
523+
}
524+
521525
const SQLITE_DELETE = 9;
522526
const SQLITE_INSERT = 18;
523527
const SQLITE_UPDATE = 23;

sqlite3/lib/src/vfs.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ abstract class VirtualFileSystemFile {
130130

131131
/// Returns the lock state held by any process on this file.
132132
int xCheckReservedLock();
133+
134+
///
135+
int get xDeviceCharacteristics;
133136
}
134137

135138
/// A [VirtualFileSystem] implementation that uses a [Random] instance for
@@ -162,6 +165,9 @@ abstract class BaseVfsFile implements VirtualFileSystemFile {
162165
/// must not be used after this function returns.
163166
int readInto(Uint8List buffer, int offset);
164167

168+
@override
169+
int get xDeviceCharacteristics => 0;
170+
165171
@override
166172
void xRead(Uint8List target, int fileOffset) {
167173
final bytesRead = readInto(target, fileOffset);

sqlite3/lib/src/wasm/environment.dart

Lines changed: 0 additions & 24 deletions
This file was deleted.

sqlite3/lib/src/wasm/file_system.dart

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)