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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
/*!
Nonblocking mode LOB methods.
> **Note** that for various reasons piece-wise LOB content operations - i.e. `read_first`, `read_next`
and `write_first`, `write_next`, `write_last` methods - are not supported in nonblocking mode.
*/
use super::{LOB, InternalLob, LOB_IS_OPEN, LOB_FILE_IS_OPEN, LOB_IS_TEMP};
use crate::{oci::*, session::Session, task, BFile, Error, Result};
use std::sync::atomic::Ordering;
fn check_oci_result(res: i32, err: Ptr<OCIError>) -> Result<()> {
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(())
}
}
impl<'a,T> LOB<'a,T> where T: DescriptorType<OCIType=OCILobLocator> {
fn get_ptr<O>(&self) -> Ptr<O>
where
O: OCIStruct,
LOB<'a,T>: AsRef<O>
{
let oci_ref : &O = self.as_ref();
Ptr::from(oci_ref)
}
fn get_env_ptr(&self) -> Ptr<OCIEnv> {
self.get_ptr()
}
fn get_err_ptr(&self) -> Ptr<OCIError> {
self.get_ptr()
}
fn get_svc_ptr(&self) -> Ptr<OCISvcCtx> {
self.get_ptr()
}
fn get_lob_ptr(&self) -> Ptr<OCILobLocator> {
self.get_ptr()
}
async fn execute_blocking<F,R>(&self, f: F) -> Result<R>
where
F: FnOnce() -> R + Send + 'static,
R: Send + 'static,
{
self.inner.svc.set_blocking_mode()?;
let res = task::execute_blocking(f).await;
self.inner.svc.set_nonblocking_mode()?;
res
}
/**
Closes a previously opened internal or external LOB.
Closing a LOB requires a round-trip to the server for both internal and external LOBs.
For internal LOBs, `close` triggers other code that relies on the close call and for external
LOBs (BFILEs), close actually closes the server-side operating system file.
If you open a LOB, you must close it before you commit the transaction; an error is produced
if you do not. When an internal LOB is closed, it updates the functional and domain indexes
on the LOB column.
It is an error to commit the transaction before closing all opened LOBs that were opened by
the transaction. When the error is returned, the openness of the open LOBs is discarded, but
the transaction is successfully committed. Hence, all the changes made to the LOB and non-LOB
data in the transaction are committed, but the domain and function-based indexes are not updated.
If this happens, you should rebuild the functional and domain indexes on the LOB column.
# Failures
- The internal LOB is not open.
No error is returned if the BFILE exists but is not opened.
# Example
See [`LOB<T>::open()`]
*/
pub async fn close(&self) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let res = self.execute_blocking(move || -> i32 {
unsafe { OCILobClose(svc.get(), err.get(), loc.get()) }
}).await?;
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
self.inner.status_flags.fetch_and(!LOB_IS_OPEN, Ordering::Relaxed);
Ok(())
}
}
/**
Returns the length of a LOB.
For character LOBs, it is the number of characters; for binary LOBs and BFILEs, it is the number of
bytes in the LOB.
## Notes
- If the LOB is NULL, the length is undefined.
- The length of a BFILE includes the EOF, if it exists.
- The length of an empty internal LOB is zero.
- Any zero-byte or space fillers in the LOB written by previous calls to `erase` or `write` are also
included in the length count.
*/
pub async fn len(&self) -> Result<usize> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let len = self.execute_blocking(move || -> Result<u64> {
let mut len = 0;
let res = unsafe { OCILobGetLength2(svc.get(), err.get(), loc.get(), &mut len) };
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(len)
}
}).await??;
Ok(len as usize)
}
/**
Returns `true` if the internal LOB is open or if the BFILE was opened using the input locator.
If the input BFILE locator was never passed to `open` or `open_file` the BFILE is considered
not to be opened by this BFILE locator. However, a different BFILE locator may have opened
the BFILE. Multiple opens can be performed on the same BFILE using different locators. In other
words, openness is associated with a specific locator for BFILEs.
For internal LOBs openness is associated with the LOB, not with the locator. If locator1 opened
the LOB, then locator2 also sees the LOB as open.
For internal LOBs, this call requires a server round-trip because it checks the state on the
server to see if the LOB is open. For external LOBs (BFILEs), this call also requires a round-trip
because the operating system file on the server side must be checked to see if it is open.
*/
pub async fn is_open(&self) -> Result<bool> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let is_open = self.execute_blocking(move || -> Result<bool> {
let mut flag = Aligned::new(0u8);
let res = unsafe { OCILobIsOpen(svc.get(), err.get(), loc.get(), flag.as_mut_ptr()) };
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(<u8>::from(flag) != 0)
}
}).await??;
Ok(is_open)
}
/**
Opens a LOB, internal or external, only for reading.
Opening a LOB requires a round-trip to the server for both internal and external LOBs. For internal
LOBs, the open triggers other code that relies on the open call. For external LOBs (BFILEs), open
requires a round-trip because the actual operating system file on the server side is being opened.
# Failures
- It is an error to open the same LOB twice.
- If a user tries to write to a LOB that was opened in read-only mode, an error is returned.
*/
pub async fn open_readonly(&self) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let res = self.execute_blocking(move || -> i32 {
unsafe { OCILobOpen(svc.get(), err.get(), loc.get(), OCI_LOB_READONLY) }
}).await?;
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
self.inner.status_flags.fetch_or(LOB_IS_OPEN, Ordering::Relaxed);
Ok(())
}
}
/**
For CLOBs and NCLOBs, if you do not pass `char_len`, then `char_len` is calculated internally as
`byte_len/max char width`, so if max char width is 4, `char_len` is calculated as `byte_len/4`.
**Note** that OCILobRead2() does not calculate how many bytes are required for each character. Instead, OCILobRead2()
fetches the number of characters that in the worst case can fit in `byte_len`.
*/
async fn read_piece(&self, piece: u8, piece_size: usize, offset: usize, byte_len: usize, char_len: usize, cs_form: u8, buf: &mut Vec<u8>) -> Result<(bool,usize,usize)> {
let space_available = buf.capacity() - buf.len();
if piece_size > space_available {
buf.reserve(piece_size - space_available);
}
let data_ptr = Ptr::new(unsafe { buf.as_mut_ptr().add(buf.len()) });
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let (res, num_bytes, num_chars) = self.execute_blocking(move || -> Result<(i32, u64, u64)> {
let mut byte_cnt = byte_len as u64;
let mut char_cnt = char_len as u64;
let res = lob_read2(
svc.as_ref(), err.as_ref(), loc.as_ref(),
&mut byte_cnt, &mut char_cnt, (offset + 1) as _,
data_ptr.get_mut(), piece_size as _, piece,
AL32UTF8, cs_form
);
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok((res, byte_cnt, char_cnt))
}
}).await??;
unsafe {
buf.set_len(buf.len() + num_bytes as usize);
}
Ok( (res == OCI_NEED_DATA, num_bytes as usize, num_chars as usize) )
}
}
impl<'a, T> LOB<'a,T> where T: DescriptorType<OCIType=OCILobLocator> + InternalLob {
/**
Tests if a locator points to a temporary LOB.
# Returns
* `true` - if this LOB locator points to a temporary LOB
* `flase` - if it does not.
*/
pub async fn is_temp(&self) -> Result<bool> {
let env = self.get_env_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let is_temp = self.execute_blocking(move || -> Result<bool> {
let mut flag = Aligned::new(0u8);
let res = unsafe { OCILobIsTemporary(env.as_ref(), err.as_ref(), loc.as_ref(), flag.as_mut_ptr()) };
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(<u8>::from(flag) != 0)
}
}).await??;
if is_temp {
self.inner.status_flags.fetch_or(LOB_IS_TEMP, Ordering::Release);
} else {
self.inner.status_flags.fetch_and(!LOB_IS_TEMP, Ordering::Release);
}
Ok(is_temp)
}
/**
Appends another LOB value at the end of this LOB.
# Example
```
use sibyl::{CLOB, Cache, CharSetForm};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let text1 = "
The sun does arise,
And make happy the skies.
The merry bells ring
To welcome the Spring.
";
let text2 = "
The sky-lark and thrush,
The birds of the bush,
Sing louder around,
To the bells’ cheerful sound.
While our sports shall be seen
On the Ecchoing Green.
";
let lob1 = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
lob1.append(text1).await?;
assert_eq!(lob1.len().await?, text1.len());
let lob2 = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
lob2.append(text2).await?;
// Cannot use `len` shortcut with `text2` because of the `RIGHT SINGLE QUOTATION MARK`
// in it after "bells"
assert_eq!(lob2.len().await?, text2.chars().count());
lob1.append_lob(&lob2).await?;
assert_eq!(lob1.len().await?, text1.len() + text2.chars().count());
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn append_lob(&self, other_lob: &Self) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let dst = self.get_lob_ptr();
let src = Ptr::<OCILobLocator>::from(other_lob.as_ref());
let res = self.execute_blocking(move || -> i32 {
unsafe { OCILobAppend(svc.get(), err.get(), dst.get(), src.get()) }
}).await?;
check_oci_result(res, err)
}
/**
Copies all or a portion of another LOB value.
# Parameters
- `src` - souce LOB
- `src_offset`- the absolute offset for the source LOB.
- `amount` - The number of characters for CLOBs or NCLOBs or the number of bytes for BLOBs to be copied from the source LOB to the destination LOB.
- `offset` - The absolute offset for the destination LOB
If the data exists at the destination's start position, it is overwritten with the source data.
If the destination's start position is beyond the end of the current data, zero-byte fillers (for BLOBs)
or spaces (for CLOBs) are written into the destination LOB from the end of the current data to the beginning
of the newly written data from the source.
The destination LOB is extended to accommodate the newly written data if it extends beyond the current
length of the destination LOB.
LOB buffering must not be enabled for either locator.
## Notes
- To copy the entire source LOB specify `amount` as `std::usize::MAX`.
- `offset` and `src_offset` - the number of characters (character LOB) or bytes (binary LOB) from the
beginning of the LOB - start at 0.
- You can call `len` to determine the length of the source LOB.
# Example
```
use sibyl::{CLOB, Cache, CharSetForm};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let text = "
O Nightingale, that on yon bloomy Spray
Warbl'st at eeve, when all the Woods are still,
Thou with fresh hope the Lovers heart dost fill,
While the jolly hours lead on propitious May,
Thy liquid notes that close the eye of Day,
First heard before the shallow Cuccoo's bill
Portend success in love; O if Jove's will
Have linkt that amorous power to thy soft lay,
...........................................
...........................................
..........................................
For my relief; yet hadst no reason why,
Whether the Muse, or Love call thee his mate,
Both them I serve, and of their train am I.
";
let lob1 = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
lob1.append(text).await?;
let lost_text = "
Now timely sing, ere the rude Bird of Hate
Foretell my hopeles doom, in som Grove ny:
As thou from yeer to yeer hast sung too late
";
let lob2 = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
lob2.append(lost_text).await?;
// Set source offset to 1 to skip leading \n:
lob1.copy(&lob2, 1, 131, 364).await?;
// Read back the overwriten fragment.
// Start with the leading \n, to make comparison easier:
let mut fragment = String::new();
lob1.read(363, 132, &mut fragment).await?;
assert_eq!(fragment, lost_text);
// ASCII only. That means we can use `len` as a "shortcut".
let text_len = lob1.len().await?;
// Recall that the buffer needs to be allocated for the worst case
let mut sonnet = String::new();
lob1.read(0, text_len, &mut sonnet).await?;
let orig = "
O Nightingale, that on yon bloomy Spray
Warbl'st at eeve, when all the Woods are still,
Thou with fresh hope the Lovers heart dost fill,
While the jolly hours lead on propitious May,
Thy liquid notes that close the eye of Day,
First heard before the shallow Cuccoo's bill
Portend success in love; O if Jove's will
Have linkt that amorous power to thy soft lay,
Now timely sing, ere the rude Bird of Hate
Foretell my hopeles doom, in som Grove ny:
As thou from yeer to yeer hast sung too late
For my relief; yet hadst no reason why,
Whether the Muse, or Love call thee his mate,
Both them I serve, and of their train am I.
";
assert_eq!(sonnet, orig);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn copy(&self, src: &Self, src_offset: usize, amount: usize, offset: usize) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let dst = self.get_lob_ptr();
let src = Ptr::<OCILobLocator>::from(src.as_ref());
let res = self.execute_blocking(move || -> i32 {
unsafe {
OCILobCopy2(
svc.get(), err.get(), dst.get(), src.get(),
amount as _, offset as _, src_offset as _
)
}
}).await?;
check_oci_result(res, err)
}
/**
Loads and copies all or a portion of the file into an internal LOB.
# Parameters
- `src` - souce BFILE
- `src_offset`- the absolute offset for the source BFILE. It is the number of bytes from the beginning of the BFILE.
- `amount` - The number of characters for CLOBs or NCLOBs or the number of bytes for BLOBs to be copied from the source LOB to the destination LOB.
- `offset` - The absolute offset for the destination LOB. For character LOBs, it is the number of characters from the beginning of the LOB at which
to begin writing. For binary LOBs, it is the number of bytes from the beginning of the LOB.
The data are copied from the source BFILE to the destination internal LOB (BLOB or CLOB). No character set
conversions are performed when copying the BFILE data to a CLOB or NCLOB. Also, when binary data is loaded
into a BLOB, no character set conversions are performed. Therefore, the BFILE data must be in the same
character set as the LOB in the database. No error checking is performed to verify this.
If the data exists at the destination's start position, it is overwritten with the source data. If the
destination's start position is beyond the end of the current data, zero-byte fillers (for BLOBs) or spaces
(for CLOBs) are written into the destination LOB from the end of the data to the beginning of the newly
written data from the source. The destination LOB is extended to accommodate the newly written data if it
extends beyond the current length of the destination LOB.
# Failures
- This function throws an error when a remote locator is passed to it.
- It is an error to try to copy from a NULL BFILE.
# Example
Note that this example assumes that the demo directories were created (@?/demo/schema/mk_dir) and
the test user has permissions to read them (see `etc/create_sandbox.sql`)
```
use sibyl::*;
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let lob = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
//-------------------
let file = BFile::new(&session)?;
file.set_file_name("MEDIA_DIR", "hello_world.txt")?;
let file_len = file.len().await?;
file.open_file().await?;
lob.load_from_file(&file, 0, file_len, 0).await?;
file.close_file().await?;
let lob_len = lob.len().await?;
assert_eq!(lob_len, 14);
let mut text = String::new();
lob.read(0, lob_len, &mut text).await?;
assert_eq!(text, "Hello, World!\n");
//-------------------
file.set_file_name("MEDIA_DIR", "konnichiwa_sekai.txt")?;
let file_len = file.len().await?;
file.open_file().await?;
lob.load_from_file(&file, 0, file_len, lob_len).await?;
file.close().await?;
let lob_len = lob.len().await?;
assert_eq!(lob_len, 14 + 9);
text.clear();
lob.read(0, lob_len, &mut text).await?;
assert_eq!(text, "Hello, World!\nこんにちは世界!\n");
//-------------------
file.set_file_name("MEDIA_DIR", "hello_supplemental.txt")?;
let file_len = file.len().await?;
file.open_file().await?;
lob.load_from_file(&file, 0, file_len, lob_len).await?;
file.close().await?;
let lob_len = lob.len().await?;
// Note that Oracle encoded 4 symbols (see below) into 8 "characters"
assert_eq!(lob_len, 14 + 9 + 9);
text.clear();
// The reading stops at the end of the LOB value if we request more
// characters than the LOB contains
let num_read = lob.read(0, 100, &mut text).await?;
assert_eq!(num_read, 14 + 9 + 9);
assert_eq!(text, "Hello, World!\nこんにちは世界!\n🚲🛠📬🎓\n");
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn load_from_file(&self, src: &'a BFile<'a>, src_offset: usize, amount: usize, offset: usize) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let dst = self.get_lob_ptr();
let src = Ptr::<OCILobLocator>::from(src.as_ref());
let res = self.execute_blocking(move || -> i32 {
unsafe {
OCILobLoadFromFile2(
svc.get(), err.get(), dst.get(), src.get(),
amount as _, (offset + 1) as _, (src_offset + 1) as _
)
}
}).await?;
check_oci_result(res, err)
}
/**
Erases a specified portion of the internal LOB data starting at a specified offset.
For BLOBs, erasing means that zero-byte fillers overwrite the existing LOB value.
For CLOBs, erasing means that spaces overwrite the existing LOB value.
# Parameters
- `offset` - Absolute offset in characters (for CLOBs or NCLOBs) or bytes (for BLOBs)
to the start of the LOB fragment to erase
- `amount` - The number of characters or bytes to erase.
# Returns
The actual number of characters or bytes erased.
*/
pub async fn erase(&self, offset: usize, amount: usize) -> Result<usize> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
self.execute_blocking(move || -> Result<usize> {
let mut len = amount as u64;
let res = unsafe {
OCILobErase2(svc.get(), err.get(), loc.get(), &mut len, offset as _)
};
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(len as usize)
}
}).await?
}
/**
Returns the chunk size (in bytes) of a LOB.
For LOBs with storage parameter BASICFILE, chunk size is amount of a chunk's space that is
used to store the internal LOB value. This is the amount that users should use when reading
or writing the LOB value. If possible, users should start their writes at chunk boundaries,
such as the beginning of a chunk, and write a chunk at a time.
For LOBs with storage parameter SECUREFILE, chunk size is an advisory size and is provided
for backward compatibility.
When creating a table that contains an internal LOB, the user can specify the chunking factor,
which can be a multiple of Oracle Database blocks. This corresponds to the chunk size used by
the LOB data layer when accessing and modifying the LOB value. Part of the chunk is used to store
system-related information, and the rest stores the LOB value. This function returns the amount
of space used in the LOB chunk to store the LOB value. Performance is improved if the application
issues read or write requests using a multiple of this chunk size. For writes, there is an added
benefit because LOB chunks are versioned and, if all writes are done on a chunk basis, no extra
versioning is done or duplicated. Users could batch up the write until they have enough for a chunk
instead of issuing several write calls for the same chunk.
*/
pub async fn chunk_size(&self) -> Result<usize> {
let mut size = self.chunk_size.load(Ordering::Relaxed);
if size == 0 {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
size = self.execute_blocking(move || -> Result<u32> {
let mut chunk_size = size;
let res = unsafe { OCILobGetChunkSize(svc.get(), err.get(), loc.get(), &mut chunk_size) };
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(chunk_size)
}
}).await??;
self.chunk_size.store(size, Ordering::Relaxed);
}
Ok(size as usize)
}
/**
Returns he user-specified content type string for the data in a SecureFile, if set.
This function only works on SecureFiles.
*/
pub async fn content_type(&self) -> Result<String> {
let env = self.get_env_ptr();
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
self.execute_blocking(move || -> Result<String> {
let mut buf = Vec::with_capacity(OCI_LOB_CONTENTTYPE_MAXSIZE);
let mut len = 0u32;
let res = unsafe {
OCILobGetContentType(env.get(), svc.get(), err.get(), loc.get(), buf.as_mut_ptr(), &mut len, 0)
};
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
let txt = unsafe {
buf.set_len(len as _);
String::from_utf8_unchecked(buf.as_slice().to_vec())
};
Ok(txt)
}
}).await?
}
/**
Sets a content type string for the data in the SecureFile to something that can be used by an application.
This function only works on SecureFiles.
*/
pub async fn set_content_type(&self, content_type: &str) -> Result<()> {
let env = self.get_env_ptr();
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let ctx_ptr = Ptr::new(content_type.as_ptr());
let ctx_len = content_type.len() as u32;
let res = self.execute_blocking(move || -> i32 {
unsafe {
OCILobSetContentType(
env.get(), svc.get(), err.get(), loc.get(),
ctx_ptr.get(), ctx_len, 0
)
}
}).await?;
check_oci_result(res, err)
}
/**
Opens an internal LOB for reading and writing.
Opening a LOB requires a round-trip to the server for both internal and external LOBs. For internal
LOBs, the open triggers other code that relies on the open call. For external LOBs (BFILEs), open
requires a round-trip because the actual operating system file on the server side is being opened.
It is not mandatory that you wrap all LOB operations inside the open and close calls. However, if you
open a LOB, then you must close it before you commit your transaction. When an internal LOB is closed,
it updates the functional and domain indexes on the LOB column. It is an error to commit the transaction
before closing all opened LOBs that were opened by the transaction.
When the error is returned, the LOB is no longer marked as open, but the transaction is successfully
committed. Hence, all the changes made to the LOB and non-LOB data in the transaction are committed,
but the domain and function-based indexing are not updated. If this happens, rebuild your functional
and domain indexes on the LOB column.
It is not necessary to open a LOB to perform operations on it. When using function-based indexes,
extensible indexes or context, and making multiple calls to update or write to the LOB, you should
first call `open`, then update the LOB as many times as you want, and finally call `close`. This
sequence of operations ensures that the indexes are only updated once at the end of all the write
operations instead of once for each write operation.
If you do not wrap your LOB operations inside the open or close API, then the functional
and domain indexes are updated each time you write to the LOB. This can adversely affect
performance. If you have functional or domain indexes, Oracle recommends that you enclose
write operations to the LOB within the open or close statements.
# Failures
- It is an error to open the same LOB twice.
# Example
```
use sibyl::CLOB;
/*
CREATE TABLE test_lobs (
id INTEGER GENERATED ALWAYS AS IDENTITY,
text CLOB,
data BLOB,
ext_file BFILE
);
*/
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let stmt = session.prepare("
INSERT INTO test_lobs (text)
VALUES (Empty_Clob())
RETURNING text
INTO :NEW_TEXT
").await?;
let mut lob = CLOB::new(&session)?;
stmt.execute(&mut lob).await?;
let text = [
"Love seeketh not itself to please,\n",
"Nor for itself hath any care,\n",
"But for another gives its ease,\n",
"And builds a Heaven in Hell's despair.\n",
];
lob.open().await?;
/*
* It is not necessary to open a LOB to perform operations on it.
* When using function-based indexes, extensible indexes or context,
* and making multiple calls to update or write to the LOB, you should
* first call `open`, then update the LOB as many times as you want,
* and finally call `close`. This sequence of operations ensures that
* the indexes are only updated once at the end of all the write
* operations instead of once for each write operation.
*
* If you do not wrap your LOB operations inside the open or close API,
* then the functional and domain indexes are updated each time you write
* to the LOB. This can adversely affect performance.
*
* If you have functional or domain indexes, Oracle recommends that you
* enclose write operations to the LOB within the open or close statements.
*/
lob.append(text[0]).await?;
lob.append(text[1]).await?;
lob.append(text[2]).await?;
lob.append(text[3]).await?;
lob.close().await?;
session.commit().await?;
assert_eq!(lob.len().await?, text[0].len() + text[1].len() + text[2].len() + text[3].len());
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn open(&self) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let res = self.execute_blocking(move || -> i32 {
unsafe { OCILobOpen(svc.get(), err.get(), loc.get(), OCI_LOB_READWRITE) }
}).await?;
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
self.inner.status_flags.fetch_or(LOB_IS_OPEN, Ordering::Relaxed);
Ok(())
}
}
/**
Truncates the LOB value to a shorter length.
# Parameters
- `new_len` - new LOB length.
For character LOBs, `new_len` is the number of characters; for binary LOBs and BFILEs, it is the number
of bytes in the LOB.
*/
pub async fn trim(&self, new_len: usize) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let res = self.execute_blocking(move || -> i32 {
unsafe { OCILobTrim2(svc.get(), err.get(), loc.get(), new_len as _) }
}).await?;
check_oci_result(res, err)
}
async fn write_piece(&self, piece: u8, offset: usize, cs_form: u8, data: &[u8]) -> Result<(usize,usize)> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let data_ptr = Ptr::new(data.as_ptr());
let data_len = data.len() as u64;
self.execute_blocking(move || -> Result<(usize, usize)> {
let mut num_bytes = if piece == OCI_ONE_PIECE { data_len } else { 0u64 };
let mut num_chars = 0u64;
let res = unsafe {
OCILobWrite2(
svc.get(), err.get(), loc.get(),
&mut num_bytes, &mut num_chars, (offset + 1) as _,
data_ptr.get(), data_len, piece,
std::ptr::null(), std::ptr::null(),
AL32UTF8, cs_form
)
};
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok((num_bytes as usize, num_chars as usize))
}
}).await?
}
async fn append_piece(&self, piece: u8, cs_form: u8, data: &[u8]) -> Result<(usize,usize)> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let data_ptr = Ptr::new(data.as_ptr());
let data_len = data.len() as u64;
self.execute_blocking(move || -> Result<(usize, usize)> {
let mut num_bytes = if piece == OCI_ONE_PIECE { data_len } else { 0u64 };
let mut num_chars = 0u64;
let res = unsafe {
OCILobWriteAppend2(
svc.get(), err.get(), loc.get(),
&mut num_bytes, &mut num_chars,
data_ptr.get(), data_len, piece,
std::ptr::null(), std::ptr::null(),
AL32UTF8, cs_form
)
};
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok((num_bytes as usize, num_chars as usize))
}
}).await?
}
async fn create_temp(session: &'a Session<'a>, csform: u8, lob_type: u8, cache: Cache) -> Result<LOB<'a,T>> {
let lob_descriptor = Descriptor::new(session)?;
let svc: Ptr<OCISvcCtx> = Ptr::from(session.as_ref());
let err: Ptr<OCIError> = Ptr::from(session.as_ref());
let lob: &OCILobLocator = &lob_descriptor;
let lob = Ptr::from(lob);
let svc_ctx = session.get_svc();
svc_ctx.set_blocking_mode()?;
let res = task::execute_blocking(move || -> i32 {
unsafe {
OCILobCreateTemporary(
svc.get(), err.get(), lob.get(),
OCI_DEFAULT as _, csform, lob_type, cache as _, OCI_DURATION_SESSION
)
}
}).await;
svc_ctx.set_nonblocking_mode()?;
let res = res?;
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(Self::make_temp(lob_descriptor, session))
}
}
}
impl<'a> LOB<'a,OCICLobLocator> {
/**
Creates an empty temporary CLOB or NCLOB and its corresponding index in the user's temporary tablespace.
# Parameters
- `csform` - The LOB character set form of the data.
- `cache` - Indicates whether the temporary LOB should be read into the cache.
The temporary LOB is freed automatically either when a LOB goes out of scope or at the end of the session whichever comes first.
*/
pub async fn temp(session: &'a Session<'a>, csform: CharSetForm, cache: Cache) -> Result<LOB<'a,OCICLobLocator>> {
Self::create_temp(session, csform as _, OCI_TEMP_CLOB, cache).await
}
/**
Writes a buffer into a LOB.
# Parameters
- `offset` - the absolute offset (in number of characters) from the beginning of the LOB,
- `text` - slice of text to be written into this LOB.
# Returns
The number of characters written to the database.
# Example
```
use sibyl::{CLOB, Cache, CharSetForm};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let lob = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
let text = "tête-à-tête";
assert_eq!(text.len(), 14); // byte count
let written = lob.write(4, text).await?;
// Note that auto inserted spaces at 0..4 are not included
// in the number of characters written
assert_eq!(written, 11); // char count
// Note that initially the CLOB was empty, so writing at offset 4
// inserted 4 spaces before the text we were writing:
let lob_len_in_chars = lob.len().await?;
let mut lob_content = String::new();
let num_chars_read = lob.read(0, lob_len_in_chars, &mut lob_content).await?;
assert_eq!(num_chars_read, 15);
assert_eq!(lob_content, " tête-à-tête");
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn write(&self, offset: usize, text: &str) -> Result<usize> {
let cs_form = self.charset_form()? as u8;
let (_, char_count) = self.write_piece(OCI_ONE_PIECE, offset, cs_form, text.as_bytes()).await?;
Ok(char_count)
}
/**
Writes data starting at the end of a LOB.
# Parameters
* `text` - the text to be written into this LOB.
# Returns
The number of characters written to the database.
# Example
```
use sibyl::{CLOB, Cache, CharSetForm};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let lob = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
let written = lob.append("Hello, World!").await?;
assert_eq!(written, 13);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn append(&self, text: &str) -> Result<usize> {
let cs_form = self.charset_form()? as u8;
let (_, char_count) = self.append_piece(OCI_ONE_PIECE, cs_form, text.as_bytes()).await?;
Ok(char_count)
}
/**
Reads specified number of characters from this LOB, appending them to `buf`.
# Parameters
* `offset` - Offset in characters from the start of the LOB.
* `len` - The number of characters to read
* `out` - The output buffer
# Returns
The total number of characters read.
# Example
```
use sibyl::{CLOB, Cache, CharSetForm};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let lob = CLOB::temp(&session, CharSetForm::Implicit, Cache::No).await?;
lob.write(4, "tête-à-tête").await?;
let mut lob_content = String::new();
// There are 15 characters in the LOB - 11 from the text we
// inserted and 4 padding spaced at 0..4.
// We can request to read more than that. LOB will stop at the
// end of its value and return the actual number of characters
// that were read.
let num_chars_read = lob.read(0, 100, &mut lob_content).await?;
assert_eq!(num_chars_read, 15);
assert_eq!(lob_content, " tête-à-tête");
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn read(&self, offset: usize, len: usize, buf: &mut String) -> Result<usize> {
if len == 0 {
return Ok(len);
}
let buf = unsafe { buf.as_mut_vec() };
let bytes_available = buf.capacity() - buf.len();
let bytes_needed = len * 4;
if bytes_needed > bytes_available {
buf.reserve(bytes_needed - bytes_available);
}
let cs_form = self.charset_form()? as u8;
let (_, _, char_count) = self.read_piece(OCI_ONE_PIECE, bytes_needed, offset, 0, len, cs_form, buf).await?;
Ok(char_count)
}
}
impl<'a> LOB<'a,OCIBLobLocator> {
/**
Creates an empty temporary BLOB and its corresponding index in the user's temporary tablespace.
# Parameters
- `cache` - Indicates whether the temporary LOB should be read into the cache.
The temporary LOB is freed automatically either when a LOB goes out of scope or at the end of the session whichever comes first.
*/
pub async fn temp(session: &'a Session<'a>, cache: Cache) -> Result<LOB<'a,OCIBLobLocator>> {
Self::create_temp(session, 0u8, OCI_TEMP_BLOB, cache).await
}
/**
Writes a buffer into a LOB.
# Parameters
- `offset` - the number of bytes from the beginning of the LOB
- `data` - slice of bytes to write into this LOB
# Returns
The number of bytes written to the database.
# Example
```
use sibyl::{BLOB, Cache};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let lob = BLOB::temp(&session, Cache::No).await?;
let written = lob.write(3, "Hello, World!".as_bytes()).await?;
assert_eq!(written, 13);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn write(&self, offset: usize, data: &[u8]) -> Result<usize> {
let (byte_count, _) = self.write_piece(OCI_ONE_PIECE, offset, 0, data).await?;
Ok(byte_count)
}
/**
Writes data starting at the end of a LOB.
# Parameters
- `data` - bytes to append to this LOB
# Returns
The number of bytes written to the database.
# Example
```
use sibyl::{BLOB, Cache};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let lob = BLOB::temp(&session, Cache::No).await?;
let written = lob.append("Hello, World!".as_bytes()).await?;
assert_eq!(13, written);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn append(&self, data: &[u8]) -> Result<usize> {
let (byte_count, _) = self.append_piece(OCI_ONE_PIECE, 0, data).await?;
Ok(byte_count)
}
/**
Reads specified number of bytes from this LOB, appending them to `buf`.
# Parameters
- `offset` - The absolute offset (in bytes) from the beginning of the LOB value.
- `len` - The maximum number of bytes to read into the buffer.
- `buf` - The output buffer.
# Returns
The number of bytes that were read and appended to `buf`.
# Example
```
use sibyl::{BLOB, Cache, BFile};
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let lob = BLOB::temp(&session, Cache::No).await?;
let file = BFile::new(&session)?;
file.set_file_name("MEDIA_DIR", "modem.jpg")?;
assert!(file.file_exists().await?);
let file_len = file.len().await?;
file.open_readonly().await?;
lob.load_from_file(&file, 0, file_len, 0).await?;
file.close_file().await?;
assert_eq!(lob.len().await?, file_len);
let mut data = Vec::new();
let num_read = lob.read(0, file_len, &mut data).await?;
assert_eq!(num_read, file_len);
assert_eq!(data.len(), file_len);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn read(&self, offset: usize, len: usize, buf: &mut Vec<u8>) -> Result<usize> {
if len == 0 {
return Ok(0);
}
let (_, byte_count, _) = self.read_piece(OCI_ONE_PIECE, len, offset, len, 0, 0, buf).await?;
Ok( byte_count )
}
}
impl<'a> LOB<'a,OCIBFileLocator> {
/**
Closes a previously opened BFILE.
No error is returned if the BFILE exists but is not opened.
This function is only meaningful the first time it is called for a particular
BFILE locator. Subsequent calls to this function using the same BFILE locator
have no effect.
*/
pub async fn close_file(&self) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let res = self.execute_blocking(move || -> i32 {
unsafe { OCILobFileClose(svc.get(), err.get(), loc.get()) }
}).await?;
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
self.inner.status_flags.fetch_and(!LOB_FILE_IS_OPEN, Ordering::Relaxed);
Ok(())
}
}
/**
Tests to see if the BFILE exists on the server's operating system.
# Example
```
use sibyl::BFile;
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let file = BFile::new(&session)?;
file.set_file_name("MEDIA_DIR", "formatted_doc.txt")?;
assert!(file.file_exists().await?);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn file_exists(&self) -> Result<bool> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
self.execute_blocking(move || -> Result<bool> {
let mut flag = Aligned::new(0u8);
let res = unsafe { OCILobFileExists(svc.get(), err.get(), loc.get(), flag.as_mut_ptr()) };
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(<u8>::from(flag) != 0)
}
}).await?
}
/**
Returns `true` if the BFILE was opened using this particular locator.
However, a different locator may have the file open. Openness is associated
with a particular locator.
# Example
```
use sibyl::BFile;
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let file = BFile::new(&session)?;
file.set_file_name("MEDIA_DIR", "hello_world.txt")?;
assert!(!file.is_file_open().await?);
file.open_file().await?;
assert!(file.is_file_open().await?);
file.close_file().await?;
assert!(!file.is_file_open().await?);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn is_file_open(&self) -> Result<bool> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let res = self.execute_blocking(move || -> Result<bool> {
let mut flag = Aligned::new(0u8);
let res = unsafe { OCILobFileIsOpen(svc.get(), err.get(), loc.get(), flag.as_mut_ptr()) };
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
Ok(<u8>::from(flag) != 0)
}
}).await?;
if res.is_ok() {
self.inner.status_flags.fetch_or(LOB_FILE_IS_OPEN, Ordering::Relaxed);
}
res
}
/**
Opens a BFILE on the file system of the server. The BFILE can only be opened
for read-only access. BFILEs can not be written through Oracle Database.
This function is only meaningful the first time it is called for a particular
BFILE locator. Subsequent calls to this function using the same BFILE locator
have no effect.
# Returns
True if the BFILE was opened using this particular locator; returns false if it was not.
# Example
See [`LOB<T>::is_file_open()`]
*/
pub async fn open_file(&self) -> Result<()> {
let svc = self.get_svc_ptr();
let err = self.get_err_ptr();
let loc = self.get_lob_ptr();
let res = self.execute_blocking(move || -> i32 {
unsafe { OCILobFileOpen(svc.get(), err.get(), loc.get(), OCI_FILE_READONLY) }
}).await?;
if res < 0 {
Err(Error::oci(err.as_ref(), res))
} else {
self.inner.status_flags.fetch_or(LOB_FILE_IS_OPEN, Ordering::Relaxed);
Ok(())
}
}
/**
Reads specified number of bytes from this LOB, appending them to `buf`.
# Parameters
- `offset` - The absolute offset (in bytes) from the beginning of the LOB value.
- `len` - The total maximum number of bytes to read.
- `buf` - The output buffer.
# Returns
The number of bytes that were read and appended to `buf`.
# Example
```
use sibyl::BFile;
# sibyl::block_on(async {
# let session = sibyl::test_env::get_session().await?;
let file = BFile::new(&session)?;
file.set_file_name("MEDIA_DIR", "hello_world.txt")?;
let file_len = file.len().await?;
file.open_file().await?;
let mut data = Vec::new();
let num_read = file.read(0, file_len, &mut data).await?;
assert_eq!(num_read, file_len);
assert_eq!(data, [0xfeu8, 0xff, 0x00, 0x48, 0x00, 0x65, 0x00, 0x6c, 0x00, 0x6c, 0x00, 0x6f, 0x00, 0x2c, 0x00, 0x20, 0x00, 0x57, 0x00, 0x6f, 0x00, 0x72, 0x00, 0x6c, 0x00, 0x64, 0x00, 0x21, 0x00, 0x0a]);
# Ok::<(),sibyl::Error>(()) }).expect("Ok from async");
```
*/
pub async fn read(&self, offset: usize, len: usize, buf: &mut Vec<u8>) -> Result<usize> {
if len == 0 {
return Ok(0);
}
let (_, byte_count, _) = self.read_piece(OCI_ONE_PIECE, len, offset, len, 0, 0, buf).await?;
Ok(byte_count)
}
}