Skip to main content

catalog/kvbackend/
table_cache.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 std::sync::Arc;
16
17use common_meta::cache::{
18    CacheContainer, InitStrategy, Initializer, TableInfoCacheRef, TableNameCacheRef,
19};
20use common_meta::error::{Result as MetaResult, ValueNotExistSnafu};
21use common_meta::instruction::CacheIdent;
22use futures::future::BoxFuture;
23use moka::future::Cache;
24use snafu::OptionExt;
25use table::TableRef;
26use table::dist_table::DistTable;
27use table::table_name::TableName;
28
29pub type TableCacheRef = Arc<TableCache>;
30
31/// [TableCache] caches the [TableName] to [TableRef] mapping.
32pub type TableCache = CacheContainer<TableName, TableRef, CacheIdent>;
33
34/// Constructs a [TableCache].
35pub fn new_table_cache(
36    name: String,
37    cache: Cache<TableName, TableRef>,
38    table_info_cache: TableInfoCacheRef,
39    table_name_cache: TableNameCacheRef,
40) -> TableCache {
41    let init = init_factory(table_info_cache, table_name_cache);
42
43    CacheContainer::with_strategy(
44        name,
45        cache,
46        Box::new(invalidator),
47        init,
48        filter,
49        InitStrategy::VersionChecked,
50    )
51}
52
53fn init_factory(
54    table_info_cache: TableInfoCacheRef,
55    table_name_cache: TableNameCacheRef,
56) -> Initializer<TableName, TableRef> {
57    Arc::new(move |table_name| {
58        let table_info_cache = table_info_cache.clone();
59        let table_name_cache = table_name_cache.clone();
60        Box::pin(async move {
61            let table_id = table_name_cache
62                .get_by_ref(table_name)
63                .await?
64                .context(ValueNotExistSnafu)?;
65            let table_info = table_info_cache
66                .get_by_ref(&table_id)
67                .await?
68                .context(ValueNotExistSnafu)?;
69
70            Ok(Some(DistTable::table(table_info)))
71        })
72    })
73}
74
75fn invalidator<'a>(
76    cache: &'a Cache<TableName, TableRef>,
77    idents: &'a [&CacheIdent],
78) -> BoxFuture<'a, MetaResult<()>> {
79    Box::pin(async move {
80        for ident in idents {
81            if let CacheIdent::TableName(table_name) = ident {
82                cache.invalidate(table_name).await
83            }
84        }
85        Ok(())
86    })
87}
88
89fn filter(ident: &CacheIdent) -> bool {
90    matches!(ident, CacheIdent::TableName(_))
91}