Skip to content

Commit

Permalink
Updated store and state methods to accept key of type AsRef<str>
Browse files Browse the repository at this point in the history
  • Loading branch information
maoueh committed Jul 29, 2022
1 parent a1f7517 commit 4337ab3
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 106 deletions.
52 changes: 52 additions & 0 deletions docs/release-notes/change-log.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,58 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

**New updatePolicy `append`**, allows one to build a store that concatenates values and supports parallelism. This affects the server, the manifest format (additive only), the substreams crate and the generated code therein.

### Rust API

- Store APIs methods now accept `key` of type `AsRef<str>` which means for example that both `String` an `&str` are accepted as inputs in:

- `StoreSet::set`
- `StoreSet::set_many`
- `StoreSet::set_if_not_exists`
- `StoreSet::set_if_not_exists_many`
- `StoreAddInt64::add`
- `StoreAddInt64::add_many`
- `StoreAddFloat64::add`
- `StoreAddFloat64::add_many`
- `StoreAddBigFloat::add`
- `StoreAddBigFloat::add_many`
- `StoreAddBigInt::add`
- `StoreAddBigInt::add_many`
- `StoreMaxInt64::max`
- `StoreMaxFloat64::max`
- `StoreMaxBigInt::max`
- `StoreMaxBigFloat::max`
- `StoreMinInt64::min`
- `StoreMinFloat64::min`
- `StoreMinBigInt::min`
- `StoreMinBigFloat::min`
- `StoreAppend::append`
- `StoreAppend::append_bytes`
- `StoreGet::get_at`
- `StoreGet::get_last`
- `StoreGet::get_first`

- Low-level state methods now accept `key` of type `AsRef<str>` which means for example that both `String` an `&str` are accepted as inputs in:

- `state::get_at`
- `state::get_last`
- `state::get_first`
- `state::set`
- `state::set_if_not_exists`
- `state::append`
- `state::delete_prefix`
- `state::add_bigint`
- `state::add_int64`
- `state::add_float64`
- `state::add_bigfloat`
- `state::set_min_int64`
- `state::set_min_bigint`
- `state::set_min_float64`
- `state::set_min_bigfloat`
- `state::set_max_int64`
- `state::set_max_bigint`
- `state::set_max_float64`
- `state::set_max_bigfloat`

### CLI

* Changed the output modes: `module-*` modes are gone and become the
Expand Down
151 changes: 76 additions & 75 deletions rust/substreams/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::externs;
use crate::memory;
use num_bigint::{BigInt};
use bigdecimal::BigDecimal;
use num_bigint::BigInt;

