Skip to content

Commit ab3017d

Browse files
committed
feat!: use i64 for CreateSequence
See discussion: * https://lists.apache.org/thread/4o3rl49rdj5y0134df922zgc8clyt86s * https://issues.apache.org/jira/browse/ZOOKEEPER-4706 Though the discussion is in very early stage but I toward to 64-bit ephemeral sequence. And `i64` could cover current situation.
1 parent d20c161 commit ab3017d

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

src/client/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,24 @@ impl<'a> CreateOptions<'a> {
165165
///
166166
/// It prints in ten decimal digits with possible leading padding 0.
167167
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
168-
pub struct CreateSequence(pub i32);
168+
pub struct CreateSequence(i64);
169169

170170
impl std::fmt::Display for CreateSequence {
171171
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
172-
write!(f, "{:010}", self.0)
172+
// Discussion for 64-bit ephemeral sequence number:
173+
// * https://lists.apache.org/thread/4o3rl49rdj5y0134df922zgc8clyt86s
174+
// * https://issues.apache.org/jira/browse/ZOOKEEPER-4706
175+
if self.0 <= i32::MAX.into() {
176+
write!(f, "{:010}", self.0)
177+
} else {
178+
write!(f, "{:019}", self.0)
179+
}
180+
}
181+
}
182+
183+
impl CreateSequence {
184+
pub fn into_i64(self) -> i64 {
185+
self.0
173186
}
174187
}
175188

@@ -338,7 +351,7 @@ impl Client {
338351

339352
fn parse_sequence(client_path: &str, prefix: &str) -> Result<CreateSequence> {
340353
if let Some(sequence_path) = client_path.strip_prefix(prefix) {
341-
match sequence_path.parse::<i32>() {
354+
match sequence_path.parse::<i64>() {
342355
Err(_) => Err(Error::UnexpectedError(format!("sequential node get no i32 path {}", client_path))),
343356
Ok(i) => Ok(CreateSequence(i)),
344357
}

tests/zookeeper.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ async fn test_data_node() {
497497
let path = "/abc";
498498
let data = random_data();
499499
let (stat, sequence) = client.create(path, &data, PERSISTENT_OPEN).await.unwrap();
500-
assert_eq!(sequence.0, -1);
500+
assert_eq!(sequence.into_i64(), -1);
501501

502502
assert_eq!(stat, client.check_stat(path).await.unwrap().unwrap());
503503
assert_eq!((data, stat), client.get_data(path).await.unwrap());
@@ -528,7 +528,7 @@ async fn test_create_sequential() {
528528
.await
529529
.unwrap();
530530

531-
assert!(sequence2.0 > sequence1.0);
531+
assert!(sequence2.into_i64() > sequence1.into_i64());
532532

533533
let path1 = format!("{}{}", prefix, sequence1);
534534
let path2 = format!("{}{}", prefix, sequence2);

0 commit comments

Comments
 (0)