Skip to content

Commit

Permalink
SONAR-6259 Persist components in db
Browse files Browse the repository at this point in the history
  • Loading branch information
julienlancelot committed May 21, 2015
1 parent 303f3f2 commit a27d5ef
Show file tree
Hide file tree
Showing 15 changed files with 1,279 additions and 138 deletions.
Expand Up @@ -136,6 +136,10 @@ public List<String> apply(List<String> partition) {
}); });
} }


public List<ComponentDto> selectComponentsFromProjectUuid(DbSession session, String projectKey) {
return mapper(session).selectComponentsFromProjectUuid(projectKey);
}

public List<ComponentDto> selectByKeys(DbSession session, Collection<String> keys) { public List<ComponentDto> selectByKeys(DbSession session, Collection<String> keys) {
return mapper(session).selectByKeys(keys); return mapper(session).selectByKeys(keys);
} }
Expand All @@ -153,24 +157,6 @@ public ComponentDto selectNullableByKey(DbSession session, String key) {
return mapper(session).selectByKey(key); return mapper(session).selectByKey(key);
} }


public void insert(DbSession session, ComponentDto item) {
mapper(session).insert(item);
}

public void insert(DbSession session, Collection<ComponentDto> items) {
for (ComponentDto item : items) {
insert(session, item);
}
}

public void insert(DbSession session, ComponentDto item, ComponentDto... others) {
insert(session, Lists.asList(item, others));
}

public List<String> selectProjectUuids(DbSession session) {
return mapper(session).selectProjectUuids();
}

public List<UuidWithProjectUuidDto> selectAllViewsAndSubViews(DbSession session) { public List<UuidWithProjectUuidDto> selectAllViewsAndSubViews(DbSession session) {
return mapper(session).selectUuidsForQualifiers(Qualifiers.VIEW, Qualifiers.SUBVIEW); return mapper(session).selectUuidsForQualifiers(Qualifiers.VIEW, Qualifiers.SUBVIEW);
} }
Expand Down Expand Up @@ -221,6 +207,24 @@ private void addProjectQualifier(Map<String, String> parameters) {
parameters.put("qualifier", Qualifiers.PROJECT); parameters.put("qualifier", Qualifiers.PROJECT);
} }


public void insert(DbSession session, ComponentDto item) {
mapper(session).insert(item);
}

public void insert(DbSession session, Collection<ComponentDto> items) {
for (ComponentDto item : items) {
insert(session, item);
}
}

public void insert(DbSession session, ComponentDto item, ComponentDto... others) {
insert(session, Lists.asList(item, others));
}

public void update(DbSession session, ComponentDto item) {
mapper(session).update(item);
}

private ComponentMapper mapper(DbSession session) { private ComponentMapper mapper(DbSession session) {
return session.getMapper(ComponentMapper.class); return session.getMapper(ComponentMapper.class);
} }
Expand Down
@@ -0,0 +1,55 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.computation.component;

public class Component {

private Long id;
private String uuid;
private String key;

public Long getId() {
return id;
}

public Component setId(Long id) {
this.id = id;
return this;
}

public String getKey() {
return key;
}

public Component setKey(String key) {
this.key = key;
return this;
}

public String getUuid() {
return uuid;
}

public Component setUuid(String uuid) {
this.uuid = uuid;
return this;
}
}
@@ -0,0 +1,44 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.computation.step;

import org.sonar.batch.protocol.output.BatchReportReader;
import org.sonar.server.computation.component.Component;

import java.util.HashMap;
import java.util.Map;

/**
* To be finished
*/
public class ComponentsCache {

private final Map<Integer, Component> componentsByRef;

public ComponentsCache(final BatchReportReader reader) {
this.componentsByRef = new HashMap<>();
}

public Component getComponent(int componentRef) {
// TODO should we return null or fail on unknown component ?
return null;
}
}
@@ -0,0 +1,107 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

package org.sonar.server.computation.step;

import com.google.common.collect.Maps;
import org.sonar.api.resources.Qualifiers;
import org.sonar.api.utils.internal.Uuids;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport;
import org.sonar.batch.protocol.output.BatchReportReader;
import org.sonar.core.component.ComponentDto;
import org.sonar.core.component.ComponentKeys;
import org.sonar.core.persistence.DbSession;
import org.sonar.core.util.NonNullInputFunction;
import org.sonar.server.computation.ComputationContext;
import org.sonar.server.computation.component.Component;
import org.sonar.server.db.DbClient;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* To be finished
*/
public class FeedComponentsCacheStep implements ComputationStep {

private final DbClient dbClient;

public FeedComponentsCacheStep(DbClient dbClient) {
this.dbClient = dbClient;
}

@Override
public String[] supportedProjectQualifiers() {
return new String[] {Qualifiers.PROJECT};
}

@Override
public void execute(ComputationContext context) {
DbSession session = dbClient.openSession(false);
try {
List<ComponentDto> components = dbClient.componentDao().selectComponentsFromProjectUuid(session, context.getProject().uuid());
Map<String, ComponentDto> componentDtosByKey = componentDtosByKey(components);
Map<Integer, Component> componentsByRef = new HashMap<>();
int rootComponentRef = context.getReportMetadata().getRootComponentRef();
recursivelyProcessComponent(context, rootComponentRef, context.getReportReader().readComponent(rootComponentRef), componentDtosByKey, componentsByRef);
} finally {
session.close();
}
}

private void recursivelyProcessComponent(ComputationContext context, int componentRef, BatchReport.Component nearestModule,
Map<String, ComponentDto> componentDtosByKey, Map<Integer, Component> componentsByRef) {
BatchReportReader reportReader = context.getReportReader();
BatchReport.Component reportComponent = reportReader.readComponent(componentRef);
String componentKey = ComponentKeys.createKey(nearestModule.getKey(), reportComponent.getPath(), context.getReportMetadata().getBranch());

Component component = new Component().setKey(componentKey);
ComponentDto componentDto = componentDtosByKey.get(componentKey);
if (componentDto == null) {
component.setUuid(Uuids.create());
} else {
component.setId(component.getId());
component.setKey(componentKey);
}

for (Integer childRef : reportComponent.getChildRefList()) {
// If current component is not a module or a project, we need to keep the parent reference to the nearest module
BatchReport.Component nextNearestModule = !reportComponent.getType().equals(Constants.ComponentType.PROJECT) && !reportComponent.getType().equals(Constants.ComponentType.MODULE) ?
nearestModule : reportComponent;
recursivelyProcessComponent(context, childRef, nextNearestModule, componentDtosByKey, componentsByRef);
}
}

private Map<String, ComponentDto> componentDtosByKey(List<ComponentDto> components){
return Maps.uniqueIndex(components, new NonNullInputFunction<ComponentDto, String>() {
@Override
public String doApply(ComponentDto input) {
return input.key();
}
});
}

@Override
public String getDescription() {
return "Feed components cache";
}
}

0 comments on commit a27d5ef

Please sign in to comment.