Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sui-field-count: const instead of fn and use it in sui-indexer-alt #20143

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
sui-field-count: const instead of fn and use it in sui-indexer-alt
  • Loading branch information
gegaowp committed Dec 2, 2024
commit d8a440cc6cf9d05411a1a0f7c0f5e7fd4bcc71bb
4 changes: 1 addition & 3 deletions crates/sui-field-count-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ pub fn field_count_derive(input: TokenStream) -> TokenStream {

let expanded = quote! {
impl #impl_generics FieldCount for #name #ty_generics #where_clause {
fn field_count() -> usize {
#fields_count
}
const FIELD_COUNT: usize = #fields_count;
}
};

Expand Down
16 changes: 8 additions & 8 deletions crates/sui-field-count/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
pub use sui_field_count_derive::*;

pub trait FieldCount {
fn field_count() -> usize;
const FIELD_COUNT: usize;
}

#[cfg(test)]
Expand All @@ -22,8 +22,8 @@ mod tests {
_field3: bool,
}

assert_eq!(BasicStruct::field_count(), 3);
assert_eq!(EmptyStruct::field_count(), 0);
assert_eq!(BasicStruct::FIELD_COUNT, 3);
assert_eq!(EmptyStruct::FIELD_COUNT, 0);
}

#[test]
Expand All @@ -35,7 +35,7 @@ mod tests {
_field3: &'a Vec<String>,
}

assert_eq!(LifetimeStruct::field_count(), 3);
assert_eq!(LifetimeStruct::FIELD_COUNT, 3);
}

#[test]
Expand All @@ -47,8 +47,8 @@ mod tests {
_field3: Option<T>,
}

assert_eq!(GenericStruct::<String>::field_count(), 3);
assert_eq!(GenericStruct::<i32>::field_count(), 3);
assert_eq!(GenericStruct::<String>::FIELD_COUNT, 3);
assert_eq!(GenericStruct::<i32>::FIELD_COUNT, 3);
}

#[test]
Expand All @@ -62,7 +62,7 @@ mod tests {
_field2: Vec<T>,
}

assert_eq!(WhereStruct::<String>::field_count(), 2);
assert_eq!(WhereStruct::<i32>::field_count(), 2);
assert_eq!(WhereStruct::<String>::FIELD_COUNT, 2);
assert_eq!(WhereStruct::<i32>::FIELD_COUNT, 2);
}
}
11 changes: 2 additions & 9 deletions crates/sui-indexer-alt/src/handlers/sum_coin_balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ use crate::{
schema::sum_coin_balances,
};

/// Each insert or update will include at most this many rows -- the size is chosen to maximize the
/// rows without hitting the limit on bind parameters.
const UPDATE_CHUNK_ROWS: usize = i16::MAX as usize / 5;
gegaowp marked this conversation as resolved.
Show resolved Hide resolved

/// Each deletion will include at most this many rows.
const DELETE_CHUNK_ROWS: usize = i16::MAX as usize;

pub struct SumCoinBalances;

impl Processor for SumCoinBalances {
Expand Down Expand Up @@ -159,8 +152,8 @@ impl Handler for SumCoinBalances {
}
}

let update_chunks = updates.chunks(UPDATE_CHUNK_ROWS).map(Either::Left);
let delete_chunks = deletes.chunks(DELETE_CHUNK_ROWS).map(Either::Right);
let update_chunks = updates.chunks(Self::INSERT_CHUNK_ROWS).map(Either::Left);
let delete_chunks = deletes.chunks(Self::DELETE_CHUNK_ROWS).map(Either::Right);

let futures = update_chunks.chain(delete_chunks).map(|chunk| match chunk {
Either::Left(update) => Either::Left(
Expand Down
11 changes: 2 additions & 9 deletions crates/sui-indexer-alt/src/handlers/sum_obj_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ use crate::{
schema::sum_obj_types,
};

/// Each insert or update will include at most this many rows -- the size is chosen to maximize the
/// rows without hitting the limit on bind parameters.
const UPDATE_CHUNK_ROWS: usize = i16::MAX as usize / 8;

/// Each deletion will include at most this many rows.
const DELETE_CHUNK_ROWS: usize = i16::MAX as usize;

pub struct SumObjTypes;

impl Processor for SumObjTypes {
Expand Down Expand Up @@ -158,8 +151,8 @@ impl Handler for SumObjTypes {
}
}

let update_chunks = updates.chunks(UPDATE_CHUNK_ROWS).map(Either::Left);
let delete_chunks = deletes.chunks(DELETE_CHUNK_ROWS).map(Either::Right);
let update_chunks = updates.chunks(Self::INSERT_CHUNK_ROWS).map(Either::Left);
let delete_chunks = deletes.chunks(Self::DELETE_CHUNK_ROWS).map(Either::Right);

let futures = update_chunks.chain(delete_chunks).map(|chunk| match chunk {
Either::Left(update) => Either::Left(
Expand Down
7 changes: 6 additions & 1 deletion crates/sui-indexer-alt/src/models/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct StoredObject {
pub serialized_object: Option<Vec<u8>>,
}

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = obj_versions, primary_key(object_id, object_version))]
pub struct StoredObjVersion {
pub object_id: Vec<u8>,
Expand Down Expand Up @@ -99,6 +99,11 @@ pub struct StoredWalObjType {
pub cp_sequence_number: i64,
}

/// StoredObjectUpdate is a wrapper type, we want to count the fields of the inner type.
impl<T: FieldCount> FieldCount for StoredObjectUpdate<T> {
const FIELD_COUNT: usize = T::FIELD_COUNT;
}

impl<DB: Backend> serialize::ToSql<SmallInt, DB> for StoredOwnerKind
where
i16: serialize::ToSql<SmallInt, DB>,
Expand Down
10 changes: 9 additions & 1 deletion crates/sui-indexer-alt/src/pipeline/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::sync::atomic::AtomicU64;
use std::sync::Arc;

use mysten_metrics::spawn_monitored_task;
use sui_field_count::FieldCount;
use sui_types::full_checkpoint_content::CheckpointData;
use tokio::{sync::mpsc, task::JoinHandle};
use tokio_stream::wrappers::ReceiverStream;
Expand All @@ -25,8 +26,15 @@ pub trait Processor {
/// How much concurrency to use when processing checkpoint data.
const FANOUT: usize = 10;

/// Each insert or update will include at most this many rows -- the size is chosen to maximize the
/// rows without hitting the limit on bind parameters.
const INSERT_CHUNK_ROWS: usize = i16::MAX as usize / Self::Value::FIELD_COUNT;

/// Each deletion will include at most this many rows without hitting the limit on bind parameters.
const DELETE_CHUNK_ROWS: usize = i16::MAX as usize;

/// The type of value being inserted by the handler.
type Value: Send + Sync + 'static;
type Value: Send + Sync + 'static + FieldCount;
gegaowp marked this conversation as resolved.
Show resolved Hide resolved

/// The processing logic for turning a checkpoint into rows of the table.
fn process(&self, checkpoint: &Arc<CheckpointData>) -> anyhow::Result<Vec<Self::Value>>;
Expand Down