Skip to content

Commit

Permalink
Use memchr::memmem::find instead of windows for support of empty data.
Browse files Browse the repository at this point in the history
  • Loading branch information
EthanYuan committed Jan 18, 2024
1 parent d0ef686 commit 09accaf
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 81 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions util/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ ckb-async-runtime = { path = "../runtime", version = "= 0.114.0-pre" }
rhai = { version = "1.10.0", features = ["no_function", "no_float", "no_module", "sync"]}
serde_json = "1.0"
numext-fixed-uint = "0.1"
memchr = "2.7"

[dev-dependencies]
tempfile.workspace = true
Expand Down
110 changes: 31 additions & 79 deletions util/indexer/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ckb_types::{
prelude::*,
H256,
};
use memchr::memmem;
use rocksdb::{prelude::*, Direction, IteratorMode};
use std::convert::TryInto;
use std::num::NonZeroUsize;
Expand Down Expand Up @@ -349,23 +350,6 @@ impl IndexerHandle {
return Err(Error::invalid_params("limit should be greater than 0"));
}

if search_key
.filter
.as_ref()
.and_then(|filter| {
if filter.output_data_filter_mode == Some(IndexerSearchMode::Partial) {
filter.output_data.as_ref()
} else {
None
}
})
.map_or(false, |data| data.is_empty())
{
return Err(Error::invalid_params(
"search_key.filter.output_data empty data is not allowed",
));
}

let (prefix, from_key, direction, skip) = build_query_options(
&search_key,
KeyPrefix::CellLockScript,
Expand Down Expand Up @@ -475,13 +459,7 @@ impl IndexerHandle {
}
}
IndexerSearchMode::Partial => {
if !output_data
.raw_data()
.windows(data.len())
.any(|window| window == data)
{
return None;
}
memmem::find(&output_data.raw_data(), data)?;
}
}
}
Expand Down Expand Up @@ -828,23 +806,6 @@ impl IndexerHandle {
));
}

if search_key
.filter
.as_ref()
.and_then(|filter| {
if filter.output_data_filter_mode == Some(IndexerSearchMode::Partial) {
filter.output_data.as_ref()
} else {
None
}
})
.map_or(false, |data| data.is_empty())
{
return Err(Error::invalid_params(
"search_key.filter.output_data empty data is not allowed",
));
}

let (prefix, from_key, direction, skip) = build_query_options(
&search_key,
KeyPrefix::CellLockScript,
Expand Down Expand Up @@ -952,13 +913,7 @@ impl IndexerHandle {
}
}
IndexerSearchMode::Partial => {
if !output_data
.raw_data()
.windows(data.len())
.any(|window| window == data)
{
return None;
}
memmem::find(&output_data.raw_data(), data)?;
}
}
}
Expand Down Expand Up @@ -2137,25 +2092,23 @@ mod tests {
assert_eq!(1, cells.objects.len(),);

// test get_cells rpc with output_data Partial search mode
let cells = rpc.get_cells(
IndexerSearchKey {
script: lock_script11.clone().into(),
filter: Some(IndexerSearchKeyFilter {
output_data: Some(JsonBytes::from_vec(vec![])),
output_data_filter_mode: Some(IndexerSearchMode::Partial),
let cells = rpc
.get_cells(
IndexerSearchKey {
script: lock_script11.clone().into(),
filter: Some(IndexerSearchKeyFilter {
output_data: Some(JsonBytes::from_vec(vec![])),
output_data_filter_mode: Some(IndexerSearchMode::Partial),
..Default::default()
}),
..Default::default()
}),
..Default::default()
},
IndexerOrder::Asc,
1000.into(),
None,
);
let error = cells.err().unwrap();
assert_eq!(
error.to_string(),
"Invalid params search_key.filter.output_data empty data is not allowed",
);
},
IndexerOrder::Asc,
1000.into(),
None,
)
.unwrap();
assert_eq!(1, cells.objects.len(),);

// test get_cells_capacity rpc with output_data Exact search mode
let mut data = [0u8; 7];
Expand Down Expand Up @@ -2224,19 +2177,18 @@ mod tests {
assert_eq!(200000000000, capacity);

// test get_cells_capacity rpc with output_data Partial search mode
let cells_capacity = rpc.get_cells_capacity(IndexerSearchKey {
script: lock_script11.clone().into(),
filter: Some(IndexerSearchKeyFilter {
output_data: Some(JsonBytes::from_vec(vec![])),
output_data_filter_mode: Some(IndexerSearchMode::Partial),
let cells_capacity = rpc
.get_cells_capacity(IndexerSearchKey {
script: lock_script11.clone().into(),
filter: Some(IndexerSearchKeyFilter {
output_data: Some(JsonBytes::from_vec(vec![])),
output_data_filter_mode: Some(IndexerSearchMode::Partial),
..Default::default()
}),
..Default::default()
}),
..Default::default()
});
let error = cells_capacity.err().unwrap();
assert_eq!(
error.to_string(),
"Invalid params search_key.filter.output_data empty data is not allowed",
);
})
.unwrap();
let capacity: u64 = cells_capacity.unwrap().capacity.into();
assert_eq!(200000000000, capacity);
}
}

0 comments on commit 09accaf

Please sign in to comment.