forked from 0LNetworkCommunity/libra-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e6b832
commit 4c29675
Showing
10 changed files
with
208 additions
and
29 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
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,96 @@ | ||
use anyhow::{bail, Context, Result}; | ||
use neo4rs::Graph; | ||
|
||
pub async fn update_task( | ||
pool: &Graph, | ||
archive_id: &str, | ||
completed: bool, | ||
batch: usize, | ||
) -> Result<String> { | ||
let cypher_string = format!( | ||
r#"MERGE (a:Queue {{ archive_id: "{}", batch: {} }}) | ||
SET a.completed = {} | ||
RETURN a.archive_id AS archive_id"#, | ||
archive_id, | ||
batch, | ||
completed.to_string().to_lowercase(), | ||
); | ||
|
||
let cypher_query = neo4rs::query(&cypher_string); | ||
|
||
let mut res = pool | ||
.execute(cypher_query) | ||
.await | ||
.context("execute query error")?; | ||
|
||
let row = res.next().await?.context("no row returned")?; | ||
let task_id: String = row.get("archive_id").context("no created_accounts field")?; | ||
Ok(task_id) | ||
} | ||
|
||
pub async fn get_queued(pool: &Graph) -> Result<Vec<String>> { | ||
let cypher_string = r#" | ||
MATCH (a:Queue) | ||
WHERE a.completed = false | ||
RETURN DISTINCT a.archive_id | ||
"#; | ||
|
||
let cypher_query = neo4rs::query(cypher_string); | ||
|
||
let mut res = pool | ||
.execute(cypher_query) | ||
.await | ||
.context("execute query error")?; | ||
|
||
let mut archive_ids: Vec<String> = vec![]; | ||
|
||
while let Some(row) = res.next().await? { | ||
// Extract `archive_id` as a String | ||
if let Ok(archive_name) = row.get::<String>("a.archive_id") { | ||
archive_ids.push(archive_name); | ||
} | ||
} | ||
|
||
Ok(archive_ids) | ||
} | ||
|
||
// Three options: Not found in DB, found and complete, found and incomplete | ||
pub async fn is_complete(pool: &Graph, archive_id: &str, batch: usize) -> Result<Option<bool>> { | ||
let cypher_string = format!( | ||
r#" | ||
MATCH (a:Queue {{ archive_id: "{}", batch: {} }}) | ||
RETURN DISTINCT a.completed | ||
"#, | ||
archive_id, batch | ||
); | ||
|
||
let cypher_query = neo4rs::query(&cypher_string); | ||
|
||
let mut res = pool | ||
.execute(cypher_query) | ||
.await | ||
.context("execute query error")?; | ||
|
||
if let Some(row) = res.next().await? { | ||
// Extract `archive_id` as a String | ||
Ok(row.get::<bool>("a.completed").ok()) | ||
} else { | ||
bail!("not found") | ||
} | ||
} | ||
|
||
// clear queue | ||
pub async fn clear(pool: &Graph) -> Result<()> { | ||
let cypher_string = r#" | ||
MATCH (a:Queue) | ||
DELETE a | ||
"#.to_string(); | ||
|
||
let cypher_query = neo4rs::query(&cypher_string); | ||
|
||
let mut _res = pool | ||
.execute(cypher_query) | ||
.await | ||
.context("execute query error")?; | ||
Ok(()) | ||
} |
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
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,44 @@ | ||
mod support; | ||
|
||
use anyhow::Result; | ||
use libra_warehouse::{ | ||
neo4j_init::{get_neo4j_localhost_pool, maybe_create_indexes}, | ||
queue, | ||
scan::scan_dir_archive, | ||
}; | ||
|
||
use support::{fixtures, neo4j_testcontainer::start_neo4j_container}; | ||
|
||
#[tokio::test] | ||
async fn test_queue() -> Result<()> { | ||
let c = start_neo4j_container(); | ||
let port = c.get_host_port_ipv4(7687); | ||
let pool = get_neo4j_localhost_pool(port) | ||
.await | ||
.expect("could not get neo4j connection pool"); | ||
maybe_create_indexes(&pool).await?; | ||
|
||
let start_here = fixtures::v7_tx_manifest_fixtures_path(); | ||
|
||
let s = scan_dir_archive(&start_here, None)?; | ||
let (_, man_info) = s.0.first_key_value().unwrap(); | ||
let batch = 0usize; | ||
|
||
let id = queue::update_task(&pool, &man_info.archive_id, false, batch).await?; | ||
assert!(id == man_info.archive_id); | ||
|
||
let list = queue::get_queued(&pool).await?; | ||
|
||
assert!(*"transaction_38100001-.541f" == list[0]); | ||
|
||
let c = queue::is_complete(&pool, "transaction_38100001-.541f", batch).await; | ||
assert!(!c?.unwrap()); | ||
|
||
// Now we update the task, with ID and batch | ||
let _id = queue::update_task(&pool, &man_info.archive_id, true, batch).await?; | ||
|
||
let c = queue::is_complete(&pool, "transaction_38100001-.541f", batch).await; | ||
assert!(c?.unwrap()); | ||
|
||
Ok(()) | ||
} |
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