From cde0238d623be856e840c50c33d7d72ef7f6baf3 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Wed, 8 May 2019 15:18:30 +0200 Subject: [PATCH] get sorting of query result right --- server/texture-sync-server/src/model/mod.rs | 38 +++++++++++++++++-- .../texture-sync-server/src/model/sha256.rs | 2 +- .../src/model/texture_format.rs | 2 +- server/texture-sync-server/src/search/mod.rs | 6 ++- 4 files changed, 40 insertions(+), 8 deletions(-) diff --git a/server/texture-sync-server/src/model/mod.rs b/server/texture-sync-server/src/model/mod.rs index c7b4af4..28f59f4 100644 --- a/server/texture-sync-server/src/model/mod.rs +++ b/server/texture-sync-server/src/model/mod.rs @@ -10,14 +10,17 @@ pub use date::Date; mod texture_format; pub use texture_format::TextureFormat; -#[derive(Eq, PartialEq, Clone, Serialize, Deserialize, Debug, Hash)] +#[derive(Eq, PartialEq, PartialOrd, Ord, Clone, Serialize, Deserialize, Debug, Hash)] pub struct Texture { - pub id: String, + // Mainly sort by: (Ord is derived.) pub name: String, - pub tags: Vec, - pub format: TextureFormat, pub added_on: Date, + // other attributes + pub id: String, + pub tags: Vec, pub resolution: (u64, u64), + // + pub format: TextureFormat, pub texture_hash: Sha256, } @@ -47,3 +50,30 @@ impl From for ProtocolError { } } } + +#[cfg(test)] +mod test { + use super::*; + use std::str::FromStr; + + /// just a shorthand + fn tex(id: i32, name: &str, tags: &str, added_on: &str, resolution: u64) -> Texture { + Texture { + id: format!("{}", id), // Id should actaly be a uuid, but for testing this is fine. + name: name.to_string(), + tags: tags.split(",").map(|s| s.trim().to_string()).collect(), + added_on: Date::from_str(added_on).unwrap(), + resolution: (resolution, resolution), + format: TextureFormat::JPEG, + texture_hash: Sha256::from_data(b"Some Hash"), + } + } + + #[test] + fn textures_are_sorted_by_name() { + let tex1 = tex(1, "AAAAA", "Z", "2019-05-15", 2); + let tex2 = tex(0, "B", "A", "2000-01-11", 1); + + assert!(tex1 < tex2); + } +} diff --git a/server/texture-sync-server/src/model/sha256.rs b/server/texture-sync-server/src/model/sha256.rs index 5ee440d..cbb0181 100644 --- a/server/texture-sync-server/src/model/sha256.rs +++ b/server/texture-sync-server/src/model/sha256.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; -#[derive(Clone, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)] +#[derive(Clone, Debug, PartialOrd, Ord, Deserialize, Serialize, Eq, Hash, PartialEq)] pub struct Sha256(#[serde(serialize_with = "as_hex", deserialize_with = "from_hex")] pub [u8; 32]); fn hash_to_hex_string(hash: &[u8; 32]) -> String { diff --git a/server/texture-sync-server/src/model/texture_format.rs b/server/texture-sync-server/src/model/texture_format.rs index 5729af7..2579471 100644 --- a/server/texture-sync-server/src/model/texture_format.rs +++ b/server/texture-sync-server/src/model/texture_format.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; -#[derive(Clone, Debug, Deserialize, Serialize, Eq, Hash, PartialEq)] +#[derive(Clone, Debug, Deserialize, Serialize, Eq, Hash, PartialEq, PartialOrd, Ord)] pub enum TextureFormat { #[serde(rename = "png")] PNG, diff --git a/server/texture-sync-server/src/search/mod.rs b/server/texture-sync-server/src/search/mod.rs index c133618..636a38c 100644 --- a/server/texture-sync-server/src/search/mod.rs +++ b/server/texture-sync-server/src/search/mod.rs @@ -36,11 +36,13 @@ impl Query { } if score >= required_score { - results.push((score, texture)) + // multiply by -1 to get order right. + results.push((score * -1, texture)) } } - results.sort_by_key(|(score, _)| score * -1); + // We can sort unstable, since at least the IDs are different. + results.sort_unstable(); results .iter()