From 22a4eaeb820f50d4aac63c8b98030e41525a3809 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Tue, 23 Sep 2014 13:49:15 +0200 Subject: [PATCH] SONAR-5531 - Optimized Synchronizer (no upsert) --- .../sonar/server/search/action/InsertDto.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/search/action/InsertDto.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/action/InsertDto.java b/server/sonar-server/src/main/java/org/sonar/server/search/action/InsertDto.java new file mode 100644 index 00000000000..107d04326a6 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/search/action/InsertDto.java @@ -0,0 +1,72 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.search.action; + +import org.elasticsearch.action.ActionRequest; +import org.elasticsearch.action.update.UpdateRequest; +import org.sonar.core.persistence.Dto; +import org.sonar.server.search.Index; + +import java.util.ArrayList; +import java.util.List; + +public class InsertDto extends IndexAction { + + private final DTO dto; + + public InsertDto(String indexType, DTO dto) { + this(indexType, dto, true); + } + + public InsertDto(String indexType, DTO dto, boolean requiresRefresh) { + super(indexType, requiresRefresh); + this.dto = dto; + } + + @Override + public String getKey() { + return dto.getKey().toString(); + } + + @Override + public Class getPayloadClass() { + return dto.getClass(); + } + + @Override + public List doCall(Index index) throws Exception { + List inserts = new ArrayList(); + List updates = index.getNormalizer().normalize(dto); + for (UpdateRequest update : updates) { + if (update.doc() != null) { + inserts.add(update.upsertRequest() + .index(index.getIndexName()) + .type(index.getIndexType()) + .id(update.id()) + .routing(update.routing())); + } else { + inserts.add(update + .index(index.getIndexName()) + .type(index.getIndexType())); + } + } + return inserts; + } +} -- 2.39.5