3232import org .joda .time .LocalDate ;
3333import org .joda .time .LocalDateTime ;
3434import org .tikv .common .ExtendedDateTime ;
35+ import org .tikv .common .exception .CodecException ;
3536import org .tikv .common .exception .ConvertOverflowException ;
3637import org .tikv .common .exception .InvalidCodecFormatException ;
3738import org .tikv .common .exception .TypeException ;
3839import org .tikv .common .exception .UnsupportedSyntaxException ;
40+ import org .tikv .common .types .BytesType ;
41+ import org .tikv .common .types .DataType ;
42+ import org .tikv .common .types .DecimalType ;
43+ import org .tikv .common .types .IntegerType ;
44+ import org .tikv .common .types .JsonType ;
45+ import org .tikv .common .types .RealType ;
46+ import org .tikv .common .types .TimeType ;
3947
4048public class Codec {
4149
@@ -57,6 +65,41 @@ public static boolean isNullFlag(int flag) {
5765 return flag == NULL_FLAG ;
5866 }
5967
68+ public static Object decodeOne (byte [] colData ) {
69+ if (colData .length <= 1 ) {
70+ throw new CodecException ("invalid encoded column data, length <=1" );
71+ }
72+ int flag = colData [0 ];
73+ DataType tp ;
74+ switch (flag ) {
75+ case INT_FLAG :
76+ case UINT_FLAG :
77+ case VARINT_FLAG :
78+ case UVARINT_FLAG :
79+ tp = IntegerType .BIGINT ;
80+ break ;
81+ case FLOATING_FLAG :
82+ tp = RealType .DOUBLE ;
83+ break ;
84+ case BYTES_FLAG :
85+ case COMPACT_BYTES_FLAG :
86+ tp = BytesType .TEXT ;
87+ break ;
88+ case DECIMAL_FLAG :
89+ tp = DecimalType .DECIMAL ;
90+ break ;
91+ case DURATION_FLAG :
92+ tp = TimeType .TIME ;
93+ break ;
94+ case JSON_FLAG :
95+ tp = JsonType .JSON ;
96+ break ;
97+ default :
98+ throw new CodecException ("Unknown type" );
99+ }
100+ return tp .decode (new CodecDataInput (colData ));
101+ }
102+
60103 public static class IntegerCodec {
61104
62105 private static long flipSignBit (long v ) {
@@ -603,10 +646,10 @@ public static void writeDateTimeProto(
603646 * Read datetime from packed Long encoded as unsigned var-len integer converting into specified
604647 * timezone
605648 *
606- * @see DateTimeCodec#fromPackedLong(long, DateTimeZone)
607649 * @param cdi codec buffer input
608650 * @param tz timezone to interpret datetime parts
609651 * @return decoded ExtendedDateTime using provided timezone
652+ * @see DateTimeCodec#fromPackedLong(long, DateTimeZone)
610653 */
611654 public static ExtendedDateTime readFromUVarInt (CodecDataInput cdi , DateTimeZone tz ) {
612655 return DateTimeCodec .fromPackedLong (IntegerCodec .readUVarLong (cdi ), tz );
@@ -615,10 +658,10 @@ public static ExtendedDateTime readFromUVarInt(CodecDataInput cdi, DateTimeZone
615658 /**
616659 * Read datetime from packed Long as unsigned fixed-len integer
617660 *
618- * @see DateTimeCodec#fromPackedLong(long, DateTimeZone)
619661 * @param cdi codec buffer input
620662 * @param tz timezone to interpret datetime parts
621663 * @return decoded ExtendedDateTime using provided timezone
664+ * @see DateTimeCodec#fromPackedLong(long, DateTimeZone)
622665 */
623666 public static ExtendedDateTime readFromUInt (CodecDataInput cdi , DateTimeZone tz ) {
624667 return DateTimeCodec .fromPackedLong (IntegerCodec .readULong (cdi ), tz );
@@ -731,9 +774,9 @@ public static void writeDateProto(CodecDataOutput cdo, Date date, DateTimeZone t
731774 * Read date from packed Long encoded as unsigned var-len integer converting into specified
732775 * timezone
733776 *
734- * @see DateCodec#fromPackedLong(long)
735777 * @param cdi codec buffer input
736778 * @return decoded DateTime using provided timezone
779+ * @see DateCodec#fromPackedLong(long)
737780 */
738781 public static LocalDate readFromUVarInt (CodecDataInput cdi ) {
739782 return DateCodec .fromPackedLong (IntegerCodec .readUVarLong (cdi ));
@@ -742,9 +785,9 @@ public static LocalDate readFromUVarInt(CodecDataInput cdi) {
742785 /**
743786 * Read date from packed Long as unsigned fixed-len integer
744787 *
745- * @see DateCodec#fromPackedLong(long)
746788 * @param cdi codec buffer input
747789 * @return decoded DateTime using provided timezone
790+ * @see DateCodec#fromPackedLong(long)
748791 */
749792 public static LocalDate readFromUInt (CodecDataInput cdi ) {
750793 return DateCodec .fromPackedLong (IntegerCodec .readULong (cdi ));
0 commit comments