IndexRecordFilterHook implements RecordUpdateHook { void calculateIndexInclusion(String repositoryName, String table, Record oldRecord, Record newRecord, IndexRecordFilterData indexFilterData) { Set<String> applicableIndexes = Sets.newHashSet(); Set<String> nonApplicableIndexes = Sets.newHashSet(); for (IndexInfo indexInfo : indexesInfo.getIndexInfos()) { String queueSubscriptionId = indexInfo.getIndexDefinition().getSubscriptionId(); if (indexMatchesRepository(repositoryName, indexInfo) && indexIsApplicable(indexInfo.getLilyIndexerConf().getRecordFilter(), table, oldRecord, newRecord)) { applicableIndexes.add(queueSubscriptionId); } else { nonApplicableIndexes.add(queueSubscriptionId); } } if (applicableIndexes.isEmpty()) { indexFilterData.setSubscriptionExclusions(IndexRecordFilterData.ALL_INDEX_SUBSCRIPTIONS); } else if (nonApplicableIndexes.isEmpty()) { indexFilterData.setSubscriptionInclusions(IndexRecordFilterData.ALL_INDEX_SUBSCRIPTIONS); } else if (applicableIndexes.size() > nonApplicableIndexes.size()) { indexFilterData.setSubscriptionInclusions(applicableIndexes); } else { indexFilterData.setSubscriptionExclusions(nonApplicableIndexes); } } IndexRecordFilterHook(IndexesInfo indexesInfo);  IndexRecordFilterHook(PluginRegistry pluginRegistry, IndexesInfo indexesInfo); @PreDestroy void destroy(); @Override void beforeUpdate(Record record, Record originalRecord, Repository repository, FieldTypes fieldTypes,
            RecordEvent recordEvent); @Override void beforeCreate(Record newRecord, Repository repository, FieldTypes fieldTypes, RecordEvent recordEvent); @Override void beforeDelete(Record originalRecord, Repository repository, FieldTypes fieldTypes,
            RecordEvent recordEvent);  }
@Test public void testCalculateIndexInclusion_MoreInclusionsThanExclusions() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); IndexInfo inclusionA = createMockIndexInfo("includeA", true); IndexInfo inclusionB = createMockIndexInfo("includeB", true); IndexInfo exclusion = createMockIndexInfo("exclude", false); when(indexesInfo.getIndexInfos()).thenReturn(Lists.newArrayList(inclusionA, inclusionB, exclusion)); indexFilterHook.calculateIndexInclusion(RepoAndTableUtil.DEFAULT_REPOSITORY, Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionInclusions(ImmutableSet.of("includeA", "includeB")); }
@Test public void testCalculateIndexInclusion_MoreExclusionsThanInclusions() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); IndexInfo inclusion = createMockIndexInfo("include", true); IndexInfo exclusionA = createMockIndexInfo("excludeA", false); IndexInfo exclusionB = createMockIndexInfo("excludeB", false); when(this.indexesInfo.getIndexInfos()).thenReturn(Lists.newArrayList(inclusion, exclusionA, exclusionB)); indexFilterHook.calculateIndexInclusion(RepoAndTableUtil.DEFAULT_REPOSITORY, Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionExclusions(ImmutableSet.of("excludeA", "excludeB")); }
@Test public void testCalculateIndexInclusion_AllIndexesIncluded() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); IndexInfo inclusion = createMockIndexInfo("include", true); when(indexesInfo.getIndexInfos()).thenReturn(Lists.newArrayList(inclusion)); indexFilterHook.calculateIndexInclusion(RepoAndTableUtil.DEFAULT_REPOSITORY, Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionInclusions(IndexRecordFilterData.ALL_INDEX_SUBSCRIPTIONS); }
@Test public void testCalculateIndexInclusion_AllIndexesExcluded() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); IndexInfo inclusion = createMockIndexInfo("exclude", false); when(indexesInfo.getIndexInfos()).thenReturn(Lists.newArrayList(inclusion)); indexFilterHook.calculateIndexInclusion(RepoAndTableUtil.DEFAULT_REPOSITORY, Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionExclusions(IndexRecordFilterData.ALL_INDEX_SUBSCRIPTIONS); }
@Test public void testCalculateIndexInclusion_NoIndexSubscriptions() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); when(indexesInfo.getIndexInfos()).thenReturn(Lists.<IndexInfo>newArrayList()); indexFilterHook.calculateIndexInclusion(RepoAndTableUtil.DEFAULT_REPOSITORY, Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionExclusions(IndexRecordFilterData.ALL_INDEX_SUBSCRIPTIONS); }
@Test public void testCalculateIndexInclusion_RepoBasedExclusion() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); IndexInfo inclusionA = createMockIndexInfo("includeA", true); IndexInfo inclusionB = createMockIndexInfo("includeButNotInThisRepo", true); when(inclusionB.getRepositoryName()).thenReturn("someOtherRepo"); IndexInfo exclusion = createMockIndexInfo("exclude", false); when(indexesInfo.getIndexInfos()).thenReturn(Lists.newArrayList(inclusionA, inclusionB, exclusion)); indexFilterHook.calculateIndexInclusion(RepoAndTableUtil.DEFAULT_REPOSITORY, Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionExclusions(ImmutableSet.of("includeButNotInThisRepo", "exclude")); }
@Test public void testCalculateIndexInclusion_ForNonDefaultRepository() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); IndexInfo inclusionA = createMockIndexInfo("inclusionA", true); IndexInfo exclusion = createMockIndexInfo("excludeA", false); IndexInfo inclusionB = createMockIndexInfo("inclusionB", true); when(inclusionB.getRepositoryName()).thenReturn("someOtherRepo"); when(this.indexesInfo.getIndexInfos()).thenReturn(Lists.newArrayList(inclusionA, exclusion, inclusionB)); indexFilterHook.calculateIndexInclusion("someOtherRepo", Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionExclusions(ImmutableSet.of("inclusionA", "excludeA")); }
@Test public void testCalculateIndexInclusion_RepoBasedInclusion() { IndexRecordFilterData indexFilterData = mock(IndexRecordFilterData.class); IndexInfo inclusionA = createMockIndexInfo("inclusionA", true); IndexInfo exclusion = createMockIndexInfo("excludeA", false); IndexInfo inclusionB = createMockIndexInfo("inclusionB", true); ArrayList<IndexInfo> infos = Lists.newArrayList(inclusionA, exclusion, inclusionB); for (IndexInfo info : infos) { when(info.getRepositoryName()).thenReturn("someOtherRepo"); } when(this.indexesInfo.getIndexInfos()).thenReturn(infos); indexFilterHook.calculateIndexInclusion("someOtherRepo", Table.RECORD.name, oldRecord, newRecord, indexFilterData); verify(indexFilterData).setSubscriptionInclusions(ImmutableSet.of("inclusionA","inclusionB")); }