forked from godror/godror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlob.go
588 lines (546 loc) · 16.1 KB
/
lob.go
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
// Copyright 2017, 2022 The Godror Authors
//
//
// SPDX-License-Identifier: UPL-1.0 OR Apache-2.0
package godror
/*
#include "dpiImpl.h"
*/
import "C"
import (
"bufio"
"errors"
"fmt"
"io"
"runtime"
"strings"
"sync"
"unicode/utf8"
"unsafe"
)
// Lob is for reading/writing a LOB.
type Lob struct {
io.Reader
IsClob bool
}
var _ = (io.Reader)((*Lob)(nil))
var _ = (io.ReaderAt)((*Lob)(nil))
// Hijack the underlying lob reader/writer, and
// return a DirectLob for reading/writing the lob directly.
//
// After this, the Lob is unusable!
func (lob *Lob) Hijack() (*DirectLob, error) {
if lob == nil || lob.Reader == nil {
return nil, errors.New("lob is nil")
}
lr, ok := lob.Reader.(*dpiLobReader)
if !ok {
return nil, fmt.Errorf("Lob.Reader is %T, not *dpiLobReader", lob.Reader)
}
lob.Reader = nil
return &DirectLob{drv: lr.drv, dpiLob: lr.dpiLob}, nil
}
// WriteTo writes data to w until there's no more data to write or when an error occurs.
// The return value n is the number of bytes written. Any error encountered during the write is also returned.
//
// Will use Lob.Reader.WriteTo, if Lob.Reader implements io.WriterTo.
func (lob *Lob) WriteTo(w io.Writer) (n int64, err error) {
if wt, ok := lob.Reader.(io.WriterTo); ok {
return wt.WriteTo(w)
}
return io.CopyBuffer(w, lob.Reader, make([]byte, 1<<20))
}
// NewBufferedReader returns a new bufio.Reader with the given size (or 1M if 0).
func (lob *Lob) NewBufferedReader(size int) *bufio.Reader {
if size <= 0 {
size = 1 << 20
}
return bufio.NewReaderSize(lob.Reader, size)
}
// Size exposes the underlying Reader's Size method, if it is supported.
func (lob *Lob) Size() (int64, error) {
if lr, ok := lob.Reader.(interface{ Size() (int64, error) }); ok {
return lr.Size()
}
return 0, ErrNotSupported
}
// ReadAt exposes the underlying Reader's ReadAt method, if it is supported.
func (lob *Lob) ReadAt(p []byte, off int64) (int, error) {
if lr, ok := lob.Reader.(io.ReaderAt); ok {
return lr.ReadAt(p, off)
}
return 0, ErrNotSupported
}
// Scan assigns a value from a database driver.
//
// The src value will be of one of the following types:
//
// int64
// float64
// bool
// []byte
// string
// time.Time
// nil - for NULL values
//
// An error should be returned if the value cannot be stored
// without loss of information.
func (dlr *dpiLobReader) Scan(src interface{}) error {
b, ok := src.([]byte)
if !ok {
return fmt.Errorf("cannot convert LOB to %T", src)
}
_ = b
return nil
}
var _ = io.ReadCloser((*dpiLobReader)(nil))
var _ = io.ReaderAt((*dpiLobReader)(nil))
type dpiLobReader struct {
*drv
dpiLob *C.dpiLob
buf []byte
offset, sizePlusOne C.uint64_t
mu sync.Mutex
chunkSize C.uint32_t
bufR, bufW int
finished bool
IsClob bool
}
// WriteTo writes data to w until there's no more data to write or when an error occurs.
// The return value n is the number of bytes written. Any error encountered during the write is also returned.
//
// Uses efficient, multiple-of-LOB-chunk-size buffered reads.
func (dlr *dpiLobReader) WriteTo(w io.Writer) (n int64, err error) {
size := dlr.ChunkSize()
const minBufferSize = 1 << 20
if size <= 0 {
size = minBufferSize
} else {
for size < minBufferSize/2 { // at most 1M
size *= 2
}
}
return io.CopyBuffer(
w,
// Mask WriteTo method
io.Reader(struct {
io.Reader
}{dlr}),
make([]byte, size))
}
// ChunkSize returns the LOB's native chunk size. Reads/writes with a multiply of this size is the most performant.
func (dlr *dpiLobReader) ChunkSize() int {
dlr.mu.Lock()
defer dlr.mu.Unlock()
if dlr.chunkSize != 0 {
return int(dlr.chunkSize)
}
if err := dlr.checkExec(func() C.int {
return C.dpiLob_getChunkSize(dlr.dpiLob, &dlr.chunkSize)
}); err != nil {
dlr.chunkSize = 0
return -1
}
return int(dlr.chunkSize)
}
// Read from LOB. It does buffer the reading internally against short buffers (io.ReadAll).
func (dlr *dpiLobReader) Read(p []byte) (int, error) {
dlr.mu.Lock()
defer dlr.mu.Unlock()
logger := getLogger()
if logger != nil {
logger.Log("msg", "Read", "bufR", dlr.bufR, "bufW", dlr.bufW, "buf", cap(dlr.buf))
}
if dlr.buf == nil {
if dlr.chunkSize == 0 {
if err := dlr.checkExec(func() C.int {
return C.dpiLob_getChunkSize(dlr.dpiLob, &dlr.chunkSize)
}); err != nil {
return 0, fmt.Errorf("getChunkSize: %w", err)
}
}
// If the dest buffer is big enough, avoid copying.
if ulen := C.uint64_t(len(p)); ulen >= C.uint64_t(dlr.chunkSize) || dlr.sizePlusOne != 0 && ulen+1 >= dlr.sizePlusOne {
if logger != nil {
logger.Log("msg", "direct read", "p", len(p), "chunkSize", dlr.chunkSize)
}
return dlr.read(p)
}
cs := int(dlr.chunkSize)
dlr.buf = make([]byte, (maxI(len(p), 1<<20-cs)/cs+1)*cs)
dlr.bufR, dlr.bufW = 0, 0
} else if dlr.bufW != 0 && cap(dlr.buf) != 0 {
var n int
if dlr.bufR < dlr.bufW {
n = copy(p, dlr.buf[dlr.bufR:dlr.bufW])
dlr.bufR += n
}
if dlr.bufR == dlr.bufW {
dlr.bufR, dlr.bufW = 0, 0
}
if n != 0 {
return n, nil
}
}
var err error
// We only read into dlr.buf when it's empty, dlr.bufR == dlr.bufW == 0
dlr.bufW, err = dlr.read(dlr.buf)
if logger != nil {
logger.Log("msg", "dlr.read", "bufR", dlr.bufR, "bufW", dlr.bufW, "chunkSize", dlr.chunkSize, "error", err)
}
dlr.bufR = copy(p, dlr.buf[:dlr.bufW])
if err == io.EOF && dlr.bufW != dlr.bufR {
err = nil
}
return dlr.bufR, err
}
var ErrCLOB = errors.New("CLOB is not supported")
// Size returns the LOB's size. It returns ErrCLOB for CLOB,
// (works only for BLOBs), as Oracle reports CLOB size in runes, not in bytes!
func (dlr *dpiLobReader) Size() (int64, error) {
dlr.mu.Lock()
err := dlr.getSize()
size := dlr.sizePlusOne - 1
isClob := dlr.IsClob
dlr.mu.Unlock()
if err == nil && isClob {
err = ErrCLOB
}
return int64(size), err
}
func (dlr *dpiLobReader) getSize() error {
if dlr.sizePlusOne != 0 {
return nil
}
var err error
runtime.LockOSThread()
if err = dlr.checkExecNoLOT(func() C.int {
return C.dpiLob_getSize(dlr.dpiLob, &dlr.sizePlusOne)
}); err != nil {
err = fmt.Errorf("getSize: %w", err)
C.dpiLob_close(dlr.dpiLob)
dlr.dpiLob = nil
}
runtime.UnlockOSThread()
dlr.sizePlusOne++
return err
}
// read does the real LOB reading.
func (dlr *dpiLobReader) read(p []byte) (int, error) {
if dlr == nil {
return 0, errors.New("read on nil dpiLobReader")
}
logger := getLogger()
if logger != nil {
logger.Log("msg", "LOB Read", "dlr", fmt.Sprintf("%p", dlr), "offset", dlr.offset, "size", dlr.sizePlusOne, "finished", dlr.finished, "clob", dlr.IsClob)
}
if dlr.finished || dlr.dpiLob == nil {
return 0, io.EOF
}
if len(p) < 1 || dlr.IsClob && len(p) < 4 {
return 0, io.ErrShortBuffer
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// For CLOB, sizePlusOne and offset counts the CHARACTERS!
// See https://oracle.github.io/odpi/doc/public_functions/dpiLob.html dpiLob_readBytes
if dlr.sizePlusOne == 0 { // first read
// never read size before
if err := dlr.getSize(); err != nil {
var coder interface{ Code() int }
if errors.As(err, &coder) && coder.Code() == 22922 || strings.Contains(err.Error(), "invalid dpiLob handle") {
return 0, io.EOF
}
return 0, err
}
var lobType C.dpiOracleTypeNum
if err := dlr.checkExecNoLOT(func() C.int {
return C.dpiLob_getType(dlr.dpiLob, &lobType)
}); err == nil &&
(2017 <= lobType && lobType <= 2019) {
dlr.IsClob = lobType == 2017 || lobType == 2018 // CLOB and NCLOB
}
}
n := C.uint64_t(len(p))
amount := n
if dlr.IsClob {
amount /= 4 // dpiLob_readBytes' amount is the number of CHARACTERS for CLOBs.
}
// fmt.Printf("%p.Read offset=%d sizePlusOne=%d n=%d\n", dlr.dpiLob, dlr.offset, dlr.sizePlusOne, n)
if logger != nil {
logger.Log("msg", "Read", "offset", dlr.offset, "sizePlusOne", dlr.sizePlusOne, "n", n, "amount", amount)
}
if !dlr.IsClob && dlr.offset+1 >= dlr.sizePlusOne {
if logger != nil {
logger.Log("msg", "LOB reached end", "offset", dlr.offset, "size", dlr.sizePlusOne)
}
return 0, io.EOF
}
if err := dlr.drv.checkExecNoLOT(func() C.int {
return C.dpiLob_readBytes(dlr.dpiLob, dlr.offset+1, amount, (*C.char)(unsafe.Pointer(&p[0])), &n)
}); err != nil {
if logger != nil {
logger.Log("msg", "readBytes", "error", err)
}
C.dpiLob_close(dlr.dpiLob)
dlr.dpiLob = nil
if logger != nil {
logger.Log("msg", "LOB read", "error", err)
}
var codeErr interface{ Code() int }
if dlr.finished = errors.As(err, &codeErr) && codeErr.Code() == 1403; dlr.finished {
dlr.offset += n
return int(n), io.EOF
}
return int(n), fmt.Errorf("dpiLob_readbytes(lob=%p offset=%d n=%d): %w", dlr.dpiLob, dlr.offset, len(p), err)
}
if logger != nil {
logger.Log("msg", "read", "n", n)
}
if dlr.IsClob {
dlr.offset += C.uint64_t(utf8.RuneCount(p[:n]))
} else {
dlr.offset += n
}
var err error
if amount != 0 && n == 0 || !dlr.IsClob && dlr.offset+1 >= dlr.sizePlusOne {
C.dpiLob_close(dlr.dpiLob)
dlr.dpiLob = nil
dlr.finished = true
err = io.EOF
}
if logger != nil {
logger.Log("msg", "LOB", "n", n, "offset", dlr.offset, "size", dlr.sizePlusOne, "finished", dlr.finished, "clob", dlr.IsClob, "error", err)
}
return int(n), err
}
// ReadAt reads at the specified offset (in bytes).
// Works only for BLOBs!
func (dlr *dpiLobReader) ReadAt(p []byte, off int64) (int, error) {
dlr.mu.Lock()
defer dlr.mu.Unlock()
if dlr.IsClob {
return 0, ErrCLOB
}
n := C.uint64_t(len(p))
err := dlr.checkExec(func() C.int {
return C.dpiLob_readBytes(dlr.dpiLob, C.uint64_t(off+1), n, (*C.char)(unsafe.Pointer(&p[0])), &n)
})
if err != nil {
err = fmt.Errorf("readBytes at %d for %d: %w", off, n, dlr.getError())
}
return int(n), err
}
func (dlr *dpiLobReader) Close() error {
if dlr == nil || dlr.dpiLob == nil {
return nil
}
lob := dlr.dpiLob
dlr.dpiLob = nil
return closeLob(dlr, lob)
}
type dpiLobWriter struct {
*drv
dpiLob *C.dpiLob
offset C.uint64_t
opened bool
isClob bool
}
func (dlw *dpiLobWriter) Write(p []byte) (int, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
lob := dlw.dpiLob
if !dlw.opened {
// fmt.Printf("open %p\n", lob)
if err := dlw.drv.checkExecNoLOT(func() C.int {
return C.dpiLob_openResource(lob)
}); err != nil {
return 0, fmt.Errorf("openResources(%p): %w", lob, err)
}
dlw.opened = true
}
n := C.uint64_t(len(p))
if err := dlw.drv.checkExecNoLOT(func() C.int {
return C.dpiLob_writeBytes(lob, dlw.offset+1, (*C.char)(unsafe.Pointer(&p[0])), n)
}); err != nil {
err = fmt.Errorf("writeBytes(%p, offset=%d, data=%d): %w", lob, dlw.offset, n, err)
dlw.dpiLob = nil
closeLob(dlw, lob)
return 0, err
}
// fmt.Printf("written %q into %p@%d\n", p[:n], lob, dlw.offset)
dlw.offset += n
return int(n), nil
}
func (dlw *dpiLobWriter) Close() error {
if dlw == nil || dlw.dpiLob == nil {
return nil
}
lob := dlw.dpiLob
dlw.dpiLob = nil
return closeLob(dlw, lob)
}
func closeLob(d interface{ getError() error }, lob *C.dpiLob) error {
if lob == nil {
return nil
}
runtime.LockOSThread()
defer runtime.UnlockOSThread()
var isOpen C.int
if C.dpiLob_getIsResourceOpen(lob, &isOpen) != C.DPI_FAILURE && isOpen == 1 {
if C.dpiLob_closeResource(lob) == C.DPI_FAILURE {
if err := d.getError(); err != nil {
var codeErr interface{ Code() int }
if errors.As(err, &codeErr) && codeErr.Code() != 22289 { // cannot perform %s operation on an unopened file or LOB
return fmt.Errorf("closeResource(%p): %w", lob, err)
}
}
}
}
C.dpiLob_release(lob)
return nil
}
// DirectLob holds a Lob and allows direct (Read/WriteAt, not streaming Read/Write) operations on it.
type DirectLob struct {
drv *drv
dpiLob *C.dpiLob
opened, isClob bool
}
var _ = io.ReaderAt((*DirectLob)(nil))
var _ = io.WriterAt((*DirectLob)(nil))
// NewTempLob returns a temporary LOB as DirectLob.
func (c *conn) NewTempLob(isClob bool) (*DirectLob, error) {
typ := C.uint(C.DPI_ORACLE_TYPE_BLOB)
if isClob {
typ = C.DPI_ORACLE_TYPE_CLOB
}
lob := DirectLob{drv: c.drv, isClob: isClob}
if err := c.checkExec(func() C.int { return C.dpiConn_newTempLob(c.dpiConn, typ, &lob.dpiLob) }); err != nil {
return nil, fmt.Errorf("newTempLob: %w", err)
}
return &lob, nil
}
// Close the Lob.
func (dl *DirectLob) Close() error {
if !dl.opened {
return nil
}
lob := dl.dpiLob
dl.opened, dl.dpiLob = false, nil
return closeLob(dl.drv, lob)
}
// Size returns the size of the LOB.
//
// WARNING: for historical reasons, Oracle stores CLOBs and NCLOBs using the UTF-16 encoding,
// regardless of what encoding is otherwise in use by the database.
// The number of characters, however, is defined by the number of UCS-2 codepoints.
// For this reason, if a character requires more than one UCS-2 codepoint,
// the size returned will be inaccurate and care must be taken to account for the difference!
func (dl *DirectLob) Size() (int64, error) {
var n C.uint64_t
if dl.dpiLob == nil {
return 0, nil
}
if err := dl.drv.checkExec(func() C.int {
return C.dpiLob_getSize(dl.dpiLob, &n)
}); err != nil {
var coder interface{ Code() int }
if errors.As(err, &coder) && coder.Code() == 22922 || strings.Contains(err.Error(), "invalid dpiLob handle") {
return 0, nil
}
return int64(n), fmt.Errorf("getSize: %w", err)
}
return int64(n), nil
}
// Trim the LOB to the given size.
func (dl *DirectLob) Trim(size int64) error {
if err := dl.drv.checkExec(func() C.int {
return C.dpiLob_trim(dl.dpiLob, C.uint64_t(size))
}); err != nil {
return fmt.Errorf("trim: %w", err)
}
return nil
}
// Set the contents of the LOB to the given byte slice.
// The LOB is cleared first.
func (dl *DirectLob) Set(p []byte) error {
if err := dl.drv.checkExec(func() C.int {
return C.dpiLob_setFromBytes(dl.dpiLob, (*C.char)(unsafe.Pointer(&p[0])), C.uint64_t(len(p)))
}); err != nil {
return fmt.Errorf("setFromBytes: %w", err)
}
return nil
}
// ReadAt reads at most len(p) bytes into p at offset.
//
// CLOB's offset must be in amount of characters, and does not work reliably!
//
// WARNING: for historical reasons, Oracle stores CLOBs and NCLOBs using the UTF-16 encoding,
// regardless of what encoding is otherwise in use by the database.
// The number of characters, however, is defined by the number of UCS-2 codepoints.
// For this reason, if a character requires more than one UCS-2 codepoint,
// the size returned will be inaccurate and care must be taken to account for the difference!
func (dl *DirectLob) ReadAt(p []byte, offset int64) (int, error) {
n := C.uint64_t(len(p))
if dl.dpiLob == nil {
return 0, io.EOF
}
amount := n
if dl.isClob {
amount /= 4
if amount == 0 {
return 0, io.ErrShortBuffer
}
}
if err := dl.drv.checkExec(func() C.int {
return C.dpiLob_readBytes(dl.dpiLob, C.uint64_t(offset)+1, n, (*C.char)(unsafe.Pointer(&p[0])), &n)
}); err != nil {
return int(n), fmt.Errorf("readBytes: %w", err)
}
return int(n), nil
}
// WriteAt writes p starting at offset.
func (dl *DirectLob) WriteAt(p []byte, offset int64) (int, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
if !dl.opened {
// fmt.Printf("open %p\n", lob)
if err := dl.drv.checkExecNoLOT(func() C.int {
return C.dpiLob_openResource(dl.dpiLob)
}); err != nil {
return 0, fmt.Errorf("openResources(%p): %w", dl.dpiLob, err)
}
dl.opened = true
}
n := C.uint64_t(len(p))
if err := dl.drv.checkExecNoLOT(func() C.int {
return C.dpiLob_writeBytes(dl.dpiLob, C.uint64_t(offset)+1, (*C.char)(unsafe.Pointer(&p[0])), n)
}); err != nil {
return int(n), fmt.Errorf("writeBytes: %w", err)
}
return int(n), nil
}
// GetFileName Return directory alias and file name for a BFILE type LOB.
func (dl *DirectLob) GetFileName() (dir, file string, err error) {
var directoryAliasLength, fileNameLength C.uint32_t
var directoryAlias, fileName *C.char
if err := dl.drv.checkExec(func() C.int {
return C.dpiLob_getDirectoryAndFileName(dl.dpiLob,
&directoryAlias,
&directoryAliasLength,
&fileName,
&fileNameLength,
)
}); err != nil {
return dir, file, fmt.Errorf("GetFileName: %w", err)
}
dir = C.GoStringN(directoryAlias, C.int(directoryAliasLength))
file = C.GoStringN(fileName, C.int(fileNameLength))
return dir, file, nil
}
func maxI(a, b int) int {
if a < b {
return b
}
return a
}