ckb_app_config/configs/
indexer.rs

1use super::rich_indexer::RichIndexerConfig;
2
3use ckb_types::H256;
4use serde::{Deserialize, Serialize};
5use std::num::NonZeroUsize;
6use std::path::{Path, PathBuf};
7
8/// Indexer config options.
9#[derive(Clone, Debug, Serialize, Deserialize)]
10pub struct IndexerConfig {
11    /// The index store path, default `data_dir / indexer / store`
12    #[serde(default)]
13    pub store: PathBuf,
14    /// The secondary_db path, default `data_dir / indexer / secondary_path`
15    #[serde(default)]
16    pub secondary_path: PathBuf,
17    /// The poll interval by secs
18    #[serde(default = "default_poll_interval")]
19    pub poll_interval: u64,
20    /// Whether to index the pending txs in the ckb tx-pool
21    #[serde(default)]
22    pub index_tx_pool: bool,
23    /// Customize block filter
24    #[serde(default)]
25    pub block_filter: Option<String>,
26    /// Customize cell filter
27    #[serde(default)]
28    pub cell_filter: Option<String>,
29    /// Maximum number of concurrent db background jobs (compactions and flushes)
30    #[serde(default)]
31    pub db_background_jobs: Option<NonZeroUsize>,
32    /// Maximal db info log files to be kept.
33    #[serde(default)]
34    pub db_keep_log_file_num: Option<NonZeroUsize>,
35    /// The init tip block hash
36    #[serde(default)]
37    pub init_tip_hash: Option<H256>,
38    /// limit of indexer request
39    #[serde(default)]
40    pub request_limit: Option<usize>,
41    /// Rich indexer config options
42    #[serde(default)]
43    pub rich_indexer: RichIndexerConfig,
44}
45
46const fn default_poll_interval() -> u64 {
47    2
48}
49
50impl Default for IndexerConfig {
51    fn default() -> Self {
52        IndexerConfig {
53            poll_interval: 2,
54            index_tx_pool: false,
55            store: PathBuf::new(),
56            secondary_path: PathBuf::new(),
57            block_filter: None,
58            cell_filter: None,
59            db_background_jobs: None,
60            db_keep_log_file_num: None,
61            init_tip_hash: None,
62            request_limit: None,
63            rich_indexer: RichIndexerConfig::default(),
64        }
65    }
66}
67
68impl IndexerConfig {
69    /// Canonicalizes paths in the config options.
70    ///
71    /// If `self.store` is not set, set it to `data_dir / indexer / store`.
72    ///
73    /// If `self.secondary_path` is not set, set it to `data_dir / indexer / secondary_path`.
74    ///
75    /// If `self.rich_indexer` is `Sqlite`, and `self.rich_indexer.sqlite.store` is not set,
76    /// set it to `data_dir / indexer / sqlite / sqlite.db`.
77    ///
78    /// If any of the above paths is relative, convert them to absolute path using
79    /// `root_dir` as current working directory.
80    pub fn adjust<P: AsRef<Path>>(&mut self, root_dir: &Path, indexer_dir: P) {
81        _adjust(root_dir, indexer_dir.as_ref(), &mut self.store, "store");
82        _adjust(
83            root_dir,
84            indexer_dir.as_ref(),
85            &mut self.secondary_path,
86            "secondary_path",
87        );
88        _adjust(
89            root_dir,
90            indexer_dir.as_ref(),
91            &mut self.rich_indexer.store,
92            "sqlite/sqlite.db",
93        );
94    }
95}
96
97fn _adjust(root_dir: &Path, indexer_dir: &Path, target: &mut PathBuf, sub: &str) {
98    if target.to_str().is_none() || target.to_str() == Some("") {
99        *target = indexer_dir.to_path_buf().join(sub);
100    } else if target.is_relative() {
101        *target = root_dir.to_path_buf().join(&target)
102    }
103}
104
105/// Indexer sync config options.
106#[derive(Clone, Debug, Serialize, Deserialize)]
107pub struct IndexerSyncConfig {
108    /// The secondary_db path, default `data_dir / indexer / secondary_path`
109    #[serde(default)]
110    pub secondary_path: PathBuf,
111    /// The poll interval by secs
112    #[serde(default = "default_poll_interval")]
113    pub poll_interval: u64,
114    /// Whether to index the pending txs in the ckb tx-pool
115    pub index_tx_pool: bool,
116    /// Maximal db info log files to be kept.
117    #[serde(default)]
118    pub db_keep_log_file_num: Option<NonZeroUsize>,
119}
120
121impl From<&IndexerConfig> for IndexerSyncConfig {
122    fn from(config: &IndexerConfig) -> IndexerSyncConfig {
123        IndexerSyncConfig {
124            secondary_path: config.secondary_path.clone(),
125            poll_interval: config.poll_interval,
126            index_tx_pool: config.index_tx_pool,
127            db_keep_log_file_num: config.db_keep_log_file_num,
128        }
129    }
130}