Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/delete doc #314

Merged
merged 4 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 61 additions & 2 deletions src/cmd/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,18 @@ pub enum DB3ClientCommand {
#[clap(long)]
documents: Vec<String>,
},

#[clap(name = "del-doc")]
DeleteDocument {
/// the address of database
#[clap(long)]
addr: String,
/// the name of collection
#[clap(long)]
collection_name: String,
/// the content of document
#[clap(long)]
ids: Vec<String>,
},
/// Get a document with given doc id
#[clap(name = "get-doc")]
GetDocument {
Expand Down Expand Up @@ -375,7 +386,8 @@ impl DB3ClientCommand {
.collect();
let document_mut = DocumentMutation {
collection_name,
document: bson_documents,
documents: bson_documents,
ids: vec![],
};
let dm = DatabaseMutation {
meta: Some(meta),
Expand All @@ -402,6 +414,53 @@ impl DB3ClientCommand {
Err(e) => Err(format!("fail to add document: {:?}", e)),
}
}
DB3ClientCommand::DeleteDocument {
addr,
collection_name,
ids,
} => {
if (ids.is_empty()) {
return Err("fail to delete with empty ids".to_string());
}
let db_id = DbId::try_from(addr.as_str()).unwrap();
let meta = BroadcastMeta {
//TODO get from network
nonce: Self::current_seconds(),
//TODO use config
chain_id: ChainId::DevNet.into(),
//TODO use config
chain_role: ChainRole::StorageShardChain.into(),
};
let document_mut = DocumentMutation {
collection_name,
documents: vec![],
ids,
};
let dm = DatabaseMutation {
meta: Some(meta),
action: DatabaseAction::DeleteDocument.into(),
db_address: db_id.as_ref().to_vec(),
document_mutations: vec![document_mut],
collection_mutations: vec![],
};
match ctx
.mutation_sdk
.as_ref()
.unwrap()
.submit_database_mutation(&dm)
.await
{
Ok((_, tx_id)) => {
println!("send delete document done");
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
table.set_titles(row!["transaction id"]);
table.add_row(row![tx_id.to_base64()]);
Ok(table)
}
Err(e) => Err(format!("fail to delete document: {:?}", e)),
}
}
}
}
}
2 changes: 1 addition & 1 deletion src/crypto/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ impl DocumentId {
}

/// collection id = document_id[OP_ENTRY_ID_LENGTH..]
pub fn get_collection_id(&self) -> std::result::Result<DocumentEntryId, DB3Error> {
pub fn get_collection_id(&self) -> std::result::Result<CollectionId, DB3Error> {
CollectionId::try_from_bytes(
self.data[TYPE_ID_LENGTH..TYPE_ID_LENGTH + OP_ENTRY_ID_LENGTH].as_ref(),
)
Expand Down
4 changes: 4 additions & 0 deletions src/error/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ pub enum DB3Error {
InvalidCollectionIdBytes(String),
#[error("invalid index id bytes {0}")]
InvalidIndexIdBytes(String),
#[error("document not exist with target id {0}")]
DocumentNotExist(String),
#[error("document modified permission error")]
DocumentModifiedPermissionError,
}

pub type Result<T> = std::result::Result<T, DB3Error>;
39 changes: 31 additions & 8 deletions src/node/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,16 +462,39 @@ mod tests {
assert!(false)
}

// verify document is added
let cmd = DB3ClientCommand::GetDocument {
id: doc_id2.to_string(),
};
if let Ok(table) = cmd.execute(&mut ctx).await {
assert_eq!(
doc_id2,
table.get_row(0).unwrap().get_cell(0).unwrap().get_content()
);
} else {
assert!(false)
}
let table = cmd.execute(&mut ctx).await.unwrap();
assert_eq!(
doc_id2,
table.get_row(0).unwrap().get_cell(0).unwrap().get_content()
);

// verify test delete with empty ids
let cmd = DB3ClientCommand::DeleteDocument {
addr: addr.clone(),
collection_name: collection_books.to_string(),
ids: vec![],
};
let res = cmd.execute(&mut ctx).await;
assert!(res.is_err(), "{:?}", res.unwrap());

// test delete document cmd
let cmd = DB3ClientCommand::DeleteDocument {
addr: addr.clone(),
collection_name: collection_books.to_string(),
ids: vec![doc_id2.to_string()],
};
let table = cmd.execute(&mut ctx).await.unwrap();
assert_eq!(1, table.len());
std::thread::sleep(time::Duration::from_millis(2000));
// verify document is deleted
let cmd = DB3ClientCommand::GetDocument {
id: doc_id2.to_string(),
};
let res = cmd.execute(&mut ctx).await;
assert!(res.is_err(), "{:?}", res.unwrap());
}
}
4 changes: 3 additions & 1 deletion src/proto/proto/db3_mutation.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum DatabaseAction {
CreateDB = 0;
AddCollection = 1;
AddDocument = 2;
DeleteDocument = 3;
}

message DatabaseMutation {
Expand All @@ -47,7 +48,8 @@ message CollectionMutation {
}
message DocumentMutation {
string collection_name = 1;
repeated bytes document = 2;
repeated bytes documents = 2;
repeated string ids = 3;
}
message KVPair {
// the key of value
Expand Down
Loading