Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/entity/context_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,10 +376,17 @@ impl ContextEntitiesExt for Context {
}

fn index_property_counts<E: Entity, P: IndexableProperty<E>>(&mut self) {
let property_id = P::id();
let context_ptr: *const Context = self;
let property_store = self.entity_store.get_property_store_mut::<E>();
let current_index_type = property_store.get::<P>().index_type();
if current_index_type != PropertyIndexType::FullIndex {
property_store.set_property_indexed::<P>(PropertyIndexType::ValueCountIndex);
// SAFETY: This only creates a shared reference to `Context` while mutably borrowing
// the property store to update index internals.
unsafe {
property_store.index_unindexed_entities_for_property_id(&*context_ptr, property_id);
}
}
}

Expand Down
55 changes: 55 additions & 0 deletions src/entity/index/value_count_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,61 @@ mod tests {
);
}

#[test]
fn enabling_multi_property_value_count_index_indexes_existing_entities() {
let mut context = Context::new();

context
.add_entity(with!(Person, Age(1u8), Weight(2u8), Height(3u8)))
.unwrap();
context
.add_entity(with!(Person, Age(1u8), Weight(2u8), Height(3u8)))
.unwrap();
context
.add_entity(with!(Person, Age(3u8), Weight(2u8), Height(1u8)))
.unwrap();

context.index_property_counts::<Person, AWH>();

assert_eq!(
context.query_entity_count(with!(Person, Age(1u8), Weight(2u8), Height(3u8))),
2
);
assert_eq!(
context.query_entity_count(with!(Person, Age(3u8), Weight(2u8), Height(1u8))),
1
);
}

#[test]
fn enabling_value_count_index_indexes_existing_entities() {
let mut context = Context::new();

context.add_entity(with!(Person, Age(10))).unwrap();
context.add_entity(with!(Person, Age(10))).unwrap();

context.index_property_counts::<Person, Age>();

assert_eq!(context.query_entity_count(with!(Person, Age(10))), 2);
}

#[test]
fn changing_existing_entity_before_adding_another_does_not_double_count() {
let mut context = Context::new();

let first = context.add_entity(with!(Person, Age(10))).unwrap();
context.add_entity(with!(Person, Age(10))).unwrap();

context.index_property_counts::<Person, Age>();
context.set_property(first, Age(20));

// Adding another entity must not count the changed entity a second time.
context.add_entity(with!(Person, Age(10))).unwrap();

assert_eq!(context.query_entity_count(with!(Person, Age(20))), 1);
assert_eq!(context.query_entity_count(with!(Person, Age(10))), 2);
}

#[test]
fn test_index_value_compute_same_values() {
let value = one_shot_128(&"test value");
Expand Down