Skip to content

Commit

Permalink
Merge pull request #210 from CosmWasm/triple-primary-key
Browse files Browse the repository at this point in the history
Implement PrimaryKey for generic (T, U, V) triplet
  • Loading branch information
maurolacy authored Jan 4, 2021
2 parents ffd92a0 + 5018a50 commit 2314777
Showing 1 changed file with 43 additions and 0 deletions.
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>>
PrimaryKey<'a> for (T, U, V)
{
type Prefix = T;

fn key(&self) -> Vec<&[u8]> {
let mut keys = self.0.key();
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 @@ -319,6 +353,15 @@ mod test {
assert_eq!(key, parsed);
}

#[test]
fn parse_joined_keys_pk3() {
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

0 comments on commit 2314777

Please sign in to comment.