risingwave_ctl/cmd_impl/hummock/
list_kv.rs

1// Copyright 2022 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 core::ops::Bound::Unbounded;
16
17use risingwave_common::catalog::TableOption;
18use risingwave_common::util::epoch::is_max_epoch;
19use risingwave_hummock_sdk::HummockReadEpoch;
20use risingwave_pb::id::TableId;
21use risingwave_storage::StateStore;
22use risingwave_storage::hummock::CachePolicy;
23use risingwave_storage::store::{
24    NewReadSnapshotOptions, PrefetchOptions, ReadOptions, StateStoreIter, StateStoreRead,
25};
26
27use crate::CtlContext;
28use crate::common::HummockServiceOpts;
29
30pub async fn list_kv(
31    context: &CtlContext,
32    epoch: u64,
33    table_id: TableId,
34    data_dir: Option<String>,
35    use_new_object_prefix_strategy: bool,
36) -> anyhow::Result<()> {
37    let hummock = context
38        .hummock_store(HummockServiceOpts::from_env(
39            data_dir,
40            use_new_object_prefix_strategy,
41        )?)
42        .await?;
43    if is_max_epoch(epoch) {
44        tracing::info!("using MAX EPOCH as epoch");
45    }
46    let range = (Unbounded, Unbounded);
47    let read_snapshot = hummock
48        .new_read_snapshot(
49            HummockReadEpoch::Committed(epoch),
50            NewReadSnapshotOptions {
51                table_id,
52                table_option: TableOption::default(),
53            },
54        )
55        .await?;
56    let mut scan_result = read_snapshot
57        .iter(
58            range,
59            ReadOptions {
60                prefetch_options: PrefetchOptions::prefetch_for_large_range_scan(),
61                cache_policy: CachePolicy::NotFill,
62                ..Default::default()
63            },
64        )
65        .await?;
66    while let Some(item) = scan_result.try_next().await? {
67        let (k, v) = item;
68        let print_string = format!("[t{}]", k.user_key.table_id);
69        println!("{} {:?} => {:?}", print_string, k, v)
70    }
71    Ok(())
72}