store_api/codec.rs
1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use api::v1::WriteHint;
16use serde::{Deserialize, Serialize};
17use strum::Display;
18
19/// Primary key encoding mode.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize, Display)]
21#[serde(rename_all = "snake_case")]
22pub enum PrimaryKeyEncoding {
23 #[default]
24 /// Dense primary key encoding.
25 Dense,
26 /// Sparse primary key encoding.
27 Sparse,
28}
29
30impl From<api::v1::PrimaryKeyEncoding> for PrimaryKeyEncoding {
31 fn from(value: api::v1::PrimaryKeyEncoding) -> Self {
32 match value {
33 api::v1::PrimaryKeyEncoding::Dense => PrimaryKeyEncoding::Dense,
34 api::v1::PrimaryKeyEncoding::Sparse => PrimaryKeyEncoding::Sparse,
35 }
36 }
37}
38
39/// Infer primary key encoding from hint.
40pub fn infer_primary_key_encoding_from_hint(hint: Option<&WriteHint>) -> PrimaryKeyEncoding {
41 hint.map(|hint| PrimaryKeyEncoding::from(hint.primary_key_encoding()))
42 .unwrap_or(PrimaryKeyEncoding::Dense)
43}