Skip to content

Commit 837db50

Browse files
committed
refactor(database, state-indexer): Replace numberic(20,0) for block_heights to biging (i64) to speed inserts and updates up
1 parent dd3c534 commit 837db50

File tree

2 files changed

+20
-30
lines changed

2 files changed

+20
-30
lines changed

database/src/postgres/state_indexer/helpers.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl crate::PostgresDBManager {
165165
shard_id: near_primitives::types::ShardId,
166166
table_prefix: String,
167167
operation_name: String,
168-
updates: Vec<(String, String, bigdecimal::BigDecimal)>, // (account_id, data_key, block_height)
168+
updates: Vec<(String, String, i64)>, // (account_id, data_key, block_height)
169169
) -> anyhow::Result<()> {
170170
if updates.is_empty() {
171171
return Ok(());
@@ -186,8 +186,7 @@ impl crate::PostgresDBManager {
186186
let partition_map = self.partition_map(&shard_id, &pool, &account_ids).await?;
187187

188188
// Group updates per partition
189-
let mut updates_per_partition: HashMap<i32, Vec<(String, String, bigdecimal::BigDecimal)>> =
190-
HashMap::new();
189+
let mut updates_per_partition: HashMap<i32, Vec<(String, String, i64)>> = HashMap::new();
191190
for (account_id, data_key, block_height) in updates {
192191
if let Some(&partition) = partition_map.get(&account_id) {
193192
updates_per_partition.entry(partition).or_default().push((
@@ -294,7 +293,7 @@ impl crate::PostgresDBManager {
294293
shard_id: near_primitives::types::ShardId,
295294
table_prefix: String,
296295
operation_name: String,
297-
inserts: Vec<(String, String, Vec<u8>, bigdecimal::BigDecimal)>, // (account_id, data_key, data_value, block_height)
296+
inserts: Vec<(String, String, Vec<u8>, i64)>, // (account_id, data_key, data_value, block_height)
298297
) -> anyhow::Result<()> {
299298
if inserts.is_empty() {
300299
return Ok(());
@@ -313,10 +312,8 @@ impl crate::PostgresDBManager {
313312
let partition_map = self.partition_map(&shard_id, &pool, &account_ids).await?;
314313

315314
// Group inserts by partition for efficient batch processing
316-
let mut inserts_per_partition: HashMap<
317-
i32,
318-
Vec<(String, String, Vec<u8>, bigdecimal::BigDecimal)>,
319-
> = HashMap::new();
315+
let mut inserts_per_partition: HashMap<i32, Vec<(String, String, Vec<u8>, i64)>> =
316+
HashMap::new();
320317
for (account_id, data_key, data_value, block_height) in inserts {
321318
if let Some(&partition) = partition_map.get(&account_id) {
322319
inserts_per_partition.entry(partition).or_default().push((
@@ -421,7 +418,7 @@ impl crate::PostgresDBManager {
421418
shard_id: near_primitives::types::ShardId,
422419
table_prefix: String,
423420
operation_name: String,
424-
inserts: Vec<(String, Vec<u8>, bigdecimal::BigDecimal)>, // (account_id, data_value, block_height)
421+
inserts: Vec<(String, Vec<u8>, i64)>, // (account_id, data_value, block_height)
425422
) -> anyhow::Result<()> {
426423
if inserts.is_empty() {
427424
return Ok(());
@@ -439,10 +436,7 @@ impl crate::PostgresDBManager {
439436
let partition_map = self.partition_map(&shard_id, &pool, &account_ids).await?;
440437

441438
// Group inserts per partition
442-
let mut inserts_per_partition: HashMap<
443-
i32,
444-
Vec<(String, Vec<u8>, bigdecimal::BigDecimal)>,
445-
> = HashMap::new();
439+
let mut inserts_per_partition: HashMap<i32, Vec<(String, Vec<u8>, i64)>> = HashMap::new();
446440
for (account_id, data_value, block_height) in inserts {
447441
if let Some(&partition) = partition_map.get(&account_id) {
448442
inserts_per_partition.entry(partition).or_default().push((

database/src/postgres/state_indexer/mod.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
229229
self.record_shard_write_metric(shard_id, "save_state_changes_data", "state_changes_data");
230230

231231
// Extract relevant data
232-
let inserts: Vec<(String, String, Vec<u8>, bigdecimal::BigDecimal)> = state_changes
232+
let inserts: Vec<(String, String, Vec<u8>, i64)> = state_changes
233233
.iter()
234234
.filter_map(|change| {
235235
if let near_primitives::views::StateChangeValueView::DataUpdate {
@@ -243,7 +243,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
243243
account_id.to_string(),
244244
data_key,
245245
value.clone().to_vec(),
246-
bigdecimal::BigDecimal::from(block_height),
246+
block_height as i64, // Convert to i64 for database compatibility
247247
))
248248
} else {
249249
None
@@ -269,7 +269,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
269269
) -> anyhow::Result<()> {
270270
self.record_shard_write_metric(shard_id, "save_state_changes_data", "state_changes_data");
271271

272-
let updates: Vec<(String, String, bigdecimal::BigDecimal)> =
272+
let updates: Vec<(String, String, i64)> =
273273
state_changes
274274
.iter()
275275
.filter_map(|change| match &change.value {
@@ -287,7 +287,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
287287
Some((
288288
account_id.to_string(),
289289
data_key,
290-
bigdecimal::BigDecimal::from(block_height),
290+
block_height as i64, // Convert to i64 for database compatibility
291291
))
292292
}
293293
_ => None,
@@ -320,7 +320,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
320320
);
321321

322322
// Extract relevant updates
323-
let inserts: Vec<(String, String, Vec<u8>, bigdecimal::BigDecimal)> = state_changes
323+
let inserts: Vec<(String, String, Vec<u8>, i64)> = state_changes
324324
.iter()
325325
.filter_map(|change| {
326326
if let near_primitives::views::StateChangeValueView::AccessKeyUpdate {
@@ -338,7 +338,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
338338
account_id.to_string(),
339339
data_key,
340340
data_value,
341-
bigdecimal::BigDecimal::from(block_height),
341+
block_height as i64, // Convert to i64 for database compatibility
342342
))
343343
} else {
344344
None
@@ -379,8 +379,8 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
379379
);
380380

381381
// Collect updates as triples (account_id, data_key, block_height)
382-
let block_height_bd = bigdecimal::BigDecimal::from(block_height);
383-
let updates: Vec<(String, String, bigdecimal::BigDecimal)> = state_changes
382+
let block_height_bd = block_height as i64; // Convert to i64 for database compatibility
383+
let updates: Vec<(String, String, i64)> = state_changes
384384
.iter()
385385
.filter_map(|c| match &c.value {
386386
near_primitives::views::StateChangeValueView::AccessKeyUpdate {
@@ -394,7 +394,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
394394
} => Some((
395395
account_id.to_string(),
396396
hex::encode(public_key.key_data()),
397-
block_height_bd.clone(), // same height for all rows
397+
block_height_bd, // same height for all rows
398398
)),
399399
_ => None,
400400
})
@@ -428,19 +428,15 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
428428
);
429429

430430
// Extract only ContractCodeUpdate
431-
let inserts: Vec<(String, Vec<u8>, bigdecimal::BigDecimal)> = state_changes
431+
let inserts: Vec<(String, Vec<u8>, i64)> = state_changes
432432
.into_iter()
433433
.filter_map(|change| {
434434
if let near_primitives::views::StateChangeValueView::ContractCodeUpdate {
435435
account_id,
436436
code,
437437
} = change.value
438438
{
439-
Some((
440-
account_id.to_string(),
441-
code.to_vec(),
442-
bigdecimal::BigDecimal::from(block_height),
443-
))
439+
Some((account_id.to_string(), code.to_vec(), block_height as i64))
444440
} else {
445441
None
446442
}
@@ -536,7 +532,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
536532
);
537533

538534
// Extract account updates
539-
let inserts: Vec<(String, Vec<u8>, bigdecimal::BigDecimal)> = state_changes
535+
let inserts: Vec<(String, Vec<u8>, i64)> = state_changes
540536
.into_iter()
541537
.filter_map(|change| {
542538
if let near_primitives::views::StateChangeValueView::AccountUpdate {
@@ -550,7 +546,7 @@ impl crate::StateIndexerDbManager for crate::PostgresDBManager {
550546
Some((
551547
account_id.to_string(),
552548
data_value,
553-
bigdecimal::BigDecimal::from(block_height),
549+
block_height as i64, // Convert to i64 for database compatibility
554550
))
555551
} else {
556552
None

0 commit comments

Comments
 (0)