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
|
/*
* Copyright 2006-2010 Amazon Technologies, Inc. or its affiliates.
* Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
* of Amazon Technologies, Inc. or its affiliates. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.amazon.carbonado.spi;
import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Map;
import java.util.List;
import org.cojen.util.KeyFactory;
import com.amazon.carbonado.Cursor;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.FetchNoneException;
import com.amazon.carbonado.IsolationLevel;
import com.amazon.carbonado.PersistException;
import com.amazon.carbonado.PersistNoneException;
import com.amazon.carbonado.Repository;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.Transaction;
import com.amazon.carbonado.Trigger;
import com.amazon.carbonado.info.StorableInfo;
import com.amazon.carbonado.info.StorableIntrospector;
import com.amazon.carbonado.info.StorableProperty;
import com.amazon.carbonado.lob.AbstractBlob;
import com.amazon.carbonado.lob.Blob;
import com.amazon.carbonado.lob.BlobClob;
import com.amazon.carbonado.lob.Clob;
import com.amazon.carbonado.lob.Lob;
import com.amazon.carbonado.sequence.SequenceValueGenerator;
import com.amazon.carbonado.sequence.SequenceValueProducer;
import com.amazon.carbonado.util.SoftValuedCache;
/**
* Complete Lob support for repositories, although repository is responsible
* for binding Lob properties to this engine. Lobs are referenced by locators,
* which are non-zero long integers. A zero locator is equivalent to null.
*
* @author Brian S O'Neill
* @see #getSupportTrigger(Class, int)
*/
public class LobEngine {
public static <S extends Storable> boolean hasLobs(Class<S> type) {
StorableInfo<S> info = StorableIntrospector.examine(type);
for (StorableProperty<? extends S> prop : info.getAllProperties().values()) {
if (Lob.class.isAssignableFrom(prop.getType())) {
return true;
}
}
return false;
}
static IOException toIOException(RepositoryException e) {
IOException ioe = new IOException(e.getMessage());
ioe.initCause(e);
return ioe;
}
final Repository mRepo;
final Storage<StoredLob> mLobStorage;
final Storage<StoredLob.Block> mLobBlockStorage;
final SequenceValueProducer mLocatorSequence;
private SoftValuedCache mTriggers;
/**
* @param lobRepo storage for Lobs - should not be replicated
* @param locatorRepo storage for producing unique values for Lob locators
* - should be root repository
* @since 1.2
*/
public LobEngine(Repository lobRepo, Repository locatorRepo) throws RepositoryException {
// Cannot reliably use sequences provided by Lob repository, since
// LobEngine is used internally by repositories.
this(lobRepo, new SequenceValueGenerator(locatorRepo, StoredLob.class.getName()));
}
/**
* @param lobRepo storage for Lobs - should not be replicated
* @param locatorSequenceProducer source of unique values for Lob locators
* @since 1.2
*/
public LobEngine(Repository lobRepo, SequenceValueProducer locatorSequenceProducer)
throws RepositoryException
{
mRepo = lobRepo;
mLobStorage = lobRepo.storageFor(StoredLob.class);
mLobBlockStorage = lobRepo.storageFor(StoredLob.Block.class);
mLocatorSequence = locatorSequenceProducer;
}
/**
* Returns a new Blob whose length is zero.
*
* @param blockSize block size (in <i>bytes</i>) to use
* @return new empty Blob
*/
public Blob createNewBlob(int blockSize) throws PersistException {
StoredLob lob = mLobStorage.prepare();
lob.setLocator(mLocatorSequence.nextLongValue());
lob.setBlockSize(blockSize);
lob.setLength(0);
lob.insert();
return new BlobImpl(lob.getLocator());
}
/**
* Returns a new Clob whose length is zero.
*
* @param blockSize block size (in <i>bytes</i>) to use
* @return new empty Clob
*/
public Clob createNewClob(int blockSize) throws PersistException {
StoredLob lob = mLobStorage.prepare();
lob.setLocator(mLocatorSequence.nextLongValue());
lob.setBlockSize(blockSize);
lob.setLength(0);
lob.insert();
return new ClobImpl(lob.getLocator());
}
/**
* Returns the locator for the given Lob, or zero if null.
*
* @throws ClassCastException if Lob is unrecognized
*/
public long getLocator(Lob lob) {
if (lob == null) {
return 0;
}
Long locator = (Long) lob.getLocator();
return locator == null ? 0 : locator;
}
/**
* Deletes Lob data, freeing up all space consumed by it.
*/
public void deleteLob(long locator) throws PersistException {
if (locator == 0) {
return;
}
Transaction txn = mRepo.enterTransaction(IsolationLevel.READ_COMMITTED);
try {
StoredLob lob = mLobStorage.prepare();
lob.setLocator(locator);
if (lob.tryDelete()) {
try {
mLobBlockStorage.query("locator = ?").with(lob.getLocator()).deleteAll();
} catch (FetchException e) {
throw e.toPersistException();
}
}
txn.commit();
} finally {
txn.exit();
}
}
/**
* Deletes Lob data, freeing up all space consumed by it.
*/
public void deleteLob(Lob lob) throws PersistException {
deleteLob(getLocator(lob));
}
/**
* Loads a Blob value, without checking if it exists or not.
*
* @param locator lob locator as returned by getLocator
* @return Blob value or null
*/
public Blob getBlobValue(long locator) {
if (locator == 0) {
return null;
}
return new BlobImpl(locator);
}
/**
* Loads a Clob value, without checking if it exists or not.
*
* @param locator lob locator as returned by getLocator
* @return Clob value or null
*/
public Clob getClobValue(long locator) {
if (locator == 0) {
return null;
}
return new ClobImpl(locator);
}
/**
* Stores a value into a Blob, replacing anything that was there
* before. Passing null deletes the Blob, which is a convenience for
* auto-generated code that may call this method.
*
* @param locator lob locator as created by createNewBlob
* @param data source of data for Blob, which may be null to delete
* @throws IllegalArgumentException if locator is zero
*/
public void setBlobValue(long locator, Blob data) throws PersistException, IOException {
if (data == null) {
deleteLob(locator);
return;
}
if (locator == 0) {
throw new IllegalArgumentException("Cannot use locator zero");
}
if (data instanceof BlobImpl) {
BlobImpl impl = (BlobImpl) data;
if (impl.getEnclosing() == this && impl.mLocator == locator) {
// Blob is ours and locator is the same, so nothing to do.
return;
}
}
try {
setBlobValue(locator, data.openInputStream(0, 0));
} catch (FetchException e) {
throw e.toPersistException();
}
}
/**
* Stores a value into a Blob, replacing anything that was there
* before. Passing null deletes the Blob, which is a convenience for
* auto-generated code that may call this method.
*
* @param locator lob locator as created by createNewBlob
* @param data source of data for Blob, which may be null to delete
* @throws IllegalArgumentException if locator is zero
*/
public void setBlobValue(long locator, InputStream data) throws PersistException, IOException {
if (data == null) {
deleteLob(locator);
return;
}
if (locator == 0) {
throw new IllegalArgumentException("Cannot use locator zero");
}
Transaction txn = mRepo.enterTransaction(IsolationLevel.READ_COMMITTED);
txn.setForUpdate(true);
try {
StoredLob lob = mLobStorage.prepare();
lob.setLocator(locator);
try {
lob.load();
} catch (FetchNoneException e) {
throw new PersistNoneException("Lob deleted: " + this);
}
Output out = new Output(lob, 0, txn);
byte[] buffer = new byte[lob.getBlockSize()];
long total = 0;
int amt;
try {
while ((amt = data.read(buffer)) > 0) {
out.write(buffer, 0, amt);
total += amt;
}
} finally {
data.close();
}
// Close but don't commit the transaction. This close is explicitly
// not put into a finally block in order for an exception to cause
// the transaction to rollback.
out.close(false);
if (total < lob.getLength()) {
// Adjust length after closing stream to avoid OptimisticLockException.
new BlobImpl(lob).setLength(total);
}
txn.commit();
} catch (IOException e) {
if (e.getCause() instanceof RepositoryException) {
RepositoryException re = (RepositoryException) e.getCause();
throw re.toPersistException();
}
throw e;
} catch (FetchException e) {
throw e.toPersistException();
} finally {
txn.exit();
}
}
/**
* Stores a value into a Clob, replacing anything that was there
* before. Passing null deletes the Clob, which is a convenience for
* auto-generated code that may call this method.
*
* @param locator lob locator as created by createNewClob
* @param data source of data for Clob, which may be null to delete
* @throws IllegalArgumentException if locator is zero
*/
public void setClobValue(long locator, Clob data) throws PersistException, IOException {
if (data == null) {
deleteLob(locator);
return;
}
if (locator == 0) {
throw new IllegalArgumentException("Cannot use locator zero");
}
if (data instanceof ClobImpl) {
BlobImpl impl = ((ClobImpl) data).getWrappedBlob();
if (impl.getEnclosing() == this && impl.mLocator == locator) {
// Blob is ours and locator is the same, so nothing to do.
return;
}
}
try {
setClobValue(locator, data.openReader(0, 0));
} catch (FetchException e) {
throw e.toPersistException();
}
}
/**
* Stores a value into a Clob, replacing anything that was there
* before. Passing null deletes the Clob, which is a convenience for
* auto-generated code that may call this method.
*
* @param locator lob locator as created by createNewClob
* @param data source of data for Clob, which may be null to delete
* @throws IllegalArgumentException if locator is zero
*/
public void setClobValue(long locator, Reader data) throws PersistException, IOException {
if (data == null) {
deleteLob(locator);
return;
}
if (locator == 0) {
throw new IllegalArgumentException("Cannot use locator zero");
}
Transaction txn = mRepo.enterTransaction(IsolationLevel.READ_COMMITTED);
txn.setForUpdate(true);
try {
StoredLob lob = mLobStorage.prepare();
lob.setLocator(locator);
try {
lob.load();
} catch (FetchNoneException e) {
throw new PersistNoneException("Lob deleted: " + this);
}
ClobImpl clob = new ClobImpl(lob);
Writer writer = clob.openWriter(0, 0);
char[] buffer = new char[lob.getBlockSize() >> 1];
long total = 0;
int amt;
try {
while ((amt = data.read(buffer)) > 0) {
writer.write(buffer, 0, amt);
total += amt;
}
} finally {
data.close();
}
writer.close();
if (total < lob.getLength()) {
clob.setLength(total);
}
txn.commit();
} catch (IOException e) {
if (e.getCause() instanceof RepositoryException) {
RepositoryException re = (RepositoryException) e.getCause();
throw re.toPersistException();
}
throw e;
} catch (FetchException e) {
throw e.toPersistException();
} finally {
txn.exit();
}
}
/**
* Returns a Trigger for binding to this LobEngine. Storage implementations
* which use LobEngine must install this Trigger. Trigger instances are
* cached, so subsequent calls for the same trigger return the same
* instance.
*
* @param type type of Storable to create trigger for
* @param blockSize block size to use
* @return support trigger or null if storable type has no lob properties
*/
public synchronized <S extends Storable> Trigger<S>
getSupportTrigger(Class<S> type, int blockSize)
{
Object key = KeyFactory.createKey(new Object[] {type, blockSize});
Trigger<S> trigger = (mTriggers == null) ? null : (Trigger<S>) mTriggers.get(key);
if (trigger == null) {
StorableInfo<S> info = StorableIntrospector.examine(type);
List<LobProperty<?>> lobProperties = null;
for (StorableProperty<? extends S> prop : info.getAllProperties().values()) {
if (Blob.class.isAssignableFrom(prop.getType())) {
if (lobProperties == null) {
lobProperties = new ArrayList<LobProperty<?>>();
}
lobProperties.add(new BlobProperty(this, prop.getName()));
} else if (Clob.class.isAssignableFrom(prop.getType())) {
if (lobProperties == null) {
lobProperties = new ArrayList<LobProperty<?>>();
}
lobProperties.add(new ClobProperty(this, prop.getName()));
}
}
if (lobProperties != null) {
trigger = new LobEngineTrigger<S>(this, type, blockSize, lobProperties);
}
if (mTriggers == null) {
mTriggers = SoftValuedCache.newCache(7);
}
mTriggers.put(key, trigger);
}
return trigger;
}
private class BlobImpl extends AbstractBlob implements Lob {
final Long mLocator;
final StoredLob mStoredLob;
BlobImpl(long locator) {
super(mRepo);
mLocator = locator;
mStoredLob = null;
}
BlobImpl(StoredLob lob) {
super(mRepo);
mLocator = lob.getLocator();
mStoredLob = lob;
}
public InputStream openInputStream() throws FetchException {
return openInputStream(0);
}
public InputStream openInputStream(long pos) throws FetchException {
if (pos < 0) {
throw new IllegalArgumentException("Position is negative: " + pos);
}
StoredLob lob = mStoredLob;
Transaction txn = mRepo.enterTransaction(IsolationLevel.READ_COMMITTED);
if (lob == null) {
lob = mLobStorage.prepare();
lob.setLocator(mLocator);
try {
lob.load();
} catch (FetchException e) {
try {
txn.exit();
} catch (PersistException e2) {
// Don't care.
}
if (e instanceof FetchNoneException) {
throw new FetchNoneException("Lob deleted: " + this);
}
throw e;
}
}
return new Input(lob, pos, txn);
}
public InputStream openInputStream(long pos, int bufferSize) throws FetchException {
return openInputStream(pos);
}
public long getLength() throws FetchException {
StoredLob lob = mStoredLob;
if (lob == null) {
lob = mLobStorage.prepare();
lob.setLocator(mLocator);
try {
lob.load();
} catch (FetchNoneException e) {
throw new FetchNoneException("Lob deleted: " + this);
}
}
return lob.getLength();
}
public OutputStream openOutputStream() throws PersistException {
return openOutputStream(0);
}
public OutputStream openOutputStream(long pos) throws PersistException {
if (pos < 0) {
throw new IllegalArgumentException("Position is negative: " + pos);
}
StoredLob lob = mStoredLob;
Transaction txn = mRepo.enterTransaction(IsolationLevel.READ_COMMITTED);
txn.setForUpdate(true);
try {
if (lob == null) {
lob = mLobStorage.prepare();
lob.setLocator(mLocator);
try {
lob.load();
} catch (FetchNoneException e) {
throw new PersistNoneException("Lob deleted: " + this);
} catch (FetchException e) {
throw e.toPersistException();
}
}
return new Output(lob, pos, txn);
} catch (PersistException e) {
try {
txn.exit();
} catch (PersistException e2) {
// Don't care.
}
throw e;
}
}
public OutputStream openOutputStream(long pos, int bufferSize) throws PersistException {
return openOutputStream(pos);
}
public void setLength(long length) throws PersistException {
if (length < 0) {
throw new IllegalArgumentException("Length is negative: " + length);
}
Transaction txn = mRepo.enterTransaction();
try {
StoredLob lob = mStoredLob;
if (lob == null) {
lob = mLobStorage.prepare();
lob.setLocator(mLocator);
txn.setForUpdate(true);
try {
lob.load();
} catch (FetchNoneException e) {
throw new PersistNoneException("Lob deleted: " + this);
}
txn.setForUpdate(false);
}
long oldLength = lob.getLength();
if (length == oldLength) {
return;
}
long oldBlockCount = lob.getBlockCount();
lob.setLength(length);
if (length < oldLength) {
// Free unused blocks.
long newBlockCount = lob.getBlockCount();
if (newBlockCount < oldBlockCount) {
mLobBlockStorage.query("locator = ? & blockNumber >= ?")
.with(lob.getLocator())
// Subtract 0x80000000 such that block zero is
// physically stored with the smallest integer.
.with(((int) newBlockCount) - 0x80000000)
.deleteAll();
}
// Clear space in last block.
int lastBlockLength = lob.getLastBlockLength();
if (lastBlockLength != 0) {
StoredLob.Block block = mLobBlockStorage.prepare();
block.setLocator(mLocator);
// Subtract 0x80000000 such that block zero is
// physically stored with the smallest
// integer. Subtract one more to convert one-based
// count to zero-based index.
block.setBlockNumber(((int) newBlockCount) - 0x80000001);
txn.setForUpdate(true);
if (block.tryLoad()) {
byte[] data = block.getData();
if (data.length > lastBlockLength) {
byte[] newData = new byte[lastBlockLength];
System.arraycopy(data, 0, newData, 0, lastBlockLength);
block.setData(newData);
block.update();
}
}
txn.setForUpdate(false);
}
}
lob.update();
txn.commit();
} catch (FetchException e) {
throw e.toPersistException();
} finally {
txn.exit();
}
}
public Long getLocator() {
return mLocator;
}
@Override
public int hashCode() {
return mLocator.hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof BlobImpl) {
BlobImpl other = (BlobImpl) obj;
return LobEngine.this == other.getEnclosing() && mLocator.equals(other.mLocator);
}
return false;
}
@Override
public String toString() {
return "Blob@" + getLocator();
}
LobEngine getEnclosing() {
return LobEngine.this;
}
}
private class ClobImpl extends BlobClob implements Lob {
ClobImpl(long locator) {
super(new BlobImpl(locator));
}
ClobImpl(StoredLob lob) {
super(new BlobImpl(lob));
}
@Override
public Long getLocator() {
return ((BlobImpl) super.getWrappedBlob()).getLocator();
}
@Override
public int hashCode() {
return super.getWrappedBlob().hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof ClobImpl) {
return getWrappedBlob().equals(((ClobImpl) obj).getWrappedBlob());
}
return false;
}
@Override
public String toString() {
return "Clob@" + getLocator();
}
// Override to gain permission.
@Override
protected BlobImpl getWrappedBlob() {
return (BlobImpl) super.getWrappedBlob();
}
}
private class Input extends InputStream {
private final long mLocator;
private final int mBlockSize;
private final long mLength;
private long mPos;
private int mBlockNumber;
private int mBlockPos;
private Transaction mTxn;
private Cursor<StoredLob.Block> mCursor;
private StoredLob.Block mStoredBlock;
Input(StoredLob lob, long pos, Transaction txn) throws FetchException {
mLocator = lob.getLocator();
mBlockSize = lob.getBlockSize();
mLength = lob.getLength();
mPos = pos;
mBlockNumber = ((int) (pos / mBlockSize)) - 0x80000000;
mBlockPos = (int) (pos % mBlockSize);
mTxn = txn;
mCursor = mLobBlockStorage.query("locator = ? & blockNumber >= ?")
.with(mLocator).with(mBlockNumber)
.fetch();
}
@Override
public synchronized int read() throws IOException {
if (mCursor == null) {
throw new IOException("Closed");
}
if (mPos >= mLength) {
return -1;
}
byte[] block = getBlockData();
int blockPos = mBlockPos;
int b;
if (block == null || blockPos >= block.length) {
b = 0;
} else {
b = block[blockPos] & 0xff;
}
mPos++;
if (++blockPos >= mBlockSize) {
mBlockNumber++;
blockPos = 0;
}
mBlockPos = blockPos;
return b;
}
@Override
public int read(byte[] bytes) throws IOException {
return read(bytes, 0, bytes.length);
}
@Override
public synchronized int read(byte[] bytes, int offset, int length) throws IOException {
if (length <= 0) {
return 0;
}
if (mCursor == null) {
throw new IOException("Closed");
}
int avail = Math.min((int) (mLength - mPos), mBlockSize - mBlockPos);
if (avail <= 0) {
return -1;
}
if (length > avail) {
length = avail;
}
byte[] block = getBlockData();
int blockPos = mBlockPos;
if (block == null) {
Arrays.fill(bytes, offset, offset + length, (byte) 0);
} else {
int blockAvail = block.length - blockPos;
if (blockAvail >= length) {
System.arraycopy(block, blockPos, bytes, offset, length);
} else {
System.arraycopy(block, blockPos, bytes, offset, blockAvail);
Arrays.fill(bytes, offset + blockAvail, offset + length, (byte) 0);
}
}
mPos += length;
if ((blockPos += length) >= mBlockSize) {
mBlockNumber++;
blockPos = 0;
}
mBlockPos = blockPos;
return length;
}
@Override
public synchronized long skip(long n) throws IOException {
if (n <= 0) {
return 0;
}
if (mCursor == null) {
throw new IOException("Closed");
}
long oldPos = mPos;
if (n > Integer.MAX_VALUE) {
n = Integer.MAX_VALUE;
}
long newPos = oldPos + n;
if (newPos >= mLength) {
newPos = mLength;
n = newPos - oldPos;
if (n <= 0) {
return 0;
}
}
// Note: could open a new cursor here, but we'd potentially lose
// the thread-local transaction. The next call to getBlockData will
// detect that the desired block number differs from the actual one
// and will skip one block at a time until cursor is at the correct
// position.
mPos = newPos;
mBlockNumber = ((int) (newPos / mBlockSize)) - 0x80000000;
mBlockPos = (int) (newPos % mBlockSize);
return n;
}
@Override
public synchronized void close() throws IOException {
if (mTxn != null) {
try {
// This should also cause the cursor to close.
mTxn.exit();
} catch (PersistException e) {
throw toIOException(e);
}
mTxn = null;
}
if (mCursor != null) {
try {
mCursor.close();
} catch (FetchException e) {
throw toIOException(e);
}
mCursor = null;
mStoredBlock = null;
}
}
// Caller must be synchronized and have checked if stream is closed
private byte[] getBlockData() throws IOException {
while (mStoredBlock == null || mBlockNumber > mStoredBlock.getBlockNumber()) {
try {
if (!mCursor.hasNext()) {
mStoredBlock = null;
return null;
}
mStoredBlock = mCursor.next();
} catch (FetchException e) {
try {
close();
} catch (IOException e2) {
// Don't care.
}
throw toIOException(e);
}
}
if (mBlockNumber < mStoredBlock.getBlockNumber()) {
return null;
}
return mStoredBlock.getData();
}
}
private class Output extends OutputStream {
private final StoredLob mStoredLob;
private long mPos;
private int mBlockNumber;
private int mBlockPos;
private Transaction mTxn;
private StoredLob.Block mStoredBlock;
private byte[] mBlockData;
private int mBlockLength;
private boolean mDoInsert;
Output(StoredLob lob, long pos, Transaction txn) throws PersistException {
mStoredLob = lob;
mPos = pos;
mBlockNumber = ((int) (pos / lob.getBlockSize())) - 0x80000000;
mBlockPos = (int) (pos % lob.getBlockSize());
mTxn = txn;
}
@Override
public synchronized void write(int b) throws IOException {
if (mTxn == null) {
throw new IOException("Closed");
}
prepareBlockData();
int blockPos = mBlockPos;
if (blockPos >= mBlockData.length) {
byte[] newBlockData = new byte[mStoredLob.getBlockSize()];
System.arraycopy(mBlockData, 0, newBlockData, 0, mBlockData.length);
mBlockData = newBlockData;
}
mBlockData[blockPos++] = (byte) b;
if (blockPos > mBlockLength) {
mBlockLength = blockPos;
}
if (blockPos >= mStoredLob.getBlockSize()) {
mBlockNumber++;
blockPos = 0;
}
mBlockPos = blockPos;
mPos++;
}
@Override
public void write(byte[] bytes) throws IOException {
write(bytes, 0, bytes.length);
}
@Override
public synchronized void write(byte[] bytes, int offset, int length) throws IOException {
if (length <= 0) {
return;
}
if (mTxn == null) {
throw new IOException("Closed");
}
while (length > 0) {
prepareBlockData();
int avail = mStoredLob.getBlockSize() - mBlockPos;
if (avail > length) {
avail = length;
}
if ((mBlockPos + avail) >= mBlockData.length) {
byte[] newBlockData = new byte[mStoredLob.getBlockSize()];
System.arraycopy(mBlockData, 0, newBlockData, 0, mBlockData.length);
mBlockData = newBlockData;
}
System.arraycopy(bytes, offset, mBlockData, mBlockPos, avail);
offset += avail;
length -= avail;
mBlockPos += avail;
if (mBlockPos > mBlockLength) {
mBlockLength = mBlockPos;
}
if (mBlockPos >= mStoredLob.getBlockSize()) {
mBlockNumber++;
mBlockPos = 0;
}
mPos += avail;
}
}
@Override
public synchronized void flush() throws IOException {
if (mTxn == null) {
throw new IOException("Closed");
}
try {
updateBlock();
} catch (PersistException e) {
try {
close();
} catch (IOException e2) {
// Don't care.
}
throw toIOException(e);
}
}
@Override
public void close() throws IOException {
close(true);
}
synchronized void close(boolean commit) throws IOException {
if (mTxn != null) {
try {
updateBlock();
if (mPos > mStoredLob.getLength()) {
mStoredLob.setLength(mPos);
mStoredLob.update();
}
if (commit) {
mTxn.commit();
}
} catch (PersistException e) {
throw toIOException(e);
} finally {
if (commit) {
try {
mTxn.exit();
} catch (PersistException e) {
throw toIOException(e);
}
}
}
mTxn = null;
}
}
// Caller must be synchronized and have checked if stream is closed
private void updateBlock() throws PersistException {
if (mStoredBlock != null) {
byte[] blockData = mBlockData;
if (blockData.length != mBlockLength) {
byte[] truncated = new byte[mBlockLength];
System.arraycopy(blockData, 0, truncated, 0, truncated.length);
blockData = truncated;
}
mStoredBlock.setData(blockData);
if (mDoInsert) {
mStoredBlock.insert();
mDoInsert = false;
} else {
mStoredBlock.update();
}
}
}
// Caller must be synchronized and have checked if stream is closed
private void prepareBlockData() throws IOException {
if (mStoredBlock == null || mBlockNumber > mStoredBlock.getBlockNumber()) {
try {
updateBlock();
mStoredBlock = mLobBlockStorage.prepare();
mStoredBlock.setLocator(mStoredLob.getLocator());
mStoredBlock.setBlockNumber(mBlockNumber);
try {
if (mStoredBlock.tryLoad()) {
mBlockData = mStoredBlock.getData();
mBlockLength = mBlockData.length;
mDoInsert = false;
} else {
mBlockData = new byte[mStoredLob.getBlockSize()];
mBlockLength = 0;
mDoInsert = true;
}
} catch (FetchException e) {
throw e.toPersistException();
}
} catch (PersistException e) {
try {
close();
} catch (IOException e2) {
// Don't care.
}
throw toIOException(e);
}
}
}
}
}
|