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
43    #[inline(always)]
44    fn fory_get_type_id(_: &TypeResolver) -> Result<TypeId, Error> {
45        Ok(TypeId::DECIMAL)
46    }
47
48    #[inline(always)]
49    fn fory_type_id_dyn(&self, _: &TypeResolver) -> Result<TypeId, Error> {
50        Ok(TypeId::DECIMAL)
51    }
52
53    #[inline(always)]
54    fn fory_static_type_id() -> TypeId {
55        TypeId::DECIMAL
56    }
57
58    #[inline(always)]
59    fn as_any(&self) -> &dyn std::any::Any {
60        self
61    }
62
63    #[inline(always)]
64    fn fory_write_type_info(context: &mut WriteContext) -> Result<(), Error> {
65        context.writer.write_var_u32(TypeId::DECIMAL as u32);
66        Ok(())
67    }
68
69    #[inline(always)]
70    fn fory_read_type_info(context: &mut ReadContext) -> Result<(), Error> {
71        read_basic_type_info::<Self>(context)
72    }
73}
74
75impl ForyDefault for Decimal {
76    #[inline(always)]
77    fn fory_default() -> Self {
78        Self {
79            unscaled: BigInt::from(0),
80            scale: 0,
81        }
82    }
83}
84
85fn write_decimal_unscaled(value: &BigInt, writer: &mut Writer) -> Result<(), Error> {
86    if let Some(small_value) = can_use_small_encoding(value) {
87        writer.write_var_u64(encode_zigzag64(small_value) << 1);
88        return Ok(());
89    }
90
91    let (sign, payload) = value.to_bytes_le();
92    if payload.is_empty() {
93        return Err(Error::invalid_data(
94            "zero must use the small decimal encoding".to_string(),
95        ));
96    }
97    let meta = ((payload.len() as u64) << 1) | u64::from(matches!(sign, Sign::Minus));
98    writer.write_var_u64((meta << 1) | 1);
99    writer.write_bytes(&payload);
100    Ok(())
101}
102
103fn read_decimal_unscaled(reader: &mut Reader) -> Result<BigInt, Error> {
104    let header = reader.read_var_u64()?;
105    if (header & 1) == 0 {
106        return Ok(BigInt::from(decode_zigzag64(header >> 1)));
107    }
108
109    let meta = header >> 1;
110    let sign = (meta & 1) != 0;
111    let len = (meta >> 1) as usize;
112    if len == 0 {
113        return Err(Error::invalid_data(
114            "invalid decimal magnitude length 0".to_string(),
115        ));
116    }
117    let payload = reader.read_bytes(len)?;
118    if payload[len - 1] == 0 {
119        return Err(Error::invalid_data(
120            "non-canonical decimal payload: trailing zero byte".to_string(),
121        ));
122    }
123    let magnitude = BigInt::from_bytes_le(Sign::Plus, payload);
124    if magnitude == BigInt::from(0) {
125        return Err(Error::invalid_data(
126            "big decimal encoding must not represent zero".to_string(),
127        ));
128    }
129    Ok(if sign { -magnitude } else { magnitude })
130}
131
132fn can_use_small_encoding(value: &BigInt) -> Option<i64> {
133    let small_value = i64::try_from(value).ok()?;
134    if (encode_zigzag64(small_value) & (1u64 << 63)) == 0 {
135        Some(small_value)
136    } else {
137        None
138    }
139}
140
141#[inline(always)]
142fn encode_zigzag64(value: i64) -> u64 {
143    ((value << 1) ^ (value >> 63)) as u64
144}
145
146#[inline(always)]
147fn decode_zigzag64(value: u64) -> i64 {
148    ((value >> 1) as i64) ^ -((value & 1) as i64)
149}