Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow descending order of chain query filter #1539

Merged
merged 6 commits into from Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 9 additions & 7 deletions crates/holochain_state/src/source_chain.rs
Expand Up @@ -764,14 +764,15 @@ where
});
sql.push_str(
"
)
AND
(:entry_type IS NULL OR Action.entry_type = :entry_type)
AND
(:action_type IS NULL OR Action.type = :action_type)
ORDER BY Action.seq ASC
",
)
AND
(:entry_type IS NULL OR Action.entry_type = :entry_type)
AND
(:action_type IS NULL OR Action.type = :action_type)
ORDER BY Action.seq
",
);
sql.push_str(if query.order_descending {" DESC"} else {" ASC"});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 - this manual string manipulation frustrates me every time I see it. But your fix is good within the context of this strategy already being used : )

let mut stmt = txn.prepare(&sql)?;
let records = stmt
.query_and_then(
Expand Down Expand Up @@ -2000,6 +2001,7 @@ pub mod tests {
entry_type: entry_type.clone(),
entry_hashes: entry_hashes.clone(),
include_entries,
order_descending: false,
};
if sequence_range != ChainQueryFilterRange::Unbounded
&& (action_type.is_some()
Expand Down
2 changes: 2 additions & 0 deletions crates/holochain_zome_types/CHANGELOG.md
Expand Up @@ -4,6 +4,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased](https://github.com/holochain/holochain/holochain_zome_types-v0.0.2-alpha.1...HEAD)

`ChainQueryFilter` has a `descending()` function which will cause the query results to be returned in descending order. This can be reversed by calling `ascending()`. The default order is still ascending. [\#1539](https://github.com/holochain/holochain/pull/1539)

## 0.0.44

## 0.0.43
Expand Down
21 changes: 21 additions & 0 deletions crates/holochain_zome_types/src/query.rs
Expand Up @@ -78,6 +78,8 @@ pub struct ChainQueryFilter {
pub action_type: Option<ActionType>,
/// Include the entries in the records
pub include_entries: bool,
/// The query should be ordered in descending order (default is ascending)
pub order_descending: bool,
}

#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, SerializedBytes)]
Expand Down Expand Up @@ -207,6 +209,18 @@ impl ChainQueryFilter {
self
}

/// Set the order to ascending.
pub fn ascending(mut self) -> Self {
self.order_descending = false;
self
}

/// Set the order to ascending.
pub fn descending(mut self) -> Self {
self.order_descending = true;
self
}

/// If the sequence range supports fork disambiguation, apply it to remove
/// actions that are not in the correct branch.
/// Numerical range bounds do NOT support fork disambiguation, and neither
Expand Down Expand Up @@ -401,6 +415,13 @@ mod tests {
map_query(&query_2, &actions),
[false, true, false, true, false, false, false].to_vec()
);
assert_eq!(
map_query(&query_2.descending(), &actions),
[false, true, false, true, false, false, false]
.into_iter()
.rev()
.collect::<Vec<_>>()
);
}

#[test]
Expand Down
8 changes: 4 additions & 4 deletions crates/test_utils/wasm/wasm_workspace/Cargo.lock

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