Debian Codegraph @

lang/java/src/com/sleepycat/persist/impl/SimpleFormat.java

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 */
7
8 package com.sleepycat.persist.impl;
9
10 import java.lang.reflect.Field;
11 import java.math.BigDecimal;
12 import java.math.BigInteger;
13 import java.util.Date;
14 import java.util.Map;
15 import java.util.Set;
16
17 import com.sleepycat.compat.DbCompat;
18 import com.sleepycat.db.DatabaseEntry;
19 import com.sleepycat.persist.model.EntityModel;
20
21 /**
22 * Format for simple types, including primitives. Additional methods are
23 * included to optimize the handling of primitives. Other classes such as
24 * PrimitiveArrayFormat and ReflectAccessor take advantage of these methods.
25 *
26 * @author Mark Hayes
27 */
28 public abstract class SimpleFormat extends Format {
29
30 private static final long serialVersionUID = 4595245575868697702L;
31
32 private final boolean primitive;
33 private SimpleFormat wrapperFormat;
34
35 SimpleFormat(Catalog catalog, Class type, boolean primitive) {
36 super(catalog, type);
37 this.primitive = primitive;
38 }
39
40 void setWrapperFormat(SimpleFormat wrapperFormat) {
41 this.wrapperFormat = wrapperFormat;
42 }
43
44 @Override
45 Format getWrapperFormat() {
46 return wrapperFormat;
47 }
48
49 @Override
50 public boolean isSimple() {
51 return true;
52 }
53
54 @Override
55 public boolean isPrimitive() {
56 return primitive;
57 }
58
59 @Override
60 void collectRelatedFormats(Catalog catalog,
61 Map<String, Format> newFormats) {
62 }
63
64 @Override
65 void initialize(Catalog catalog, EntityModel model, int initVersion) {
66 }
67
68 @Override
69 public Object readObject(Object o, EntityInput input, boolean rawAccess) {
70 /* newInstance reads the value -- do nothing here. */
71 return o;
72 }
73
74 @Override
75 boolean evolve(Format newFormat, Evolver evolver) {
76 evolver.useOldFormat(this, newFormat);
77 return true;
78 }
79
80 /* -- Begin methods to be overridden by primitive formats only. -- */
81
82 Object newPrimitiveArray(int len, EntityInput input)
83 throws RefreshException {
84
85 throw DbCompat.unexpectedState();
86 }
87
88 void writePrimitiveArray(Object o, EntityOutput output) {
89 throw DbCompat.unexpectedState();
90 }
91
92 int getPrimitiveLength() {
93 throw DbCompat.unexpectedState();
94 }
95
96 /**
97 * @throws IllegalAccessException from subclasses.
98 */
99 void readPrimitiveField(Object o, EntityInput input, Field field)
100 throws IllegalAccessException, RefreshException {
101
102 throw DbCompat.unexpectedState();
103 }
104
105 /**
106 * @throws IllegalAccessException from subclasses.
107 */
108 void writePrimitiveField(Object o, EntityOutput output, Field field)
109 throws IllegalAccessException {
110
111 throw DbCompat.unexpectedState();
112 }
113
114 /* -- End methods to be overridden by primitive formats only. -- */
115
116 void skipPrimitiveArray(int len, RecordInput input) {
117 input.skipFast(len * getPrimitiveLength());
118 }
119
120 void copySecMultiKeyPrimitiveArray(int len,
121 RecordInput input,
122 Set results) {
123 int primLen = getPrimitiveLength();
124 for (int i = 0; i < len; i += 1) {
125 DatabaseEntry entry = new DatabaseEntry
126 (input.getBufferBytes(), input.getBufferOffset(), primLen);
127 results.add(entry);
128 input.skipFast(primLen);
129 }
130 }
131
132 public static class FBool extends SimpleFormat {
133
134 private static final long serialVersionUID = -7724949525068533451L;
135
136 FBool(Catalog catalog, boolean primitive) {
137 super(catalog, primitive ? Boolean.TYPE : Boolean.class,
138 primitive);
139 }
140
141 @Override
142 Object newArray(int len) {
143 return new Boolean[len];
144 }
145
146 @Override
147 public Object newInstance(EntityInput input, boolean rawAccess)
148 throws RefreshException {
149
150 return Boolean.valueOf(input.readBoolean());
151 }
152
153 @Override
154 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
155 output.writeBoolean(((Boolean) o).booleanValue());
156 }
157
158 @Override
159 void skipContents(RecordInput input) {
160 input.skipFast(1);
161 }
162
163 @Override
164 void copySecKey(RecordInput input, RecordOutput output) {
165 output.writeFast(input.readFast());
166 }
167
168 @Override
169 Object newPrimitiveArray(int len, EntityInput input)
170 throws RefreshException {
171
172 boolean[] a = new boolean[len];
173 for (int i = 0; i < len; i += 1) {
174 a[i] = input.readBoolean();
175 }
176 return a;
177 }
178
179 @Override
180 void writePrimitiveArray(Object o, EntityOutput output) {
181 boolean[] a = (boolean[]) o;
182 int len = a.length;
183 output.writeArrayLength(len);
184 for (int i = 0; i < len; i += 1) {
185 output.writeBoolean(a[i]);
186 }
187 }
188
189 @Override
190 int getPrimitiveLength() {
191 return 1;
192 }
193
194 @Override
195 void readPrimitiveField(Object o, EntityInput input, Field field)
196 throws IllegalAccessException, RefreshException {
197
198 field.setBoolean(o, input.readBoolean());
199 }
200
201 @Override
202 void writePrimitiveField(Object o, EntityOutput output, Field field)
203 throws IllegalAccessException {
204
205 output.writeBoolean(field.getBoolean(o));
206 }
207 }
208
209 public static class FByte extends SimpleFormat {
210
211 private static final long serialVersionUID = 3651752958101447257L;
212
213 FByte(Catalog catalog, boolean primitive) {
214 super(catalog, primitive ? Byte.TYPE : Byte.class, primitive);
215 }
216
217 @Override
218 Object newArray(int len) {
219 return new Byte[len];
220 }
221
222 @Override
223 public Object newInstance(EntityInput input, boolean rawAccess)
224 throws RefreshException {
225
226 return Byte.valueOf(input.readByte());
227 }
228
229 @Override
230 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
231 output.writeByte(((Number) o).byteValue());
232 }
233
234 @Override
235 void skipContents(RecordInput input) {
236 input.skipFast(1);
237 }
238
239 @Override
240 void copySecKey(RecordInput input, RecordOutput output) {
241 output.writeFast(input.readFast());
242 }
243
244 @Override
245 Object newPrimitiveArray(int len, EntityInput input)
246 throws RefreshException {
247
248 byte[] a = new byte[len];
249 for (int i = 0; i < len; i += 1) {
250 a[i] = input.readByte();
251 }
252 return a;
253 }
254
255 @Override
256 void writePrimitiveArray(Object o, EntityOutput output) {
257 byte[] a = (byte[]) o;
258 int len = a.length;
259 output.writeArrayLength(len);
260 for (int i = 0; i < len; i += 1) {
261 output.writeByte(a[i]);
262 }
263 }
264
265 @Override
266 int getPrimitiveLength() {
267 return 1;
268 }
269
270 @Override
271 void readPrimitiveField(Object o, EntityInput input, Field field)
272 throws IllegalAccessException, RefreshException {
273
274 field.setByte(o, input.readByte());
275 }
276
277 @Override
278 void writePrimitiveField(Object o, EntityOutput output, Field field)
279 throws IllegalAccessException {
280
281 output.writeByte(field.getByte(o));
282 }
283
284 @Override
285 Format getSequenceKeyFormat() {
286 return this;
287 }
288 }
289
290 public static class FShort extends SimpleFormat {
291
292 private static final long serialVersionUID = -4909138198491785624L;
293
294 FShort(Catalog catalog, boolean primitive) {
295 super(catalog, primitive ? Short.TYPE : Short.class, primitive);
296 }
297
298 @Override
299 Object newArray(int len) {
300 return new Short[len];
301 }
302
303 @Override
304 public Object newInstance(EntityInput input, boolean rawAccess)
305 throws RefreshException {
306
307 return Short.valueOf(input.readShort());
308 }
309
310 @Override
311 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
312 output.writeShort(((Number) o).shortValue());
313 }
314
315 @Override
316 void skipContents(RecordInput input) {
317 input.skipFast(2);
318 }
319
320 @Override
321 void copySecKey(RecordInput input, RecordOutput output) {
322 output.writeFast(input.readFast());
323 output.writeFast(input.readFast());
324 }
325
326 @Override
327 Object newPrimitiveArray(int len, EntityInput input)
328 throws RefreshException {
329
330 short[] a = new short[len];
331 for (int i = 0; i < len; i += 1) {
332 a[i] = input.readShort();
333 }
334 return a;
335 }
336
337 @Override
338 void writePrimitiveArray(Object o, EntityOutput output) {
339 short[] a = (short[]) o;
340 int len = a.length;
341 output.writeArrayLength(len);
342 for (int i = 0; i < len; i += 1) {
343 output.writeShort(a[i]);
344 }
345 }
346
347 @Override
348 int getPrimitiveLength() {
349 return 2;
350 }
351
352 @Override
353 void readPrimitiveField(Object o, EntityInput input, Field field)
354 throws IllegalAccessException, RefreshException {
355
356 field.setShort(o, input.readShort());
357 }
358
359 @Override
360 void writePrimitiveField(Object o, EntityOutput output, Field field)
361 throws IllegalAccessException {
362
363 output.writeShort(field.getShort(o));
364 }
365
366 @Override
367 Format getSequenceKeyFormat() {
368 return this;
369 }
370 }
371
372 public static class FInt extends SimpleFormat {
373
374 private static final long serialVersionUID = 2695910006049980013L;
375
376 FInt(Catalog catalog, boolean primitive) {
377 super(catalog, primitive ? Integer.TYPE : Integer.class,
378 primitive);
379 }
380
381 @Override
382 Object newArray(int len) {
383 return new Integer[len];
384 }
385
386 @Override
387 public Object newInstance(EntityInput input, boolean rawAccess)
388 throws RefreshException {
389
390 return Integer.valueOf(input.readInt());
391 }
392
393 @Override
394 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
395 output.writeInt(((Number) o).intValue());
396 }
397
398 @Override
399 void skipContents(RecordInput input) {
400 input.skipFast(4);
401 }
402
403 @Override
404 void copySecKey(RecordInput input, RecordOutput output) {
405 output.writeFast(input.readFast());
406 output.writeFast(input.readFast());
407 output.writeFast(input.readFast());
408 output.writeFast(input.readFast());
409 }
410
411 @Override
412 Object newPrimitiveArray(int len, EntityInput input)
413 throws RefreshException {
414
415 int[] a = new int[len];
416 for (int i = 0; i < len; i += 1) {
417 a[i] = input.readInt();
418 }
419 return a;
420 }
421
422 @Override
423 void writePrimitiveArray(Object o, EntityOutput output) {
424 int[] a = (int[]) o;
425 int len = a.length;
426 output.writeArrayLength(len);
427 for (int i = 0; i < len; i += 1) {
428 output.writeInt(a[i]);
429 }
430 }
431
432 @Override
433 int getPrimitiveLength() {
434 return 4;
435 }
436
437 @Override
438 void readPrimitiveField(Object o, EntityInput input, Field field)
439 throws IllegalAccessException, RefreshException {
440
441 field.setInt(o, input.readInt());
442 }
443
444 @Override
445 void writePrimitiveField(Object o, EntityOutput output, Field field)
446 throws IllegalAccessException {
447
448 output.writeInt(field.getInt(o));
449 }
450
451 @Override
452 Format getSequenceKeyFormat() {
453 return this;
454 }
455 }
456
457 public static class FLong extends SimpleFormat {
458
459 private static final long serialVersionUID = 1872661106534776520L;
460
461 FLong(Catalog catalog, boolean primitive) {
462 super(catalog, primitive ? Long.TYPE : Long.class, primitive);
463 }
464
465 @Override
466 Object newArray(int len) {
467 return new Long[len];
468 }
469
470 @Override
471 public Object newInstance(EntityInput input, boolean rawAccess)
472 throws RefreshException {
473
474 return Long.valueOf(input.readLong());
475 }
476
477 @Override
478 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
479 output.writeLong(((Number) o).longValue());
480 }
481
482 @Override
483 void skipContents(RecordInput input) {
484 input.skipFast(8);
485 }
486
487 @Override
488 void copySecKey(RecordInput input, RecordOutput output) {
489 output.writeFast
490 (input.getBufferBytes(), input.getBufferOffset(), 8);
491 input.skipFast(8);
492 }
493
494 @Override
495 Object newPrimitiveArray(int len, EntityInput input)
496 throws RefreshException {
497
498 long[] a = new long[len];
499 for (int i = 0; i < len; i += 1) {
500 a[i] = input.readLong();
501 }
502 return a;
503 }
504
505 @Override
506 void writePrimitiveArray(Object o, EntityOutput output) {
507 long[] a = (long[]) o;
508 int len = a.length;
509 output.writeArrayLength(len);
510 for (int i = 0; i < len; i += 1) {
511 output.writeLong(a[i]);
512 }
513 }
514
515 @Override
516 int getPrimitiveLength() {
517 return 8;
518 }
519
520 @Override
521 void readPrimitiveField(Object o, EntityInput input, Field field)
522 throws IllegalAccessException, RefreshException {
523
524 field.setLong(o, input.readLong());
525 }
526
527 @Override
528 void writePrimitiveField(Object o, EntityOutput output, Field field)
529 throws IllegalAccessException {
530
531 output.writeLong(field.getLong(o));
532 }
533
534 @Override
535 Format getSequenceKeyFormat() {
536 return this;
537 }
538 }
539
540 public static class FFloat extends SimpleFormat {
541
542 private static final long serialVersionUID = 1033413049495053602L;
543
544 FFloat(Catalog catalog, boolean primitive) {
545 super(catalog, primitive ? Float.TYPE : Float.class, primitive);
546 }
547
548 @Override
549 Object newArray(int len) {
550 return new Float[len];
551 }
552
553 @Override
554 public Object newInstance(EntityInput input, boolean rawAccess)
555 throws RefreshException {
556
557 return Float.valueOf(input.readSortedFloat());
558 }
559
560 @Override
561 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
562 output.writeSortedFloat(((Number) o).floatValue());
563 }
564
565 @Override
566 void skipContents(RecordInput input) {
567 input.skipFast(4);
568 }
569
570 @Override
571 void copySecKey(RecordInput input, RecordOutput output) {
572 output.writeFast(input.readFast());
573 output.writeFast(input.readFast());
574 output.writeFast(input.readFast());
575 output.writeFast(input.readFast());
576 }
577
578 @Override
579 Object newPrimitiveArray(int len, EntityInput input)
580 throws RefreshException {
581
582 float[] a = new float[len];
583 for (int i = 0; i < len; i += 1) {
584 a[i] = input.readSortedFloat();
585 }
586 return a;
587 }
588
589 @Override
590 void writePrimitiveArray(Object o, EntityOutput output) {
591 float[] a = (float[]) o;
592 int len = a.length;
593 output.writeArrayLength(len);
594 for (int i = 0; i < len; i += 1) {
595 output.writeSortedFloat(a[i]);
596 }
597 }
598
599 @Override
600 int getPrimitiveLength() {
601 return 4;
602 }
603
604 @Override
605 void readPrimitiveField(Object o, EntityInput input, Field field)
606 throws IllegalAccessException, RefreshException {
607
608 field.setFloat(o, input.readSortedFloat());
609 }
610
611 @Override
612 void writePrimitiveField(Object o, EntityOutput output, Field field)
613 throws IllegalAccessException {
614
615 output.writeSortedFloat(field.getFloat(o));
616 }
617 }
618
619 public static class FDouble extends SimpleFormat {
620
621 private static final long serialVersionUID = 646904456811041423L;
622
623 FDouble(Catalog catalog, boolean primitive) {
624 super(catalog, primitive ? Double.TYPE : Double.class, primitive);
625 }
626
627 @Override
628 Object newArray(int len) {
629 return new Double[len];
630 }
631
632 @Override
633 public Object newInstance(EntityInput input, boolean rawAccess)
634 throws RefreshException {
635
636 return Double.valueOf(input.readSortedDouble());
637 }
638
639 @Override
640 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
641 output.writeSortedDouble(((Number) o).doubleValue());
642 }
643
644 @Override
645 void skipContents(RecordInput input) {
646 input.skipFast(8);
647 }
648
649 @Override
650 void copySecKey(RecordInput input, RecordOutput output) {
651 output.writeFast
652 (input.getBufferBytes(), input.getBufferOffset(), 8);
653 input.skipFast(8);
654 }
655
656 @Override
657 Object newPrimitiveArray(int len, EntityInput input)
658 throws RefreshException {
659
660 double[] a = new double[len];
661 for (int i = 0; i < len; i += 1) {
662 a[i] = input.readSortedDouble();
663 }
664 return a;
665 }
666
667 @Override
668 void writePrimitiveArray(Object o, EntityOutput output) {
669 double[] a = (double[]) o;
670 int len = a.length;
671 output.writeArrayLength(len);
672 for (int i = 0; i < len; i += 1) {
673 output.writeSortedDouble(a[i]);
674 }
675 }
676
677 @Override
678 int getPrimitiveLength() {
679 return 8;
680 }
681
682 @Override
683 void readPrimitiveField(Object o, EntityInput input, Field field)
684 throws IllegalAccessException, RefreshException {
685
686 field.setDouble(o, input.readSortedDouble());
687 }
688
689 @Override
690 void writePrimitiveField(Object o, EntityOutput output, Field field)
691 throws IllegalAccessException {
692
693 output.writeSortedDouble(field.getDouble(o));
694 }
695 }
696
697 public static class FChar extends SimpleFormat {
698
699 private static final long serialVersionUID = -7609118195770005374L;
700
701 FChar(Catalog catalog, boolean primitive) {
702 super(catalog, primitive ? Character.TYPE : Character.class,
703 primitive);
704 }
705
706 @Override
707 Object newArray(int len) {
708 return new Character[len];
709 }
710
711 @Override
712 public Object newInstance(EntityInput input, boolean rawAccess)
713 throws RefreshException {
714
715 return Character.valueOf(input.readChar());
716 }
717
718 @Override
719 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
720 output.writeChar(((Character) o).charValue());
721 }
722
723 @Override
724 void skipContents(RecordInput input) {
725 input.skipFast(2);
726 }
727
728 @Override
729 void copySecKey(RecordInput input, RecordOutput output) {
730 output.writeFast(input.readFast());
731 output.writeFast(input.readFast());
732 }
733
734 @Override
735 Object newPrimitiveArray(int len, EntityInput input)
736 throws RefreshException {
737
738 char[] a = new char[len];
739 for (int i = 0; i < len; i += 1) {
740 a[i] = input.readChar();
741 }
742 return a;
743 }
744
745 @Override
746 void writePrimitiveArray(Object o, EntityOutput output) {
747 char[] a = (char[]) o;
748 int len = a.length;
749 output.writeArrayLength(len);
750 for (int i = 0; i < len; i += 1) {
751 output.writeChar(a[i]);
752 }
753 }
754
755 @Override
756 int getPrimitiveLength() {
757 return 2;
758 }
759
760 @Override
761 void readPrimitiveField(Object o, EntityInput input, Field field)
762 throws IllegalAccessException, RefreshException {
763
764 field.setChar(o, input.readChar());
765 }
766
767 @Override
768 void writePrimitiveField(Object o, EntityOutput output, Field field)
769 throws IllegalAccessException {
770
771 output.writeChar(field.getChar(o));
772 }
773 }
774
775 public static class FString extends SimpleFormat {
776
777 private static final long serialVersionUID = 5710392786480064612L;
778
779 FString(Catalog catalog) {
780 super(catalog, String.class, false);
781 }
782
783 @Override
784 Object newArray(int len) {
785 return new String[len];
786 }
787
788 @Override
789 public Object newInstance(EntityInput input, boolean rawAccess)
790 throws RefreshException {
791
792 return input.readString();
793 }
794
795 @Override
796 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
797 output.writeString((String) o);
798 }
799
800 @Override
801 void skipContents(RecordInput input) {
802 input.skipFast(input.getStringByteLength());
803 }
804
805 @Override
806 void copySecKey(RecordInput input, RecordOutput output) {
807 int len = input.getStringByteLength();
808 output.writeFast
809 (input.getBufferBytes(), input.getBufferOffset(), len);
810 input.skipFast(len);
811 }
812 }
813
814 public static class FBigInt extends SimpleFormat {
815
816 private static final long serialVersionUID = -5027098112507644563L;
817
818 FBigInt(Catalog catalog) {
819 super(catalog, BigInteger.class, false);
820 }
821
822 @Override
823 Object newArray(int len) {
824 return new BigInteger[len];
825 }
826
827 @Override
828 public Object newInstance(EntityInput input, boolean rawAccess)
829 throws RefreshException {
830
831 return input.readBigInteger();
832 }
833
834 @Override
835 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
836 output.writeBigInteger((BigInteger) o);
837 }
838
839 @Override
840 void skipContents(RecordInput input) {
841 input.skipFast(input.getBigIntegerByteLength());
842 }
843
844 @Override
845 void copySecKey(RecordInput input, RecordOutput output) {
846 int len = input.getBigIntegerByteLength();
847 output.writeFast
848 (input.getBufferBytes(), input.getBufferOffset(), len);
849 input.skipFast(len);
850 }
851 }
852
853 public static class FBigDec extends SimpleFormat {
854 private static final long serialVersionUID = 6108874887143696463L;
855
856 FBigDec(Catalog catalog) {
857 super(catalog, BigDecimal.class, false);
858 }
859
860 @Override
861 Object newArray(int len) {
862 return new BigDecimal[len];
863 }
864
865 @Override
866 public Object newInstance(EntityInput input, boolean rawAccess)
867 throws RefreshException {
868
869 return input.readSortedBigDecimal();
870 }
871
872 @Override
873 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
874 output.writeSortedBigDecimal((BigDecimal) o);
875 }
876
877 @Override
878 void skipContents(RecordInput input) {
879 input.skipFast(input.getSortedBigDecimalByteLength());
880 }
881
882 @Override
883 void copySecKey(RecordInput input, RecordOutput output) {
884 int len = input.getSortedBigDecimalByteLength();
885 output.writeFast
886 (input.getBufferBytes(), input.getBufferOffset(), len);
887 input.skipFast(len);
888 }
889
890 @Override
891 public boolean allowEvolveFromProxy() {
892 return true;
893 }
894 }
895
896 public static class FDate extends SimpleFormat {
897
898 private static final long serialVersionUID = -5665773229869034145L;
899
900 FDate(Catalog catalog) {
901 super(catalog, Date.class, false);
902 }
903
904 @Override
905 Object newArray(int len) {
906 return new Date[len];
907 }
908
909 @Override
910 public Object newInstance(EntityInput input, boolean rawAccess)
911 throws RefreshException {
912
913 return new Date(input.readLong());
914 }
915
916 @Override
917 void writeObject(Object o, EntityOutput output, boolean rawAccess) {
918 output.writeLong(((Date) o).getTime());
919 }
920
921 @Override
922 void skipContents(RecordInput input) {
923 input.skipFast(8);
924 }
925
926 @Override
927 void copySecKey(RecordInput input, RecordOutput output) {
928 output.writeFast
929 (input.getBufferBytes(), input.getBufferOffset(), 8);
930 input.skipFast(8);
931 }
932 }
933 }