Skip to main content

fory_core/serializer/
decimal.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::buffer::{Reader, Writer};
19use crate::context::{ReadContext, WriteContext};
20use crate::error::Error;
21use crate::resolver::TypeResolver;
22use crate::serializer::util::read_basic_type_info;
23use crate::serializer::{ForyDefault, Serializer};
24use crate::type_id::TypeId;
25use crate::types::Decimal;
26use num_bigint::{BigInt, Sign};
27use std::convert::TryFrom;
28
29impl Serializer for Decimal {
30    #[inline(always)]
31    fn fory_write_data(&self, context: &mut WriteContext) -> Result<(), Error> {
32        context.writer.write_var_i32(self.scale);
33        write_decimal_unscaled(&self.unscaled, &mut context.writer)
34    }
35
36    #[inline(always)]
37    fn fory_read_data(context: &mut ReadContext) -> Result<Self, Error> {
38        let scale = context.reader.read_var_i32()?;
39        let unscaled = read_decimal_unscaled(&mut context.reader)?;
40        Ok(Self { unscaled, scale })
41    }
42    #[inline]
43    fn fory_read_data_as_send_sync_any(
44        context: &mut ReadContext,
45    ) -> Result<Box<dyn std::any::Any + Send + Sync>, Error>
46    where
47        Self: Sized + ForyDefault,
48    {
49        Ok(crate::serializer::box_send_sync(Self::fory_read_data(
50            context,
51        )?))
52    }
53
54    #[inline(always)]
55    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
56        Ok(TypeId::DECIMAL)
57    }
58
59    #[inline(always)]
60    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
61        Ok(TypeId::DECIMAL)
62    }
63
64    #[inline(always)]
65    fn fory_static_type_id() -> TypeId {
66        TypeId::DECIMAL
67    }
68
69    #[inline(always)]
70    fn as_any(&self) -> &dyn std::any::Any {
71        self
72    }
73
74    #[inline(always)]
75    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
76        context.writer.write_var_u32(TypeId::DECIMAL as u32);
77        Ok(())
78    }
79
80    #[inline(always)]
81    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
82        read_basic_type_info::<Self>(context)
83    }
84}
85
86impl ForyDefault for Decimal {
87    #[inline(always)]
88    fn fory_default() -> Self {
89        Self {
90            unscaled: BigInt::from(0),
91            scale: 0,
92        }
93    }
94}
95
96fn write_decimal_unscaled(value: &BigInt, writer: &mut Writer) -> Result<(), Error> {
97    if let Some(small_value) = can_use_small_encoding(value) {
98        writer.write_var_u64(encode_zigzag64(small_value) << 1);
99        return Ok(());
100    }
101
102    let (sign, payload) = value.to_bytes_le();
103    if payload.is_empty() {
104        return Err(Error::invalid_data(
105            "zero must use the small decimal encoding".to_string(),
106        ));
107    }
108    let meta = ((payload.len() as u64) << 1) | u64::from(matches!(sign, Sign::Minus));
109    writer.write_var_u64((meta << 1) | 1);
110    writer.write_bytes(&payload);
111    Ok(())
112}
113
114fn read_decimal_unscaled(reader: &mut Reader) -> Result<BigInt, Error> {
115    let header = reader.read_var_u64()?;
116    if (header & 1) == 0 {
117        return Ok(BigInt::from(decode_zigzag64(header >> 1)));
118    }
119
120    let meta = header >> 1;
121    let sign = (meta & 1) != 0;
122    let len = (meta >> 1) as usize;
123    if len == 0 {
124        return Err(Error::invalid_data(
125            "invalid decimal magnitude length 0".to_string(),
126        ));
127    }
128    let payload = reader.read_bytes(len)?;
129    if payload[len - 1] == 0 {
130        return Err(Error::invalid_data(
131            "non-canonical decimal payload: trailing zero byte".to_string(),
132        ));
133    }
134    let magnitude = BigInt::from_bytes_le(Sign::Plus, payload);
135    if magnitude == BigInt::from(0) {
136        return Err(Error::invalid_data(
137            "big decimal encoding must not represent zero".to_string(),
138        ));
139    }
140    Ok(if sign { -magnitude } else { magnitude })
141}
142
143fn can_use_small_encoding(value: &BigInt) -> Option<i64> {
144    let small_value = i64::try_from(value).ok()?;
145    if (encode_zigzag64(small_value) & (1u64 << 63)) == 0 {
146        Some(small_value)
147    } else {
148        None
149    }
150}
151
152#[inline(always)]
153fn encode_zigzag64(value: i64) -> u64 {
154    ((value << 1) ^ (value >> 63)) as u64
155}
156
157#[inline(always)]
158fn decode_zigzag64(value: u64) -> i64 {
159    ((value >> 1) as i64) ^ -((value & 1) as i64)
160}