-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblob.go
More file actions
305 lines (288 loc) · 9.29 KB
/
Copy pathblob.go
File metadata and controls
305 lines (288 loc) · 9.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package sqlite // import "gosqlite.org"
import (
"errors"
"fmt"
"io"
"math"
"sync"
"unsafe"
"modernc.org/libc"
sqlite3 "modernc.org/sqlite/lib"
)
// Blob is a handle to a single open BLOB or TEXT value referenced by rowid,
// returned from Conn.OpenBlob. It exposes incremental binary I/O via
// sqlite3_blob_read / sqlite3_blob_write, plus zero-allocation re-binding to
// another row in the same table via Reopen.
//
// A Blob is bound to the connection that opened it and is not safe for
// concurrent use; callers must serialize access. Like the connection it is
// drawn from, the Blob must be closed explicitly via Close.
type Blob struct {
c *conn
pBlob uintptr // *sqlite3_blob
size int64
offset int64
write bool
mu sync.Mutex
closed bool
}
// OpenBlob opens a handle to a single BLOB or TEXT value identified by
// (schema, table, column, rowid). Pass schema="main" for the default database
// or "" to use SQLite's default. If write is true the handle is opened for
// read+write; otherwise it is read-only.
//
// The blob handle is valid until Close is called or until the underlying row
// changes in a way that invalidates the handle (in which case I/O operations
// return SQLITE_ABORT). Use Reopen to rebind a single handle across many
// rows in the same column without reallocating, which is materially cheaper
// than repeated OpenBlob/Close cycles.
//
// A Blob is fixed-size (see [Blob.WriteAt], [Blob.Size]). For an unbounded,
// growable, randomly-writable byte stream, use [gosqlite.org/blobstore],
// which manages the chunking for you.
//
// Wraps sqlite3_blob_open: https://sqlite.org/c3ref/blob_open.html
func (c *Conn) OpenBlob(schema, table, column string, rowid int64, write bool) (*Blob, error) {
if schema == "" {
schema = "main"
}
zSchema, err := libc.CString(schema)
if err != nil {
return nil, err
}
defer libc.Xfree(c.tls, zSchema)
zTable, err := libc.CString(table)
if err != nil {
return nil, err
}
defer libc.Xfree(c.tls, zTable)
zColumn, err := libc.CString(column)
if err != nil {
return nil, err
}
defer libc.Xfree(c.tls, zColumn)
flag := int32(0)
if write {
flag = 1
}
// The output blob-handle slot must live in C-managed memory, not on the
// Go stack: sqlite3_blob_open writes *ppBlob through the pointer, and a
// Go stack address passed as uintptr is not tracked by the runtime, so a
// stack move (likely when this runs reentrantly from inside a UDF/vtab
// step, deep in the call graph) would leave the write targeting stale
// memory and our handle reading as 0. Allocate the slot via c.malloc —
// the same idiom conn.prepare uses for ppStmt.
ppBlob, err := c.malloc(int(ptrSize))
if err != nil {
return nil, err
}
defer c.free(ppBlob)
rc := sqlite3.Xsqlite3_blob_open(
c.tls, c.db,
zSchema, zTable, zColumn,
rowid, flag,
ppBlob,
)
pBlob := *(*uintptr)(unsafe.Pointer(ppBlob))
if rc != sqlite3.SQLITE_OK {
// Per the SQLite docs, sqlite3_blob_open may leave *ppBlob set even
// on failure; close defensively before surfacing the error.
if pBlob != 0 {
sqlite3.Xsqlite3_blob_close(c.tls, pBlob)
}
return nil, c.errstr(rc)
}
n := sqlite3.Xsqlite3_blob_bytes(c.tls, pBlob)
if n < 0 {
// sqlite3_blob_bytes returns int32; a value > MaxInt32 (a 2 GiB
// blob if the build raised SQLITE_MAX_LENGTH) sign-extends to a
// negative int64 here, breaking every bounds check in
// ReadAt/WriteAt. We refuse rather than serve a Blob whose size
// reads as negative.
sqlite3.Xsqlite3_blob_close(c.tls, pBlob)
return nil, fmt.Errorf("sqlite: blob size %d overflows int32; use streaming reads", uint32(n))
}
return &Blob{
c: c,
pBlob: pBlob,
size: int64(n),
write: write,
}, nil
}
// Close releases the underlying sqlite3_blob handle. Subsequent calls are
// no-ops and return nil.
func (b *Blob) Close() error {
b.mu.Lock()
defer b.mu.Unlock()
if b.closed {
return nil
}
b.closed = true
rc := sqlite3.Xsqlite3_blob_close(b.c.tls, b.pBlob)
b.pBlob = 0
if rc != sqlite3.SQLITE_OK {
return b.c.errstr(rc)
}
return nil
}
// Size reports the length in bytes of the bound BLOB or TEXT value at the
// time the handle was opened (or last reopened). The size is fixed for the
// life of the handle; growing the value requires UPDATEing the row to a
// larger pre-sized value (e.g. SET col = zeroblob(N), then re-write the
// content), NOT `col || zeroblob(delta)` — SQLite silently drops zeroblob
// operands under `||`, producing a shorter value than intended. For a
// growable stream, use [gosqlite.org/blobstore].
func (b *Blob) Size() int64 { return b.size }
// Reopen rebinds the handle to a different rowid in the same database,
// table, and column without reallocating the underlying handle. Returns an
// error wrapping SQLITE_ABORT if the rebind fails (e.g. the new row does
// not exist); the handle is then unusable and should be closed.
//
// Wraps sqlite3_blob_reopen: https://sqlite.org/c3ref/blob_reopen.html
func (b *Blob) Reopen(rowid int64) error {
b.mu.Lock()
defer b.mu.Unlock()
if b.closed {
return errors.New("sqlite: Blob.Reopen on closed handle")
}
rc := sqlite3.Xsqlite3_blob_reopen(b.c.tls, b.pBlob, rowid)
if rc != sqlite3.SQLITE_OK {
return b.c.errstr(rc)
}
n := sqlite3.Xsqlite3_blob_bytes(b.c.tls, b.pBlob)
if n < 0 {
// Same overflow guard as OpenBlob — refuse to serve a Blob
// whose size is unrepresentable as a non-negative int64.
return fmt.Errorf("sqlite: blob size %d overflows int32; use streaming reads", uint32(n))
}
b.size = int64(n)
b.offset = 0
return nil
}
// ReadAt implements io.ReaderAt. It reads up to len(p) bytes starting at
// off into p. Reads past the end of the value return io.EOF; reads that
// straddle the end return the partial bytes plus io.EOF on the next call.
//
// Wraps sqlite3_blob_read: https://sqlite.org/c3ref/blob_read.html
func (b *Blob) ReadAt(p []byte, off int64) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
if b.closed {
return 0, errors.New("sqlite: Blob.ReadAt on closed handle")
}
if off < 0 {
return 0, errors.New("sqlite: Blob.ReadAt: negative offset")
}
if off >= b.size {
return 0, io.EOF
}
n := len(p)
if int64(n) > b.size-off {
n = int(b.size - off)
}
if n == 0 {
return 0, nil
}
// sqlite3_blob_read/write take int32 size + offset. BLOBs > 2 GiB
// are pathological in SQLite (rowids are int64 but per-blob byte
// length is int32) but error rather than silently truncate.
if off > math.MaxInt32 || n > math.MaxInt32 {
return 0, errors.New("sqlite: Blob.ReadAt: offset or length exceeds int32")
}
rc := sqlite3.Xsqlite3_blob_read(
b.c.tls, b.pBlob,
uintptr(unsafe.Pointer(unsafe.SliceData(p))),
int32(n), int32(off),
)
if rc != sqlite3.SQLITE_OK {
return 0, b.c.errstr(rc)
}
if int64(n)+off >= b.size {
return n, io.EOF
}
return n, nil
}
// WriteAt implements io.WriterAt. Returns an error if the handle was
// opened read-only, or if the write would extend past the current value
// size (BLOB writes cannot grow the row — the row must be sized at INSERT
// time with zeroblob(N) or via an UPDATE).
//
// To grow a value, re-size it with zeroblob(N) at INSERT/UPDATE time and
// re-open the handle. Do NOT use `col || zeroblob(delta)`: SQLite drops
// zeroblob operands under `||`, silently truncating. For an unbounded,
// growable stream, use [gosqlite.org/blobstore] instead of growing by hand.
//
// Wraps sqlite3_blob_write: https://sqlite.org/c3ref/blob_write.html
func (b *Blob) WriteAt(p []byte, off int64) (int, error) {
b.mu.Lock()
defer b.mu.Unlock()
if b.closed {
return 0, errors.New("sqlite: Blob.WriteAt on closed handle")
}
if !b.write {
return 0, errors.New("sqlite: Blob.WriteAt on read-only handle")
}
if off < 0 {
return 0, errors.New("sqlite: Blob.WriteAt: negative offset")
}
if int64(len(p))+off > b.size {
return 0, errors.New("sqlite: Blob.WriteAt: write past end of blob (use zeroblob(N) to pre-size)")
}
if len(p) == 0 {
return 0, nil
}
if off > math.MaxInt32 || len(p) > math.MaxInt32 {
return 0, errors.New("sqlite: Blob.WriteAt: offset or length exceeds int32")
}
rc := sqlite3.Xsqlite3_blob_write(
b.c.tls, b.pBlob,
uintptr(unsafe.Pointer(unsafe.SliceData(p))),
int32(len(p)), int32(off),
)
if rc != sqlite3.SQLITE_OK {
return 0, b.c.errstr(rc)
}
return len(p), nil
}
// Read implements io.Reader, advancing an internal cursor. The cursor
// starts at 0 and is reset by Reopen. Mix freely with ReadAt; ReadAt does
// not advance the cursor.
func (b *Blob) Read(p []byte) (int, error) {
n, err := b.ReadAt(p, b.offset)
b.mu.Lock()
b.offset += int64(n)
b.mu.Unlock()
return n, err
}
// Write implements io.Writer, advancing the same cursor as Read.
func (b *Blob) Write(p []byte) (int, error) {
n, err := b.WriteAt(p, b.offset)
b.mu.Lock()
b.offset += int64(n)
b.mu.Unlock()
return n, err
}
// Seek implements io.Seeker over the cursor used by Read/Write.
func (b *Blob) Seek(offset int64, whence int) (int64, error) {
b.mu.Lock()
defer b.mu.Unlock()
if b.closed {
return 0, errors.New("sqlite: Blob.Seek on closed handle")
}
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = b.offset + offset
case io.SeekEnd:
abs = b.size + offset
default:
return 0, errors.New("sqlite: Blob.Seek: invalid whence")
}
if abs < 0 {
return 0, errors.New("sqlite: Blob.Seek: negative position")
}
b.offset = abs
return abs, nil
}