@@ -28,9 +28,27 @@ impl PageState {
2828 }
2929}
3030
31+ /// This struct is used to manage the connection to a specific shard database.
32+ /// It contains the shard_id, data_range_id, and a reference to the database pool.
33+ /// `shard_id` is used to identify the shard database.
34+ /// `data_range_id` is used to identify the range of data in the shard database.
35+ /// `pool` is a reference to the database pool that is used to connect to the shard database.
36+ ///
37+ /// `data_range_id` is calculated based on the block height.
38+ /// Since we split the database into ranges of 500,000 blocks,
39+ /// we can use the block height to determine the range.
40+ /// For database, we store ranges in different tables and use `data_range_id` as the suffix.
41+ /// `state_changes_data_{data_range_id}`
42+ ///
43+ /// Example:
44+ /// block_height 73_908_345, the `data_range_id` will be 735 -> `state_changes_data_735`
45+ /// block_height 130_501_000, the `data_range_id` will be 1305 -> `state_changes_data_1305`
46+ ///
47+ /// How to get `data_range_id` from block_height
48+ /// see more details in the `get_data_range_id` function below.
3149pub struct ShardIdPool < ' a > {
3250 shard_id : near_primitives:: types:: ShardId ,
33- table_number : u64 ,
51+ data_range_id : u64 ,
3452 pool : & ' a sqlx:: Pool < sqlx:: Postgres > ,
3553}
3654
@@ -39,7 +57,6 @@ pub struct PostgresDBManager {
3957 shards_pool :
4058 std:: collections:: HashMap < near_primitives:: types:: ShardId , sqlx:: Pool < sqlx:: Postgres > > ,
4159 meta_db_pool : sqlx:: Pool < sqlx:: Postgres > ,
42- earliest_available_block : Option < near_primitives:: types:: BlockHeight > ,
4360}
4461
4562impl PostgresDBManager {
@@ -81,61 +98,14 @@ impl PostgresDBManager {
8198 let shard_id = self . shard_layout . account_id_to_shard_id ( account_id) ;
8299 Ok ( ShardIdPool {
83100 shard_id,
84- table_number : self . get_table_number ( block_height) . await ?,
101+ data_range_id : configuration :: utils :: get_data_range_id ( block_height) . await ?,
85102 pool : self . shards_pool . get ( & shard_id) . ok_or ( anyhow:: anyhow!(
86103 "Database connection for Shard_{} not found" ,
87104 shard_id
88105 ) ) ?,
89106 } )
90107 }
91108
92- /// # Explanation of the logic:
93- ///
94- /// 1. `block_height / 500000`
95- ///
96- /// * This integer division truncates any remainder, effectively grouping numbers into bins of 500,000.
97- ///
98- /// * Example: 73908345 / 500000 = 147 (as integer division discards the remainder).
99- ///
100- /// 2. `* 5`
101- ///
102- /// * Since each bin represents a multiple of 500,000, multiplying by 5 scales the result to match the pattern you provided.
103- ///
104- /// # Example Calculations:
105- /// Example 1: 73908345
106- ///
107- /// 1. 73908345 / 500000 = 147
108- ///
109- /// 2. 147 * 5 = 735
110- ///
111- /// Output: 735
112- ///
113- /// Example 2: 130501000
114- ///
115- /// 1. 130501000 / 500000 = 261
116- ///
117- /// 2. 261 * 5 = 1305
118- ///
119- /// Output: 1305
120- async fn get_table_number (
121- & self ,
122- block_height : & near_primitives:: types:: BlockHeight ,
123- ) -> anyhow:: Result < u64 > {
124- if let Some ( earliest_available_block) = self . earliest_available_block {
125- if * block_height < earliest_available_block {
126- anyhow:: bail!(
127- "Block {} has been garbage collected. The earliest available block is {}" ,
128- block_height,
129- earliest_available_block
130- )
131- } else {
132- Ok ( ( block_height / 500000 ) * 5 )
133- }
134- } else {
135- Ok ( ( block_height / 500000 ) * 5 )
136- }
137- }
138-
139109 async fn run_migrations (
140110 migrator : & sqlx:: migrate:: Migrator ,
141111 pool : & sqlx:: Pool < sqlx:: Postgres > ,
@@ -172,10 +142,6 @@ impl crate::BaseDbManager for PostgresDBManager {
172142 shard_layout,
173143 shards_pool,
174144 meta_db_pool,
175- // TODO: This should be set from the config and not hardcoded
176- // Should be updated when garbage collection is run
177- // or should be None for archive node_mode
178- earliest_available_block : None ,
179145 } ) )
180146 }
181147}
0 commit comments