From 10c6e3edc70ef694e4aa4de16027bd5b0a652362 Mon Sep 17 00:00:00 2001 From: Przemyslaw Hugh Kaznowski Date: Thu, 18 Apr 2024 09:45:06 +0100 Subject: [PATCH] Cleanup some code (#3901) --- core/src/cf/writer.rs | 91 +++++++++++++++++++++-------------------- core/src/dbs/options.rs | 2 - src/telemetry/mod.rs | 2 +- 3 files changed, 47 insertions(+), 48 deletions(-) diff --git a/core/src/cf/writer.rs b/core/src/cf/writer.rs index 19f71b9f..c850705a 100644 --- a/core/src/cf/writer.rs +++ b/core/src/cf/writer.rs @@ -152,18 +152,19 @@ mod tests { const DONT_STORE_PREVIOUS: bool = false; + const NS: &str = "myns"; + const DB: &str = "mydb"; + const TB: &str = "mytb"; + #[tokio::test] async fn test_changefeed_read_write() { let ts = crate::sql::Datetime::default(); - let ns = "myns"; - let db = "mydb"; - let tb = "mytb"; let dns = DefineNamespaceStatement { - name: crate::sql::Ident(ns.to_string()), + name: crate::sql::Ident(NS.to_string()), ..Default::default() }; let ddb = DefineDatabaseStatement { - name: crate::sql::Ident(db.to_string()), + name: crate::sql::Ident(DB.to_string()), changefeed: Some(ChangeFeed { expiry: Duration::from_secs(10), store_original: false, @@ -171,7 +172,7 @@ mod tests { ..Default::default() }; let dtb = DefineTableStatement { - name: tb.into(), + name: TB.into(), changefeed: Some(ChangeFeed { expiry: Duration::from_secs(10), store_original: false, @@ -187,11 +188,11 @@ mod tests { // let mut tx0 = ds.transaction(Write, Optimistic).await.unwrap(); - let ns_root = crate::key::root::ns::new(ns); + let ns_root = crate::key::root::ns::new(NS); tx0.put(ns_root.key_category(), &ns_root, dns).await.unwrap(); - let db_root = crate::key::namespace::db::new(ns, db); + let db_root = crate::key::namespace::db::new(NS, DB); tx0.put(db_root.key_category(), &db_root, ddb).await.unwrap(); - let tb_root = crate::key::database::tb::new(ns, db, tb); + let tb_root = crate::key::database::tb::new(NS, DB, TB); tx0.put(tb_root.key_category(), &tb_root, dtb.clone()).await.unwrap(); tx0.commit().await.unwrap(); @@ -205,15 +206,15 @@ mod tests { let mut tx1 = ds.transaction(Write, Optimistic).await.unwrap(); let thing_a = Thing { - tb: tb.to_owned(), + tb: TB.to_owned(), id: Id::String("A".to_string()), }; let value_a: super::Value = "a".into(); let previous = Cow::from(Value::None); tx1.record_change( - ns, - db, - tb, + NS, + DB, + TB, &thing_a, previous.clone(), Cow::Borrowed(&value_a), @@ -224,14 +225,14 @@ mod tests { let mut tx2 = ds.transaction(Write, Optimistic).await.unwrap(); let thing_c = Thing { - tb: tb.to_owned(), + tb: TB.to_owned(), id: Id::String("C".to_string()), }; let value_c: Value = "c".into(); tx2.record_change( - ns, - db, - tb, + NS, + DB, + TB, &thing_c, previous.clone(), Cow::Borrowed(&value_c), @@ -243,28 +244,28 @@ mod tests { let x = ds.transaction(Write, Optimistic).await; let mut tx3 = x.unwrap(); let thing_b = Thing { - tb: tb.to_owned(), + tb: TB.to_owned(), id: Id::String("B".to_string()), }; let value_b: Value = "b".into(); tx3.record_change( - ns, - db, - tb, + NS, + DB, + TB, &thing_b, previous.clone(), Cow::Borrowed(&value_b), DONT_STORE_PREVIOUS, ); let thing_c2 = Thing { - tb: tb.to_owned(), + tb: TB.to_owned(), id: Id::String("C".to_string()), }; let value_c2: Value = "c2".into(); tx3.record_change( - ns, - db, - tb, + NS, + DB, + TB, &thing_c2, previous.clone(), Cow::Borrowed(&value_c2), @@ -281,7 +282,7 @@ mod tests { let mut tx4 = ds.transaction(Write, Optimistic).await.unwrap(); let r = - crate::cf::read(&mut tx4, ns, db, Some(tb), ShowSince::Versionstamp(start), Some(10)) + crate::cf::read(&mut tx4, NS, DB, Some(TB), ShowSince::Versionstamp(start), Some(10)) .await .unwrap(); tx4.commit().await.unwrap(); @@ -290,15 +291,15 @@ mod tests { ChangeSet( vs::u64_to_versionstamp(2), DatabaseMutation(vec![TableMutations( - "mytb".to_string(), + TB.to_string(), match FFLAGS.change_feed_live_queries.enabled() { true => vec![TableMutation::SetWithDiff( - Thing::from(("mytb".to_string(), "A".to_string())), + Thing::from((TB.to_string(), "A".to_string())), Value::None, vec![], )], false => vec![TableMutation::Set( - Thing::from(("mytb".to_string(), "A".to_string())), + Thing::from((TB.to_string(), "A".to_string())), Value::from("a"), )], }, @@ -307,15 +308,15 @@ mod tests { ChangeSet( vs::u64_to_versionstamp(3), DatabaseMutation(vec![TableMutations( - "mytb".to_string(), + TB.to_string(), match FFLAGS.change_feed_live_queries.enabled() { true => vec![TableMutation::SetWithDiff( - Thing::from(("mytb".to_string(), "C".to_string())), + Thing::from((TB.to_string(), "C".to_string())), Value::None, vec![], )], false => vec![TableMutation::Set( - Thing::from(("mytb".to_string(), "C".to_string())), + Thing::from((TB.to_string(), "C".to_string())), Value::from("c"), )], }, @@ -324,27 +325,27 @@ mod tests { ChangeSet( vs::u64_to_versionstamp(4), DatabaseMutation(vec![TableMutations( - "mytb".to_string(), + TB.to_string(), match FFLAGS.change_feed_live_queries.enabled() { true => vec![ TableMutation::SetWithDiff( - Thing::from(("mytb".to_string(), "B".to_string())), + Thing::from((TB.to_string(), "B".to_string())), Value::None, vec![], ), TableMutation::SetWithDiff( - Thing::from(("mytb".to_string(), "C".to_string())), + Thing::from((TB.to_string(), "C".to_string())), Value::None, vec![], ), ], false => vec![ TableMutation::Set( - Thing::from(("mytb".to_string(), "B".to_string())), + Thing::from((TB.to_string(), "B".to_string())), Value::from("b"), ), TableMutation::Set( - Thing::from(("mytb".to_string(), "C".to_string())), + Thing::from((TB.to_string(), "C".to_string())), Value::from("c2"), ), ], @@ -357,14 +358,14 @@ mod tests { let mut tx5 = ds.transaction(Write, Optimistic).await.unwrap(); // gc_all needs to be committed before we can read the changes - crate::cf::gc_db(&mut tx5, ns, db, vs::u64_to_versionstamp(4), Some(10)).await.unwrap(); + crate::cf::gc_db(&mut tx5, NS, DB, vs::u64_to_versionstamp(4), Some(10)).await.unwrap(); // We now commit tx5, which should persist the gc_all resullts tx5.commit().await.unwrap(); // Now we should see the gc_all results let mut tx6 = ds.transaction(Write, Optimistic).await.unwrap(); let r = - crate::cf::read(&mut tx6, ns, db, Some(tb), ShowSince::Versionstamp(start), Some(10)) + crate::cf::read(&mut tx6, NS, DB, Some(TB), ShowSince::Versionstamp(start), Some(10)) .await .unwrap(); tx6.commit().await.unwrap(); @@ -372,27 +373,27 @@ mod tests { let want: Vec = vec![ChangeSet( vs::u64_to_versionstamp(4), DatabaseMutation(vec![TableMutations( - "mytb".to_string(), + TB.to_string(), match FFLAGS.change_feed_live_queries.enabled() { true => vec![ TableMutation::SetWithDiff( - Thing::from(("mytb".to_string(), "B".to_string())), + Thing::from((TB.to_string(), "B".to_string())), Value::None, vec![], ), TableMutation::SetWithDiff( - Thing::from(("mytb".to_string(), "C".to_string())), + Thing::from((TB.to_string(), "C".to_string())), Value::None, vec![], ), ], false => vec![ TableMutation::Set( - Thing::from(("mytb".to_string(), "B".to_string())), + Thing::from((TB.to_string(), "B".to_string())), Value::from("b"), ), TableMutation::Set( - Thing::from(("mytb".to_string(), "C".to_string())), + Thing::from((TB.to_string(), "C".to_string())), Value::from("c2"), ), ], @@ -405,7 +406,7 @@ mod tests { ds.tick_at((ts.0.timestamp() + 5).try_into().unwrap()).await.unwrap(); let mut tx7 = ds.transaction(Write, Optimistic).await.unwrap(); - let r = crate::cf::read(&mut tx7, ns, db, Some(tb), ShowSince::Timestamp(ts), Some(10)) + let r = crate::cf::read(&mut tx7, NS, DB, Some(TB), ShowSince::Timestamp(ts), Some(10)) .await .unwrap(); tx7.commit().await.unwrap(); diff --git a/core/src/dbs/options.rs b/core/src/dbs/options.rs index 91cc9fab..323a120a 100644 --- a/core/src/dbs/options.rs +++ b/core/src/dbs/options.rs @@ -355,13 +355,11 @@ impl Options { /// Get currently selected NS pub fn ns(&self) -> &str { self.ns.as_ref().map(AsRef::as_ref).unwrap() - // self.ns.as_ref().map(AsRef::as_ref).ok_or(Error::Unreachable) } /// Get currently selected DB pub fn db(&self) -> &str { self.db.as_ref().map(AsRef::as_ref).unwrap() - // self.db.as_ref().map(AsRef::as_ref).ok_or(Error::Unreachable) } /// Check whether this request supports realtime queries diff --git a/src/telemetry/mod.rs b/src/telemetry/mod.rs index 34c7b831..7e55c39a 100644 --- a/src/telemetry/mod.rs +++ b/src/telemetry/mod.rs @@ -108,7 +108,7 @@ pub fn filter_from_value(v: &str) -> Result Ok(EnvFilter::default().add_directive(Level::ERROR.into())), // Specify the log level for each code area "warn" | "info" | "debug" | "trace" => EnvFilter::builder() - .parse(format!("error,surreal={v},surrealdb={v},surrealdb::kvs::tx=error")), + .parse(format!("error,surreal={v},surrealdb={v},surrealdb::core::kvs::tx=error")), // Let's try to parse the custom log level _ => EnvFilter::builder().parse(v), }