Cleanup some code (#3901)

This commit is contained in:
Przemyslaw Hugh Kaznowski 2024-04-18 09:45:06 +01:00 committed by GitHub
parent cb751465e4
commit 10c6e3edc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 47 additions and 48 deletions

View file

@ -152,18 +152,19 @@ mod tests {
const DONT_STORE_PREVIOUS: bool = false; const DONT_STORE_PREVIOUS: bool = false;
const NS: &str = "myns";
const DB: &str = "mydb";
const TB: &str = "mytb";
#[tokio::test] #[tokio::test]
async fn test_changefeed_read_write() { async fn test_changefeed_read_write() {
let ts = crate::sql::Datetime::default(); let ts = crate::sql::Datetime::default();
let ns = "myns";
let db = "mydb";
let tb = "mytb";
let dns = DefineNamespaceStatement { let dns = DefineNamespaceStatement {
name: crate::sql::Ident(ns.to_string()), name: crate::sql::Ident(NS.to_string()),
..Default::default() ..Default::default()
}; };
let ddb = DefineDatabaseStatement { let ddb = DefineDatabaseStatement {
name: crate::sql::Ident(db.to_string()), name: crate::sql::Ident(DB.to_string()),
changefeed: Some(ChangeFeed { changefeed: Some(ChangeFeed {
expiry: Duration::from_secs(10), expiry: Duration::from_secs(10),
store_original: false, store_original: false,
@ -171,7 +172,7 @@ mod tests {
..Default::default() ..Default::default()
}; };
let dtb = DefineTableStatement { let dtb = DefineTableStatement {
name: tb.into(), name: TB.into(),
changefeed: Some(ChangeFeed { changefeed: Some(ChangeFeed {
expiry: Duration::from_secs(10), expiry: Duration::from_secs(10),
store_original: false, store_original: false,
@ -187,11 +188,11 @@ mod tests {
// //
let mut tx0 = ds.transaction(Write, Optimistic).await.unwrap(); 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(); 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(); 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.put(tb_root.key_category(), &tb_root, dtb.clone()).await.unwrap();
tx0.commit().await.unwrap(); tx0.commit().await.unwrap();
@ -205,15 +206,15 @@ mod tests {
let mut tx1 = ds.transaction(Write, Optimistic).await.unwrap(); let mut tx1 = ds.transaction(Write, Optimistic).await.unwrap();
let thing_a = Thing { let thing_a = Thing {
tb: tb.to_owned(), tb: TB.to_owned(),
id: Id::String("A".to_string()), id: Id::String("A".to_string()),
}; };
let value_a: super::Value = "a".into(); let value_a: super::Value = "a".into();
let previous = Cow::from(Value::None); let previous = Cow::from(Value::None);
tx1.record_change( tx1.record_change(
ns, NS,
db, DB,
tb, TB,
&thing_a, &thing_a,
previous.clone(), previous.clone(),
Cow::Borrowed(&value_a), Cow::Borrowed(&value_a),
@ -224,14 +225,14 @@ mod tests {
let mut tx2 = ds.transaction(Write, Optimistic).await.unwrap(); let mut tx2 = ds.transaction(Write, Optimistic).await.unwrap();
let thing_c = Thing { let thing_c = Thing {
tb: tb.to_owned(), tb: TB.to_owned(),
id: Id::String("C".to_string()), id: Id::String("C".to_string()),
}; };
let value_c: Value = "c".into(); let value_c: Value = "c".into();
tx2.record_change( tx2.record_change(
ns, NS,
db, DB,
tb, TB,
&thing_c, &thing_c,
previous.clone(), previous.clone(),
Cow::Borrowed(&value_c), Cow::Borrowed(&value_c),
@ -243,28 +244,28 @@ mod tests {
let x = ds.transaction(Write, Optimistic).await; let x = ds.transaction(Write, Optimistic).await;
let mut tx3 = x.unwrap(); let mut tx3 = x.unwrap();
let thing_b = Thing { let thing_b = Thing {
tb: tb.to_owned(), tb: TB.to_owned(),
id: Id::String("B".to_string()), id: Id::String("B".to_string()),
}; };
let value_b: Value = "b".into(); let value_b: Value = "b".into();
tx3.record_change( tx3.record_change(
ns, NS,
db, DB,
tb, TB,
&thing_b, &thing_b,
previous.clone(), previous.clone(),
Cow::Borrowed(&value_b), Cow::Borrowed(&value_b),
DONT_STORE_PREVIOUS, DONT_STORE_PREVIOUS,
); );
let thing_c2 = Thing { let thing_c2 = Thing {
tb: tb.to_owned(), tb: TB.to_owned(),
id: Id::String("C".to_string()), id: Id::String("C".to_string()),
}; };
let value_c2: Value = "c2".into(); let value_c2: Value = "c2".into();
tx3.record_change( tx3.record_change(
ns, NS,
db, DB,
tb, TB,
&thing_c2, &thing_c2,
previous.clone(), previous.clone(),
Cow::Borrowed(&value_c2), Cow::Borrowed(&value_c2),
@ -281,7 +282,7 @@ mod tests {
let mut tx4 = ds.transaction(Write, Optimistic).await.unwrap(); let mut tx4 = ds.transaction(Write, Optimistic).await.unwrap();
let r = 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 .await
.unwrap(); .unwrap();
tx4.commit().await.unwrap(); tx4.commit().await.unwrap();
@ -290,15 +291,15 @@ mod tests {
ChangeSet( ChangeSet(
vs::u64_to_versionstamp(2), vs::u64_to_versionstamp(2),
DatabaseMutation(vec![TableMutations( DatabaseMutation(vec![TableMutations(
"mytb".to_string(), TB.to_string(),
match FFLAGS.change_feed_live_queries.enabled() { match FFLAGS.change_feed_live_queries.enabled() {
true => vec![TableMutation::SetWithDiff( true => vec![TableMutation::SetWithDiff(
Thing::from(("mytb".to_string(), "A".to_string())), Thing::from((TB.to_string(), "A".to_string())),
Value::None, Value::None,
vec![], vec![],
)], )],
false => vec![TableMutation::Set( false => vec![TableMutation::Set(
Thing::from(("mytb".to_string(), "A".to_string())), Thing::from((TB.to_string(), "A".to_string())),
Value::from("a"), Value::from("a"),
)], )],
}, },
@ -307,15 +308,15 @@ mod tests {
ChangeSet( ChangeSet(
vs::u64_to_versionstamp(3), vs::u64_to_versionstamp(3),
DatabaseMutation(vec![TableMutations( DatabaseMutation(vec![TableMutations(
"mytb".to_string(), TB.to_string(),
match FFLAGS.change_feed_live_queries.enabled() { match FFLAGS.change_feed_live_queries.enabled() {
true => vec![TableMutation::SetWithDiff( true => vec![TableMutation::SetWithDiff(
Thing::from(("mytb".to_string(), "C".to_string())), Thing::from((TB.to_string(), "C".to_string())),
Value::None, Value::None,
vec![], vec![],
)], )],
false => vec![TableMutation::Set( false => vec![TableMutation::Set(
Thing::from(("mytb".to_string(), "C".to_string())), Thing::from((TB.to_string(), "C".to_string())),
Value::from("c"), Value::from("c"),
)], )],
}, },
@ -324,27 +325,27 @@ mod tests {
ChangeSet( ChangeSet(
vs::u64_to_versionstamp(4), vs::u64_to_versionstamp(4),
DatabaseMutation(vec![TableMutations( DatabaseMutation(vec![TableMutations(
"mytb".to_string(), TB.to_string(),
match FFLAGS.change_feed_live_queries.enabled() { match FFLAGS.change_feed_live_queries.enabled() {
true => vec![ true => vec![
TableMutation::SetWithDiff( TableMutation::SetWithDiff(
Thing::from(("mytb".to_string(), "B".to_string())), Thing::from((TB.to_string(), "B".to_string())),
Value::None, Value::None,
vec![], vec![],
), ),
TableMutation::SetWithDiff( TableMutation::SetWithDiff(
Thing::from(("mytb".to_string(), "C".to_string())), Thing::from((TB.to_string(), "C".to_string())),
Value::None, Value::None,
vec![], vec![],
), ),
], ],
false => vec![ false => vec![
TableMutation::Set( TableMutation::Set(
Thing::from(("mytb".to_string(), "B".to_string())), Thing::from((TB.to_string(), "B".to_string())),
Value::from("b"), Value::from("b"),
), ),
TableMutation::Set( TableMutation::Set(
Thing::from(("mytb".to_string(), "C".to_string())), Thing::from((TB.to_string(), "C".to_string())),
Value::from("c2"), Value::from("c2"),
), ),
], ],
@ -357,14 +358,14 @@ mod tests {
let mut tx5 = ds.transaction(Write, Optimistic).await.unwrap(); let mut tx5 = ds.transaction(Write, Optimistic).await.unwrap();
// gc_all needs to be committed before we can read the changes // 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 // We now commit tx5, which should persist the gc_all resullts
tx5.commit().await.unwrap(); tx5.commit().await.unwrap();
// Now we should see the gc_all results // Now we should see the gc_all results
let mut tx6 = ds.transaction(Write, Optimistic).await.unwrap(); let mut tx6 = ds.transaction(Write, Optimistic).await.unwrap();
let r = 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 .await
.unwrap(); .unwrap();
tx6.commit().await.unwrap(); tx6.commit().await.unwrap();
@ -372,27 +373,27 @@ mod tests {
let want: Vec<ChangeSet> = vec![ChangeSet( let want: Vec<ChangeSet> = vec![ChangeSet(
vs::u64_to_versionstamp(4), vs::u64_to_versionstamp(4),
DatabaseMutation(vec![TableMutations( DatabaseMutation(vec![TableMutations(
"mytb".to_string(), TB.to_string(),
match FFLAGS.change_feed_live_queries.enabled() { match FFLAGS.change_feed_live_queries.enabled() {
true => vec![ true => vec![
TableMutation::SetWithDiff( TableMutation::SetWithDiff(
Thing::from(("mytb".to_string(), "B".to_string())), Thing::from((TB.to_string(), "B".to_string())),
Value::None, Value::None,
vec![], vec![],
), ),
TableMutation::SetWithDiff( TableMutation::SetWithDiff(
Thing::from(("mytb".to_string(), "C".to_string())), Thing::from((TB.to_string(), "C".to_string())),
Value::None, Value::None,
vec![], vec![],
), ),
], ],
false => vec![ false => vec![
TableMutation::Set( TableMutation::Set(
Thing::from(("mytb".to_string(), "B".to_string())), Thing::from((TB.to_string(), "B".to_string())),
Value::from("b"), Value::from("b"),
), ),
TableMutation::Set( TableMutation::Set(
Thing::from(("mytb".to_string(), "C".to_string())), Thing::from((TB.to_string(), "C".to_string())),
Value::from("c2"), Value::from("c2"),
), ),
], ],
@ -405,7 +406,7 @@ mod tests {
ds.tick_at((ts.0.timestamp() + 5).try_into().unwrap()).await.unwrap(); ds.tick_at((ts.0.timestamp() + 5).try_into().unwrap()).await.unwrap();
let mut tx7 = ds.transaction(Write, Optimistic).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 .await
.unwrap(); .unwrap();
tx7.commit().await.unwrap(); tx7.commit().await.unwrap();

View file

@ -355,13 +355,11 @@ impl Options {
/// Get currently selected NS /// Get currently selected NS
pub fn ns(&self) -> &str { pub fn ns(&self) -> &str {
self.ns.as_ref().map(AsRef::as_ref).unwrap() 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 /// Get currently selected DB
pub fn db(&self) -> &str { pub fn db(&self) -> &str {
self.db.as_ref().map(AsRef::as_ref).unwrap() 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 /// Check whether this request supports realtime queries

View file

@ -108,7 +108,7 @@ pub fn filter_from_value(v: &str) -> Result<EnvFilter, tracing_subscriber::filte
"error" => Ok(EnvFilter::default().add_directive(Level::ERROR.into())), "error" => Ok(EnvFilter::default().add_directive(Level::ERROR.into())),
// Specify the log level for each code area // Specify the log level for each code area
"warn" | "info" | "debug" | "trace" => EnvFilter::builder() "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 // Let's try to parse the custom log level
_ => EnvFilter::builder().parse(v), _ => EnvFilter::builder().parse(v),
} }