risingwave_common/config/streaming/
over_window.rs

1// Copyright 2025 RisingWave Labs
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 std::str::FromStr;
16
17use enum_as_inner::EnumAsInner;
18use parse_display::Display;
19use risingwave_pb::stream_plan::PbOverWindowCachePolicy;
20use serde_with::{DeserializeFromStr, SerializeDisplay};
21
22#[derive(
23    Copy,
24    Default,
25    Debug,
26    Clone,
27    PartialEq,
28    Eq,
29    Display,
30    EnumAsInner,
31    SerializeDisplay,
32    DeserializeFromStr,
33)]
34#[display(style = "snake_case")]
35pub enum OverWindowCachePolicy {
36    /// Cache all entries.
37    #[default]
38    Full,
39    /// Cache only recently accessed range of entries.
40    Recent,
41    /// Cache only the first N entries in recently accessed range.
42    RecentFirstN,
43    /// Cache only the last N entries in recently accessed range.
44    RecentLastN,
45}
46
47impl FromStr for OverWindowCachePolicy {
48    type Err = &'static str;
49
50    fn from_str(s: &str) -> Result<Self, Self::Err> {
51        let s = s.to_ascii_lowercase().replace('-', "_");
52        match s.as_str() {
53            "full" => Ok(Self::Full),
54            "recent" => Ok(Self::Recent),
55            "recent_first_n" => Ok(Self::RecentFirstN),
56            "recent_last_n" => Ok(Self::RecentLastN),
57            _ => Err("expect one of [full, recent, recent_first_n, recent_last_n]"),
58        }
59    }
60}
61
62impl OverWindowCachePolicy {
63    pub fn to_protobuf(self) -> PbOverWindowCachePolicy {
64        match self {
65            Self::Full => PbOverWindowCachePolicy::Full,
66            Self::Recent => PbOverWindowCachePolicy::Recent,
67            Self::RecentFirstN => PbOverWindowCachePolicy::RecentFirstN,
68            Self::RecentLastN => PbOverWindowCachePolicy::RecentLastN,
69        }
70    }
71
72    pub fn from_protobuf(pb: PbOverWindowCachePolicy) -> Self {
73        match pb {
74            PbOverWindowCachePolicy::Unspecified => Self::default(),
75            PbOverWindowCachePolicy::Full => Self::Full,
76            PbOverWindowCachePolicy::Recent => Self::Recent,
77            PbOverWindowCachePolicy::RecentFirstN => Self::RecentFirstN,
78            PbOverWindowCachePolicy::RecentLastN => Self::RecentLastN,
79        }
80    }
81}
82
83#[cfg(test)]
84mod tests {
85    use super::*;
86
87    #[test]
88    fn parse_over_window_cache_policy() {
89        assert_eq!(
90            OverWindowCachePolicy::from_str("full").unwrap(),
91            OverWindowCachePolicy::Full
92        );
93        assert_eq!(
94            OverWindowCachePolicy::from_str("recent").unwrap(),
95            OverWindowCachePolicy::Recent
96        );
97        assert_eq!(
98            OverWindowCachePolicy::from_str("RECENT").unwrap(),
99            OverWindowCachePolicy::Recent
100        );
101        assert_eq!(
102            OverWindowCachePolicy::from_str("recent_first_n").unwrap(),
103            OverWindowCachePolicy::RecentFirstN
104        );
105        assert_eq!(
106            OverWindowCachePolicy::from_str("recent_last_n").unwrap(),
107            OverWindowCachePolicy::RecentLastN
108        );
109        assert_eq!(
110            OverWindowCachePolicy::from_str("recent-last-n").unwrap(),
111            OverWindowCachePolicy::RecentLastN
112        );
113        assert_eq!(
114            OverWindowCachePolicy::from_str("recent_last_N").unwrap(),
115            OverWindowCachePolicy::RecentLastN
116        );
117        assert!(OverWindowCachePolicy::from_str("foo").is_err());
118    }
119}