001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: SimpleFormat.java,v 1.20.2.4 2008/01/07 15:14:20 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.impl;
010:
011: import java.lang.reflect.Field;
012: import java.math.BigInteger;
013: import java.util.Date;
014: import java.util.Map;
015: import java.util.Set;
016:
017: import com.sleepycat.je.DatabaseEntry;
018:
019: /**
020: * Format for simple types, including primitives. Additional methods are
021: * included to optimize the handling of primitives. Other classes such as
022: * PrimitiveArrayFormat and ReflectAccessor take advantage of these methods.
023: *
024: * @author Mark Hayes
025: */
026: public abstract class SimpleFormat extends Format {
027:
028: private static final long serialVersionUID = 4595245575868697702L;
029:
030: private boolean primitive;
031: private SimpleFormat wrapperFormat;
032:
033: SimpleFormat(Class type, boolean primitive) {
034: super (type);
035: this .primitive = primitive;
036: }
037:
038: void setWrapperFormat(SimpleFormat wrapperFormat) {
039: this .wrapperFormat = wrapperFormat;
040: }
041:
042: @Override
043: Format getWrapperFormat() {
044: return wrapperFormat;
045: }
046:
047: @Override
048: public boolean isSimple() {
049: return true;
050: }
051:
052: @Override
053: public boolean isPrimitive() {
054: return primitive;
055: }
056:
057: @Override
058: void collectRelatedFormats(Catalog catalog,
059: Map<String, Format> newFormats) {
060: }
061:
062: @Override
063: void initialize(Catalog catalog, int initVersion) {
064: }
065:
066: @Override
067: public Object readObject(Object o, EntityInput input,
068: boolean rawAccess) {
069: /* newInstance reads the value -- do nothing here. */
070: return o;
071: }
072:
073: @Override
074: boolean evolve(Format newFormat, Evolver evolver) {
075: evolver.useOldFormat(this , newFormat);
076: return true;
077: }
078:
079: /* -- Begin methods to be overridden by primitive formats only. -- */
080:
081: Object newPrimitiveArray(int len, EntityInput input) {
082: throw new UnsupportedOperationException();
083: }
084:
085: void writePrimitiveArray(Object o, EntityOutput output) {
086: throw new UnsupportedOperationException();
087: }
088:
089: int getPrimitiveLength() {
090: throw new UnsupportedOperationException();
091: }
092:
093: void readPrimitiveField(Object o, EntityInput input, Field field)
094: throws IllegalAccessException {
095:
096: throw new UnsupportedOperationException();
097: }
098:
099: void writePrimitiveField(Object o, EntityOutput output, Field field)
100: throws IllegalAccessException {
101:
102: throw new UnsupportedOperationException();
103: }
104:
105: /* -- End methods to be overridden by primitive formats only. -- */
106:
107: void skipPrimitiveArray(int len, RecordInput input) {
108: input.skipFast(len * getPrimitiveLength());
109: }
110:
111: void copySecMultiKeyPrimitiveArray(int len, RecordInput input,
112: Set results) {
113: int primLen = getPrimitiveLength();
114: for (int i = 0; i < len; i += 1) {
115: DatabaseEntry entry = new DatabaseEntry(input
116: .getBufferBytes(), input.getBufferOffset(), primLen);
117: results.add(entry);
118: input.skipFast(primLen);
119: }
120: }
121:
122: public static class FBool extends SimpleFormat {
123:
124: private static final long serialVersionUID = -7724949525068533451L;
125:
126: FBool(boolean primitive) {
127: super (primitive ? Boolean.TYPE : Boolean.class, primitive);
128: }
129:
130: @Override
131: Object newArray(int len) {
132: return new Boolean[len];
133: }
134:
135: @Override
136: public Object newInstance(EntityInput input, boolean rawAccess) {
137: return Boolean.valueOf(input.readBoolean());
138: }
139:
140: @Override
141: void writeObject(Object o, EntityOutput output,
142: boolean rawAccess) {
143: output.writeBoolean(((Boolean) o).booleanValue());
144: }
145:
146: @Override
147: void skipContents(RecordInput input) {
148: input.skipFast(1);
149: }
150:
151: @Override
152: void copySecKey(RecordInput input, RecordOutput output) {
153: output.writeFast(input.readFast());
154: }
155:
156: @Override
157: Object newPrimitiveArray(int len, EntityInput input) {
158: boolean[] a = new boolean[len];
159: for (int i = 0; i < len; i += 1) {
160: a[i] = input.readBoolean();
161: }
162: return a;
163: }
164:
165: @Override
166: void writePrimitiveArray(Object o, EntityOutput output) {
167: boolean[] a = (boolean[]) o;
168: int len = a.length;
169: output.writeArrayLength(len);
170: for (int i = 0; i < len; i += 1) {
171: output.writeBoolean(a[i]);
172: }
173: }
174:
175: @Override
176: int getPrimitiveLength() {
177: return 1;
178: }
179:
180: @Override
181: void readPrimitiveField(Object o, EntityInput input, Field field)
182: throws IllegalAccessException {
183:
184: field.setBoolean(o, input.readBoolean());
185: }
186:
187: @Override
188: void writePrimitiveField(Object o, EntityOutput output,
189: Field field) throws IllegalAccessException {
190:
191: output.writeBoolean(field.getBoolean(o));
192: }
193: }
194:
195: public static class FByte extends SimpleFormat {
196:
197: private static final long serialVersionUID = 3651752958101447257L;
198:
199: FByte(boolean primitive) {
200: super (primitive ? Byte.TYPE : Byte.class, primitive);
201: }
202:
203: @Override
204: Object newArray(int len) {
205: return new Byte[len];
206: }
207:
208: @Override
209: public Object newInstance(EntityInput input, boolean rawAccess) {
210: return Byte.valueOf(input.readByte());
211: }
212:
213: @Override
214: void writeObject(Object o, EntityOutput output,
215: boolean rawAccess) {
216: output.writeByte(((Number) o).byteValue());
217: }
218:
219: @Override
220: void skipContents(RecordInput input) {
221: input.skipFast(1);
222: }
223:
224: @Override
225: void copySecKey(RecordInput input, RecordOutput output) {
226: output.writeFast(input.readFast());
227: }
228:
229: @Override
230: Object newPrimitiveArray(int len, EntityInput input) {
231: byte[] a = new byte[len];
232: for (int i = 0; i < len; i += 1) {
233: a[i] = input.readByte();
234: }
235: return a;
236: }
237:
238: @Override
239: void writePrimitiveArray(Object o, EntityOutput output) {
240: byte[] a = (byte[]) o;
241: int len = a.length;
242: output.writeArrayLength(len);
243: for (int i = 0; i < len; i += 1) {
244: output.writeByte(a[i]);
245: }
246: }
247:
248: @Override
249: int getPrimitiveLength() {
250: return 1;
251: }
252:
253: @Override
254: void readPrimitiveField(Object o, EntityInput input, Field field)
255: throws IllegalAccessException {
256:
257: field.setByte(o, input.readByte());
258: }
259:
260: @Override
261: void writePrimitiveField(Object o, EntityOutput output,
262: Field field) throws IllegalAccessException {
263:
264: output.writeByte(field.getByte(o));
265: }
266:
267: @Override
268: Format getSequenceKeyFormat() {
269: return this ;
270: }
271: }
272:
273: public static class FShort extends SimpleFormat {
274:
275: private static final long serialVersionUID = -4909138198491785624L;
276:
277: FShort(boolean primitive) {
278: super (primitive ? Short.TYPE : Short.class, primitive);
279: }
280:
281: @Override
282: Object newArray(int len) {
283: return new Short[len];
284: }
285:
286: @Override
287: public Object newInstance(EntityInput input, boolean rawAccess) {
288: return Short.valueOf(input.readShort());
289: }
290:
291: @Override
292: void writeObject(Object o, EntityOutput output,
293: boolean rawAccess) {
294: output.writeShort(((Number) o).shortValue());
295: }
296:
297: @Override
298: void skipContents(RecordInput input) {
299: input.skipFast(2);
300: }
301:
302: @Override
303: void copySecKey(RecordInput input, RecordOutput output) {
304: output.writeFast(input.readFast());
305: output.writeFast(input.readFast());
306: }
307:
308: @Override
309: Object newPrimitiveArray(int len, EntityInput input) {
310: short[] a = new short[len];
311: for (int i = 0; i < len; i += 1) {
312: a[i] = input.readShort();
313: }
314: return a;
315: }
316:
317: @Override
318: void writePrimitiveArray(Object o, EntityOutput output) {
319: short[] a = (short[]) o;
320: int len = a.length;
321: output.writeArrayLength(len);
322: for (int i = 0; i < len; i += 1) {
323: output.writeShort(a[i]);
324: }
325: }
326:
327: @Override
328: int getPrimitiveLength() {
329: return 2;
330: }
331:
332: @Override
333: void readPrimitiveField(Object o, EntityInput input, Field field)
334: throws IllegalAccessException {
335:
336: field.setShort(o, input.readShort());
337: }
338:
339: @Override
340: void writePrimitiveField(Object o, EntityOutput output,
341: Field field) throws IllegalAccessException {
342:
343: output.writeShort(field.getShort(o));
344: }
345:
346: @Override
347: Format getSequenceKeyFormat() {
348: return this ;
349: }
350: }
351:
352: public static class FInt extends SimpleFormat {
353:
354: private static final long serialVersionUID = 2695910006049980013L;
355:
356: FInt(boolean primitive) {
357: super (primitive ? Integer.TYPE : Integer.class, primitive);
358: }
359:
360: @Override
361: Object newArray(int len) {
362: return new Integer[len];
363: }
364:
365: @Override
366: public Object newInstance(EntityInput input, boolean rawAccess) {
367: return Integer.valueOf(input.readInt());
368: }
369:
370: @Override
371: void writeObject(Object o, EntityOutput output,
372: boolean rawAccess) {
373: output.writeInt(((Number) o).intValue());
374: }
375:
376: @Override
377: void skipContents(RecordInput input) {
378: input.skipFast(4);
379: }
380:
381: @Override
382: void copySecKey(RecordInput input, RecordOutput output) {
383: output.writeFast(input.readFast());
384: output.writeFast(input.readFast());
385: output.writeFast(input.readFast());
386: output.writeFast(input.readFast());
387: }
388:
389: @Override
390: Object newPrimitiveArray(int len, EntityInput input) {
391: int[] a = new int[len];
392: for (int i = 0; i < len; i += 1) {
393: a[i] = input.readInt();
394: }
395: return a;
396: }
397:
398: @Override
399: void writePrimitiveArray(Object o, EntityOutput output) {
400: int[] a = (int[]) o;
401: int len = a.length;
402: output.writeArrayLength(len);
403: for (int i = 0; i < len; i += 1) {
404: output.writeInt(a[i]);
405: }
406: }
407:
408: @Override
409: int getPrimitiveLength() {
410: return 4;
411: }
412:
413: @Override
414: void readPrimitiveField(Object o, EntityInput input, Field field)
415: throws IllegalAccessException {
416:
417: field.setInt(o, input.readInt());
418: }
419:
420: @Override
421: void writePrimitiveField(Object o, EntityOutput output,
422: Field field) throws IllegalAccessException {
423:
424: output.writeInt(field.getInt(o));
425: }
426:
427: @Override
428: Format getSequenceKeyFormat() {
429: return this ;
430: }
431: }
432:
433: public static class FLong extends SimpleFormat {
434:
435: private static final long serialVersionUID = 1872661106534776520L;
436:
437: FLong(boolean primitive) {
438: super (primitive ? Long.TYPE : Long.class, primitive);
439: }
440:
441: @Override
442: Object newArray(int len) {
443: return new Long[len];
444: }
445:
446: @Override
447: public Object newInstance(EntityInput input, boolean rawAccess) {
448: return Long.valueOf(input.readLong());
449: }
450:
451: @Override
452: void writeObject(Object o, EntityOutput output,
453: boolean rawAccess) {
454: output.writeLong(((Number) o).longValue());
455: }
456:
457: @Override
458: void skipContents(RecordInput input) {
459: input.skipFast(8);
460: }
461:
462: @Override
463: void copySecKey(RecordInput input, RecordOutput output) {
464: output.writeFast(input.getBufferBytes(), input
465: .getBufferOffset(), 8);
466: input.skipFast(8);
467: }
468:
469: @Override
470: Object newPrimitiveArray(int len, EntityInput input) {
471: long[] a = new long[len];
472: for (int i = 0; i < len; i += 1) {
473: a[i] = input.readLong();
474: }
475: return a;
476: }
477:
478: @Override
479: void writePrimitiveArray(Object o, EntityOutput output) {
480: long[] a = (long[]) o;
481: int len = a.length;
482: output.writeArrayLength(len);
483: for (int i = 0; i < len; i += 1) {
484: output.writeLong(a[i]);
485: }
486: }
487:
488: @Override
489: int getPrimitiveLength() {
490: return 8;
491: }
492:
493: @Override
494: void readPrimitiveField(Object o, EntityInput input, Field field)
495: throws IllegalAccessException {
496:
497: field.setLong(o, input.readLong());
498: }
499:
500: @Override
501: void writePrimitiveField(Object o, EntityOutput output,
502: Field field) throws IllegalAccessException {
503:
504: output.writeLong(field.getLong(o));
505: }
506:
507: @Override
508: Format getSequenceKeyFormat() {
509: return this ;
510: }
511: }
512:
513: public static class FFloat extends SimpleFormat {
514:
515: private static final long serialVersionUID = 1033413049495053602L;
516:
517: FFloat(boolean primitive) {
518: super (primitive ? Float.TYPE : Float.class, primitive);
519: }
520:
521: @Override
522: Object newArray(int len) {
523: return new Float[len];
524: }
525:
526: @Override
527: public Object newInstance(EntityInput input, boolean rawAccess) {
528: return Float.valueOf(input.readSortedFloat());
529: }
530:
531: @Override
532: void writeObject(Object o, EntityOutput output,
533: boolean rawAccess) {
534: output.writeSortedFloat(((Number) o).floatValue());
535: }
536:
537: @Override
538: void skipContents(RecordInput input) {
539: input.skipFast(4);
540: }
541:
542: @Override
543: void copySecKey(RecordInput input, RecordOutput output) {
544: output.writeFast(input.readFast());
545: output.writeFast(input.readFast());
546: output.writeFast(input.readFast());
547: output.writeFast(input.readFast());
548: }
549:
550: @Override
551: Object newPrimitiveArray(int len, EntityInput input) {
552: float[] a = new float[len];
553: for (int i = 0; i < len; i += 1) {
554: a[i] = input.readSortedFloat();
555: }
556: return a;
557: }
558:
559: @Override
560: void writePrimitiveArray(Object o, EntityOutput output) {
561: float[] a = (float[]) o;
562: int len = a.length;
563: output.writeArrayLength(len);
564: for (int i = 0; i < len; i += 1) {
565: output.writeSortedFloat(a[i]);
566: }
567: }
568:
569: @Override
570: int getPrimitiveLength() {
571: return 4;
572: }
573:
574: @Override
575: void readPrimitiveField(Object o, EntityInput input, Field field)
576: throws IllegalAccessException {
577:
578: field.setFloat(o, input.readSortedFloat());
579: }
580:
581: @Override
582: void writePrimitiveField(Object o, EntityOutput output,
583: Field field) throws IllegalAccessException {
584:
585: output.writeSortedFloat(field.getFloat(o));
586: }
587: }
588:
589: public static class FDouble extends SimpleFormat {
590:
591: private static final long serialVersionUID = 646904456811041423L;
592:
593: FDouble(boolean primitive) {
594: super (primitive ? Double.TYPE : Double.class, primitive);
595: }
596:
597: @Override
598: Object newArray(int len) {
599: return new Double[len];
600: }
601:
602: @Override
603: public Object newInstance(EntityInput input, boolean rawAccess) {
604: return Double.valueOf(input.readSortedDouble());
605: }
606:
607: @Override
608: void writeObject(Object o, EntityOutput output,
609: boolean rawAccess) {
610: output.writeSortedDouble(((Number) o).doubleValue());
611: }
612:
613: @Override
614: void skipContents(RecordInput input) {
615: input.skipFast(8);
616: }
617:
618: @Override
619: void copySecKey(RecordInput input, RecordOutput output) {
620: output.writeFast(input.getBufferBytes(), input
621: .getBufferOffset(), 8);
622: input.skipFast(8);
623: }
624:
625: @Override
626: Object newPrimitiveArray(int len, EntityInput input) {
627: double[] a = new double[len];
628: for (int i = 0; i < len; i += 1) {
629: a[i] = input.readSortedDouble();
630: }
631: return a;
632: }
633:
634: @Override
635: void writePrimitiveArray(Object o, EntityOutput output) {
636: double[] a = (double[]) o;
637: int len = a.length;
638: output.writeArrayLength(len);
639: for (int i = 0; i < len; i += 1) {
640: output.writeSortedDouble(a[i]);
641: }
642: }
643:
644: @Override
645: int getPrimitiveLength() {
646: return 8;
647: }
648:
649: @Override
650: void readPrimitiveField(Object o, EntityInput input, Field field)
651: throws IllegalAccessException {
652:
653: field.setDouble(o, input.readSortedDouble());
654: }
655:
656: @Override
657: void writePrimitiveField(Object o, EntityOutput output,
658: Field field) throws IllegalAccessException {
659:
660: output.writeSortedDouble(field.getDouble(o));
661: }
662: }
663:
664: public static class FChar extends SimpleFormat {
665:
666: private static final long serialVersionUID = -7609118195770005374L;
667:
668: FChar(boolean primitive) {
669: super (primitive ? Character.TYPE : Character.class,
670: primitive);
671: }
672:
673: @Override
674: Object newArray(int len) {
675: return new Character[len];
676: }
677:
678: @Override
679: public Object newInstance(EntityInput input, boolean rawAccess) {
680: return Character.valueOf(input.readChar());
681: }
682:
683: @Override
684: void writeObject(Object o, EntityOutput output,
685: boolean rawAccess) {
686: output.writeChar(((Character) o).charValue());
687: }
688:
689: @Override
690: void skipContents(RecordInput input) {
691: input.skipFast(2);
692: }
693:
694: @Override
695: void copySecKey(RecordInput input, RecordOutput output) {
696: output.writeFast(input.readFast());
697: output.writeFast(input.readFast());
698: }
699:
700: @Override
701: Object newPrimitiveArray(int len, EntityInput input) {
702: char[] a = new char[len];
703: for (int i = 0; i < len; i += 1) {
704: a[i] = input.readChar();
705: }
706: return a;
707: }
708:
709: @Override
710: void writePrimitiveArray(Object o, EntityOutput output) {
711: char[] a = (char[]) o;
712: int len = a.length;
713: output.writeArrayLength(len);
714: for (int i = 0; i < len; i += 1) {
715: output.writeChar(a[i]);
716: }
717: }
718:
719: @Override
720: int getPrimitiveLength() {
721: return 2;
722: }
723:
724: @Override
725: void readPrimitiveField(Object o, EntityInput input, Field field)
726: throws IllegalAccessException {
727:
728: field.setChar(o, input.readChar());
729: }
730:
731: @Override
732: void writePrimitiveField(Object o, EntityOutput output,
733: Field field) throws IllegalAccessException {
734:
735: output.writeChar(field.getChar(o));
736: }
737: }
738:
739: public static class FString extends SimpleFormat {
740:
741: private static final long serialVersionUID = 5710392786480064612L;
742:
743: FString() {
744: super (String.class, false);
745: }
746:
747: @Override
748: Object newArray(int len) {
749: return new String[len];
750: }
751:
752: @Override
753: public Object newInstance(EntityInput input, boolean rawAccess) {
754: return input.readString();
755: }
756:
757: @Override
758: void writeObject(Object o, EntityOutput output,
759: boolean rawAccess) {
760: output.writeString((String) o);
761: }
762:
763: @Override
764: void skipContents(RecordInput input) {
765: input.skipFast(input.getStringByteLength());
766: }
767:
768: @Override
769: void copySecKey(RecordInput input, RecordOutput output) {
770: int len = input.getStringByteLength();
771: output.writeFast(input.getBufferBytes(), input
772: .getBufferOffset(), len);
773: input.skipFast(len);
774: }
775: }
776:
777: public static class FBigInt extends SimpleFormat {
778:
779: private static final long serialVersionUID = -5027098112507644563L;
780:
781: FBigInt() {
782: super (BigInteger.class, false);
783: }
784:
785: @Override
786: Object newArray(int len) {
787: return new BigInteger[len];
788: }
789:
790: @Override
791: public Object newInstance(EntityInput input, boolean rawAccess) {
792: return input.readBigInteger();
793: }
794:
795: @Override
796: void writeObject(Object o, EntityOutput output,
797: boolean rawAccess) {
798: output.writeBigInteger((BigInteger) o);
799: }
800:
801: @Override
802: void skipContents(RecordInput input) {
803: input.skipFast(input.getBigIntegerByteLength());
804: }
805:
806: @Override
807: void copySecKey(RecordInput input, RecordOutput output) {
808: int len = input.getBigIntegerByteLength();
809: output.writeFast(input.getBufferBytes(), input
810: .getBufferOffset(), len);
811: input.skipFast(len);
812: }
813: }
814:
815: public static class FDate extends SimpleFormat {
816:
817: private static final long serialVersionUID = -5665773229869034145L;
818:
819: FDate() {
820: super (Date.class, false);
821: }
822:
823: @Override
824: Object newArray(int len) {
825: return new Date[len];
826: }
827:
828: @Override
829: public Object newInstance(EntityInput input, boolean rawAccess) {
830: return new Date(input.readLong());
831: }
832:
833: @Override
834: void writeObject(Object o, EntityOutput output,
835: boolean rawAccess) {
836: output.writeLong(((Date) o).getTime());
837: }
838:
839: @Override
840: void skipContents(RecordInput input) {
841: input.skipFast(8);
842: }
843:
844: @Override
845: void copySecKey(RecordInput input, RecordOutput output) {
846: output.writeFast(input.getBufferBytes(), input
847: .getBufferOffset(), 8);
848: input.skipFast(8);
849: }
850: }
851: }
|