Written task to java. It uses postgresql, spring boot, rest. Everything is simple, you need to read the data from the database, an object with two fields of ID and a name, and then pass it in response to json. Here is checking it one of the errors was the inefficient use of RAM.
One of the conditions is not to use SQL capabilities. Also in the database by the condition of more than a million records.
What to do to fix? I will not throw off the controller yet, because it seems that the problem is on the lower layers, but if I have to lay it out right away.
My code is:
@Repository public class ContactRepositoryImpl implements ContactRepository { private static final BeanPropertyRowMapper<Contact> ROW_MAPPER = BeanPropertyRowMapper.newInstance(Contact.class); JdbcTemplate jdbcTemplate; @Autowired public ContactRepositoryImpl(@Qualifier("dataSource") DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } public List<Contact> getAll() { System.out.println(jdbcTemplate.toString()); return jdbcTemplate.query("SELECT * FROM contacts", ROW_MAPPER); } } @Service public class ContactServiceImpl implements ContactService { @Autowired private ContactRepository repository; @Override public List<Contact> getFilteredContacts(String nameFilter) { Pattern pattern = Pattern.compile(nameFilter); List<Contact> list = repository.getAll(); List<Contact> list2 = list.stream() .filter(Objects::nonNull) .filter(it -> it.getName() != null) .filter(it -> !pattern.matcher(it.getName()).matches()) .collect(Collectors.toList()); return list2; } }