+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db;
-
-import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
-import java.io.Serializable;
-import java.sql.Timestamp;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.apache.ibatis.session.ResultContext;
-import org.sonar.api.utils.System2;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.db.Dao;
-import org.sonar.db.DbSession;
-import org.sonar.db.Dto;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.search.DbSynchronizationHandler;
-import org.sonar.server.search.IndexDefinition;
-import org.sonar.server.search.action.DeleteKey;
-import org.sonar.server.search.action.DeleteNestedItem;
-import org.sonar.server.search.action.InsertDto;
-import org.sonar.server.search.action.RefreshIndex;
-import org.sonar.server.search.action.UpsertDto;
-import org.sonar.server.search.action.UpsertNestedItem;
-
-import static com.google.common.collect.Maps.newHashMap;
-
-/**
- * naming convention for DAO
- * =========================
- * <p/>
- * The DAO manages a Business Domain for a set of DTO. There is a Main DTO (i.e. RuleDto)
- * that has a few nested/child DTOs. The DAO supports nested DTOs for 1-to-1 and 1-to-many
- * relations. Many-to-many relations are handled by their own DAO classes (i.e. ActiveRuleDao)
- * <p/>
- * Main DTO
- * -------------------------
- * <p/>
- * * GET Methods
- * - returns a single DTO
- * - DTO is fully loaded (no field will return null)
- * - returns null (and not empty)
- * - examples:
- * - RuleDto = ruleDao.getNullableByKey(dto.getKey());
- * <p/>
- * * FIND Methods
- * - returns a List of DTO.
- * - Returns an empty list id no match
- * - method name is FULLY-NOMINATIVE
- * - examples:
- * - List<RuleDto> rules findByQualityProfile(QualityProfile qprofile)
- * - List<RuleDto> rules findByQualityProfile(QualityProfileKey qprofileKey)
- * - List<RuleDto> rules findByQualityProfileAndCreatedAfter(QualityProfileKey qprofileKey, Date date)
- * <p/>
- * * CRUD Methods
- * - insert(DTO)
- * - udpate(DTO)
- * - delete(DTO)
- * <p/>
- * Nested DTO
- * -------------------------
- * <p/>
- * Some DAO implementations also manage nested DTO. RuleTag for example is managed by the RuleDao class
- * Nested DTO are accessible following a similar convention for the Main DTO:
- * <p/>
- * * GET Methods
- * - returns a single DTO
- * - DTO is fully loaded (no field will return null)
- * - returns null (and not empty)
- * - prefixed with DTO type
- * - examples:
- * - RuleTagDto = ruleDao.getTagByKey(tagDto.getKey());
- * <p/>
- * * FIND Methods
- * - returns a List of DTO.
- * - Returns an empty list id no match
- * - method name is FULLY-NOMINATIVE
- * - prefixed with DTO type
- * - examples:
- * - List<RuleTagDto> tags findRuleTagByRuleKey(RuleKey key)
- * - List<RuleTagDto> tags findRuleTagByRepositoryAndLanguage(RepositoryKey key, String language)
- * <p/>
- * * CRUD Methods are slightly different because they REQUIRE the main DTO to be valid
- * - Nested dto methods MUST have the Main DTO or it's key as param
- * - add
- * - remove
- * - update
- * - examples:
- * - RuleTagDto tag add(RuleTagDto tag, RuleKey key)
- * - RuleParamDto param add(RuleParamDto param, RuleDto rule)
- *
- * @param <MAPPER> iBatis Mapper class
- * @param <DTO> Produced DTO class from this dao
- * @param <KEY> DTO Key class
- */
-public abstract class BaseDao<MAPPER, DTO extends Dto<KEY>, KEY extends Serializable> implements DeprecatedDao<DTO, KEY>, Dao {
-
- private static final Logger LOGGER = Loggers.get(BaseDao.class);
-
- protected IndexDefinition indexDefinition;
- private Class<MAPPER> mapperClass;
- private System2 system2;
-
- protected boolean hasIndex() {
- return indexDefinition != null;
- }
-
- protected BaseDao(@Nullable IndexDefinition indexDefinition, Class<MAPPER> mapperClass, System2 system2) {
- this.mapperClass = mapperClass;
- this.indexDefinition = indexDefinition;
- this.system2 = system2;
- }
-
- public String getIndexType() {
- return indexDefinition != null ? this.indexDefinition.getIndexType() : null;
- }
-
- protected MAPPER mapper(DbSession session) {
- return session.getMapper(mapperClass);
- }
-
- @Override
- @CheckForNull
- public DTO getNullableByKey(DbSession session, KEY key) {
- return doGetNullableByKey(session, key);
- }
-
- @Override
- public DTO getByKey(DbSession session, KEY key) {
- DTO value = doGetNullableByKey(session, key);
- if (value == null) {
- throw new NotFoundException(String.format("Key '%s' not found", key));
- }
- return value;
- }
-
- @Override
- public DTO update(DbSession session, DTO item) {
- Date now = new Date(system2.now());
- update(session, item, now);
- return item;
- }
-
- @Override
- public DTO update(DbSession session, DTO item, DTO... others) {
- update(session, Lists.asList(item, others));
- return item;
- }
-
- @Override
- public Collection<DTO> update(DbSession session, Collection<DTO> items) {
- Date now = new Date(system2.now());
- for (DTO item : items) {
- update(session, item, now);
- }
- return items;
- }
-
- private void update(DbSession session, DTO item, Date now) {
- try {
- item.setUpdatedAt(now);
- doUpdate(session, item);
- if (hasIndex()) {
- session.enqueue(new UpsertDto<>(getIndexType(), item));
- }
- } catch (Exception e) {
- throw new IllegalStateException("Fail to update item in db: " + item, e);
- }
- }
-
- @Override
- public DTO insert(DbSession session, DTO item) {
- insert(session, item, new Date(system2.now()));
- return item;
- }
-
- @Override
- public Collection<DTO> insert(DbSession session, Collection<DTO> items) {
- Date now = new Date(system2.now());
- for (DTO item : items) {
- insert(session, item, now);
- }
- return items;
- }
-
- @Override
- public DTO insert(DbSession session, DTO item, DTO... others) {
- insert(session, Lists.asList(item, others));
- return item;
- }
-
- private void insert(DbSession session, DTO item, Date now) {
- if (item.getCreatedAt() == null) {
- item.setCreatedAt(now);
- }
- item.setUpdatedAt(now);
- try {
- doInsert(session, item);
- if (hasIndex()) {
- session.enqueue(new UpsertDto<>(getIndexType(), item));
- }
- } catch (Exception e) {
- throw new IllegalStateException("Fail to insert item in db: " + item, e);
- }
- }
-
- @Override
- public void delete(DbSession session, DTO item) {
- deleteByKey(session, item.getKey());
- }
-
- @Override
- public void delete(DbSession session, DTO item, DTO... others) {
- delete(session, Lists.asList(item, others));
- }
-
- @Override
- public void delete(DbSession session, Collection<DTO> items) {
- for (DTO item : items) {
- delete(session, item);
- }
- }
-
- @Override
- public void deleteByKey(DbSession session, KEY key) {
- Preconditions.checkNotNull(key, "Missing key");
- try {
- doDeleteByKey(session, key);
- if (hasIndex()) {
- session.enqueue(new DeleteKey<>(getIndexType(), key));
- }
- } catch (Exception e) {
- throw new IllegalStateException("Fail to delete item from db: " + key, e);
- }
- }
-
- protected final void enqueueUpdate(Object nestedItem, KEY key, DbSession session) {
- if (hasIndex()) {
- session.enqueue(new UpsertNestedItem<>(
- this.getIndexType(), key, nestedItem));
- }
- }
-
- public void enqueueDelete(Object nestedItem, KEY key, DbSession session) {
- if (hasIndex()) {
- session.enqueue(new DeleteNestedItem<>(
- this.getIndexType(), key, nestedItem));
- session.commit();
- }
- }
-
- public void enqueueInsert(Object nestedItem, KEY key, DbSession session) {
- if (hasIndex()) {
- this.enqueueUpdate(nestedItem, key, session);
- }
- }
-
- // Synchronization methods
-
- protected DbSynchronizationHandler getSynchronizationResultHandler(final DbSession session, Map<String, String> params) {
- return new DbSynchronizationHandler(session, params) {
- private int count = 0;
-
- @Override
- public void handleResult(ResultContext resultContext) {
- DTO dto = (DTO) resultContext.getResultObject();
- // session.enqueue(new UpsertDto<DTO>(getIndexType(), dto, false));
- getSession().enqueue(new InsertDto<>(getIndexType(), dto, false));
- count++;
- if (count % 100000 == 0) {
- LOGGER.info("Synchronized {} {}", count, getIndexType());
- }
- }
-
- @Override
- public void enqueueCollected() {
- // Do nothing in this case
- }
- };
- }
-
- protected Map<String, Object> getSynchronizationParams(@Nullable Date date, Map<String, String> params) {
- Map<String, Object> finalParams = newHashMap();
- if (date != null) {
- finalParams.put("date", new Timestamp(date.getTime()));
- }
- return finalParams;
- }
-
- @Override
- public void synchronizeAfter(final DbSession session) {
- this.synchronizeAfter(session, null, Collections.<String, String>emptyMap());
- }
-
- @Override
- public void synchronizeAfter(final DbSession session, @Nullable Date date) {
- this.synchronizeAfter(session, date, Collections.<String, String>emptyMap());
- }
-
- @Override
- public void synchronizeAfter(final DbSession session, @Nullable Date date, Map<String, String> params) {
- DbSynchronizationHandler handler = getSynchronizationResultHandler(session, params);
- session.select(getSynchronizeStatementFQN(), getSynchronizationParams(date, params), handler);
- handler.enqueueCollected();
- session.enqueue(new RefreshIndex(this.getIndexType()));
- session.commit();
- }
-
- private String getSynchronizeStatementFQN() {
- return mapperClass.getName() + "." + this.getSynchronizationStatementName();
- }
-
- @CheckForNull
- protected abstract DTO doGetNullableByKey(DbSession session, KEY key);
-
- protected String getSynchronizationStatementName() {
- return "selectAfterDate";
- }
-
- protected DTO doInsert(DbSession session, DTO item) {
- throw notImplemented(this);
- }
-
- protected DTO doUpdate(DbSession session, DTO item) {
- throw notImplemented(this);
- }
-
- protected void doDeleteByKey(DbSession session, KEY key) {
- throw notImplemented(this);
- }
-
- private static RuntimeException notImplemented(BaseDao baseDao) {
- throw new IllegalStateException("Not implemented yet for class [" + baseDao.getClass().getSimpleName() + "]");
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db;
-
-import java.io.Serializable;
-import java.util.Collection;
-import java.util.Date;
-import java.util.Map;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.sonar.db.DbSession;
-import org.sonar.db.Dto;
-
-public interface DeprecatedDao<DTO extends Dto<KEY>, KEY extends Serializable> {
-
- /**
- * Get a DTO by its key. Return <code>null</code> if the key does not exist.
- */
- @CheckForNull
- DTO getNullableByKey(DbSession session, KEY key);
-
- /**
- * Get a DTO by its key.
- *
- * @throws org.sonar.server.exceptions.NotFoundException if the key does not exist
- */
- DTO getByKey(DbSession session, KEY key);
-
- /**
- * Update a table row. DTO id must be set. The field updatedAt
- * is changed by this method.
- */
- DTO update(DbSession session, DTO dto);
-
- /**
- * Update one or more table rows. Note that the returned DTO is only
- * the first updated one.
- */
- DTO update(DbSession session, DTO dto, DTO... others);
-
- Collection<DTO> update(DbSession session, Collection<DTO> dtos);
-
- DTO insert(DbSession session, DTO dto);
-
- /**
- * Insert one or more database rows. Note
- * that the returned DTO is only the first inserted one.
- */
- DTO insert(DbSession session, DTO dto, DTO... others);
-
- Collection<DTO> insert(DbSession session, Collection<DTO> dtos);
-
- void delete(DbSession session, DTO dto);
-
- /**
- * Delete one or more table rows.
- */
- void delete(DbSession session, DTO dto, DTO... others);
-
- void delete(DbSession session, Collection<DTO> dtos);
-
- void deleteByKey(DbSession session, KEY key);
-
- void synchronizeAfter(final DbSession session);
-
- void synchronizeAfter(DbSession session, @Nullable Date date);
-
- void synchronizeAfter(DbSession session, @Nullable Date date, Map<String, String> params);
-
-}
import org.sonar.server.ruby.PlatformRackBridge;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.search.EsSearchModule;
-import org.sonar.server.search.IndexQueue;
import org.sonar.server.user.ThreadLocalUserSession;
public class PlatformLevel1 extends PlatformLevel {
// must instantiate deprecated class in 5.2 and only this one (and not its replacement)
// to avoid having two SqlSessionFactory instances
org.sonar.core.persistence.MyBatis.class,
- IndexQueue.class,
DatabaseServerCompatibility.class,
DatabaseVersion.class,
PurgeProfiler.class,
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import com.google.common.collect.ArrayListMultimap;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Multimap;
-import java.io.Serializable;
-import java.util.ArrayDeque;
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.NoSuchElementException;
-import java.util.Queue;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
-import org.elasticsearch.action.count.CountRequestBuilder;
-import org.elasticsearch.action.count.CountResponse;
-import org.elasticsearch.action.get.GetRequestBuilder;
-import org.elasticsearch.action.get.GetResponse;
-import org.elasticsearch.action.get.MultiGetItemResponse;
-import org.elasticsearch.action.get.MultiGetRequest;
-import org.elasticsearch.action.get.MultiGetRequestBuilder;
-import org.elasticsearch.action.get.MultiGetResponse;
-import org.elasticsearch.action.search.SearchRequestBuilder;
-import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.action.search.SearchScrollRequestBuilder;
-import org.elasticsearch.common.settings.ImmutableSettings;
-import org.elasticsearch.common.settings.Settings;
-import org.elasticsearch.common.unit.TimeValue;
-import org.elasticsearch.index.query.BoolFilterBuilder;
-import org.elasticsearch.index.query.FilterBuilder;
-import org.elasticsearch.index.query.FilterBuilders;
-import org.elasticsearch.index.query.QueryBuilder;
-import org.elasticsearch.index.query.QueryBuilders;
-import org.elasticsearch.search.SearchHit;
-import org.elasticsearch.search.aggregations.Aggregation;
-import org.elasticsearch.search.aggregations.AggregationBuilders;
-import org.elasticsearch.search.aggregations.Aggregations;
-import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
-import org.elasticsearch.search.aggregations.bucket.terms.Terms;
-import org.elasticsearch.search.aggregations.metrics.max.Max;
-import org.elasticsearch.search.aggregations.metrics.valuecount.InternalValueCount;
-import org.elasticsearch.search.fetch.source.FetchSourceContext;
-import org.joda.time.DateTime;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.db.Dto;
-import org.sonar.server.exceptions.NotFoundException;
-
-/**
- * @deprecated replaced by {@link org.sonar.server.es.BaseIndex}
- */
-@Deprecated
-public abstract class BaseIndex<DOMAIN, DTO extends Dto<KEY>, KEY extends Serializable>
- implements Index<DOMAIN, DTO, KEY> {
-
- private static final Logger LOG = Loggers.get(BaseIndex.class);
-
- public static final int SCROLL_TIME_IN_MINUTES = 3;
-
- private final SearchClient client;
- private final BaseNormalizer<DTO, KEY> normalizer;
- private final IndexDefinition indexDefinition;
-
- protected BaseIndex(IndexDefinition indexDefinition, BaseNormalizer<DTO, KEY> normalizer, SearchClient client) {
- this.normalizer = normalizer;
- this.client = client;
- this.indexDefinition = indexDefinition;
- }
-
- @Override
- public BaseNormalizer<DTO, KEY> getNormalizer() {
- return normalizer;
- }
-
- @Override
- public final String getIndexName() {
- return this.indexDefinition.getIndexName();
- }
-
- @Override
- public final String getIndexType() {
- return this.indexDefinition.getIndexType();
- }
-
- /* Component Methods */
-
- @Override
- public void start() {
- /* Setup the index if necessary */
- initializeIndex();
- }
-
- @Override
- public void stop() {
- // nothing to do
- }
-
- public SearchClient getClient() {
- return client;
- }
-
- // Scrolling within the index
- @Override
- public Iterator<DOMAIN> scroll(final String scrollId) {
- return new Iterator<DOMAIN>() {
-
- private final Queue<SearchHit> hits = new ArrayDeque<>();
-
- @Override
- public boolean hasNext() {
- if (hits.isEmpty()) {
- SearchScrollRequestBuilder esRequest = client.prepareSearchScroll(scrollId)
- .setScroll(TimeValue.timeValueMinutes(SCROLL_TIME_IN_MINUTES));
- Collections.addAll(hits, esRequest.get().getHits().getHits());
- }
- return !hits.isEmpty();
- }
-
- @Override
- public DOMAIN next() {
- if (!hasNext()) {
- throw new NoSuchElementException();
- }
- return toDoc(hits.poll().getSource());
- }
-
- @Override
- public void remove() {
- throw new UnsupportedOperationException("Cannot remove item from scroll");
- }
- };
- }
-
- /* Cluster And ES Stats/Client methods */
-
- protected void initializeIndex() {
- String index = this.getIndexName();
-
- IndicesExistsResponse indexExistsResponse = client.prepareIndicesExist(index).get();
- try {
-
- if (!indexExistsResponse.isExists()) {
- LOG.debug("Setup of {} for type {}", this.getIndexName(), this.getIndexType());
- client.prepareCreate(index)
- .setSettings(getIndexSettings())
- .get();
- }
-
- LOG.debug("Update of index {} for type {}", this.getIndexName(), this.getIndexType());
- client.preparePutMapping(index)
- .setType(getIndexType())
- .setIgnoreConflicts(true)
- .setSource(mapDomain())
- .get();
-
- } catch (Exception e) {
- throw new IllegalStateException("Invalid configuration for index " + this.getIndexName(), e);
- }
- }
-
- @Override
- public IndexStat getIndexStat() {
- CountRequestBuilder countRequest = client.prepareCount(this.getIndexName())
- .setTypes(this.getIndexType())
- .setQuery(QueryBuilders.matchAllQuery());
- CountResponse response = countRequest.get();
- return new IndexStat(getLastSynchronization(), response.getCount());
- }
-
- /* Synchronization methods */
-
- @Override
- @CheckForNull
- public Date getLastSynchronization() {
- return getLastSynchronization(Collections.<String, String>emptyMap());
- }
-
- @Override
- @CheckForNull
- public Date getLastSynchronization(Map<String, String> params) {
- SearchRequestBuilder request = client.prepareSearch(this.getIndexName())
- .setTypes(this.getIndexType())
- .setQuery(QueryBuilders.filteredQuery(
- QueryBuilders.matchAllQuery(),
- getLastSynchronizationBuilder(params)
- ))
- .setSize(0)
- .addAggregation(AggregationBuilders.max("latest")
- .field(BaseNormalizer.UPDATED_AT_FIELD));
-
- SearchResponse response = request.get();
-
- Max max = response.getAggregations().get("latest");
- if (max.getValue() > 0) {
- Date date = new DateTime((long) max.getValue()).toDate();
- LOG.debug("Index {}:{} has last update of {}", this.getIndexName(), this.getIndexType(), date);
- return date;
- } else {
- LOG.debug("Index {}:{} has no last update date", this.getIndexName(), this.getIndexType());
- return null;
- }
- }
-
- protected FilterBuilder getLastSynchronizationBuilder(Map<String, String> params) {
- return FilterBuilders.matchAllFilter();
- }
-
- /* Index management methods */
-
- protected abstract String getKeyValue(KEY key);
-
- public final Settings getIndexSettings() {
- ImmutableSettings.Builder settings = this.getBaseIndexSettings();
-
- // In case there is a replication factor set by the index,
- // it is removed since we're using global cluster state
- // see https://jira.sonarsource.com/browse/SONAR-5687
- settings.remove("index.number_of_replicas");
-
- return settings.build();
- }
-
- protected abstract Map mapProperties();
-
- protected abstract Map mapKey();
-
- protected Map mapDomain() {
- Map<String, Object> mapping = new HashMap<>();
- mapping.put("dynamic", false);
- mapping.put("_all", ImmutableMap.of("enabled", false));
- if (mapKey() != null) {
- mapping.put("_id", mapKey());
- }
- mapping.put("properties", mapProperties());
- LOG.debug("Index Mapping {}", mapping.get("properties"));
- return mapping;
- }
-
- protected Map mapField(IndexField field) {
- return mapField(field, true);
- }
-
- protected Map mapField(IndexField field, boolean allowRecursive) {
- if (field.type() == IndexField.Type.TEXT) {
- return mapTextField(field, allowRecursive);
- } else if (field.type() == IndexField.Type.STRING) {
- return mapStringField(field, allowRecursive);
- } else if (field.type() == IndexField.Type.BOOLEAN) {
- return mapBooleanField();
- } else if (field.type() == IndexField.Type.OBJECT) {
- return mapNestedField(field);
- } else if (field.type() == IndexField.Type.DATE) {
- return mapDateField();
- } else if (field.type() == IndexField.Type.DOUBLE) {
- return mapDoubleField();
- } else {
- throw new IllegalStateException("Mapping does not exist for type: " + field.type());
- }
- }
-
- protected Map mapDoubleField() {
- return ImmutableMap.of("type", "double");
- }
-
- protected Map mapBooleanField() {
- return ImmutableMap.of("type", "boolean");
- }
-
- protected Map mapNestedField(IndexField field) {
- Map<String, Object> mapping = new HashMap<>();
- mapping.put("type", "nested");
- mapping.put("dynamic", "true");
- Map<String, Object> mappings = new HashMap<>();
- for (IndexField nestedField : field.nestedFields()) {
- if (nestedField != null) {
- mappings.put(nestedField.field(), mapField(nestedField));
- }
- }
- mapping.put("properties", mappings);
- return mapping;
- }
-
- protected Map mapDateField() {
- return ImmutableMap.of(
- "type", "date",
- "format", "date_time");
- }
-
- protected boolean needMultiField(IndexField field) {
- return (field.type() == IndexField.Type.TEXT
- || field.type() == IndexField.Type.STRING)
- && (field.isSortable() || field.isSearchable());
- }
-
- protected Map mapGramsField() {
- return ImmutableMap.of(
- "type", "string",
- "index", "analyzed",
- "index_analyzer", "index_grams",
- "search_analyzer", "search_grams");
- }
-
- protected Map mapWordsField() {
- return ImmutableMap.of(
- "type", "string",
- "index", "analyzed",
- "index_analyzer", "index_words",
- "search_analyzer", "search_words");
- }
-
- protected Map mapMultiField(IndexField field) {
- Map<String, Object> mapping = new HashMap<>();
- if (field.isSortable()) {
- mapping.put(IndexField.SORT_SUFFIX, ImmutableMap.of(
- "type", "string",
- "index", "analyzed",
- "analyzer", "sortable"));
- }
- if (field.isSearchable()) {
- if (field.type() != IndexField.Type.TEXT) {
- mapping.put(IndexField.SEARCH_PARTIAL_SUFFIX, mapGramsField());
- }
- mapping.put(IndexField.SEARCH_WORDS_SUFFIX, mapWordsField());
- }
- mapping.put(field.field(), mapField(field, false));
- return mapping;
- }
-
- protected Map mapStringField(IndexField field, boolean allowRecursive) {
- Map<String, Object> mapping = new HashMap<>();
- // check if the field needs to be MultiField
- if (allowRecursive && needMultiField(field)) {
- mapping.put("type", "multi_field");
- mapping.put("fields", mapMultiField(field));
- } else {
- mapping.put("type", "string");
- mapping.put("index", "analyzed");
- mapping.put("index_analyzer", "keyword");
- mapping.put("search_analyzer", "whitespace");
- }
- return mapping;
- }
-
- protected Map mapTextField(IndexField field, boolean allowRecursive) {
- Map<String, Object> mapping = new HashMap<>();
- // check if the field needs to be MultiField
- if (allowRecursive && needMultiField(field)) {
- mapping.put("type", "multi_field");
- mapping.put("fields", mapMultiField(field));
- } else {
- mapping.put("type", "string");
- mapping.put("index", "analyzed");
- mapping.put("index_analyzer", "keyword");
- mapping.put("search_analyzer", "whitespace");
- }
- return mapping;
- }
-
- /* Base CRUD methods */
-
- protected abstract DOMAIN toDoc(Map<String, Object> fields);
-
- public DOMAIN getByKey(KEY key) {
- DOMAIN value = getNullableByKey(key);
- if (value == null) {
- throw new NotFoundException(String.format("Key '%s' not found", key));
- }
- return value;
- }
-
- @CheckForNull
- @Override
- public DOMAIN getNullableByKey(KEY key) {
- GetRequestBuilder request = client.prepareGet()
- .setType(this.getIndexType())
- .setIndex(this.getIndexName())
- .setId(this.getKeyValue(key))
- .setFetchSource(true)
- .setRouting(this.getKeyValue(key));
-
- GetResponse response = request.get();
-
- if (response.isExists()) {
- return toDoc(response.getSource());
- }
- return null;
- }
-
- public List<DOMAIN> getByKeys(Collection<KEY> keys) {
- if (keys.isEmpty()) {
- return Collections.emptyList();
- }
- List<DOMAIN> results = new ArrayList<>();
- MultiGetRequestBuilder request = client.prepareMultiGet()
- .setPreference("_local");
- for (KEY key : keys) {
- request.add(new MultiGetRequest
- .Item(getIndexName(), getIndexType(), getKeyValue(key))
- .routing(getKeyValue(key))
- .fetchSourceContext(FetchSourceContext.FETCH_SOURCE));
- }
-
- MultiGetResponse response = request.get();
- if (response.getResponses() != null) {
- for (MultiGetItemResponse item : response.getResponses()) {
- Map<String, Object> source = item.getResponse().getSource();
- if (source != null) {
- results.add(toDoc(source));
- }
- }
- }
- return results;
- }
-
- public Collection<DOMAIN> getByKeys(KEY... keys) {
- return getByKeys(ImmutableSet.copyOf(keys));
- }
-
- /* ES QueryHelper Methods */
-
- protected BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable Collection<String> values) {
- if (values != null && !values.isEmpty()) {
- BoolFilterBuilder valuesFilter = FilterBuilders.boolFilter();
- for (String value : values) {
- FilterBuilder valueFilter = FilterBuilders.termFilter(field, value);
- valuesFilter.should(valueFilter);
- }
- filter.must(valuesFilter);
- }
- return filter;
- }
-
- protected BoolFilterBuilder addTermFilter(BoolFilterBuilder filter, String field, @Nullable String value) {
- if (value != null && !value.isEmpty()) {
- filter.must(FilterBuilders.termFilter(field, value));
- }
- return filter;
- }
-
- public Long countAll() {
- return client.prepareCount(this.getIndexName())
- .setTypes(this.getIndexType())
- .get().getCount();
- }
-
- public Map<String, Long> countByField(IndexField indexField, FilterBuilder filter) {
- Map<String, Long> counts = new HashMap<>();
-
- SearchRequestBuilder request = client.prepareSearch(this.getIndexName())
- .setTypes(this.getIndexType())
- .setQuery(QueryBuilders.filteredQuery(
- QueryBuilders.matchAllQuery(),
- filter))
- .setSize(0)
- .addAggregation(AggregationBuilders
- .terms(indexField.field())
- .field(indexField.field())
- .order(Terms.Order.count(false))
- .size(Integer.MAX_VALUE)
- .minDocCount(0));
-
- SearchResponse response = request.get();
-
- Terms values =
- response.getAggregations().get(indexField.field());
-
- for (Terms.Bucket value : values.getBuckets()) {
- counts.put(value.getKey(), value.getDocCount());
- }
- return counts;
- }
-
- public Map<String, Long> countByField(IndexField indexField) {
- return countByField(indexField, FilterBuilders.matchAllFilter());
- }
-
- // Response helpers
- protected Multimap<String, FacetValue> processAggregations(Aggregations aggregations) {
- Multimap<String, FacetValue> stats = ArrayListMultimap.create();
- if (aggregations != null) {
- for (Aggregation aggregation : aggregations.asList()) {
- if (aggregation instanceof StringTerms) {
- for (Terms.Bucket value : ((Terms) aggregation).getBuckets()) {
- FacetValue facetValue = new FacetValue(value.getKey(), value.getDocCount());
- stats.put(aggregation.getName(), facetValue);
- }
- } else if (aggregation instanceof InternalValueCount) {
- InternalValueCount count = (InternalValueCount) aggregation;
- FacetValue facetValue = new FacetValue(count.getName(), count.getValue());
- stats.put(count.getName(), facetValue);
- }
- }
- }
- return stats;
- }
-
- private ImmutableSettings.Builder getBaseIndexSettings() {
- return ImmutableSettings.builder()
-
- .put("index.number_of_replicas", 0)
- .put("index.number_of_shards", 1)
-
- // Disallow dynamic mapping (too expensive)
- .put("index.mapper.dynamic", false)
-
- // Sortable text analyzer
- .put("index.analysis.analyzer.sortable.type", "custom")
- .put("index.analysis.analyzer.sortable.tokenizer", "keyword")
- .putArray("index.analysis.analyzer.sortable.filter", "trim", "lowercase")
-
- // Edge NGram index-analyzer
- .put("index.analysis.analyzer.index_grams.type", "custom")
- .put("index.analysis.analyzer.index_grams.tokenizer", "whitespace")
- .putArray("index.analysis.analyzer.index_grams.filter", "trim", "lowercase", "gram_filter")
-
- // Edge NGram search-analyzer
- .put("index.analysis.analyzer.search_grams.type", "custom")
- .put("index.analysis.analyzer.search_grams.tokenizer", "whitespace")
- .putArray("index.analysis.analyzer.search_grams.filter", "trim", "lowercase")
-
- // Word index-analyzer
- .put("index.analysis.analyzer.index_words.type", "custom")
- .put("index.analysis.analyzer.index_words.tokenizer", "standard")
- .putArray("index.analysis.analyzer.index_words.filter",
- "standard", "word_filter", "lowercase", "stop", "asciifolding", "porter_stem")
-
- // Word search-analyzer
- .put("index.analysis.analyzer.search_words.type", "custom")
- .put("index.analysis.analyzer.search_words.tokenizer", "standard")
- .putArray("index.analysis.analyzer.search_words.filter",
- "standard", "lowercase", "stop", "asciifolding", "porter_stem")
-
- // Edge NGram filter
- .put("index.analysis.filter.gram_filter.type", "edgeNGram")
- .put("index.analysis.filter.gram_filter.min_gram", 2)
- .put("index.analysis.filter.gram_filter.max_gram", 15)
- .putArray("index.analysis.filter.gram_filter.token_chars", "letter", "digit", "punctuation", "symbol")
-
- // Word filter
- .put("index.analysis.filter.word_filter.type", "word_delimiter")
- .put("index.analysis.filter.word_filter.generate_word_parts", true)
- .put("index.analysis.filter.word_filter.catenate_words", true)
- .put("index.analysis.filter.word_filter.catenate_numbers", true)
- .put("index.analysis.filter.word_filter.catenate_all", true)
- .put("index.analysis.filter.word_filter.split_on_case_change", true)
- .put("index.analysis.filter.word_filter.preserve_original", true)
- .put("index.analysis.filter.word_filter.split_on_numerics", true)
- .put("index.analysis.filter.word_filter.stem_english_possessive", true)
-
- // Path Analyzer
- .put("index.analysis.analyzer.path_analyzer.type", "custom")
- .put("index.analysis.analyzer.path_analyzer.tokenizer", "path_hierarchy")
-
- // UUID Module analyzer
- .put("index.analysis.tokenizer.dot_tokenizer.type", "pattern")
- .put("index.analysis.tokenizer.dot_tokenizer.pattern", "\\.")
- .put("index.analysis.analyzer.uuid_analyzer.type", "custom")
- .putArray("index.analysis.analyzer.uuid_analyzer.filter", "trim", "lowercase")
- .put("index.analysis.analyzer.uuid_analyzer.tokenizer", "dot_tokenizer");
-
- }
-
- protected StickyFacetBuilder stickyFacetBuilder(QueryBuilder query, Map<String, FilterBuilder> filters) {
- return new StickyFacetBuilder(query, filters);
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import org.elasticsearch.action.update.UpdateRequest;
-import org.sonar.db.DbClient;
-import org.sonar.db.Dto;
-
-public abstract class BaseNormalizer<DTO extends Dto<KEY>, KEY extends Serializable> {
-
- public static final String UPDATED_AT_FIELD = "updatedAt";
-
- protected final DbClient db;
-
- protected BaseNormalizer(DbClient db) {
- this.db = db;
- }
-
- protected Map<String, Object> getUpsertFor(Set<IndexField> fields, Map<String, Object> update) {
- Map<String, Object> upsert = new HashMap<>(update);
- for (IndexField field : fields) {
- if (!upsert.containsKey(field.field())) {
- if (field.type().equals(IndexField.Type.OBJECT)) {
- upsert.put(field.field(), new ArrayList<String>());
- } else {
- upsert.put(field.field(), null);
- }
- }
- }
- return upsert;
- }
-
- public List<UpdateRequest> deleteNested(Object object, KEY key) {
- throw new IllegalStateException("Nested Delete not implemented in current normalizer!");
- }
-
- public List<UpdateRequest> normalizeNested(Object object, KEY key) {
- throw new IllegalStateException("Nested Normalize not implemented in current normalizer!");
- }
-
- public abstract List<UpdateRequest> normalize(DTO dto);
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import org.apache.ibatis.session.ResultContext;
-import org.apache.ibatis.session.ResultHandler;
-import org.sonar.db.DbSession;
-
-import javax.annotation.CheckForNull;
-
-import java.util.Map;
-
-public abstract class DbSynchronizationHandler implements ResultHandler {
-
- private final DbSession session;
- private final Map<String, String> params;
-
- protected DbSynchronizationHandler(DbSession session, Map<String, String> params) {
- this.session = session;
- this.params = params;
- }
-
- @Override
- public abstract void handleResult(ResultContext context);
-
- @CheckForNull
- public abstract void enqueueCollected();
-
- public final DbSession getSession() {
- return session;
- }
-
- public final Map<String, String> getParams() {
- return params;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import org.picocontainer.Startable;
-import org.sonar.api.server.ServerSide;
-import org.sonar.db.Dto;
-
-import javax.annotation.CheckForNull;
-
-import java.io.Serializable;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.Map;
-
-@ServerSide
-public interface Index<DOMAIN, DTO extends Dto<KEY>, KEY extends Serializable> extends Startable {
-
- @CheckForNull
- DOMAIN getNullableByKey(KEY key);
-
- String getIndexType();
-
- String getIndexName();
-
- @CheckForNull
- Date getLastSynchronization();
-
- @CheckForNull
- Date getLastSynchronization(Map<String, String> params);
-
- IndexStat getIndexStat();
-
- Iterator<DOMAIN> scroll(String scrollId);
-
- BaseNormalizer<DTO, KEY> getNormalizer();
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import org.sonar.api.server.ServerSide;
-
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- * Facade for all Elasticsearch indices
- */
-@ServerSide
-public class IndexClient {
-
- private final Map<Class<?>, Index<?, ?, ?>> indexComponents;
-
- public IndexClient(Index<?, ?, ?>... indexComponents) {
-
- this.indexComponents = new HashMap<>();
-
- for (Index<?, ?, ?> indexComponent : indexComponents) {
- this.indexComponents.put(indexComponent.getClass(), indexComponent);
- }
- }
-
- public <K extends Index> K get(Class<K> clazz) {
- return (K) this.indexComponents.get(clazz);
- }
-
- public Collection<Index<?, ?, ?>> allIndices() {
- return indexComponents.values();
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import org.elasticsearch.action.ActionRequest;
-import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
-import org.elasticsearch.action.admin.indices.refresh.RefreshRequestBuilder;
-import org.elasticsearch.action.admin.indices.refresh.RefreshResponse;
-import org.elasticsearch.action.bulk.BulkRequestBuilder;
-import org.elasticsearch.action.bulk.BulkResponse;
-import org.elasticsearch.action.delete.DeleteRequest;
-import org.elasticsearch.action.index.IndexRequest;
-import org.elasticsearch.action.update.UpdateRequest;
-import org.sonar.api.server.ServerSide;
-import org.sonar.api.utils.log.Logger;
-import org.sonar.api.utils.log.Loggers;
-import org.sonar.db.deprecated.WorkQueue;
-import org.sonar.core.platform.ComponentContainer;
-import org.sonar.server.search.action.IndexAction;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-import java.util.concurrent.TimeUnit;
-
-@ServerSide
-public class IndexQueue implements WorkQueue<IndexAction<?>> {
-
- private final SearchClient searchClient;
- private final ComponentContainer container;
-
- private static final Logger LOGGER = Loggers.get(IndexQueue.class);
-
- private static final Integer CONCURRENT_NORMALIZATION_FACTOR = 1;
-
- public IndexQueue(SearchClient searchClient, ComponentContainer container) {
- this.searchClient = searchClient;
- this.container = container;
- }
-
- @Override
- public void enqueue(List<IndexAction<?>> actions) {
- if (actions.isEmpty()) {
- return;
- }
- boolean refreshRequired = false;
-
- Map<String, Index> indexes = getIndexMap();
- Set<String> indices = new HashSet<>();
- for (IndexAction action : actions) {
- Index index = indexes.get(action.getIndexType());
- action.setIndex(index);
- if (action.needsRefresh()) {
- refreshRequired = true;
- indices.add(index.getIndexName());
- }
- }
-
- BulkRequestBuilder bulkRequestBuilder = searchClient.prepareBulk();
-
- processActionsIntoQueries(bulkRequestBuilder, actions);
-
- if (bulkRequestBuilder.numberOfActions() > 0) {
- // execute the request
- BulkResponse response = bulkRequestBuilder.setRefresh(false).get();
-
- if (refreshRequired) {
- this.refreshRequiredIndex(indices);
- }
-
- if (response.hasFailures()) {
- throw new IllegalStateException("Errors while indexing stack: " + response.buildFailureMessage());
- }
- }
- }
-
- private void refreshRequiredIndex(Set<String> indices) {
- if (!indices.isEmpty()) {
- RefreshRequestBuilder refreshRequest = searchClient.prepareRefresh(indices.toArray(new String[indices.size()]))
- .setForce(false);
-
- RefreshResponse refreshResponse = refreshRequest.get();
-
- if (refreshResponse.getFailedShards() > 0) {
- LOGGER.warn("{} Shard(s) did not refresh", refreshResponse.getFailedShards());
- }
- }
- }
-
- private void processActionsIntoQueries(BulkRequestBuilder bulkRequestBuilder, List<IndexAction<?>> actions) {
- try {
- boolean hasInlineRefreshRequest = false;
- ExecutorService executorService = Executors.newFixedThreadPool(CONCURRENT_NORMALIZATION_FACTOR);
- // invokeAll() blocks until ALL tasks submitted to executor complete
- List<Future<List<? extends ActionRequest>>> requests = (List) executorService.invokeAll(actions, 20, TimeUnit.MINUTES);
- for (Future<List<? extends ActionRequest>> updates : requests) {
- for (ActionRequest update : updates.get()) {
-
- if (IndexRequest.class.isAssignableFrom(update.getClass())) {
- bulkRequestBuilder.add((IndexRequest) update);
- } else if (UpdateRequest.class.isAssignableFrom(update.getClass())) {
- bulkRequestBuilder.add((UpdateRequest) update);
- } else if (DeleteRequest.class.isAssignableFrom(update.getClass())) {
- bulkRequestBuilder.add((DeleteRequest) update);
- } else if (RefreshRequest.class.isAssignableFrom(update.getClass())) {
- hasInlineRefreshRequest = true;
- } else {
- throw new IllegalStateException("Un-managed request type: " + update.getClass());
- }
- }
- }
- executorService.shutdown();
- bulkRequestBuilder.setRefresh(hasInlineRefreshRequest);
- } catch (Exception e) {
- LOGGER.error("Could not execute normalization for stack", e);
- throw new IllegalStateException("Could not execute normalization for stack", e);
- }
- }
-
- private Map<String, Index> getIndexMap() {
- Map<String, Index> indexes = new HashMap<>();
- for (Index index : container.getComponentsByType(Index.class)) {
- indexes.put(index.getIndexType(), index);
- }
- return indexes;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import java.util.Date;
-
-public class IndexStat {
-
- private final Date lastUpdate;
- private final long documentCount;
-
- public IndexStat(@Nullable Date lastUpdate, long documentCount) {
- this.lastUpdate = lastUpdate;
- this.documentCount = documentCount;
- }
-
- @CheckForNull
- public Date getLastUpdate() {
- return lastUpdate;
- }
-
- public long getDocumentCount() {
- return documentCount;
- }
-}
public static final int DEFAULT_OFFSET = 0;
public static final int DEFAULT_LIMIT = 10;
public static final int MAX_LIMIT = 500;
- public static final boolean DEFAULT_FACET = false;
private int offset = DEFAULT_OFFSET;
private int limit = DEFAULT_LIMIT;
}
/**
- * Whether or not the search returns facets for the domain. Defaults to {@link #DEFAULT_FACET}
+ * Whether or not the search returns facets for the domain.
*/
public boolean isFacet() {
return !facets.isEmpty();
*/
package org.sonar.server.search;
-import com.google.common.base.Preconditions;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.elasticsearch.action.search.SearchResponse;
-import org.elasticsearch.search.SearchHit;
public class Result<K> {
private final List<K> hits;
private final Facets facets;
private final long total;
- private final String scrollId;
- private final BaseIndex<K, ?, ?> index;
public Result(SearchResponse response) {
- this(null, response);
- }
-
- public Result(@Nullable BaseIndex<K, ?, ?> index, SearchResponse response) {
- this.index = index;
- this.scrollId = response.getScrollId();
this.facets = new Facets(response);
this.total = (int) response.getHits().totalHits();
this.hits = new ArrayList<>();
- if (index != null) {
- for (SearchHit hit : response.getHits()) {
- this.hits.add(index.toDoc(hit.getSource()));
- }
- }
- }
-
- public Iterator<K> scroll() {
- Preconditions.checkState(scrollId != null, "Result is not scrollable. Please use QueryOptions.setScroll()");
- return index.scroll(scrollId);
}
public List<K> getHits() {
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.delete.DeleteRequest;
-import org.elasticsearch.client.Requests;
-import org.sonar.server.search.Index;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-public class DeleteKey<K extends Serializable> extends IndexAction<DeleteRequest> {
-
- private final K key;
-
- public DeleteKey(String indexType, K key) {
- super(indexType);
- this.key = key;
- }
-
- @Override
- public String getKey() {
- return key.toString();
- }
-
- @Override
- public List<DeleteRequest> doCall(Index index) {
- List<DeleteRequest> requests = new ArrayList<>();
- requests.add(Requests.deleteRequest(index.getIndexName())
- .id(getKey())
- .type(indexType)
- .refresh(needsRefresh()));
- return requests;
- }
-
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.update.UpdateRequest;
-import org.sonar.server.search.Index;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-public class DeleteNestedItem<K extends Serializable> extends IndexAction<UpdateRequest> {
-
- private final K key;
- private final Object item;
- private final Object[] items;
-
- public DeleteNestedItem(String indexType, K key, Object item, Object... items) {
- super(indexType);
- this.key = key;
- this.item = item;
- this.items = items;
- }
-
- @Override
- public String getKey() {
- return this.key.toString();
- }
-
- @Override
- public List<UpdateRequest> doCall(Index index) {
- List<UpdateRequest> updates = new ArrayList<>();
- updates.addAll(deleteItem(index, item, key));
- for (Object otherItem : items) {
- updates.addAll(deleteItem(index, otherItem, key));
- }
- return updates;
- }
-
- private List<UpdateRequest> deleteItem(Index index, Object item, K key) {
- List<UpdateRequest> updates = index.getNormalizer().deleteNested(item, key);
- for (UpdateRequest update : updates) {
- update.index(index.getIndexName())
- .type(index.getIndexType())
- .refresh(needsRefresh());
- }
- return updates;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.sonar.db.deprecated.ClusterAction;
-import org.sonar.server.search.Index;
-
-import java.util.List;
-
-public abstract class IndexAction<K extends ActionRequest> implements ClusterAction<List<K>> {
-
- public static final String MISSING_INDEX_EXCEPTION = "Cannot execute request on null index";
-
- protected final String indexType;
- private final boolean requiresRefresh;
- private Index index;
-
- protected IndexAction(String indexType) {
- this(indexType, true);
- }
-
- protected IndexAction(String indexType, boolean requiresRefresh) {
- this.indexType = indexType;
- this.requiresRefresh = requiresRefresh;
- }
-
- public abstract String getKey();
-
- public String getIndexType() {
- return indexType;
- }
-
- public IndexAction<K> setIndex(Index index) {
- this.index = index;
- return this;
- }
-
- @Override
- public final List<K> call() throws IllegalStateException {
- if (index == null) {
- throw new IllegalStateException(MISSING_INDEX_EXCEPTION);
- }
- return doCall(index);
- }
-
- public abstract List<K> doCall(Index index);
-
- public boolean needsRefresh() {
- return this.requiresRefresh;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.Dto;
-import org.sonar.server.search.Index;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class InsertDto<DTO extends Dto> extends IndexAction<ActionRequest> {
-
- private final DTO dto;
-
- public InsertDto(String indexType, DTO dto, boolean requiresRefresh) {
- super(indexType, requiresRefresh);
- this.dto = dto;
- }
-
- @Override
- public String getKey() {
- return dto.getKey().toString();
- }
-
- @Override
- public List<ActionRequest> doCall(Index index) {
- List<ActionRequest> inserts = new ArrayList<>();
- List<UpdateRequest> 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;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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 com.google.common.collect.ImmutableList;
-import java.util.List;
-import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
-import org.sonar.server.search.Index;
-
-public class RefreshIndex extends IndexAction<RefreshRequest> {
-
- public RefreshIndex(String indexType) {
- super(indexType);
- }
-
- @Override
- public String getKey() {
- throw new IllegalStateException("Refresh Action has no key");
- }
-
- @Override
- public List<RefreshRequest> doCall(Index index) {
- return ImmutableList.of(
- new RefreshRequest()
- .indices(index.getIndexName()));
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.update.UpdateRequest;
-import org.sonar.db.Dto;
-import org.sonar.server.search.Index;
-
-import java.util.List;
-
-public class UpsertDto<DTO extends Dto> extends IndexAction<UpdateRequest> {
-
- private final DTO dto;
-
- public UpsertDto(String indexType, DTO dto) {
- this(indexType, dto, true);
- }
-
- public UpsertDto(String indexType, DTO dto, boolean requiresRefresh) {
- super(indexType, requiresRefresh);
- this.dto = dto;
- }
-
- @Override
- public String getKey() {
- return dto.getKey().toString();
- }
-
- @Override
- public List<UpdateRequest> doCall(Index index) {
- List<UpdateRequest> updates = index.getNormalizer().normalize(dto);
- for (UpdateRequest update : updates) {
- update.index(index.getIndexName())
- .type(index.getIndexType())
- .refresh(needsRefresh());
- }
- return updates;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.update.UpdateRequest;
-import org.sonar.server.search.Index;
-
-import java.io.Serializable;
-import java.util.ArrayList;
-import java.util.List;
-
-public class UpsertNestedItem<K extends Serializable> extends IndexAction<UpdateRequest> {
-
- private final K key;
- private final Object item;
- private final Object[] items;
-
- public UpsertNestedItem(String indexType, K key, Object item, Object... items) {
- super(indexType);
- this.key = key;
- this.item = item;
- this.items = items;
- }
-
- @Override
- public String getKey() {
- return key.toString();
- }
-
- @Override
- public List<UpdateRequest> doCall(Index index) {
- List<UpdateRequest> updates = new ArrayList<>();
- updates.addAll(normalizeItem(index, item, key));
- for (Object otherItem : items) {
- updates.addAll(normalizeItem(index, otherItem, key));
- }
- return updates;
- }
-
- private List<UpdateRequest> normalizeItem(Index index, Object item, K key) {
- List<UpdateRequest> updates = index.getNormalizer().normalizeNested(item, key);
- for (UpdateRequest update : updates) {
- update.index(index.getIndexName())
- .type(index.getIndexType())
- .refresh(needsRefresh());
- }
- return updates;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.
- */
-@ParametersAreNonnullByDefault
-package org.sonar.server.search.action;
-
-import javax.annotation.ParametersAreNonnullByDefault;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
ComponentDbTester componentDb = new ComponentDbTester(db);
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ComponentFinder underTest = new ComponentFinder(db.getDbClient());
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db;
-
-import java.util.Date;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.experimental.categories.Category;
-import org.sonar.api.utils.System2;
-import org.sonar.core.util.Uuids;
-import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
-import org.sonar.db.MyBatis;
-import org.sonar.server.db.fake.FakeDao;
-import org.sonar.server.db.fake.FakeDto;
-import org.sonar.server.db.fake.FakeMapper;
-import org.sonar.test.DbTests;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-@Category(DbTests.class)
-public class BaseDaoTest {
-
- @Rule
- public DbTester db = DbTester.createForSchema(System2.INSTANCE, BaseDaoTest.class, "schema.sql");
-
- private static final String DTO_ALIAS = "fake";
-
- private FakeDao dao;
- private DbSession session;
-
- @Before
- public void setupBatis() {
- MyBatis batis = db.myBatis();
- batis.getSessionFactory().getConfiguration().getTypeAliasRegistry().registerAlias(DTO_ALIAS, FakeDto.class);
- batis.getSessionFactory().getConfiguration().addMapper(FakeMapper.class);
- }
-
- @Before
- public void before() {
- this.session = db.myBatis().openSession(false);
- this.dao = new FakeDao(System2.INSTANCE);
- }
-
- @After
- public void after() {
- this.session.close();
- db.executeUpdateSql("TRUNCATE TABLE fake");
- }
-
- @Test
- public void has_fake_mapper() {
- FakeMapper mapper = db.myBatis().getSessionFactory()
- .getConfiguration().getMapper(FakeMapper.class, session);
- assertThat(mapper).isNotNull();
- }
-
- @Test
- public void can_insert_and_select_by_key() {
- long t0 = System.currentTimeMillis() - 1000;
-
- String key = Uuids.create();
- FakeDto myDto = new FakeDto()
- .setKey(key);
- dao.insert(session, myDto);
-
- session.commit();
- assertThat(myDto.getId()).isGreaterThan(0);
-
- long t1 = System.currentTimeMillis() + 1000;
-
- FakeDto dto = dao.getByKey(session, key);
- assertThat(dto).isNotNull();
-
- assertThat(dto.getUpdatedAt().getTime()).isGreaterThan(t0);
- assertThat(dto.getCreatedAt().getTime()).isLessThan(t1);
- }
-
- @Test
- public void does_enqueue_on_insert() {
- FakeDto myDto = new FakeDto()
- .setKey(Uuids.create());
- dao.insert(session, myDto);
- session.commit();
- assertThat(session.getActionCount()).isEqualTo(1);
- }
-
- @Test
- public void synchronize_to_es_after_date() {
- long t0 = System.currentTimeMillis() - 1000;
-
- String key = Uuids.create();
- FakeDto myDto = new FakeDto()
- .setKey(key);
- dao.insert(session, myDto);
-
- session.commit();
- assertThat(session.getActionCount()).isEqualTo(1);
-
- dao.synchronizeAfter(session, new Date(t0));
- // Synchronize adds an implicit action to the queue before finishing.
- assertThat(session.getActionCount()).isEqualTo(3);
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.fake;
-
-import org.sonar.api.utils.System2;
-import org.sonar.db.DbSession;
-import org.sonar.server.db.BaseDao;
-import org.sonar.server.search.IndexDefinition;
-
-public class FakeDao extends BaseDao<FakeMapper, FakeDto, String> {
-
- public FakeDao(System2 system2) {
- super(IndexDefinition.createFor("test", "fake"), FakeMapper.class, system2);
- }
-
- @Override
- protected FakeDto doInsert(DbSession session, FakeDto item) {
- mapper(session).insert(item);
- return item;
- }
-
- @Override
- protected FakeDto doGetNullableByKey(DbSession session, String key) {
- return mapper(session).selectByKey(key);
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.fake;
-
-import org.sonar.db.Dto;
-
-public class FakeDto extends Dto<String> {
-
- private long id;
- private String key;
-
- @Override
- public String getKey() {
- return key;
- }
-
- public long getId() {
- return id;
- }
-
- public FakeDto setId(long id) {
- this.id = id;
- return this;
- }
-
- public FakeDto setKey(String key) {
- this.key = key;
- return this;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.fake;
-
-import org.apache.ibatis.annotations.Param;
-
-import javax.annotation.Nullable;
-
-import java.sql.Timestamp;
-import java.util.List;
-
-public interface FakeMapper {
-
- void insert(FakeDto dto);
-
- FakeDto selectByKey(@Param("key") String key);
-
- List<FakeDto> selectAfterDate(@Nullable @Param("date") Timestamp date);
-}
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ComponentDao componentDao = db.getDbClient().componentDao();
import org.sonar.server.issue.index.IssueIndexer;
import org.sonar.server.permission.PermissionChange;
import org.sonar.server.permission.PermissionUpdater;
-import org.sonar.server.search.IndexClient;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
DbClient db;
- IndexClient indexClient;
DbSession session;
IssueCommentService service;
public void setUp() {
tester.clearDbAndIndexes();
db = tester.get(DbClient.class);
- indexClient = tester.get(IndexClient.class);
session = db.openSession(false);
service = tester.get(IssueCommentService.class);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsTester ws;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsTester ws;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsTester ws;
ComponentDto defaultProject;
@ClassRule
public static EsTester es = new EsTester().addDefinitions(new UserIndexDefinition(new Settings()));
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
System2 system = mock(System2.class);
WsTester ws;
public DbTester db = DbTester.create(System2.INSTANCE);
ComponentDbTester componentDb = new ComponentDbTester(db);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsActionTester ws = new WsActionTester(new ComponentAction(dbClient, new ComponentFinder(dbClient), userSession));
public DbTester db = DbTester.create(System2.INSTANCE);
ComponentDbTester componentDb = new ComponentDbTester(db);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsActionTester ws = new WsActionTester(
new ComponentTreeAction(
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsTester ws;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
MetricDao metricDao;
WsTester ws;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsTester ws;
@Before
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsTester ws;
ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
private PermissionUpdater permissionUpdater;
private ArgumentCaptor<PermissionChange> permissionChangeCaptor = ArgumentCaptor.forClass(PermissionChange.class);
- private DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
@Before
public void setUp() {
WsActionTester ws;
I18nRule i18n = new I18nRule();
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ResourceTypesRule resourceTypes = new ResourceTypesRule();
SearchProjectPermissionsDataLoader dataLoader;
public DbTester db = DbTester.create(System2.INSTANCE);
ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsActionTester ws;
TemplateUsersAction underTest;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.issue.index.IssueAuthorizationIndexer;
-import org.sonar.server.permission.PermissionFinder;
import org.sonar.server.permission.PermissionService;
import org.sonar.server.permission.ws.PermissionDependenciesFinder;
import org.sonar.server.tester.UserSessionRule;
dbSession = db.getSession();
PermissionRepository repository = new PermissionRepository(dbClient, new Settings());
- PermissionFinder permissionFinder = new PermissionFinder(dbClient);
ComponentFinder componentFinder = new ComponentFinder(dbClient);
PermissionService permissionService = new PermissionService(dbClient, repository, issueAuthorizationIndexer, userSession, componentFinder);
PermissionDependenciesFinder permissionDependenciesFinder = new PermissionDependenciesFinder(dbClient, componentFinder, new UserGroupFinder(dbClient), resourceTypes);
WsActionTester ws;
I18nRule i18n = new I18nRule();
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ResourceTypes resourceTypes = mock(ResourceTypes.class);
SearchTemplatesDataLoader dataLoader;
WsTester ws;
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ResourceType resourceType;
@Before
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ResourceType resourceType;
DbClient dbClient = db.getDbClient();
- DbSession session = db.getSession();
+ final DbSession session = db.getSession();
QualityProfileDao qualityProfileDao = dbClient.qualityProfileDao();
@Before
public void before() {
- session = dbClient.openSession(false);
backuper = mock(QProfileBackuper.class);
db.truncateTables();
// TODO remove mock
private QProfileLoader profileLoader = mock(QProfileLoader.class);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
+ final DbClient dbClient = db.getDbClient();
+ final DbSession dbSession = db.getSession();
private QualityProfileDao qualityProfileDao = dbClient.qualityProfileDao();
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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;
-
-import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
-import org.sonar.api.config.Settings;
-import org.sonar.process.ProcessProperties;
-import org.sonar.server.es.EsServerHolder;
-
-import java.io.Serializable;
-import java.util.Collections;
-import java.util.Map;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class BaseIndexTest {
-
- @ClassRule
- public static TemporaryFolder temp = new TemporaryFolder();
-
- SearchClient searchClient;
-
- @Before
- public void setup() {
- EsServerHolder holder = EsServerHolder.get();
- Settings settings = new Settings();
- settings.setProperty(ProcessProperties.CLUSTER_ACTIVATE, false);
- settings.setProperty(ProcessProperties.CLUSTER_NAME, holder.getClusterName());
- settings.setProperty(ProcessProperties.CLUSTER_NODE_NAME, holder.getNodeName());
- settings.setProperty(ProcessProperties.SEARCH_PORT, String.valueOf(holder.getPort()));
- settings.setProperty(ProcessProperties.SEARCH_HOST, String.valueOf(holder.getHostName()));
- searchClient = new SearchClient(settings);
- searchClient.start();
- }
-
- @After
- public void tearDown() {
- if (searchClient != null) {
- searchClient.stop();
- }
- }
-
- @Test
- public void can_load() {
- BaseIndex index = getIndex(searchClient);
- assertThat(index).isNotNull();
- }
-
- @Test
- public void creates_domain_index() {
- BaseIndex index = getIndex(this.searchClient);
-
- IndicesExistsResponse indexExistsResponse = index.getClient().nativeClient().admin().indices()
- .prepareExists(IndexDefinition.TEST.getIndexName()).execute().actionGet();
-
- assertThat(indexExistsResponse.isExists()).isTrue();
- }
-
- @Test
- public void settings_has_no_replication_factor() {
- BaseIndex index = getIndex(this.searchClient);
-
- // base case, there are no replication factors.
- assertThat(index.getIndexSettings().get("index.number_of_replicas")).isNull();
-
- // replication factor removed from settings when set in index
- BaseIndex newIndex = new BaseIndex(
- IndexDefinition.TEST,
- null, searchClient) {
-
- @Override
- protected String getKeyValue(Serializable key) {
- return null;
- }
-
- @Override
- protected Map mapProperties() {
- return Collections.emptyMap();
- }
-
- @Override
- protected Map mapKey() {
- return Collections.emptyMap();
- }
-
- @Override
- public Object toDoc(Map fields) {
- return null;
- }
- };
- newIndex.start();
-
- assertThat(index.getIndexSettings().get("index.number_of_replicas")).isNull();
-
- }
-
- private BaseIndex getIndex(final SearchClient searchClient) {
- BaseIndex index = new BaseIndex(
- IndexDefinition.TEST,
- null, searchClient) {
- @Override
- protected String getKeyValue(Serializable key) {
- return null;
- }
-
- @Override
- protected Map mapProperties() {
- return Collections.emptyMap();
- }
-
- @Override
- protected Map mapKey() {
- return Collections.emptyMap();
- }
-
- @Override
- public Object toDoc(Map fields) {
- return null;
- }
- };
- index.start();
- return index;
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.delete.DeleteRequest;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.server.search.Index;
-import org.sonar.server.search.IndexDefinition;
-
-import java.util.List;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class DeleteKeyTest {
-
- IndexDefinition TEST_INDEX = IndexDefinition.createFor("TEST", "TESTING");
-
- Index index;
-
- @Before
- public void setUp() {
- index = mock(Index.class);
- when(index.getIndexName()).thenReturn(TEST_INDEX.getIndexName());
- }
-
- @Test
- public void get_delete_request() {
- String key = "test_key";
- DeleteKey<String> deleteAction = new DeleteKey<>(TEST_INDEX.getIndexType(), key);
-
- try {
- deleteAction.call();
- fail();
- } catch (Exception e) {
- assertThat(e.getMessage()).isEqualTo(IndexAction.MISSING_INDEX_EXCEPTION);
- }
-
- // Insert Index for action
- deleteAction.setIndex(index);
-
- List<DeleteRequest> requests = deleteAction.call();
- assertThat(requests).hasSize(1);
-
- DeleteRequest request = requests.get(0);
- assertThat(request.type()).isEqualTo(TEST_INDEX.getIndexType());
- assertThat(request.index()).isEqualTo(TEST_INDEX.getIndexName());
- assertThat(request.id()).isEqualTo(key);
- assertThat(request.refresh()).isTrue();
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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 java.util.List;
-import org.elasticsearch.action.admin.indices.refresh.RefreshRequest;
-import org.junit.Before;
-import org.junit.Test;
-import org.sonar.server.search.Index;
-import org.sonar.server.search.IndexDefinition;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.fail;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
-
-public class RefreshIndexTest {
- IndexDefinition TEST_INDEX = IndexDefinition.createFor("TEST", "TESTING");
-
- Index index;
-
- @Before
- public void setUp() {
- index = mock(Index.class);
- when(index.getIndexName()).thenReturn(TEST_INDEX.getIndexName());
- }
-
- @Test
- public void get_delete_request() {
- RefreshIndex refreshAction = new RefreshIndex(TEST_INDEX.getIndexType());
-
- try {
- refreshAction.call();
- fail();
- } catch (Exception e) {
- assertThat(e.getMessage()).isEqualTo(IndexAction.MISSING_INDEX_EXCEPTION);
- }
-
- try {
- refreshAction.getKey();
- fail();
- } catch (Exception e) {
- assertThat(e.getMessage()).isEqualTo("Refresh Action has no key");
- }
-
- // Insert Index for action
- refreshAction.setIndex(index);
-
- List<RefreshRequest> requests = refreshAction.call();
- assertThat(requests).hasSize(1);
-
- RefreshRequest request = requests.get(0);
- assertThat(request.indices()).containsOnly(TEST_INDEX.getIndexName());
- }
-}
public DbTester db = DbTester.create(System2.INSTANCE);
UserDbTester userDb = new UserDbTester(db);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsTester ws;
UserIndex index;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
UserDbTester userDb = new UserDbTester(db);
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
WsActionTester ws = new WsActionTester(new SearchAction(dbClient, userSession));
package org.sonar.core.persistence;
import org.sonar.db.Database;
-import org.sonar.db.deprecated.WorkQueue;
/**
* Kept for backward compatibility of plugins/libs (like sonar-license) that are directly calling classes from the core
@Deprecated
public class MyBatis extends org.sonar.db.MyBatis {
- public MyBatis(Database database, WorkQueue<?> queue) {
- super(database, queue);
+ public MyBatis(Database database) {
+ super(database);
}
}
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
-import org.sonar.db.deprecated.ClusterAction;
-import org.sonar.db.deprecated.WorkQueue;
public class BatchSession extends DbSession {
private final int batchSize;
private int count = 0;
- public BatchSession(WorkQueue<?> queue, SqlSession session) {
- this(queue, session, MAX_BATCH_SIZE);
+ public BatchSession(SqlSession session) {
+ this(session, MAX_BATCH_SIZE);
}
- BatchSession(WorkQueue<?> queue, SqlSession session, int batchSize) {
- super(queue, session);
+ BatchSession(SqlSession session, int batchSize) {
+ super(session);
this.batchSize = batchSize;
}
- @Override
- public void enqueue(ClusterAction action) {
- increment();
- super.enqueue(action);
- }
-
@Override
public void select(String statement, Object parameter, ResultHandler handler) {
reset();
package org.sonar.db;
import java.sql.Connection;
-import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.executor.BatchResult;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.apache.ibatis.session.SqlSession;
-import org.sonar.db.deprecated.ClusterAction;
-import org.sonar.db.deprecated.WorkQueue;
public class DbSession implements SqlSession {
- private List<ClusterAction> actions;
-
private SqlSession session;
private int actionCount;
- public DbSession(WorkQueue queue, SqlSession session) {
+ public DbSession(SqlSession session) {
this.actionCount = 0;
this.session = session;
- this.actions = new ArrayList<>();
- }
-
- /**
- * @deprecated since 5.5, not used anymore
- */
- @Deprecated
- public void enqueue(ClusterAction action) {
- actionCount++;
- this.actions.add(action);
- }
-
- /**
- * @deprecated since 5.5, not used anymore
- */
- @Deprecated
- public int getActionCount() {
- return actionCount;
}
@Override
public void commit() {
session.commit();
- actions.clear();
}
@Override
public void commit(boolean force) {
session.commit(force);
- actions.clear();
}
/**
import org.sonar.db.dashboard.WidgetPropertyDto;
import org.sonar.db.dashboard.WidgetPropertyMapper;
import org.sonar.db.debt.RequirementMigrationDto;
-import org.sonar.db.deprecated.WorkQueue;
import org.sonar.db.duplication.DuplicationMapper;
import org.sonar.db.duplication.DuplicationUnitDto;
import org.sonar.db.event.EventDto;
private final Database database;
private SqlSessionFactory sessionFactory;
- private WorkQueue<?> queue;
- public MyBatis(Database database, WorkQueue<?> queue) {
+ public MyBatis(Database database) {
this.database = database;
- this.queue = queue;
}
// FIXME should be visible only to DAOs -> to be moved to AbstractDao
public DbSession openSession(boolean batch) {
if (batch) {
SqlSession session = sessionFactory.openSession(ExecutorType.BATCH);
- return new BatchSession(queue, session);
+ return new BatchSession(session);
}
SqlSession session = sessionFactory.openSession(ExecutorType.REUSE);
- return new DbSession(queue, session);
+ return new DbSession(session);
}
/**
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.deprecated;
-
-import java.util.concurrent.Callable;
-
-public interface ClusterAction<K> extends Callable<K> {
-
- @Override
- K call() throws Exception;
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.deprecated;
-
-import java.util.List;
-
-public class NullQueue implements WorkQueue<ClusterAction> {
-
- @Override
- public void enqueue(List<ClusterAction> actions) {
- // do nothing
- }
-}
+++ /dev/null
-/*
- * SonarQube
- * Copyright (C) 2009-2016 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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.db.deprecated;
-
-import java.util.List;
-import java.util.concurrent.Callable;
-
-public interface WorkQueue<K extends Callable> {
-
- void enqueue(List<K> actions);
-
-}
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
-import org.sonar.db.deprecated.ClusterAction;
-import org.sonar.db.deprecated.WorkQueue;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.mock;
@Test
public void shouldCommitWhenReachingBatchSize() {
SqlSession mybatisSession = mock(SqlSession.class);
- WorkQueue<?> queue = mock(WorkQueue.class);
- BatchSession session = new BatchSession(queue, mybatisSession, 10);
+ BatchSession session = new BatchSession(mybatisSession, 10);
for (int i = 0; i < 9; i++) {
session.insert("id" + i);
@Test
public void shouldCommitWhenReachingBatchSizeWithoutCommits() {
SqlSession mybatisSession = mock(SqlSession.class);
- WorkQueue<?> queue = mock(WorkQueue.class);
- BatchSession session = new BatchSession(queue, mybatisSession, 10);
-
- ClusterAction action = new ClusterAction() {
- @Override
- public Object call() throws Exception {
- return null;
- }
- };
+ BatchSession session = new BatchSession(mybatisSession, 10);
for (int i = 0; i < 9; i++) {
- session.enqueue(action);
+ session.delete("delete something");
verify(mybatisSession, never()).commit();
verify(mybatisSession, never()).commit(anyBoolean());
}
- session.enqueue(action);
+ session.delete("delete something");
verify(mybatisSession).commit();
session.close();
}
@Test
public void shouldResetCounterAfterCommit() {
SqlSession mybatisSession = mock(SqlSession.class);
- WorkQueue<?> queue = mock(WorkQueue.class);
- BatchSession session = new BatchSession(queue, mybatisSession, 10);
+ BatchSession session = new BatchSession(mybatisSession, 10);
for (int i = 0; i < 35; i++) {
session.insert("id" + i);
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.sonar.db.deprecated.WorkQueue;
import org.sonar.db.rule.RuleMapper;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
-import static org.mockito.Mockito.mock;
public class MyBatisTest {
private static H2Database database;
- private WorkQueue<?> queue = mock(WorkQueue.class);
@BeforeClass
public static void start() {
@Test
public void shouldConfigureMyBatis() {
- MyBatis myBatis = new MyBatis(database, queue);
+ MyBatis myBatis = new MyBatis(database);
myBatis.start();
Configuration conf = myBatis.getSessionFactory().getConfiguration();
@Test
public void shouldOpenBatchSession() {
- MyBatis myBatis = new MyBatis(database, queue);
+ MyBatis myBatis = new MyBatis(database);
myBatis.start();
SqlSession session = myBatis.openSession(false);
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
-import org.sonar.db.deprecated.NullQueue;
import org.sonar.db.dialect.H2;
/**
commands = DatabaseCommands.forDialect(db.getDialect());
tester = new DataSourceDatabaseTester(db.getDataSource(), commands.useLoginAsSchema() ? login : null);
- myBatis = new MyBatis(db, new NullQueue());
+ myBatis = new MyBatis(db);
myBatis.start();
}
}
public DbTester db = DbTester.create(System2.INSTANCE);
ComponentDbTester componentDb = new ComponentDbTester(db);
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ComponentDao underTest = new ComponentDao();
DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
ComponentDao underTest = new ComponentDao();
import org.sonar.api.resources.Scopes;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
-import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.test.DbTests;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
ComponentDbTester componentDb = new ComponentDbTester(db);
- DbClient dbClient = db.getDbClient();
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
SnapshotDao underTest = db.getDbClient().snapshotDao();
assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentId(1L).setSort(BY_DATE, DESC)).get(0).getId()).isEqualTo(3L);
assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setScope(Scopes.PROJECT).setQualifier(Qualifiers.PACKAGE))).extracting("id").containsOnly(1L);
- assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setScope(Scopes.DIRECTORY).setQualifier(Qualifiers.PACKAGE))).extracting("id").containsOnly(2L, 3L, 4L, 5L, 6L);
+ assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setScope(Scopes.DIRECTORY).setQualifier(Qualifiers.PACKAGE))).extracting("id").containsOnly(
+ 2L, 3L, 4L, 5L, 6L);
assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("ABCD"))).hasSize(3);
assertThat(underTest.selectSnapshotsByQuery(db.getSession(), new SnapshotQuery().setComponentUuid("UNKOWN"))).isEmpty();
newSnapshotForProject(project).setCreatedAt(5L),
newSnapshotForProject(project).setCreatedAt(2L),
newSnapshotForProject(project).setCreatedAt(1L)
- );
+ );
dbSession.commit();
SnapshotDto dto = underTest.selectOldestSnapshot(dbSession, project.getId());
db.getSession().commit();
assertThat(dto.getId()).isNotNull();
- db.assertDbUnit(getClass(), "insert-result.xml", new String[]{"id"}, "snapshots");
+ db.assertDbUnit(getClass(), "insert-result.xml", new String[] {"id"}, "snapshots");
}
@Test
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- DbSession dbSession = db.getSession();
+ final DbSession dbSession = db.getSession();
DuplicationDao dao = db.getDbClient().duplicationDao();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- private DbClient dbClient = db.getDbClient();
- private DbSession dbSession = db.getSession();
+ final DbClient dbClient = db.getDbClient();
+ final DbSession dbSession = db.getSession();
MeasureDao underTest = dbClient.measureDao();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- DbSession session = db.getSession();
+ final DbSession session = db.getSession();
MeasureFilterDao underTest = db.getDbClient().measureFilterDao();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- DbSession session = db.getSession();
+ final DbSession session = db.getSession();
MeasureFilterFavouriteDao underTest = db.getDbClient().measureFilterFavouriteDao();
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- DbSession session = db.getSession();
+ final DbSession session = db.getSession();
PermissionDao underTest = new PermissionDao(db.myBatis());
@Rule
public ExpectedException expectedException = ExpectedException.none();
- DbSession session = db.getSession();
+ final DbSession session = db.getSession();
DbClient dbClient = db.getDbClient();
PermissionTemplateDao underTest = new PermissionTemplateDao(db.myBatis(), system);
import org.junit.experimental.categories.Category;
import org.sonar.api.utils.DateUtils;
import org.sonar.api.utils.System2;
-import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.test.DbTests;
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
- DbSession dbSession = db.getSession();
- DbClient dbClient = db.getDbClient();
+ final DbSession dbSession = db.getSession();
System2 system2 = mock(System2.class);
GroupDao underTest = new GroupDao(system2);
DbClient dbClient = db.getDbClient();
UserDao underTest = db.getDbClient().userDao();
- DbSession session = db.getSession();
+ final DbSession session = db.getSession();
@Before
public void setUp() throws Exception {