IndexFetchStrategy implements DataSourceStrategy { @Override public DataContainer process(DataSource dataSource, Map<String, String> tableAliases) { if (dataSource.getRightDataSource().isPresent()) { throw new IllegalArgumentException("Index fetch strategy can not process joins"); } if (!dataSource.getTableAlias().isPresent()) { throw new IllegalArgumentException("Index fetch strategy datasource should contain table alias"); } List<String> columnNames = dataSource.getColumns(); if (columnNames.isEmpty()) { throw new IllegalArgumentException("Column names are required for index fetch strategy"); } String tableAlias = dataSource.getTableAlias().get(); String tableName = tableAliases.get(tableAlias); IndexSignature indexSignature = new IndexSignature(tableName, new LinkedHashSet<>(columnNames)); Optional<Index> indexCandidate = indexStorage.get(indexSignature); if (!indexCandidate.isPresent()) { throw new IllegalStateException(String.format("Index for table \"%s\" and column names \"%s\" was not found", tableName, columnNames)); } Index index = indexCandidate.get(); DataContainer result = new DataContainer(Collections.singletonMap(tableAlias, columnNames)); index.getKeys().forEach(k -> { int numIds = index.get(k).size(); IntStream.rangeClosed(1, numIds).forEach(any -> result.addRow(k.getValues())); }); return result; } @Autowired  IndexFetchStrategy(IndexStorage indexStorage); @Override DataContainer process(DataSource dataSource, Map<String, String> tableAliases);  }
@Test public void testProcess() { DataSource dataSource = new DataSource(TABLE_ALIAS); dataSource.getColumns().addAll(COLUMNS); DataSourceStrategy strategy = getStrategy(); DataContainer result = strategy.process(dataSource, TABLE_ALIASES); assertThat(result.getColumnsMap().keySet(), contains(TABLE_ALIAS)); String[] columnsAsArray = COLUMNS.toArray(new String[COLUMNS.size()]); assertThat(result.getColumnsMap().get(TABLE_ALIAS), contains(columnsAsArray)); List<DataRow> rows = result.getRows(); assertThat(rows, hasSize(3)); Set<Object[]> rowsAsValues = rows.stream().map(dr -> dr.getValues().toArray()).collect(Collectors.toSet()); assertThat( rowsAsValues, containsInAnyOrder( COLUMN_VALUES.get(0), COLUMN_VALUES.get(0), COLUMN_VALUES.get(1)) ); }
@Test(expected = IllegalArgumentException.class) public void testNoTableAlias() { DataSource dataSource = new DataSource(new DataSource(TABLE_ALIAS)); assertThat(dataSource.getTableAlias().isPresent(), is(false)); DataSourceStrategy strategy = getStrategy(); strategy.process(dataSource, TABLE_ALIASES); }
@Test(expected = IllegalArgumentException.class) public void testJoinIsPresent() { DataSource dataSource = new DataSource(TABLE_ALIAS); dataSource.setRightDataSource(new DataSource("anything")); DataSourceStrategy strategy = getStrategy(); strategy.process(dataSource, TABLE_ALIASES); }
@Test(expected = IllegalArgumentException.class) public void testEmptyColumnNames() { DataSource dataSource = new DataSource(TABLE_ALIAS); assertThat(dataSource.getColumns(), is(empty())); DataSourceStrategy strategy = getStrategy(); strategy.process(dataSource, TABLE_ALIASES); }