Skip to content

Commit

Permalink
add StoreSetSum<T> type which gives the user the ability to update a …
Browse files Browse the repository at this point in the history
…sum to a fixed value in case updates are required
  • Loading branch information
colindickson committed May 14, 2024
1 parent 7d5b5a3 commit 5fd5d35
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased

- Add new store type StoreSetSum<T> which allows both summing and setting a value in a store. This is useful for storing aggregated values in a store.

## 0.5.14

- Add index keys protobuf in substreams crate
Expand Down
28 changes: 28 additions & 0 deletions substreams/src/externs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,33 @@ pub mod state {
value_ptr: *const u8,
value_len: u32,
);
pub fn set_sum_bigint(
ord: i64,
key_ptr: *const u8,
key_len: u32,
value_ptr: *const u8,
value_len: u32,
);
pub fn set_sum_bigdecimal(
ord: i64,
key_ptr: *const u8,
key_len: u32,
value_ptr: *const u8,
value_len: u32,
);
pub fn set_sum_int64(
ord: i64,
key_ptr: *const u8,
key_len: u32,
value_ptr: *const u8,
value_len: u32,
);
pub fn set_sum_float64(
ord: i64,
key_ptr: *const u8,
key_len: u32,
value_ptr: *const u8,
value_len: u32,
);
}
}
92 changes: 92 additions & 0 deletions substreams/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,3 +423,95 @@ where
}
}
}

#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))]
pub fn set_sum_bigint<K, V>(ord: i64, key: K, value: V)
where
K: AsRef<str>,
V: AsRef<str>, String: From<V>
{
#[cfg(target_arch = "wasm32")]
{
let key = key.as_ref();
let data: String = value.into();

unsafe {
externs::state::set_sum_bigint(
ord,
key.as_ptr(),
key.len() as u32,
data.as_ptr(),
data.len() as u32,
)
}
}
}

#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))]
pub fn set_sum_bigdecimal<K, V>(ord: i64, key: K, value: V)
where
K: AsRef<str>,
V: AsRef<str>, String: From<V>
{
#[cfg(target_arch = "wasm32")]
{
let key = key.as_ref();
let data: String = value.into();

unsafe {
externs::state::set_sum_bigdecimal(
ord,
key.as_ptr(),
key.len() as u32,
data.as_ptr(),
data.len() as u32,
)
}
}
}

#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))]
pub fn set_sum_int64<K, V>(ord: i64, key: K, value: V)
where
K: AsRef<str>,
V: AsRef<str>, String: From<V>
{
#[cfg(target_arch = "wasm32")]
{
let key = key.as_ref();
let data: String = value.into();

unsafe {
externs::state::set_sum_int64(
ord,
key.as_ptr(),
key.len() as u32,
data.as_ptr(),
data.len() as u32,
)
}
}
}

#[cfg_attr(not(target_arch = "wasm32"), allow(unused_variables))]
pub fn set_sum_float64<K, V>(ord: i64, key: K, value: V)
where
K: AsRef<str>,
V: AsRef<str>, String: From<V>
{
#[cfg(target_arch = "wasm32")]
{
let key = key.as_ref();
let data: String = value.into();

unsafe {
externs::state::set_sum_float64(
ord,
key.as_ptr(),
key.len() as u32,
data.as_ptr(),
data.len() as u32,
)
}
}
}
79 changes: 79 additions & 0 deletions substreams/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,85 @@ where
}
}

// -------------------- StoreSetSum -------------------- //
pub trait StoreSetSum<T> {
fn new() -> Self;
fn set<K: AsRef<str>>(&self, ord: u64, key: K, value: T);
fn sum<K: AsRef<str>>(&self, ord: u64, key: K, value: T);
}

pub struct StoreSetSumInt64 {}

impl StoreSetSum<i64> for StoreSetSumInt64 {
fn new() -> Self {
StoreSetSumInt64 {}
}

fn set<K: AsRef<str>>(&self, ord: u64, key: K, value: i64) {
let v = format!("set:{}", value.to_string());
state::set_sum_int64(ord as i64, key, v);
}

fn sum<K: AsRef<str>>(&self, ord: u64, key: K, value: i64) {
let v = format!("sum:{}", value.to_string());
state::set_sum_int64(ord as i64, key, v);
}
}

pub struct StoreSetSumFloat64 {}

impl StoreSetSum<f64> for StoreSetSumFloat64 {
fn new() -> Self {
StoreSetSumFloat64 {}
}

fn set<K: AsRef<str>>(&self, ord: u64, key: K, value: f64) {
let v = format!("set:{}", value.to_string());
state::set_sum_float64(ord as i64, key, v);
}

fn sum<K: AsRef<str>>(&self, ord: u64, key: K, value: f64) {
let v = format!("sum:{}", value.to_string());
state::set_sum_float64(ord as i64, key, v);
}
}

pub struct StoreSetSumBigInt {}

impl StoreSetSum<BigInt> for StoreSetSumBigInt {
fn new() -> Self {
StoreSetSumBigInt {}
}

fn set<K: AsRef<str>>(&self, ord: u64, key: K, value: BigInt) {
let v = format!("set:{}", value.to_string());
state::set_sum_bigint(ord as i64, key, v);
}

fn sum<K: AsRef<str>>(&self, ord: u64, key: K, value: BigInt) {
let v = format!("sum:{}", value.to_string());
state::set_sum_bigint(ord as i64, key, v);
}
}

pub struct StoreSetSumBigDecimal {}

impl StoreSetSum<BigDecimal> for StoreSetSumBigDecimal {
fn new() -> Self {
StoreSetSumBigDecimal {}
}

fn set<K: AsRef<str>>(&self, ord: u64, key: K, value: BigDecimal) {
let v = format!("set:{}", value.to_string());
state::set_sum_bigdecimal(ord as i64, key, v);
}

fn sum<K: AsRef<str>>(&self, ord: u64, key: K, value: BigDecimal) {
let v = format!("sum:{}", value.to_string());
state::set_sum_bigdecimal(ord as i64, key, v);
}
}

// -------------------- StoreGet -------------------- //
/// StoreGet is a trait which is implemented on any type of typed StoreGet
pub trait StoreGet<T> {
Expand Down

0 comments on commit 5fd5d35

Please sign in to comment.