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