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: Add Tid::now and Tid::from_datetime constructors #277

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DrChat
Copy link

@DrChat DrChat commented Feb 1, 2025

This simply adds some new Tid convenience constructors so that users do not have to write their own if they want to construct a timestamp.

@sugyan sugyan self-requested a review February 3, 2025 14:28
fn s32_encode(mut i: u64) -> String {
const S32_CHAR: &[u8] = b"234567abcdefghijklmnopqrstuvwxyz";

let mut s = String::new();
Copy link
Owner

Choose a reason for hiding this comment

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

nits: this should be String::with_capacity(13)

}

// Reverse the string to convert it to big-endian format.
s.as_str().chars().rev().collect()
Copy link
Owner

Choose a reason for hiding this comment

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

nits: .as_str() may be unnecessary

///
/// Clock IDs 0-31 can be used as an ad-hoc clock ID if you are not concerned
/// with this parameter.
pub fn from_datetime(cid: u32, time: chrono::DateTime<chrono::Utc>) -> Self {
Copy link
Owner

@sugyan sugyan Feb 3, 2025

Choose a reason for hiding this comment

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

The parameter name cid is easily confused with the Cid of ipld_core, so I think we should use the name clock_identifier or clock_id instead of omitting it here.

Copy link
Author

Choose a reason for hiding this comment

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

Good call! Yeah I was thinking about clkid before submitting this. Will change it to one of the above :)

///
/// Clock IDs 0-31 can be used as an ad-hoc clock ID if you are not concerned
/// with this parameter.
pub fn from_datetime(cid: u32, time: chrono::DateTime<chrono::Utc>) -> Self {
Copy link
Owner

Choose a reason for hiding this comment

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

And what would be the appropriate type for the clock identifier?

This value is only used for the lower 10 bits, and atrium_api::types::integer defines a type that takes into account various bounds. I think LimitedU32<1023> would be appropriate here, what do you think?

@@ -436,6 +452,27 @@ impl Tid {
}
}

/// Construct a new timestamp with the specified clock ID.
///
/// Clock IDs 0-31 can be used as an ad-hoc clock ID if you are not concerned
Copy link
Owner

Choose a reason for hiding this comment

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

Is the Clock ID 0-31? I am wondering because the specs say 10bit and it seems to be a value in the range 0-1023. If you know, I would like to know.

Copy link
Author

Choose a reason for hiding this comment

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

Here's some more info that I've found online: bluesky-social/atproto#1160 (comment)
It appears that the clock ID partitioning did not make it into the specification - but based on this, clocks 0-31 are ad-hoc identifiers (and one is randomly chosen by the reference implementation).

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the reply! I don't think we need to bother writing about the 0-31 range as long as it is not explicitly stated in the specification.

It seems more important to have a mechanism to ensure that a value larger than the previously issued timestamp is generated in order to avoid collisions.

@DrChat
Copy link
Author

DrChat commented Feb 8, 2025

@sugyan Btw sorry - I've been pretty spotty just because I'm limited on time here; I'm doing this work in my free time at this point.

If you want to speed this along, absolutely feel free to push changes to my branch here. If not, I will follow-up eventually :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants