Skip to content

Commit

Permalink
chore: Check if message count is less than prune size before pruning (#…
Browse files Browse the repository at this point in the history
…1977)

## Motivation

Minor cleanups + check to see if we need to prune at all

## Merge Checklist

_Choose all relevant options below by adding an `x` now or at any time
before submitting for review_

- [X] PR title adheres to the [conventional
commits](https://www.conventionalcommits.org/en/v1.0.0/) standard
- [X] PR has a
[changeset](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#35-adding-changesets)
- [ ] PR has been tagged with a change label(s) (i.e. documentation,
feature, bugfix, or chore)
- [ ] PR includes
[documentation](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#32-writing-docs)
if necessary.
- [X] All [commits have been
signed](https://github.com/farcasterxyz/hub-monorepo/blob/main/CONTRIBUTING.md#22-signing-commits)

## Additional Context

If this is a relatively large or complex change, provide more details
here that will help reviewers


<!-- start pr-codex -->

---

## PR-Codex overview
This PR updates the `@farcaster/hubble` package and refactors the
`rustStoreBase.ts` and various files in the `trie` module for improved
functionality.

### Detailed summary
- Added a fix to check the need for pruning before pruning in
`rustStoreBase.ts`
- Fixed a typo in a message in `merkle_trie.rs`
- Removed unnecessary `Arc` import in `trie_node.rs`
- Refactored `TrieNode` methods to remove `Arc<RocksDB>` parameters
- Improved key grouping logic in `TrieNode`
- Updated method signatures and logic in `TrieNode` for better
functionality

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
adityapk00 authored May 2, 2024
1 parent 7b850fb commit 4286432
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-cycles-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@farcaster/hubble": patch
---

fix: Check if we need to prune before actually pruning
2 changes: 1 addition & 1 deletion apps/hubble/src/addon/src/trie/merkle_trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ impl MerkleTrie {

let child_node = self.get_node(&child_prefix).ok_or(HubError {
code: "bad_request.internal_error".to_string(),
message: "Child node not found".to_string(),
message: "Child Node not found".to_string(),
})?;

children.insert(
Expand Down
41 changes: 16 additions & 25 deletions apps/hubble/src/addon/src/trie/trie_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
store::{blake3_20, bytes_compare, HubError, RootPrefix},
};
use prost::Message as _;
use std::{collections::HashMap, sync::Arc};
use std::collections::HashMap;

use super::merkle_trie::TrieSnapshot;

Expand Down Expand Up @@ -124,7 +124,7 @@ impl TrieNode {

pub fn get_node_from_trie(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
prefix: &[u8],
current_index: usize,
) -> Option<&mut TrieNode> {
Expand Down Expand Up @@ -155,7 +155,7 @@ impl TrieNode {
*/
pub fn insert(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
txn: &mut RocksDbTransactionBatch,
mut keys: Vec<Vec<u8>>,
current_index: usize,
Expand Down Expand Up @@ -239,30 +239,21 @@ impl TrieNode {
let mut grouped_keys = HashMap::new();
for (i, key) in remaining_keys.into_iter() {
let char = key[current_index];
grouped_keys
.entry(char)
.or_insert_with(|| vec![])
.push((i, key));
let entry = grouped_keys.entry(char).or_insert_with(|| (vec![], vec![]));

entry.0.push(i);
entry.1.push(key);
}

let mut successes = 0;
for (char, child_keys) in grouped_keys.into_iter() {
for (char, (is, keys)) in grouped_keys.into_iter() {
if !self.children.contains_key(&char) {
self.children
.insert(char, TrieNodeType::Node(TrieNode::default()));
}

// Recurse into a non-leaf node and instruct it to insert the value.
let child = self.get_or_load_child(db, &prefix, char)?;

// Split the child_keys into the "i"s and the keys
let mut is = vec![];
let mut keys = vec![];
for (i, key) in child_keys.into_iter() {
is.push(i);
keys.push(key);
}

let child_results = child.insert(db, txn, keys, current_index + 1)?;

for (i, result) in is.into_iter().zip(child_results) {
Expand All @@ -285,7 +276,7 @@ impl TrieNode {

pub fn delete(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
txn: &mut RocksDbTransactionBatch,
keys: Vec<Vec<u8>>,
current_index: usize,
Expand Down Expand Up @@ -401,7 +392,7 @@ impl TrieNode {

pub fn exists(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
key: &[u8],
current_index: usize,
) -> Result<bool, HubError> {
Expand All @@ -428,7 +419,7 @@ impl TrieNode {
*/
pub fn split_leaf_node(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
txn: &mut RocksDbTransactionBatch,
current_index: usize,
) -> Result<(), HubError> {
Expand All @@ -452,7 +443,7 @@ impl TrieNode {

fn get_or_load_child(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
prefix: &[u8],
char: u8,
) -> Result<&mut TrieNode, HubError> {
Expand Down Expand Up @@ -485,7 +476,7 @@ impl TrieNode {
}
}

fn update_hash(&mut self, db: &Arc<RocksDB>, prefix: &[u8]) -> Result<(), HubError> {
fn update_hash(&mut self, db: &RocksDB, prefix: &[u8]) -> Result<(), HubError> {
if self.is_leaf() {
self.hash = blake3_20(&self.key.as_ref().unwrap_or(&vec![]));
} else {
Expand Down Expand Up @@ -528,7 +519,7 @@ impl TrieNode {

fn excluded_hash(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
prefix: &[u8],
prefix_char: u8,
) -> Result<(usize, String), HubError> {
Expand Down Expand Up @@ -577,7 +568,7 @@ impl TrieNode {

pub fn get_all_values(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
prefix: &[u8],
) -> Result<Vec<Vec<u8>>, HubError> {
if self.is_leaf() {
Expand Down Expand Up @@ -605,7 +596,7 @@ impl TrieNode {

pub fn get_snapshot(
&mut self,
db: &Arc<RocksDB>,
db: &RocksDB,
prefix: &[u8],
current_index: usize,
) -> Result<TrieSnapshot, HubError> {
Expand Down
2 changes: 1 addition & 1 deletion apps/hubble/src/storage/stores/rustStoreBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export abstract class RustStoreBase<TAdd extends Message, TRemove extends Messag
}

// Return immediately if there are no messages to prune
if (cachedCount.value === 0) {
if (cachedCount.value <= this._pruneSizeLimit * units.value) {
return ok([]);
}

Expand Down

0 comments on commit 4286432

Please sign in to comment.