Skip to content

Commit

Permalink
WIP : optimize search assets query
Browse files Browse the repository at this point in the history
  • Loading branch information
Nagaprasadvr committed Dec 10, 2024
1 parent a9e5340 commit 5ff6ccc
Show file tree
Hide file tree
Showing 10 changed files with 241 additions and 29 deletions.
18 changes: 9 additions & 9 deletions das_api/src/api/api_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,9 @@ impl DasApi {
return Err(DasApiError::PaginationError);
}
if let Some(sort) = &sorting {
if sort.sort_by != AssetSortBy::Id {
return Err(DasApiError::PaginationSortingValidationError);
}
// if sort.sort_by != AssetSortBy::Id {
// return Err(DasApiError::PaginationSortingValidationError);
// }
}
validate_pubkey(before.clone())?;
is_cursor_enabled = false;
Expand All @@ -115,9 +115,9 @@ impl DasApi {
return Err(DasApiError::PaginationError);
}
if let Some(sort) = &sorting {
if sort.sort_by != AssetSortBy::Id {
return Err(DasApiError::PaginationSortingValidationError);
}
// if sort.sort_by != AssetSortBy::Id {
// return Err(DasApiError::PaginationSortingValidationError);
// }
}
validate_pubkey(after.clone())?;
is_cursor_enabled = false;
Expand All @@ -126,9 +126,9 @@ impl DasApi {
page_opt.limit = limit.map(|x| x as u64).unwrap_or(1000);
if is_cursor_enabled {
if let Some(sort) = &sorting {
if sort.sort_by != AssetSortBy::Id {
return Err(DasApiError::PaginationSortingValidationError);
}
// if sort.sort_by != AssetSortBy::Id {
// return Err(DasApiError::PaginationSortingValidationError);
// }
page_opt.cursor = Some(self.get_cursor(cursor)?);
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions digital_asset_types/src/dao/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,11 @@ impl SearchAssetsQuery {
// In theory, the owner_type=single check should be sufficient,
// however there is an old bug that has marked some non-NFTs as "single" with supply > 1.
// The supply check guarentees we do not include those.
conditions = conditions.add_option(Some(
conditions = conditions.add(
asset::Column::OwnerType
.eq(OwnerType::Single)
.and(asset::Column::Supply.lte(1)),
));
);
}

if let Some(c) = self.creator_address.to_owned() {
Expand Down
22 changes: 10 additions & 12 deletions digital_asset_types/src/dao/scopes/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,8 @@ where
.join(JoinType::LeftJoin, relation.def());

if let Some(col) = sort_by {
stmt = stmt
.order_by(col, sort_direction.clone())
.order_by(asset::Column::Id, sort_direction.clone());
stmt = stmt.order_by(col, sort_direction.clone())
// .order_by(asset::Column::Id, sort_direction.clone());
}

let assets = paginate(pagination, limit, stmt, sort_direction, asset::Column::Id)
Expand Down Expand Up @@ -288,7 +287,7 @@ pub async fn get_related_for_assets(
// Get all creators for all assets in `assets_map``.
let creators = asset_creators::Entity::find()
.filter(asset_creators::Column::AssetId.is_in(ids.clone()))
.order_by_asc(asset_creators::Column::AssetId)
// .order_by_asc(asset_creators::Column::AssetId)
.order_by_asc(asset_creators::Column::Position)
.all(conn)
.await?;
Expand Down Expand Up @@ -316,7 +315,7 @@ pub async fn get_related_for_assets(
let ids = assets_map.keys().cloned().collect::<Vec<_>>();
let authorities = asset_authority::Entity::find()
.filter(asset_authority::Column::AssetId.is_in(ids.clone()))
.order_by_asc(asset_authority::Column::AssetId)
// .order_by_asc(asset_authority::Column::AssetId)
.all(conn)
.await?;
for a in authorities.into_iter() {
Expand All @@ -339,7 +338,7 @@ pub async fn get_related_for_assets(
.filter(asset_grouping::Column::AssetId.is_in(ids.clone()))
.filter(asset_grouping::Column::GroupValue.is_not_null())
.filter(cond)
.order_by_asc(asset_grouping::Column::AssetId)
// .order_by_asc(asset_grouping::Column::AssetId)
.all(conn)
.await?;
for g in grouping.into_iter() {
Expand Down Expand Up @@ -368,9 +367,8 @@ pub async fn get_assets_by_condition(
}
stmt = stmt.filter(condition);
if let Some(col) = sort_by {
stmt = stmt
.order_by(col, sort_direction.clone())
.order_by(asset::Column::Id, sort_direction.clone());
stmt = stmt.order_by(col, sort_direction.clone())
// .order_by(asset::Column::Id, sort_direction.clone());
}

let assets = paginate(pagination, limit, stmt, sort_direction, asset::Column::Id)
Expand Down Expand Up @@ -400,7 +398,7 @@ pub async fn get_by_id(
let (asset, data) = asset_data;
let authorities: Vec<asset_authority::Model> = asset_authority::Entity::find()
.filter(asset_authority::Column::AssetId.eq(asset.id.clone()))
.order_by_asc(asset_authority::Column::AssetId)
// .order_by_asc(asset_authority::Column::AssetId)
.all(conn)
.await?;
let mut creators: Vec<asset_creators::Model> = asset_creators::Entity::find()
Expand All @@ -421,7 +419,7 @@ pub async fn get_by_id(
// Therefore if verified is null, we can assume that the group is verified.
.add(asset_grouping::Column::Verified.is_null()),
)
.order_by_asc(asset_grouping::Column::AssetId)
// .order_by_asc(asset_grouping::Column::AssetId)
.all(conn)
.await?;
Ok(FullAsset {
Expand Down Expand Up @@ -500,7 +498,7 @@ pub async fn get_asset_signatures(
let stmt = asset::Entity::find()
.distinct_on([(asset::Entity, asset::Column::Id)])
.filter(asset::Column::Id.eq(asset_id))
.order_by(asset::Column::Id, Order::Desc)
// .order_by(asset::Column::Id, Order::Desc)
.limit(1);
let asset = stmt.one(conn).await?;
if let Some(asset) = asset {
Expand Down
2 changes: 1 addition & 1 deletion digital_asset_types/src/dapi/common/asset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn build_transaction_signatures_response(

pub fn create_sorting(sorting: AssetSorting) -> (sea_orm::query::Order, Option<asset::Column>) {
let sort_column = match sorting.sort_by {
AssetSortBy::Id => Some(asset::Column::Id),
// AssetSortBy::Id => Some(asset::Column::Id),
AssetSortBy::Created => Some(asset::Column::CreatedAt),
AssetSortBy::Updated => Some(asset::Column::SlotUpdated),
AssetSortBy::RecentAction => Some(asset::Column::SlotUpdated),
Expand Down
6 changes: 3 additions & 3 deletions digital_asset_types/src/rpc/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ pub struct AssetSorting {
impl Default for AssetSorting {
fn default() -> AssetSorting {
AssetSorting {
sort_by: AssetSortBy::Id,
sort_by: AssetSortBy::None,
sort_direction: Some(AssetSortDirection::default()),
}
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)]
pub enum AssetSortBy {
#[serde(rename = "id")]
Id,
// #[serde(rename = "id")]
// Id,
#[serde(rename = "created")]
Created,
#[serde(rename = "updated")]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
source: integration_tests/tests/integration_tests/mpl_core_tests.rs
assertion_line: 108
expression: response
---
{
"total": 2,
"limit": 50,
"page": 1,
"items": [
{
"interface": "MplCoreCollection",
"id": "9CSyGBw1DCVZfx621nb7UBM9SpVDsX1m9MaN6APCf1Ci",
"content": {
"$schema": "https://schema.metaplex.com/nft1.0.json",
"json_uri": "https://example.com/collection",
"files": [],
"metadata": {
"name": "Test Collection",
"symbol": ""
},
"links": {}
},
"authorities": [
{
"address": "APrZTeVysBJqAznfLXS71NAzjr2fCVTSF1A66MeErzM7",
"scopes": [
"full"
]
}
],
"compression": {
"eligible": false,
"compressed": false,
"data_hash": "",
"creator_hash": "",
"asset_hash": "",
"tree": "",
"seq": 0,
"leaf_id": 0
},
"grouping": [],
"royalty": {
"royalty_model": "creators",
"target": null,
"percent": 0.0,
"basis_points": 0,
"primary_sale_happened": false,
"locked": false
},
"creators": [],
"ownership": {
"frozen": false,
"delegated": false,
"delegate": null,
"ownership_model": "single",
"owner": "APrZTeVysBJqAznfLXS71NAzjr2fCVTSF1A66MeErzM7"
},
"supply": null,
"mutable": true,
"burnt": false,
"plugins": {},
"mpl_core_info": {
"num_minted": 1,
"current_size": 1,
"plugins_json_version": 1
},
"external_plugins": []
},
{
"interface": "MplCoreAsset",
"id": "4FcFVJVPRsYoMjt8ewDGV5nipoK63SNrJzjrBHyXvhcz",
"content": {
"$schema": "https://schema.metaplex.com/nft1.0.json",
"json_uri": "https://example.com/asset",
"files": [],
"metadata": {
"name": "Test Asset",
"symbol": ""
},
"links": {}
},
"authorities": [
{
"address": "APrZTeVysBJqAznfLXS71NAzjr2fCVTSF1A66MeErzM7",
"scopes": [
"full"
]
}
],
"compression": {
"eligible": false,
"compressed": false,
"data_hash": "",
"creator_hash": "",
"asset_hash": "",
"tree": "",
"seq": 0,
"leaf_id": 0
},
"grouping": [
{
"group_key": "collection",
"group_value": "9CSyGBw1DCVZfx621nb7UBM9SpVDsX1m9MaN6APCf1Ci"
}
],
"royalty": {
"royalty_model": "creators",
"target": null,
"percent": 0.0,
"basis_points": 0,
"primary_sale_happened": false,
"locked": false
},
"creators": [],
"ownership": {
"frozen": false,
"delegated": false,
"delegate": null,
"ownership_model": "single",
"owner": "GzYvuu9aUYXmnardj4svbAcCNmefiaGu2E3knGw9NJQQ"
},
"supply": null,
"mutable": true,
"burnt": false,
"plugins": {},
"mpl_core_info": {
"plugins_json_version": 1
},
"external_plugins": []
}
]
}
4 changes: 4 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ mod m20240319_120101_add_mpl_core_enum_vals;
mod m20240320_120101_add_mpl_core_info_items;
mod m20240520_120101_add_mpl_core_external_plugins_columns;
mod m20240718_161232_change_supply_columns_to_numeric;
mod m20241209_100813_add_unique_index_for_asset_owner_and_supply;
mod m20241209_111604_add_index_for_asset_id_group_value_verified;

pub mod model;

Expand Down Expand Up @@ -95,6 +97,8 @@ impl MigratorTrait for Migrator {
Box::new(m20240320_120101_add_mpl_core_info_items::Migration),
Box::new(m20240520_120101_add_mpl_core_external_plugins_columns::Migration),
Box::new(m20240718_161232_change_supply_columns_to_numeric::Migration),
Box::new(m20241209_100813_add_unique_index_for_asset_owner_and_supply::Migration),
Box::new(m20241209_111604_add_index_for_asset_id_group_value_verified::Migration),
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use sea_orm_migration::prelude::*;

use crate::model::table::Asset;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_index(
Index::create()
.name("idx_asset_owner_supply")
.col(Asset::Owner)
.col(Asset::Supply)
.col(Asset::Burnt)
.col(Asset::OwnerType)
.table(Asset::Table)
.to_owned(),
)
.await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_index(
Index::drop()
.name("idx_asset_owner_supply")
.table(Asset::Table)
.to_owned(),
)
.await?;

Ok(())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use sea_orm_migration::prelude::*;

use crate::model::table::AssetGrouping;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_index(
Index::create()
.unique()
.name("asset_grouping_id_value_verified_unique")
.col(AssetGrouping::AssetId)
.col(AssetGrouping::GroupValue)
.col(AssetGrouping::Verified)
.table(AssetGrouping::Table)
.to_owned(),
)
.await?;
Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_index(
sea_query::Index::drop()
.name("asset_grouping_id_value_verified_unique")
.table(AssetGrouping::Table)
.to_owned(),
)
.await?;

Ok(())
}
}
Loading

0 comments on commit 5ff6ccc

Please sign in to comment.