diff --git a/cozorocks/bridge/db.cpp b/cozorocks/bridge/db.cpp index 51c9af4a..584b143d 100644 --- a/cozorocks/bridge/db.cpp +++ b/cozorocks/bridge/db.cpp @@ -76,7 +76,7 @@ shared_ptr open_db(const DbOpts &opts, RocksDbStatus &status, boo } PessimisticRocksDb::~PessimisticRocksDb() { - if (destroy_on_exit) { + if (destroy_on_exit && (db != nullptr)) { cerr << "destroying database on exit: " << db_path << endl; auto status = db->Close(); if (!status.ok()) { @@ -85,7 +85,7 @@ PessimisticRocksDb::~PessimisticRocksDb() { db.reset(); auto status2 = DestroyDB(db_path, *options); if (!status2.ok()) { - cerr << status.ToString() << endl; + cerr << status2.ToString() << endl; } } } diff --git a/src/bin/cozo_http.rs b/src/bin/cozo_http.rs index d516d8cf..5c75a326 100644 --- a/src/bin/cozo_http.rs +++ b/src/bin/cozo_http.rs @@ -1,7 +1,7 @@ use actix_cors::Cors; use actix_web::{post, web, App, HttpResponse, HttpServer, Responder}; use clap::Parser; -use cozo::{AttrTxItem, Db}; +use cozo::Db; use cozorocks::DbBuilder; use std::fmt::{Debug, Display, Formatter}; use std::path::Path; @@ -61,10 +61,7 @@ async fn transact( body: web::Json, data: web::Data, ) -> Result { - let mut tx = data.db.transact_write()?; - let (payloads, comment) = tx.parse_tx_requests(&body)?; - tx.tx_triples(payloads)?; - tx.commit_tx(&comment, false)?; + data.db.transact_triples(&body)?; Ok(HttpResponse::Ok().body("transact")) } @@ -73,10 +70,7 @@ async fn transact_attr( body: web::Json, data: web::Data, ) -> Result { - let (attrs, comment) = AttrTxItem::parse_request(&body)?; - let mut tx = data.db.transact_write()?; - tx.tx_attrs(attrs)?; - tx.commit_tx(&comment, false)?; + data.db.transact_attributes(&body)?; Ok(HttpResponse::Ok().body("transact-attr success")) } diff --git a/src/lib.rs b/src/lib.rs index bd7ef5ca..b02c5f35 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,13 +5,10 @@ use tikv_jemallocator::Jemalloc; #[global_allocator] static GLOBAL: Jemalloc = Jemalloc; -#[cfg(test)] -mod tests; - pub(crate) mod data; pub(crate) mod runtime; pub(crate) mod transact; pub(crate) mod utils; -pub use runtime::instance::Db; pub use data::tx_attr::AttrTxItem; +pub use runtime::instance::Db; diff --git a/src/runtime/instance.rs b/src/runtime/instance.rs index d6d25562..96036393 100644 --- a/src/runtime/instance.rs +++ b/src/runtime/instance.rs @@ -1,6 +1,7 @@ use crate::data::compare::{rusty_cmp, DB_KEY_PREFIX_LEN}; use crate::data::id::TxId; use crate::runtime::transact::SessionTx; +use crate::AttrTxItem; use anyhow::Result; use cozorocks::{DbBuilder, DbIter, RocksDb}; use std::fmt::{Debug, Formatter}; @@ -103,9 +104,23 @@ impl Db { }; Ok(ret) } - pub(crate) fn total_iter(&self) -> DbIter { + pub fn total_iter(&self) -> DbIter { let mut it = self.db.transact().start().iterator().start(); it.seek_to_start(); it } + pub fn transact_triples(&self, payload: &serde_json::Value) -> Result<()> { + let mut tx = self.transact_write()?; + let (payloads, comment) = tx.parse_tx_requests(payload)?; + tx.tx_triples(payloads)?; + tx.commit_tx(&comment, false)?; + Ok(()) + } + pub fn transact_attributes(&self, payload: &serde_json::Value) -> Result<()> { + let (attrs, comment) = AttrTxItem::parse_request(payload)?; + let mut tx = self.transact_write()?; + tx.tx_attrs(attrs)?; + tx.commit_tx(&comment, false)?; + Ok(()) + } } diff --git a/src/tests.rs b/src/tests.rs deleted file mode 100644 index 9d54ee26..00000000 --- a/src/tests.rs +++ /dev/null @@ -1,97 +0,0 @@ -use crate::data::attr::{Attribute, AttributeCardinality, AttributeIndex, AttributeTyping}; -use crate::data::encode::EncodedVec; -use crate::data::id::{AttrId, EntityId, Validity}; -use crate::data::keyword::Keyword; -use crate::data::value::Value; -use crate::Db; -use anyhow::Result; -use cozorocks::DbBuilder; - -fn create_db(name: &str) -> Db { - let builder = DbBuilder::default() - .path(name) - .create_if_missing(true) - .destroy_on_exit(true); - Db::build(builder).unwrap() -} - -fn test_send_sync(_: &T) {} - -#[test] -fn creation() { - let db = create_db("_test_db"); - test_send_sync(&db); - let current_validity = Validity::current(); - let session = db.new_session().unwrap(); - let mut tx = session.transact().unwrap(); - assert_eq!( - 0, - tx.all_attrs() - .collect::>>() - .unwrap() - .len() - ); - - let mut tx = session.transact_write().unwrap(); - tx.new_attr(Attribute { - id: AttrId(0), - keyword: Keyword::try_from("hello/world").unwrap(), - cardinality: AttributeCardinality::Many, - val_type: AttributeTyping::Int, - indexing: AttributeIndex::None, - with_history: true, - }) - .unwrap(); - tx.commit_tx("", false).unwrap(); - - let mut tx = session.transact_write().unwrap(); - let attr = tx - .attr_by_kw(&Keyword::try_from("hello/world").unwrap()) - .unwrap() - .unwrap(); - tx.new_triple(EntityId(1), &attr, &Value::Int(98765), current_validity) - .unwrap(); - tx.new_triple(EntityId(2), &attr, &Value::Int(1111111), current_validity) - .unwrap(); - tx.commit_tx("haah", false).unwrap(); - - let mut tx = session.transact_write().unwrap(); - tx.amend_attr(Attribute { - id: AttrId(10000001), - keyword: Keyword::try_from("hello/sucker").unwrap(), - cardinality: AttributeCardinality::Many, - val_type: AttributeTyping::Int, - indexing: AttributeIndex::None, - with_history: true, - }) - .unwrap(); - tx.commit_tx("oops", false).unwrap(); - - let mut tx = session.transact().unwrap(); - let world_found = tx - .attr_by_kw(&Keyword::try_from("hello/world").unwrap()) - .unwrap(); - dbg!(world_found); - let sucker_found = tx - .attr_by_kw(&Keyword::try_from("hello/sucker").unwrap()) - .unwrap(); - dbg!(sucker_found); - for attr in tx.all_attrs() { - dbg!(attr.unwrap()); - } - - for r in tx.triple_a_scan_all() { - dbg!(r.unwrap()); - } - - dbg!(&session); - - let mut it = session.total_iter(); - while let Some((k, v)) = it.pair().unwrap() { - let key = EncodedVec::new(k); - let val = key.debug_value(v); - dbg!(key); - dbg!(val); - it.next(); - } -} diff --git a/tests/creation.rs b/tests/creation.rs new file mode 100644 index 00000000..b1b90e0c --- /dev/null +++ b/tests/creation.rs @@ -0,0 +1,93 @@ +use anyhow::Result; +use cozo::Db; +use cozorocks::DbBuilder; + +fn create_db(name: &str) -> Db { + let builder = DbBuilder::default() + .path(name) + .create_if_missing(true) + .destroy_on_exit(true); + Db::build(builder).unwrap() +} + +fn test_send_sync(_: &T) {} + +#[test] +fn creation() { + let db = create_db("_test_db"); + test_send_sync(&db); + // let current_validity = Validity::current(); + // let session = db.new_session().unwrap(); + // let mut tx = session.transact().unwrap(); + // assert_eq!( + // 0, + // tx.all_attrs() + // .collect::>>() + // .unwrap() + // .len() + // ); + // + // let mut tx = session.transact_write().unwrap(); + // tx.new_attr(Attribute { + // id: AttrId(0), + // keyword: Keyword::try_from("hello/world").unwrap(), + // cardinality: AttributeCardinality::Many, + // val_type: AttributeTyping::Int, + // indexing: AttributeIndex::None, + // with_history: true, + // }) + // .unwrap(); + // tx.commit_tx("", false).unwrap(); + // + // let mut tx = session.transact_write().unwrap(); + // let attr = tx + // .attr_by_kw(&Keyword::try_from("hello/world").unwrap()) + // .unwrap() + // .unwrap(); + // tx.new_triple(EntityId(1), &attr, &Value::Int(98765), current_validity) + // .unwrap(); + // tx.new_triple(EntityId(2), &attr, &Value::Int(1111111), current_validity) + // .unwrap(); + // tx.commit_tx("haah", false).unwrap(); + // + // let mut tx = session.transact_write().unwrap(); + // tx.amend_attr(Attribute { + // id: AttrId(10000001), + // keyword: Keyword::try_from("hello/sucker").unwrap(), + // cardinality: AttributeCardinality::Many, + // val_type: AttributeTyping::Int, + // indexing: AttributeIndex::None, + // with_history: true, + // }) + // .unwrap(); + // tx.commit_tx("oops", false).unwrap(); + // + // let mut tx = session.transact().unwrap(); + // let world_found = tx + // .attr_by_kw(&Keyword::try_from("hello/world").unwrap()) + // .unwrap(); + // dbg!(world_found); + // let sucker_found = tx + // .attr_by_kw(&Keyword::try_from("hello/sucker").unwrap()) + // .unwrap(); + // dbg!(sucker_found); + // for attr in tx.all_attrs() { + // dbg!(attr.unwrap()); + // } + // + // for r in tx.triple_a_scan_all() { + // dbg!(r.unwrap()); + // } + // + // dbg!(&session); + // + // let mut it = session.total_iter(); + // while let Some((k, v)) = it.pair().unwrap() { + // let key = EncodedVec::new(k); + // let val = key.debug_value(v); + // dbg!(key); + // dbg!(val); + // it.next(); + // } + dbg!(1); +}