-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(store): Improve database performance dynamic queries (#381)
* feat(macros): add to_sql_select for subjects * refactor(store): Improve database performance on queries * fix(store): pagination logic
- Loading branch information
1 parent
3a81ed6
commit 7f32333
Showing
38 changed files
with
698 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod db_item; | ||
mod packets; | ||
mod record_impl; | ||
mod store_item; | ||
pub mod subjects; | ||
pub mod types; | ||
|
||
pub use db_item::*; | ||
pub use store_item::*; | ||
pub use subjects::*; | ||
pub use types::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use std::cmp::Ordering; | ||
|
||
use fuel_streams_store::{ | ||
db::{DbError, StoreItem}, | ||
record::{DataEncoder, RecordEntity}, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive( | ||
Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow, | ||
)] | ||
pub struct BlockStoreItem { | ||
pub subject: String, | ||
pub value: Vec<u8>, | ||
pub block_height: i64, // This is our order prop | ||
} | ||
|
||
impl DataEncoder for BlockStoreItem { | ||
type Err = DbError; | ||
} | ||
|
||
impl StoreItem for BlockStoreItem { | ||
fn entity(&self) -> &RecordEntity { | ||
&RecordEntity::Block | ||
} | ||
|
||
fn encoded_value(&self) -> &[u8] { | ||
&self.value | ||
} | ||
|
||
fn subject_str(&self) -> String { | ||
self.subject.clone() | ||
} | ||
} | ||
|
||
impl PartialOrd for BlockStoreItem { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for BlockStoreItem { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
self.block_height.cmp(&other.block_height) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod db_item; | ||
mod packets; | ||
mod record_impl; | ||
mod store_item; | ||
pub mod subjects; | ||
pub mod types; | ||
|
||
pub use db_item::*; | ||
pub use store_item::*; | ||
pub use subjects::*; | ||
pub use types::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::cmp::Ordering; | ||
|
||
use fuel_streams_store::{ | ||
db::{DbError, StoreItem}, | ||
record::{DataEncoder, RecordEntity}, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive( | ||
Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow, | ||
)] | ||
pub struct InputStoreItem { | ||
pub subject: String, | ||
pub value: Vec<u8>, | ||
pub block_height: i64, | ||
pub tx_index: i64, | ||
pub input_index: i64, | ||
} | ||
|
||
impl DataEncoder for InputStoreItem { | ||
type Err = DbError; | ||
} | ||
|
||
impl StoreItem for InputStoreItem { | ||
fn entity(&self) -> &RecordEntity { | ||
&RecordEntity::Input | ||
} | ||
|
||
fn encoded_value(&self) -> &[u8] { | ||
&self.value | ||
} | ||
|
||
fn subject_str(&self) -> String { | ||
self.subject.clone() | ||
} | ||
} | ||
|
||
impl PartialOrd for InputStoreItem { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for InputStoreItem { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
// Order by block height first | ||
self.block_height | ||
.cmp(&other.block_height) | ||
// Then by transaction index within the block | ||
.then(self.tx_index.cmp(&other.tx_index)) | ||
// Finally by input index within the transaction | ||
.then(self.input_index.cmp(&other.input_index)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod db_item; | ||
mod packets; | ||
mod record_impl; | ||
mod store_item; | ||
pub mod subjects; | ||
pub mod types; | ||
|
||
pub use db_item::*; | ||
pub use store_item::*; | ||
pub use subjects::*; | ||
pub use types::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::cmp::Ordering; | ||
|
||
use fuel_streams_store::{ | ||
db::{DbError, StoreItem}, | ||
record::{DataEncoder, RecordEntity}, | ||
}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive( | ||
Debug, Clone, Serialize, Deserialize, PartialEq, Eq, sqlx::FromRow, | ||
)] | ||
pub struct OutputStoreItem { | ||
pub subject: String, | ||
pub value: Vec<u8>, | ||
pub block_height: i64, | ||
pub tx_index: i64, | ||
pub output_index: i64, | ||
} | ||
|
||
impl DataEncoder for OutputStoreItem { | ||
type Err = DbError; | ||
} | ||
|
||
impl StoreItem for OutputStoreItem { | ||
fn entity(&self) -> &RecordEntity { | ||
&RecordEntity::Output | ||
} | ||
|
||
fn encoded_value(&self) -> &[u8] { | ||
&self.value | ||
} | ||
|
||
fn subject_str(&self) -> String { | ||
self.subject.clone() | ||
} | ||
} | ||
|
||
impl PartialOrd for OutputStoreItem { | ||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { | ||
Some(self.cmp(other)) | ||
} | ||
} | ||
|
||
impl Ord for OutputStoreItem { | ||
fn cmp(&self, other: &Self) -> Ordering { | ||
// Order by block height first | ||
self.block_height | ||
.cmp(&other.block_height) | ||
// Then by transaction index within the block | ||
.then(self.tx_index.cmp(&other.tx_index)) | ||
// Finally by output index within the transaction | ||
.then(self.output_index.cmp(&other.output_index)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
mod db_item; | ||
mod packets; | ||
mod record_impl; | ||
mod store_item; | ||
pub mod subjects; | ||
pub mod types; | ||
|
||
pub use db_item::*; | ||
pub use store_item::*; | ||
pub use subjects::*; | ||
pub use types::*; |
Oops, something went wrong.