Skip to content

Commit 3c21784

Browse files
committed
Simplify URI parameters.
1 parent 019246d commit 3c21784

File tree

9 files changed

+52
-46
lines changed

9 files changed

+52
-46
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,5 +107,5 @@ The Wasm and VFS layers are also tested by running SQLite's
107107
- [`github.com/mattn/go-sqlite3`](https://pkg.go.dev/github.com/mattn/go-sqlite3)
108108
- [`github.com/zombiezen/go-sqlite`](https://pkg.go.dev/github.com/zombiezen/go-sqlite)
109109

110-
[^1]: anything else you find in [`go.mod`](./go.mod) is either a test dependency,
110+
[^1]: anything else you find in `go.mod` is either a test dependency,
111111
or needed by one of the extensions.

embed/sqlite3.wasm

9 Bytes
Binary file not shown.

vfs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The main differences are [file locking](#file-locking) and [WAL mode](write-ahea
1515
### File Locking
1616

1717
POSIX advisory locks, which SQLite uses on Unix, are
18-
[broken by design](https://sqlite.org/src/artifact/2e8b12?ln=1073-1161).
18+
[broken by design](https://github.com/sqlite/sqlite/blob/b74eb0/src/os_unix.c#L1073-L1161).
1919

2020
On Linux and macOS, this module uses
2121
[OFD locks](https://www.gnu.org/software/libc/manual/html_node/Open-File-Description-Locks.html)

vfs/filename.go

Lines changed: 43 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -103,27 +103,32 @@ func (n *Filename) URIParameter(key string) string {
103103
}
104104

105105
uriKey := n.mod.ExportedFunction("sqlite3_uri_key")
106-
uriParam := n.mod.ExportedFunction("sqlite3_uri_parameter")
106+
n.stack[0] = uint64(n.zPath)
107+
n.stack[1] = uint64(0)
108+
if err := uriKey.CallWithStack(n.ctx, n.stack[:]); err != nil {
109+
panic(err)
110+
}
107111

108-
for i := 0; ; i++ {
109-
n.stack[1] = uint64(i)
110-
n.stack[0] = uint64(n.zPath)
111-
if err := uriKey.CallWithStack(n.ctx, n.stack[:]); err != nil {
112-
panic(err)
113-
}
114-
if n.stack[0] == 0 {
112+
ptr := uint32(n.stack[0])
113+
if ptr == 0 {
114+
return ""
115+
}
116+
117+
// Parse the format from:
118+
// https://github.com/sqlite/sqlite/blob/b74eb0/src/pager.c#L4797-L4840
119+
// This avoids having to alloc/free the key just to find a value.
120+
for {
121+
k := util.ReadString(n.mod, ptr, _MAX_NAME)
122+
if k == "" {
115123
return ""
116124
}
117-
if key != util.ReadString(n.mod, uint32(n.stack[0]), _MAX_NAME) {
118-
continue
119-
}
125+
ptr += uint32(len(k)) + 1
120126

121-
n.stack[1] = n.stack[0]
122-
n.stack[0] = uint64(n.zPath)
123-
if err := uriParam.CallWithStack(n.ctx, n.stack[:]); err != nil {
124-
panic(err)
127+
v := util.ReadString(n.mod, ptr, _MAX_NAME)
128+
if k == key {
129+
return v
125130
}
126-
return util.ReadString(n.mod, uint32(n.stack[0]), _MAX_NAME)
131+
ptr += uint32(len(v)) + 1
127132
}
128133
}
129134

@@ -135,32 +140,35 @@ func (n *Filename) URIParameters() url.Values {
135140
return nil
136141
}
137142

138-
var params url.Values
139143
uriKey := n.mod.ExportedFunction("sqlite3_uri_key")
140-
uriParam := n.mod.ExportedFunction("sqlite3_uri_parameter")
144+
n.stack[0] = uint64(n.zPath)
145+
n.stack[1] = uint64(0)
146+
if err := uriKey.CallWithStack(n.ctx, n.stack[:]); err != nil {
147+
panic(err)
148+
}
141149

142-
for i := 0; ; i++ {
143-
n.stack[1] = uint64(i)
144-
n.stack[0] = uint64(n.zPath)
145-
if err := uriKey.CallWithStack(n.ctx, n.stack[:]); err != nil {
146-
panic(err)
147-
}
148-
if n.stack[0] == 0 {
150+
ptr := uint32(n.stack[0])
151+
if ptr == 0 {
152+
return nil
153+
}
154+
155+
var params url.Values
156+
157+
// Parse the format from:
158+
// https://github.com/sqlite/sqlite/blob/b74eb0/src/pager.c#L4797-L4840
159+
// This is the only way to support multiple valued keys.
160+
for {
161+
k := util.ReadString(n.mod, ptr, _MAX_NAME)
162+
if k == "" {
149163
return params
150164
}
151-
key := util.ReadString(n.mod, uint32(n.stack[0]), _MAX_NAME)
152-
if params.Has(key) {
153-
continue
154-
}
165+
ptr += uint32(len(k)) + 1
155166

156-
n.stack[1] = n.stack[0]
157-
n.stack[0] = uint64(n.zPath)
158-
if err := uriParam.CallWithStack(n.ctx, n.stack[:]); err != nil {
159-
panic(err)
160-
}
167+
v := util.ReadString(n.mod, ptr, _MAX_NAME)
161168
if params == nil {
162169
params = url.Values{}
163170
}
164-
params.Set(key, util.ReadString(n.mod, uint32(n.stack[0]), _MAX_NAME))
171+
params.Add(k, v)
172+
ptr += uint32(len(v)) + 1
165173
}
166174
}

vfs/memdb/memdb.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (memVFS) Open(name string, flags vfs.OpenFlag) (vfs.File, vfs.OpenFlag, err
2323
// This is not a problem for most SQLite file types:
2424
// - databases, which only do page aligned reads/writes;
2525
// - temp journals, as used by the sorter, which does the same:
26-
// https://sqlite.org/src/artifact/237840?ln=409-412
26+
// https://github.com/sqlite/sqlite/blob/b74eb0/src/vdbesort.c#L409-L412
2727
//
2828
// We refuse to open all other file types,
2929
// but returning OPEN_MEMORY means SQLite won't ask us to.

vfs/tests/mptest/testdata/exports.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ aligned_alloc
22
free
33
malloc
44
sqlite3_database_file_object
5-
sqlite3_uri_key
6-
sqlite3_uri_parameter
5+
sqlite3_uri_key
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:3fc3e666b2bb3e0e27622d43ab35f802111c59155c592a7c822f09d1229e8700
3-
size 470668
2+
oid sha256:1de925ac1b2a15c0e0ac4231a507d31bdf1ea576f0d768f5f4bf5c3436a1eece
3+
size 470117

vfs/tests/speedtest1/testdata/exports.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ aligned_alloc
22
free
33
malloc
44
sqlite3_database_file_object
5-
sqlite3_uri_key
6-
sqlite3_uri_parameter
5+
sqlite3_uri_key
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:4573183e549df979c07040d92c3b2de72b5dd49ad0d8e779de63ed95fba4e898
3-
size 484084
2+
oid sha256:262f1702de606413a92fbb9d7d58b812140d072cacabe79556d5fa38e00461d0
3+
size 483792

0 commit comments

Comments
 (0)