From 93f55cc58507800de5952d83eec8e480aa26e685 Mon Sep 17 00:00:00 2001 From: Kayo Phoenix Date: Sat, 27 Oct 2018 10:21:56 +0500 Subject: [PATCH] Indexing empty strings and binaries --- ledb/src/index.rs | 5 +---- ledb/src/lib.rs | 6 +++--- ledb/src/value.rs | 12 ++++++++++-- 3 files changed, 14 insertions(+), 9 deletions(-) diff --git a/ledb/src/index.rs b/ledb/src/index.rs index 3180a9b..1b9a373 100644 --- a/ledb/src/index.rs +++ b/ledb/src/index.rs @@ -460,10 +460,7 @@ fn extract_field_primitives(doc: &Value, typ: KeyType, keys: &mut HashSet { if let Some(val) = KeyData::from_val(&val) { if let Some(val) = val.into_type(typ) { - // prevent indexing empty values - if !val.is_empty() { - keys.insert(val.into_owned()); - } + keys.insert(val.into_owned()); } } } diff --git a/ledb/src/lib.rs b/ledb/src/lib.rs index adf6b5d..668fde9 100644 --- a/ledb/src/lib.rs +++ b/ledb/src/lib.rs @@ -583,9 +583,9 @@ mod tests { mk_index(&c).unwrap(); fill_data(&c).unwrap(); - assert_eq!(query!(find Value in c order by s >).unwrap().len(), 6); - assert_found!(query!(find in c order by s >), 3, 5, 6, 1, 2, 4); - assert_found!(query!(find in c order by s <), 4, 2, 1, 6, 5, 3); + assert_eq!(query!(find Value in c order by s >).unwrap().len(), 7); + assert_found!(query!(find in c order by s >), 7, 3, 5, 6, 1, 2, 4); + assert_found!(query!(find in c order by s <), 4, 2, 1, 6, 5, 3, 7); } #[test] diff --git a/ledb/src/value.rs b/ledb/src/value.rs index 697a826..1fd8f00 100644 --- a/ledb/src/value.rs +++ b/ledb/src/value.rs @@ -70,8 +70,16 @@ impl KeyData { match self { Int(val) => unsafe { transmute::<&i64, &[u8; 8]>(val) }, Float(val) => unsafe { transmute::<&f64, &[u8; 8]>(val) }, - String(val) => val.as_bytes(), - Binary(val) => val.as_slice(), + String(val) => if val.is_empty() { + b"\0" + } else { + val.as_bytes() + }, + Binary(val) => if val.is_empty() { + &[0u8] + } else { + val.as_slice() + }, Bool(val) => unsafe { transmute::<&bool, &[u8; 1]>(val) }, } }