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

Implement PrimaryKey for generic (T, U, V) triplet #210

Merged
merged 1 commit into from
Jan 4, 2021
Merged
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
43 changes: 43 additions & 0 deletions packages/storage-plus/src/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub trait PrimaryKey<'a>: Clone {
type Pk0 = ();
type Pk1<'a> = &'a [u8];
type Pk2<'a, T = &'a [u8], U = &'a [u8]> = (T, U);
type Pk3<'a, T = &'a [u8], U = &'a [u8], V = &'a [u8]> = (T, U, V);

type PkStr<'a> = &'a str;

Expand Down Expand Up @@ -74,6 +75,33 @@ impl<'a, T: PrimaryKey<'a> + Prefixer<'a>, U: PrimaryKey<'a>> PrimaryKey<'a> for
}
}

// use generics for combining there - so we can use &[u8], PkOwned, or IntKey
impl<'a, T: PrimaryKey<'a> + Prefixer<'a>, U: PrimaryKey<'a> + Prefixer<'a>, V: PrimaryKey<'a>>
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
PrimaryKey<'a> for (T, U, V)
{
type Prefix = T;
Copy link
Member

Choose a reason for hiding this comment

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

Prefix should be (T, U)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Wouldn't we want to consider both prefixes as valid? I mean, (T, U) is a prefix, but wouldn't T be another valid one?

Copy link
Member

Choose a reason for hiding this comment

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

Sure, but look how this is used.
Maybe we need to go through this all and check the usage and document it better.

I don't have the head space to go deep in here now. Best is to leave these PRs open and I will review and merge them (or comment on them) when I have the time to check all the details.

Copy link
Contributor Author

@maurolacy maurolacy Jan 5, 2021

Choose a reason for hiding this comment

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

Sure, but look how this is used.

This change will force me to change the implementation of MultiIndex, currently in review (#211).
But that's OK, as this is the right way to implement Prefix for these types. I'll do the changes, and will have a better look at the trade-offs.

Maybe we need to go through this all and check the usage and document it better.

Sure. I'll document the final format properly.

I don't have the head space to go deep in here now. Best is to leave these PRs open and I will review and merge them (or comment on them) when I have the time to check all the details.

No worries. I'll leave the MultiIndex PR open. That's the only place we're actually using / requiring this.


fn key(&self) -> Vec<&[u8]> {
let mut keys = self.0.key();
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
keys.extend(&self.1.key());
keys.extend(&self.2.key());
keys
}

fn parse_key(serialized: &'a [u8]) -> Self {
let l1 = decode_length(&serialized[0..2]);
let first = &serialized[2..2 + l1];
let l2 = decode_length(&serialized[2 + l1..2 + l1 + 2]);
let second = &serialized[2 + l1 + 2..2 + l1 + 2 + l2];
let third = &serialized[2 + l1 + 2 + l2..];
(
T::parse_key(first),
U::parse_key(second),
V::parse_key(third),
)
}
}

// pub trait Prefixer<'a>: Copy {
pub trait Prefixer<'a> {
/// returns 0 or more namespaces that should length-prefixed and concatenated for range searches
Expand All @@ -98,6 +126,12 @@ impl<'a> Prefixer<'a> for Pk2<'a> {
}
}

impl<'a> Prefixer<'a> for Pk3<'a> {
fn prefix(&self) -> Vec<&[u8]> {
vec![self.0, self.1, self.2]
}
}

// Provide a string version of this to raw encode strings
impl<'a> Prefixer<'a> for PkStr<'a> {
fn prefix<'b>(&'b self) -> Vec<&'b [u8]> {
Expand Down Expand Up @@ -312,6 +346,15 @@ mod test {
assert_eq!(key, parsed);
}

#[test]
fn parse_joined_keys_pk3() {
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
let key: Pk3 = (b"four", b"square", b"cinco");
let joined = key.joined_key();
assert_eq!(4 + 6 + 5 + 2 * (3 - 1), joined.len());
let parsed = Pk3::parse_key(&joined);
assert_eq!(key, parsed);
}

#[test]
fn parse_joined_keys_int() {
let key: U64Key = 12345678.into();
Expand Down