| 18 | import org.jetbrains.annotations.NotNull; |
| 19 | |
| 20 | public final class Meta2<K, V> { |
| 21 | private static final long map1HeadHash = Bean.hash64("Zeze.Transaction.Collections.LogMap1<"); |
| 22 | private static final long map2HeadHash = Bean.hash64("Zeze.Transaction.Collections.LogMap2<"); |
| 23 | private static final ConcurrentHashMap<Class<?>, ConcurrentHashMap<Class<?>, Meta2<?, ?>>> map1Metas = new ConcurrentHashMap<>(); |
| 24 | private static final ConcurrentHashMap<Class<?>, ConcurrentHashMap<Class<?>, Meta2<?, ?>>> map2Metas = new ConcurrentHashMap<>(); |
| 25 | |
| 26 | public final int logTypeId; |
| 27 | public final int keyEncodeType; |
| 28 | public final @NotNull BiConsumer<ByteBuffer, K> keyEncoder; |
| 29 | public final @NotNull Function<IByteBuffer, K> keyDecoder; |
| 30 | public final @NotNull SerializeHelper.ObjectIntFunction<IByteBuffer, K> keyDecoderWithType; |
| 31 | public final int valueEncodeType; |
| 32 | public final BiConsumer<ByteBuffer, V> valueEncoder; // 只用于非Bean类型 |
| 33 | public final Function<IByteBuffer, V> valueDecoder; // 只用于非Bean类型 |
| 34 | public final SerializeHelper.ObjectIntFunction<IByteBuffer, V> valueDecoderWithType; // 只用于非Bean类型 |
| 35 | public final MethodHandle valueFactory; // 只用于Bean类型 |
| 36 | public final @NotNull String name; // 主要用于分析查错 |
| 37 | |
| 38 | private Meta2(@NotNull String headStr, long headHash, @NotNull Class<K> keyClass, @NotNull Class<V> valueClass, |
| 39 | MethodHandle valueFactory) { |
| 40 | logTypeId = Bean.hashLog(headHash, keyClass, valueClass); |
| 41 | var keyCodecFuncs = SerializeHelper.createCodec(keyClass); |
| 42 | keyEncodeType = keyCodecFuncs.encodeType; |
| 43 | keyEncoder = keyCodecFuncs.encoder; |
| 44 | keyDecoder = keyCodecFuncs.decoder; |
| 45 | keyDecoderWithType = keyCodecFuncs.decoderWithType; |
| 46 | var valueCodecFuncs = SerializeHelper.createCodec(valueClass, valueFactory); |
| 47 | valueEncodeType = valueCodecFuncs.encodeType; |
| 48 | valueEncoder = valueCodecFuncs.encoder; |
| 49 | valueDecoder = valueCodecFuncs.decoder; |
| 50 | valueDecoderWithType = valueCodecFuncs.decoderWithType; |
| 51 | this.valueFactory = valueFactory; |
| 52 | name = headStr + keyClass.getName() + ',' + valueClass.getName(); |
| 53 | } |
| 54 | |
| 55 | private Meta2(@NotNull String headStr, long headHash, @NotNull Class<K> keyClass, @NotNull Class<V> valueClass, |
| 56 | @NotNull Supplier<V> ctor) { |
| 57 | this(headStr, headHash, keyClass, valueClass, toMethodHandle(ctor)); |
| 58 | } |
| 59 | |
| 60 | private static MethodHandle toMethodHandle(@NotNull Supplier<?> ctor) { |
| 61 | try { |
| 62 | return Reflect.lookup.findVirtual(Supplier.class, "get", MethodType.methodType(Object.class)).bindTo(ctor); |
| 63 | } catch (ReflectiveOperationException e) { |
| 64 | throw Task.forceThrow(e); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | private Meta2(@NotNull String headStr, long headHash, @NotNull Class<K> keyClass, @NotNull Class<V> valueClass) { |
| 69 | this(headStr, headHash, keyClass, valueClass, |
| 70 | Bean.class.isAssignableFrom(valueClass) ? Reflect.getDefaultConstructor(valueClass) : null); |
| 71 | } |
| 72 | |
| 73 | private Meta2(@NotNull Class<K> keyClass, @NotNull ToLongFunction<Bean> get, @NotNull LongFunction<Bean> create) { |
| 74 | logTypeId = Bean.hashLog(map2HeadHash, keyClass, DynamicBean.class); |
| 75 | var keyCodecFuncs = SerializeHelper.createCodec(keyClass); |
| 76 | keyEncodeType = keyCodecFuncs.encodeType; |
| 77 | keyEncoder = keyCodecFuncs.encoder; |