pub fn get_at<K: AsRef<str>>(store_idx: u32, ord: i64, key: K) -> Option<Vec<u8>> {
let key = key.as_ref();

pub fn get_at(store_idx: u32, ord: i64, key: &String) -> Option<Vec<u8>> {
unsafe {
let key_bytes = key.as_bytes();
let output_ptr = memory::alloc(8);
Expand All @@ -21,7 +23,9 @@ pub fn get_at(store_idx: u32, ord: i64, key: &String) -> Option<Vec<u8>> {
};
}
}
pub fn get_last(store_idx: u32, key: &String) -> Option<Vec<u8>> {
pub fn get_last<K: AsRef<str>>(store_idx: u32, key: K) -> Option<Vec<u8>> {
let key = key.as_ref();

unsafe {
let key_bytes = key.as_bytes();
let output_ptr = memory::alloc(8);
Expand All @@ -39,7 +43,9 @@ pub fn get_last(store_idx: u32, key: &String) -> Option<Vec<u8>> {
};
}
}
pub fn get_first(store_idx: u32, key: &String) -> Option<Vec<u8>> {
pub fn get_first<K: AsRef<str>>(store_idx: u32, key: K) -> Option<Vec<u8>> {
let key = key.as_ref();

unsafe {
let key_bytes = key.as_bytes();
let output_ptr = memory::alloc(8);
Expand All @@ -57,7 +63,9 @@ pub fn get_first(store_idx: u32, key: &String) -> Option<Vec<u8>> {
};
}
}
pub fn set(ord: i64, key: String, value: &Vec<u8>) {
pub fn set<K: AsRef<str>>(ord: i64, key: K, value: &Vec<u8>) {
let key = key.as_ref();

unsafe {
externs::state::set(
ord,
Expand All @@ -68,7 +76,9 @@ pub fn set(ord: i64, key: String, value: &Vec<u8>) {
)
}
}
pub fn set_if_not_exists(ord: i64, key: String, value: &Vec<u8>) {
pub fn set_if_not_exists<K: AsRef<str>>(ord: i64, key: K, value: &Vec<u8>) {
let key = key.as_ref();

unsafe {
externs::state::set_if_not_exists(
ord,
Expand All @@ -80,7 +90,9 @@ pub fn set_if_not_exists(ord: i64, key: String, value: &Vec<u8>) {
}
}

pub fn append(ord: i64, key: String, value: &Vec<u8>) {
pub fn append<K: AsRef<str>>(ord: i64, key: K, value: &Vec<u8>) {
let key = key.as_ref();

unsafe {
externs::state::append(
ord,
Expand All @@ -92,17 +104,16 @@ pub fn append(ord: i64, key: String, value: &Vec<u8>) {
}
}

pub fn delete_prefix(ord: i64, prefix: &String){
unsafe {
externs::state::delete_prefix(
ord,
prefix.as_ptr(),
prefix.len() as u32,
)
}
pub fn delete_prefix<K: AsRef<str>>(ord: i64, prefix: K) {
let prefix = prefix.as_ref();

unsafe { externs::state::delete_prefix(ord, prefix.as_ptr(), prefix.len() as u32) }
}
pub fn add_bigint(ord: i64, key: String, value: &BigInt) {

pub fn add_bigint<K: AsRef<str>>(ord: i64, key: K, value: &BigInt) {
let key = key.as_ref();
let data = value.to_string();

unsafe {
externs::state::add_bigint(
ord,
Expand All @@ -113,28 +124,22 @@ pub fn add_bigint(ord: i64, key: String, value: &BigInt) {
)
}
}
pub fn add_int64(ord: i64, key: String, value: i64) {
unsafe {
externs::state::add_int64(
ord,
key.as_ptr(),
key.len() as u32,
value,
)
}
pub fn add_int64<K: AsRef<str>>(ord: i64, key: K, value: i64) {
let key = key.as_ref();

unsafe { externs::state::add_int64(ord, key.as_ptr(), key.len() as u32, value) }
}
pub fn add_float64(ord: i64, key: String, value: f64) {
unsafe {
externs::state::add_float64(
ord,
key.as_ptr(),
key.len() as u32,
value
)
}

pub fn add_float64<K: AsRef<str>>(ord: i64, key: K, value: f64) {
let key = key.as_ref();

unsafe { externs::state::add_float64(ord, key.as_ptr(), key.len() as u32, value) }
}
pub fn add_bigfloat(ord: i64, key: String, value: &BigDecimal) {

pub fn add_bigfloat<K: AsRef<str>>(ord: i64, key: K, value: &BigDecimal) {
let key = key.as_ref();
let data = value.to_string();

unsafe {
externs::state::add_bigfloat(
ord,
Expand All @@ -145,18 +150,17 @@ pub fn add_bigfloat(ord: i64, key: String, value: &BigDecimal) {
)
}
}
pub fn set_min_int64(ord: i64, key: String, value: i64) {
unsafe {
externs::state::set_min_int64(
ord,
key.as_ptr(),
key.len() as u32,
value,
)
}

pub fn set_min_int64<K: AsRef<str>>(ord: i64, key: K, value: i64) {
let key = key.as_ref();

unsafe { externs::state::set_min_int64(ord, key.as_ptr(), key.len() as u32, value) }
}
pub fn set_min_bigint(ord: i64, key: String, value: &BigInt) {

pub fn set_min_bigint<K: AsRef<str>>(ord: i64, key: K, value: &BigInt) {
let key = key.as_ref();
let data = value.to_string();

unsafe {
externs::state::set_min_bigint(
ord,
Expand All @@ -167,18 +171,17 @@ pub fn set_min_bigint(ord: i64, key: String, value: &BigInt) {
)
}
}
pub fn set_min_float64(ord: i64, key: String, value: f64) {
unsafe {
externs::state::set_min_float64(
ord,
key.as_ptr(),
key.len() as u32,
value,
)
}

pub fn set_min_float64<K: AsRef<str>>(ord: i64, key: K, value: f64) {
let key = key.as_ref();

unsafe { externs::state::set_min_float64(ord, key.as_ptr(), key.len() as u32, value) }
}
pub fn set_min_bigfloat(ord: i64, key: String, value: &BigDecimal) {

pub fn set_min_bigfloat<K: AsRef<str>>(ord: i64, key: K, value: &BigDecimal) {
let key = key.as_ref();
let data = value.to_string();

unsafe {
externs::state::set_min_bigfloat(
ord,
Expand All @@ -189,18 +192,17 @@ pub fn set_min_bigfloat(ord: i64, key: String, value: &BigDecimal) {
)
}
}
pub fn set_max_int64(ord: i64, key: String, value: i64) {
unsafe {
externs::state::set_max_int64(
ord,
key.as_ptr(),
key.len() as u32,
value,
)
}

pub fn set_max_int64<K: AsRef<str>>(ord: i64, key: K, value: i64) {
let key = key.as_ref();

unsafe { externs::state::set_max_int64(ord, key.as_ptr(), key.len() as u32, value) }
}
pub fn set_max_bigint(ord: i64, key: String, value: &BigInt) {

pub fn set_max_bigint<K: AsRef<str>>(ord: i64, key: K, value: &BigInt) {
let key = key.as_ref();
let data = value.to_string();

unsafe {
externs::state::set_max_bigint(
ord,
Expand All @@ -211,18 +213,17 @@ pub fn set_max_bigint(ord: i64, key: String, value: &BigInt) {
)
}
}
pub fn set_max_float64(ord: i64, key: String, value: f64) {
unsafe {
externs::state::set_max_float64(
ord,
key.as_ptr(),
key.len() as u32,
value,
)
}

pub fn set_max_float64<K: AsRef<str>>(ord: i64, key: K, value: f64) {
let key = key.as_ref();

unsafe { externs::state::set_max_float64(ord, key.as_ptr(), key.len() as u32, value) }
}
pub fn set_max_bigfloat(ord: i64, key: String, value: &BigDecimal) {

pub fn set_max_bigfloat<K: AsRef<str>>(ord: i64, key: K, value: &BigDecimal) {
let key = key.as_ref();
let data = value.to_string();

unsafe {
externs::state::set_max_bigfloat(
ord,
Expand Down
Loading

0 comments on commit 4337ab3

Please sign in to comment.