Kodzik

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
10
Indexable
Never
@Async("importAuthorsExecutor")
    @Transactional
    public void importAuthors(BufferedReader reader, Progress progress) {

        progressService.setStatusTo(progress, ProgressStatus.RUNNING);
        try {
            AtomicInteger counter = new AtomicInteger(0);
            AtomicLong bytesImported = new AtomicLong(0);
            AtomicLong start = new AtomicLong(System.currentTimeMillis());
            reader.lines()
                    .peek(line -> bytesImported.set(bytesImported.get() + line.length()))
                    .map(CreateAuthorCommand::new)
                    .peek(c -> {
                        if (counter.incrementAndGet() % 10_000 == 0) {
                            progressService.updateProgress(progress, bytesImported);
                            entityManager.flush();
                            entityManager.clear();
                            long time = System.currentTimeMillis() - start.get();
                            log.info("Imported: {} in: {} ms", counter, time);
                            start.set(System.currentTimeMillis());
                        }

                    }).forEach(this::saveAuthor);
            progressService.setStatusTo(progress, ProgressStatus.FINISHED);
        } catch (Exception exc) {
            log.error("Exception during import", exc);
            progressService.setStatusTo(progress, ProgressStatus.ERROR);
        }
    }