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] OB Version #84

Merged
merged 2 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions src/rpc/protocol/payloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use crate::{
location::OB_INVALID_ID,
rpc::protocol::codes::ResultCodes,
serde_obkv::{util, value::Value},
util::{decode_value, duration_to_millis, security, string_from_bytes},
util::{
decode_value, duration_to_millis, obversion::ob_vsn_major, security, string_from_bytes,
},
};

#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down Expand Up @@ -411,7 +413,12 @@ impl ProtoEncoder for ObTableOperationRequest {
util::encode_bytes_string(&self.credential, buf)?;
util::encode_vstring(&self.table_name, buf)?;
util::encode_vi64(self.table_id, buf)?;
util::encode_vi64(self.partition_id, buf)?;

if ob_vsn_major() >= 4 {
buf.put_i64(self.partition_id);
} else {
util::encode_vi64(self.partition_id, buf)?;
}

buf.put_i8(self.entity_type as i8);
self.table_operation.encode(buf)?;
Expand Down
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use chrono::Utc;

use crate::serde_obkv::value::{ObjType, Value};

pub mod obversion;
pub mod permit;
pub mod security;

Expand Down
131 changes: 131 additions & 0 deletions src/util/obversion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*-
* #%L
* OBKV Table Client Framework
* %%
* Copyright (C) 2023 OceanBase
* %%
* OBKV Table Client Framework is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the
* Mulan PSL v2. You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
* NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PSL v2 for more details.
* #L%
*/

use std::sync::atomic::{AtomicU64, Ordering::Relaxed};

lazy_static! {
pub static ref OB_VERSION: AtomicU64 = AtomicU64::new(0);
}

#[allow(dead_code)]
const OB_VSN_MAJOR_SHIFT: u64 = 32;
#[allow(dead_code)]
const OB_VSN_MINOR_SHIFT: u64 = 16;
#[allow(dead_code)]
const OB_VSN_MAJOR_PATCH_SHIFT: u64 = 8;
#[allow(dead_code)]
const OB_VSN_MINOR_PATCH_SHIFT: u64 = 0;
#[allow(dead_code)]
const OB_VSN_MAJOR_MASK: u64 = 0xffffffff;
#[allow(dead_code)]
const OB_VSN_MINOR_MASK: u64 = 0xffff;
#[allow(dead_code)]
const OB_VSN_MAJOR_PATCH_MASK: u64 = 0xff;
#[allow(dead_code)]
const OB_VSN_MINOR_PATCH_MASK: u64 = 0xff;

#[allow(dead_code)]
pub fn calc_version(major: i32, minor: i16, major_patch: i8, minor_patch: i8) -> u64 {
((major as u64) << OB_VSN_MAJOR_SHIFT)
+ ((minor as u64) << OB_VSN_MINOR_SHIFT)
+ ((major_patch as u64) << OB_VSN_MAJOR_PATCH_SHIFT)
+ ((minor_patch as u64) << OB_VSN_MINOR_PATCH_SHIFT)
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we can use '|' to replace '+' here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


#[allow(dead_code)]
pub fn ob_vsn_major() -> i32 {
get_ob_vsn_major(OB_VERSION.load(Relaxed))
}

#[allow(dead_code)]
pub fn get_ob_vsn_major(version: u64) -> i32 {
((version >> OB_VSN_MAJOR_SHIFT) & OB_VSN_MAJOR_MASK) as i32
}

#[allow(dead_code)]
pub fn ob_vsn_minor() -> i16 {
get_ob_vsn_minor(OB_VERSION.load(Relaxed))
}

#[allow(dead_code)]
pub fn get_ob_vsn_minor(version: u64) -> i16 {
((version >> OB_VSN_MINOR_SHIFT) & OB_VSN_MINOR_MASK) as i16
}

#[allow(dead_code)]
pub fn ob_vsn_major_patch() -> i8 {
get_ob_vsn_major_patch(OB_VERSION.load(Relaxed))
}

#[allow(dead_code)]
pub fn get_ob_vsn_major_patch(version: u64) -> i8 {
((version >> OB_VSN_MAJOR_PATCH_SHIFT) & OB_VSN_MAJOR_PATCH_MASK) as i8
}

#[allow(dead_code)]
pub fn ob_vsn_minor_patch() -> i8 {
get_ob_vsn_minor_patch(OB_VERSION.load(Relaxed))
}

#[allow(dead_code)]
pub fn get_ob_vsn_minor_patch(version: u64) -> i8 {
((version >> OB_VSN_MINOR_PATCH_SHIFT) & OB_VSN_MINOR_PATCH_MASK) as i8
}

#[allow(dead_code)]
pub fn ob_vsn_string() -> String {
format!(
"{}.{}.{}.{}",
ob_vsn_major(),
ob_vsn_minor(),
ob_vsn_major_patch(),
ob_vsn_minor_patch()
)
}

#[allow(dead_code)]
pub fn get_ob_vsn_string(version: u64) -> String {
format!(
"{}.{}.{}.{}",
get_ob_vsn_major(version),
get_ob_vsn_minor(version),
get_ob_vsn_major_patch(version),
get_ob_vsn_minor_patch(version)
)
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_ob_version() {
assert_eq!(ob_vsn_major(), 0);
assert_eq!(ob_vsn_minor(), 0);
assert_eq!(ob_vsn_major_patch(), 0);
assert_eq!(ob_vsn_minor_patch(), 0);

let my_version = calc_version(4, 2, 1, 4);
assert_eq!(get_ob_vsn_major(my_version), 4);
assert_eq!(get_ob_vsn_minor(my_version), 2);
assert_eq!(get_ob_vsn_major_patch(my_version), 1);
assert_eq!(get_ob_vsn_minor_patch(my_version), 4);

assert_eq!(ob_vsn_string(), "0.0.0.0");
assert_eq!(get_ob_vsn_string(my_version), "4.2.1.4");
}
}