diff --git a/src/message.rs b/src/message.rs index 335fd5a..d6f15eb 100644 --- a/src/message.rs +++ b/src/message.rs @@ -3847,6 +3847,7 @@ impl Oid { } } +#[derive(Debug)] struct Index { data: HashMap>, unique: bool, @@ -3867,6 +3868,18 @@ impl Index { } } + fn internal_add(&mut self, field: &Field, oid: Oid) { + let storage = match self.data.get_mut(field) { + Some(data) => data, + None => { + let data = HashSet::new(); + self.data.insert(field.clone(), data); + self.data.get_mut(field).unwrap() + } + }; + storage.insert(oid); + } + fn add(&mut self, field: Field, oid: Oid) -> Result<(), MTTError> { let oids = match self.data.get_mut(&field) { Some(data) => data, @@ -3952,7 +3965,9 @@ impl Indexes { } fn pull(&self, field_id: &Uuid, calc: &Calculation) -> HashSet { - self.get_index(field_id).pull(calc) + println!("{:?}", field_id); + println!("{:?}", self.data); + self.data.get(field_id).unwrap().pull(calc) } fn add_to_index(&mut self, field_name: &Uuid, field: Field, oid: Oid) { @@ -3981,6 +3996,10 @@ impl Indexes { } Ok(()) } + + fn iter_mut(&mut self) -> impl Iterator { + self.data.iter_mut() + } } #[cfg(test)] @@ -4285,7 +4304,11 @@ impl DocumentFile { while self.docs.contains_key(&oid) { oid = Oid::new(); } - self.docs.insert(oid, holder.clone()); + self.docs.insert(oid.clone(), holder.clone()); + for (field_id, oids) in self.indexes.iter_mut() { + let value = holder.get(field_id).unwrap(); + oids.internal_add(value, oid.clone()); + } records.insert(holder); } records.into() @@ -4420,6 +4443,7 @@ impl DocumentFile { let mut oids: HashSet = self.docs.keys().cloned().collect(); for (field_id, calculation) in indexed.iter() { let holder = self.indexes.pull(field_id, calculation); + println!("{:?}", holder); oids = oids.intersection(&holder).cloned().collect(); } for (field_id, calculation) in unindexed.iter() { @@ -4863,37 +4887,36 @@ mod document_files { } } - /* #[test] fn gets_all_documents_in_query() { - let mut doc = TestDocument::new([FieldType::Integer].to_vec()); - doc.start(); - let values = [ - [1.into()].to_vec(), - [2.into()].to_vec(), - [1.into()].to_vec(), - [3.into()].to_vec(), - [1.into()].to_vec(), - [4.into()].to_vec(), - [1.into()].to_vec(), - [5.into()].to_vec(), - ]; - for value in values.iter() { - doc.populate(value.clone()); + let mut test_doc = TestDocument::new([FieldType::Integer].to_vec()); + test_doc.start(); + let queue = test_doc.get_queue(); + let data = 1; + let count = 5; + for i in 0..count { + let holder: i128 = (i + 5).try_into().unwrap(); + test_doc.populate([holder.into()].to_vec()); + test_doc.populate([data.into()].to_vec()); } let mut calc = Calculation::new(Operand::Equal); - calc.add_value(1); + calc.add_value(data.clone()); let mut query = Query::new(); - query.add("field0".to_string(), calc); - doc.send(query).unwrap(); - let result = doc.get_receiver().recv_timeout(TIMEOUT).unwrap(); + query.add(Name::english("field0".to_string()), calc); + queue.send(Message::new(test_doc.get_docdef().get_document_name(), query)); + let result = test_doc.get_receiver().recv_timeout(TIMEOUT).unwrap(); let action = result.get_action(); - let input: Field = 1.into(); match action { - MsgAction::Reply(data) => { - assert_eq!(data.len(), 4, "should return 4 entries"); - for doc in data.iter() { - assert_eq!(doc.get_field("field0").unwrap(), input); + MsgAction::Records(docs) => { + assert_eq!( + docs.len(), + count, + "should return one entry containing {:?} got:\n{:?}", + data, + action + ); + for doc in docs.clone() { + assert_eq!(doc.get(&Name::english("field0".to_string())).unwrap(), data.into()); } } _ => unreachable!("got {:?}: should have been a reply", action), @@ -4917,21 +4940,21 @@ mod document_files { let mut query = Query::new(); let mut calc = Calculation::new(Operand::Equal); calc.add_value("a"); - query.add("field0".to_string(), calc); + query.add(Name::english("field0".to_string()), calc); let mut calc = Calculation::new(Operand::Equal); calc.add_value("b"); - query.add("field1".to_string(), calc); + query.add(Name::english("field1".to_string()), calc); doc.send(query).unwrap(); let result = doc.get_receiver().recv_timeout(TIMEOUT).unwrap(); let action = result.get_action(); match action { - MsgAction::Reply(data) => { + MsgAction::Records(data) => { let afield: Field = "a".into(); let bfield: Field = "b".into(); assert_eq!(data.len(), 1, "should return one entry:\n{:?}", action); - for doc in data.iter() { - assert_eq!(doc.get_field("field0").unwrap(), afield); - assert_eq!(doc.get_field("field1").unwrap(), bfield); + for doc in data.clone() { + assert_eq!(doc.get(&Name::english("field0".to_string())).unwrap(), afield); + assert_eq!(doc.get(&Name::english("field1".to_string())).unwrap(), bfield); } } _ => unreachable!("got {:?}: should have been a reply", action), @@ -4943,8 +4966,8 @@ mod document_files { let mut doc = TestDocument::new([FieldType::StaticString, FieldType::StaticString].to_vec()); let docdef = doc.get_docdef_mut(); - docdef.add_index("field0".to_string(), IndexType::Index); - docdef.add_index("field1".to_string(), IndexType::Index); + docdef.add_index(&Name::english("field0".to_string()), IndexType::Index); + docdef.add_index(&Name::english("field1".to_string()), IndexType::Index); doc.start(); let values = [ ["a".into(), "a".into()].to_vec(), @@ -4958,27 +4981,28 @@ mod document_files { let mut query = Query::new(); let mut calc = Calculation::new(Operand::Equal); calc.add_value("a"); - query.add("field0".to_string(), calc); + query.add(Name::english("field0".to_string()), calc); let mut calc = Calculation::new(Operand::Equal); calc.add_value("b"); - query.add("field1".to_string(), calc); + query.add(Name::english("field1".to_string()), calc); doc.send(query).unwrap(); let result = doc.get_receiver().recv_timeout(TIMEOUT).unwrap(); let action = result.get_action(); match action { - MsgAction::Reply(data) => { + MsgAction::Records(data) => { let afield: Field = "a".into(); let bfield: Field = "b".into(); assert_eq!(data.len(), 1, "should return one entry:\n{:?}", action); - for doc in data.iter() { - assert_eq!(doc.get_field("field0").unwrap(), afield); - assert_eq!(doc.get_field("field1").unwrap(), bfield); + for doc in data.clone() { + assert_eq!(doc.get(&Name::english("field0".to_string())).unwrap(), afield); + assert_eq!(doc.get(&Name::english("field1".to_string())).unwrap(), bfield); } } _ => unreachable!("got {:?}: should have been a reply", action), } } + /* #[test] fn query_should_work_with_mixed_inexed_fields() { let mut doc =