Skip to content

Commit c92a89d

Browse files
committed
chore(sqlite): reorder methods and add doc comment for encode_key
This reorders methods so that they're grouped in "dual" pairs (encode/decode, serialize/deserialize). Also adds a doc comment to `encode_key`, as I've wondered in the past what it did.
1 parent 684f228 commit c92a89d

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

crates/matrix-sdk-sqlite/src/utils.rs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,19 @@ pub(crate) fn time_to_timestamp(time: SystemTime) -> i64 {
536536
pub(crate) trait EncryptableStore {
537537
fn get_cypher(&self) -> Option<&StoreCipher>;
538538

539+
/// If the store is using encryption, this will hash the given key. This is
540+
/// useful when we need to do queries against a given key, but we don't
541+
/// need to store the key in plain text (i.e. it's not both a key and a
542+
/// value).
543+
fn encode_key(&self, table_name: &str, key: impl AsRef<[u8]>) -> Key {
544+
let bytes = key.as_ref();
545+
if let Some(store_cipher) = self.get_cypher() {
546+
Key::Hashed(store_cipher.hash_key(table_name, bytes))
547+
} else {
548+
Key::Plain(bytes.to_owned())
549+
}
550+
}
551+
539552
fn encode_value(&self, value: Vec<u8>) -> Result<Vec<u8>> {
540553
if let Some(key) = self.get_cypher() {
541554
let encrypted = key.encrypt_value_data(value)?;
@@ -545,16 +558,6 @@ pub(crate) trait EncryptableStore {
545558
}
546559
}
547560

548-
fn serialize_value(&self, value: &impl Serialize) -> Result<Vec<u8>> {
549-
let serialized = rmp_serde::to_vec_named(value)?;
550-
self.encode_value(serialized)
551-
}
552-
553-
fn serialize_json(&self, value: &impl Serialize) -> Result<Vec<u8>> {
554-
let serialized = serde_json::to_vec(value)?;
555-
self.encode_value(serialized)
556-
}
557-
558561
fn decode_value<'a>(&self, value: &'a [u8]) -> Result<Cow<'a, [u8]>> {
559562
if let Some(key) = self.get_cypher() {
560563
let encrypted = rmp_serde::from_slice(value)?;
@@ -565,6 +568,21 @@ pub(crate) trait EncryptableStore {
565568
}
566569
}
567570

571+
fn serialize_value(&self, value: &impl Serialize) -> Result<Vec<u8>> {
572+
let serialized = rmp_serde::to_vec_named(value)?;
573+
self.encode_value(serialized)
574+
}
575+
576+
fn deserialize_value<T: DeserializeOwned>(&self, value: &[u8]) -> Result<T> {
577+
let decoded = self.decode_value(value)?;
578+
Ok(rmp_serde::from_slice(&decoded)?)
579+
}
580+
581+
fn serialize_json(&self, value: &impl Serialize) -> Result<Vec<u8>> {
582+
let serialized = serde_json::to_vec(value)?;
583+
self.encode_value(serialized)
584+
}
585+
568586
fn deserialize_json<T: DeserializeOwned>(&self, data: &[u8]) -> Result<T> {
569587
let decoded = self.decode_value(data)?;
570588

@@ -594,20 +612,6 @@ pub(crate) trait EncryptableStore {
594612
err.into_inner().into()
595613
})
596614
}
597-
598-
fn deserialize_value<T: DeserializeOwned>(&self, value: &[u8]) -> Result<T> {
599-
let decoded = self.decode_value(value)?;
600-
Ok(rmp_serde::from_slice(&decoded)?)
601-
}
602-
603-
fn encode_key(&self, table_name: &str, key: impl AsRef<[u8]>) -> Key {
604-
let bytes = key.as_ref();
605-
if let Some(store_cipher) = self.get_cypher() {
606-
Key::Hashed(store_cipher.hash_key(table_name, bytes))
607-
} else {
608-
Key::Plain(bytes.to_owned())
609-
}
610-
}
611615
}
612616

613617
#[cfg(test)]

0 commit comments

Comments
 (0)