You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OneToOneResilientIndexingListener.java 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.es;
  21. import com.google.common.collect.Multimap;
  22. import java.util.Collection;
  23. import java.util.List;
  24. import java.util.Objects;
  25. import java.util.function.Function;
  26. import java.util.stream.Collectors;
  27. import org.sonar.core.util.stream.MoreCollectors;
  28. import org.sonar.db.DbClient;
  29. import org.sonar.db.DbSession;
  30. import org.sonar.db.es.EsQueueDto;
  31. /**
  32. * Clean-up the db table "es_queue" when documents
  33. * are successfully indexed so that the recovery
  34. * daemon does not re-index them.
  35. *
  36. * This implementation assumes that one row in table es_queue
  37. * is associated to one index document and that es_queue.doc_id
  38. * equals document id.
  39. */
  40. public class OneToOneResilientIndexingListener implements IndexingListener {
  41. private final DbClient dbClient;
  42. private final DbSession dbSession;
  43. private final Multimap<DocId, EsQueueDto> itemsById;
  44. public OneToOneResilientIndexingListener(DbClient dbClient, DbSession dbSession, Collection<EsQueueDto> items) {
  45. this.dbClient = dbClient;
  46. this.dbSession = dbSession;
  47. this.itemsById = items.stream()
  48. .collect(MoreCollectors.index(i -> {
  49. IndexType.SimpleIndexMainType mainType = IndexType.parseMainType(i.getDocType());
  50. return new DocId(mainType.getIndex(), "_doc", i.getDocId());
  51. }, Function.identity()));
  52. }
  53. @Override
  54. public void onSuccess(List<DocId> successDocIds) {
  55. if (!successDocIds.isEmpty()) {
  56. Collection<EsQueueDto> itemsToDelete = successDocIds.stream()
  57. .map(itemsById::get)
  58. .flatMap(Collection::stream)
  59. .filter(Objects::nonNull)
  60. .toList();
  61. dbClient.esQueueDao().delete(dbSession, itemsToDelete);
  62. dbSession.commit();
  63. }
  64. }
  65. @Override
  66. public void onFinish(IndexingResult result) {
  67. // nothing to do, items that have been successfully indexed
  68. // are already deleted from db (see method onSuccess())
  69. }
  70. }