-
Notifications
You must be signed in to change notification settings - Fork 753
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Simplify UdfMgr::add_udf() (#14631)
* refactor: Add KVPbApi::list_pb_values() returns values without keys * refactor: add KVPbApi::upsert_pb() to update or insert * refactor: Simplify UdfMgr::add_udf()
- Loading branch information
1 parent
a9b8926
commit 4b157cd
Showing
9 changed files
with
343 additions
and
53 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use databend_common_meta_types::Change; | ||
use databend_common_meta_types::Operation; | ||
use databend_common_meta_types::SeqV; | ||
use databend_common_proto_conv::FromToProto; | ||
|
||
use crate::kv_pb_api::PbDecodeError; | ||
use crate::kv_pb_api::PbEncodeError; | ||
|
||
/// Encode an upsert Operation of T into protobuf encoded value. | ||
pub fn encode_operation<T>(value: &Operation<T>) -> Result<Operation<Vec<u8>>, PbEncodeError> | ||
where T: FromToProto { | ||
match value { | ||
Operation::Update(t) => { | ||
let p = t.to_pb()?; | ||
let mut buf = vec![]; | ||
prost::Message::encode(&p, &mut buf)?; | ||
Ok(Operation::Update(buf)) | ||
} | ||
Operation::Delete => Ok(Operation::Delete), | ||
Operation::AsIs => Ok(Operation::AsIs), | ||
} | ||
} | ||
|
||
/// Decode Change<Vec<u8>> into Change<T>, with FromToProto. | ||
pub fn decode_transition<T>(seqv: Change<Vec<u8>>) -> Result<Change<T>, PbDecodeError> | ||
where T: FromToProto { | ||
let c = Change { | ||
ident: seqv.ident, | ||
prev: seqv.prev.map(decode_seqv::<T>).transpose()?, | ||
result: seqv.result.map(decode_seqv::<T>).transpose()?, | ||
}; | ||
|
||
Ok(c) | ||
} | ||
|
||
/// Deserialize SeqV<Vec<u8>> into SeqV<T>, with FromToProto. | ||
pub fn decode_seqv<T>(seqv: SeqV) -> Result<SeqV<T>, PbDecodeError> | ||
where T: FromToProto { | ||
let buf = &seqv.data; | ||
let p: T::PB = prost::Message::decode(buf.as_ref())?; | ||
let v: T = FromToProto::from_pb(p)?; | ||
|
||
Ok(SeqV::with_meta(seqv.seq, seqv.meta, v)) | ||
} |
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,60 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
//! Defines errors used by protobuf based API. | ||
use databend_common_meta_types::InvalidArgument; | ||
use databend_common_meta_types::MetaError; | ||
use databend_common_proto_conv::Incompatible; | ||
|
||
use crate::kv_pb_api::PbDecodeError; | ||
|
||
/// An error occurred when encoding with FromToProto. | ||
#[derive(Clone, Debug, PartialEq, thiserror::Error)] | ||
#[error("PbEncodeError: {0}")] | ||
pub enum PbEncodeError { | ||
EncodeError(#[from] prost::EncodeError), | ||
Incompatible(#[from] Incompatible), | ||
} | ||
|
||
impl From<PbEncodeError> for MetaError { | ||
fn from(value: PbEncodeError) -> Self { | ||
match value { | ||
PbEncodeError::EncodeError(e) => MetaError::from(InvalidArgument::new(e, "")), | ||
PbEncodeError::Incompatible(e) => MetaError::from(InvalidArgument::new(e, "")), | ||
} | ||
} | ||
} | ||
|
||
/// An error occurs when writing protobuf encoded value to kv store. | ||
#[derive(Clone, Debug, PartialEq, thiserror::Error)] | ||
#[error("PbApiWriteError: {0}")] | ||
pub enum PbApiWriteError<E> { | ||
PbEncodeError(#[from] PbEncodeError), | ||
/// upsert reads the state transition after the operation. | ||
PbDecodeError(#[from] PbDecodeError), | ||
/// Error returned from KVApi. | ||
KvApiError(E), | ||
} | ||
|
||
impl From<PbApiWriteError<MetaError>> for MetaError { | ||
/// For KVApi that returns MetaError, convert protobuf related error to MetaError directly. | ||
fn from(value: PbApiWriteError<MetaError>) -> Self { | ||
match value { | ||
PbApiWriteError::PbEncodeError(e) => MetaError::from(e), | ||
PbApiWriteError::PbDecodeError(e) => MetaError::from(e), | ||
PbApiWriteError::KvApiError(e) => e, | ||
} | ||
} | ||
} |
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,105 @@ | ||
// Copyright 2021 Datafuse Labs | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
use std::time::Duration; | ||
|
||
use databend_common_meta_kvapi::kvapi; | ||
use databend_common_meta_types::MatchSeq; | ||
use databend_common_meta_types::MetaSpec; | ||
use databend_common_meta_types::Operation; | ||
use databend_common_meta_types::With; | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct UpsertPB<K: kvapi::Key> { | ||
pub key: K, | ||
|
||
/// Since a sequence number is always positive, using Exact(0) to perform an add-if-absent operation. | ||
/// - GE(1) to perform an update-any operation. | ||
/// - Exact(n) to perform an update on some specified version. | ||
/// - Any to perform an update or insert that always takes effect. | ||
pub seq: MatchSeq, | ||
|
||
/// The value to set. A `None` indicates to delete it. | ||
pub value: Operation<K::ValueType>, | ||
|
||
/// Meta data of a value. | ||
pub value_meta: Option<MetaSpec>, | ||
} | ||
|
||
impl<K: kvapi::Key> UpsertPB<K> { | ||
pub fn new( | ||
key: K, | ||
seq: MatchSeq, | ||
value: Operation<K::ValueType>, | ||
value_meta: Option<MetaSpec>, | ||
) -> Self { | ||
Self { | ||
key, | ||
seq, | ||
value, | ||
value_meta, | ||
} | ||
} | ||
|
||
pub fn insert(key: K, value: K::ValueType) -> Self { | ||
Self { | ||
key, | ||
seq: MatchSeq::Exact(0), | ||
value: Operation::Update(value), | ||
value_meta: None, | ||
} | ||
} | ||
|
||
pub fn update(key: K, value: K::ValueType) -> Self { | ||
Self { | ||
key, | ||
seq: MatchSeq::GE(0), | ||
value: Operation::Update(value), | ||
value_meta: None, | ||
} | ||
} | ||
|
||
pub fn delete(key: K) -> Self { | ||
Self { | ||
key, | ||
seq: MatchSeq::GE(0), | ||
value: Operation::Delete, | ||
value_meta: None, | ||
} | ||
} | ||
|
||
pub fn with_expire_sec(self, expire_at_sec: u64) -> Self { | ||
self.with(MetaSpec::new_expire(expire_at_sec)) | ||
} | ||
|
||
/// Set the time to last for the value. | ||
/// When the ttl is passed, the value is deleted. | ||
pub fn with_ttl(self, ttl: Duration) -> Self { | ||
self.with(MetaSpec::new_ttl(ttl)) | ||
} | ||
} | ||
|
||
impl<K: kvapi::Key> With<MatchSeq> for UpsertPB<K> { | ||
fn with(mut self, seq: MatchSeq) -> Self { | ||
self.seq = seq; | ||
self | ||
} | ||
} | ||
|
||
impl<K: kvapi::Key> With<MetaSpec> for UpsertPB<K> { | ||
fn with(mut self, meta: MetaSpec) -> Self { | ||
self.value_meta = Some(meta); | ||
self | ||
} | ||
} |
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
Oops, something went wrong.