InMemoryEventIndex implements EventIndexer, EventIndex { @Override public Set<AggregateId> findAggregates(String parameterName, Object parameterValue) { return valueTable.getIndexed(ParameterNameAndValue.of(parameterName, parameterValue)) .stream().map(AggregateIdParameterName::getAggregateId).collect(Collectors.toSet()); } private  InMemoryEventIndex(InMemoryTimestampedTable<AggregateIdParameterName, ParameterNameAndValue, StreamTimestamp> valueTable); static InMemoryEventIndex create(); @Override Set<AggregateId> findAggregates(String parameterName, Object parameterValue); @Override void index(AggregateId aggregateId, StreamTimestamp timestamp, String parameterName, Object parameterValue);  }
@Test public void indexesAggregateIdsByPropertyValues() { index.accept(Event.of( id1, StreamTimestamp.now(), CREATED, eventSchema.make( name.of("Arthur Putey"), age.of(42), favouriteColour.of("orange")))); index.accept(Event.of( id2, StreamTimestamp.now(), CREATED, eventSchema.make( name.of("Arthur Daley"), age.of(31), favouriteColour.of("orange")))); assertThat(index.findAggregates("name", "Arthur Putey"), contains(id1)); assertThat(index.findAggregates("name", "Arthur Daley"), contains(id2)); assertThat(index.findAggregates("favouriteColour", "orange"), containsInAnyOrder(id1, id2)); }
@Test public void newerValuesOverwriteOlderValues() { StreamTimestamp now = StreamTimestamp.now(); StreamTimestamp earlier = now.minus(1, ChronoUnit.MILLIS); StreamTimestamp later = now.plus(1, ChronoUnit.MILLIS); index.accept(Event.of( id1, now, CREATED, eventSchema.make( name.of("Arthur Putey"), age.of(42), favouriteColour.of("orange")))); index.accept(Event.of( id1, earlier, CREATED, eventSchema.make( name.of("Arthur Daley"), age.of(42), favouriteColour.of("orange")))); assertThat(index.findAggregates("name", "Arthur Daley"), hasSize(0)); assertThat(index.findAggregates("name", "Arthur Putey"), contains(id1)); index.accept(Event.of( id1, later, CREATED, eventSchema.make( name.of("Arthur Mumby"), age.of(42), favouriteColour.of("orange")))); assertThat(index.findAggregates("name", "Arthur Putey"), hasSize(0)); assertThat(index.findAggregates("name", "Arthur Mumby"), contains(id1)); }