Merge branch '16-indexing' into 'master'
Resolve "Индексировать репозиторий в БД" Closes #16 See merge request romanov73/git-extractor!11merge-requests/13/merge
commit
c401ad3a20
@ -0,0 +1,23 @@
|
|||||||
|
package ru.ulstu.extractor.model;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Author extends BaseEntity {
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
public Author() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Author(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,86 @@
|
|||||||
|
package ru.ulstu.extractor.model;
|
||||||
|
|
||||||
|
import javax.persistence.GeneratedValue;
|
||||||
|
import javax.persistence.GenerationType;
|
||||||
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.MappedSuperclass;
|
||||||
|
import javax.persistence.Version;
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
@MappedSuperclass
|
||||||
|
public abstract class BaseEntity implements Serializable, Comparable {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.TABLE)
|
||||||
|
private Integer id;
|
||||||
|
|
||||||
|
@Version
|
||||||
|
private Integer version;
|
||||||
|
|
||||||
|
public BaseEntity() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public BaseEntity(Integer id, Integer version) {
|
||||||
|
this.id = id;
|
||||||
|
this.version = version;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Integer id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getVersion() {
|
||||||
|
return version;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (!getClass().isAssignableFrom(obj.getClass())) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
BaseEntity other = (BaseEntity) obj;
|
||||||
|
if (id == null) {
|
||||||
|
if (other.id != null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} else if (!id.equals(other.id)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + (id == null ? 0 : id.hashCode());
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getClass().getSimpleName() + "{" +
|
||||||
|
"id=" + id +
|
||||||
|
", version=" + version +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(Object o) {
|
||||||
|
return id != null ? id.compareTo(((BaseEntity) o).getId()) : -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void reset() {
|
||||||
|
this.id = null;
|
||||||
|
this.version = null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,14 +1,61 @@
|
|||||||
package ru.ulstu.extractor.model;
|
package ru.ulstu.extractor.model;
|
||||||
|
|
||||||
public class Branch {
|
import org.hibernate.annotations.Fetch;
|
||||||
|
import org.hibernate.annotations.FetchMode;
|
||||||
|
|
||||||
|
import javax.persistence.CascadeType;
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
import javax.persistence.FetchType;
|
||||||
|
import javax.persistence.JoinColumn;
|
||||||
|
import javax.persistence.ManyToOne;
|
||||||
|
import javax.persistence.OneToMany;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Branch extends BaseEntity {
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
|
@ManyToOne
|
||||||
|
private Repository repository;
|
||||||
|
|
||||||
|
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
|
||||||
|
@JoinColumn(name = "branch_id", unique = true)
|
||||||
|
@Fetch(FetchMode.SUBSELECT)
|
||||||
|
private List<Commit> commits;
|
||||||
|
|
||||||
|
public Branch() {
|
||||||
|
}
|
||||||
|
|
||||||
public Branch(String name) {
|
public Branch(String name) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Branch(Repository repository, String branchName) {
|
||||||
|
this.repository = repository;
|
||||||
|
this.name = branchName;
|
||||||
|
}
|
||||||
|
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Repository getRepository() {
|
||||||
|
return repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setRepository(Repository repository) {
|
||||||
|
this.repository = repository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Commit> getCommits() {
|
||||||
|
return commits;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommits(List<Commit> commits) {
|
||||||
|
this.commits = commits;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,93 @@
|
|||||||
|
package ru.ulstu.extractor.model;
|
||||||
|
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.data.domain.Sort;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
public class OffsetablePageRequest implements Pageable, Serializable {
|
||||||
|
private final int offset;
|
||||||
|
private final int count;
|
||||||
|
private final Sort sort;
|
||||||
|
|
||||||
|
public OffsetablePageRequest(int page, long pageSize) {
|
||||||
|
this(pageSize * page, pageSize, Sort.by("id"));
|
||||||
|
}
|
||||||
|
|
||||||
|
public OffsetablePageRequest(long offset, long count, Sort sort) {
|
||||||
|
if (offset < 0) {
|
||||||
|
throw new IllegalArgumentException("Offset value must not be less than zero!");
|
||||||
|
}
|
||||||
|
if (count < 1) {
|
||||||
|
throw new IllegalArgumentException("Count value must not be less than one!");
|
||||||
|
}
|
||||||
|
this.offset = (int) offset;
|
||||||
|
this.count = (int) count;
|
||||||
|
this.sort = sort;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Sort getSort() {
|
||||||
|
return sort;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPageSize() {
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getPageNumber() {
|
||||||
|
return offset / count;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getOffset() {
|
||||||
|
return offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean hasPrevious() {
|
||||||
|
return offset > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pageable next() {
|
||||||
|
return new OffsetablePageRequest(getOffset() + getPageSize(), getPageSize(), getSort());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pageable previousOrFirst() {
|
||||||
|
return hasPrevious() ? previous() : first();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Pageable previous() {
|
||||||
|
return getOffset() == 0 ? this : new OffsetablePageRequest(getOffset() - getPageSize(), getPageSize(), getSort());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Pageable first() {
|
||||||
|
return new OffsetablePageRequest(0, getPageSize(), getSort());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (this == obj) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (obj == null || getClass() != obj.getClass()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
final OffsetablePageRequest other = (OffsetablePageRequest) obj;
|
||||||
|
return this.offset == other.offset && this.count == other.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
final int prime = 31;
|
||||||
|
int result = 1;
|
||||||
|
result = prime * result + offset;
|
||||||
|
result = prime * result + count;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package ru.ulstu.extractor.model;
|
||||||
|
|
||||||
|
import javax.persistence.Entity;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
public class Repository extends BaseEntity {
|
||||||
|
private String url;
|
||||||
|
|
||||||
|
public Repository() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Repository(String repositoryUrl) {
|
||||||
|
url = repositoryUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getUrl() {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUrl(String url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,14 @@
|
|||||||
|
package ru.ulstu.extractor.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
|
import ru.ulstu.extractor.model.Author;
|
||||||
|
import ru.ulstu.extractor.model.Repository;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface AuthorRepository extends JpaRepository<Author, Integer> {
|
||||||
|
@Query("SELECT a FROM Commit c, Repository r, Branch b, Author a WHERE c.author = a AND c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName")
|
||||||
|
List<Author> findByRepositoryAndBranch(@Param("repository") Repository repository, @Param("branchName") String branchName);
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package ru.ulstu.extractor.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.ulstu.extractor.model.Branch;
|
||||||
|
import ru.ulstu.extractor.model.Repository;
|
||||||
|
|
||||||
|
public interface BranchRepository extends JpaRepository<Branch, Integer> {
|
||||||
|
Branch findByRepositoryAndName(Repository repository, String name);
|
||||||
|
}
|
@ -1,42 +1,14 @@
|
|||||||
package ru.ulstu.extractor.repository;
|
package ru.ulstu.extractor.repository;
|
||||||
|
|
||||||
import org.springframework.jdbc.core.JdbcTemplate;
|
import org.springframework.data.domain.Page;
|
||||||
import org.springframework.jdbc.core.RowMapper;
|
import org.springframework.data.domain.Pageable;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import org.springframework.data.jpa.repository.Query;
|
||||||
|
import org.springframework.data.repository.query.Param;
|
||||||
import ru.ulstu.extractor.model.Commit;
|
import ru.ulstu.extractor.model.Commit;
|
||||||
|
import ru.ulstu.extractor.model.Repository;
|
||||||
|
|
||||||
import java.sql.ResultSet;
|
public interface CommitRepository extends JpaRepository<Commit, Integer> {
|
||||||
import java.sql.SQLException;
|
@Query("SELECT c FROM Commit c, Repository r, Branch b WHERE c.branch = b AND r = b.repository AND r = :repository AND b.name = :branchName")
|
||||||
import java.util.List;
|
Page<Commit> findByRepositoryAndBranch(Pageable pageable, @Param("repository") Repository repository, @Param("branchName") String branchName);
|
||||||
|
|
||||||
@Repository
|
|
||||||
public class CommitRepository {
|
|
||||||
private final static String SQL_SELECT_COMMITS = "SELECT * FROM commit";
|
|
||||||
private final static String SQL_INSERT_COMMITS = "INSERT INTO commit (hash, date, author, message) " +
|
|
||||||
"VALUES (?, ?, ?, ?)";
|
|
||||||
private final JdbcTemplate jdbcTemplate;
|
|
||||||
|
|
||||||
public CommitRepository(JdbcTemplate jdbcTemplate) {
|
|
||||||
this.jdbcTemplate = jdbcTemplate;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class CommitRowMapper implements RowMapper<Commit> {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Commit mapRow(ResultSet rs, int rowNum) throws SQLException {
|
|
||||||
return new Commit(rs.getString("message"),
|
|
||||||
rs.getString("author"),
|
|
||||||
rs.getDate("date"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Commit> getCommits() {
|
|
||||||
return jdbcTemplate.query(SQL_SELECT_COMMITS, new CommitRowMapper());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveCommit(Commit commit) {
|
|
||||||
jdbcTemplate.update(SQL_INSERT_COMMITS, "", commit.getDate(),
|
|
||||||
commit.getAuthor(),
|
|
||||||
commit.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
package ru.ulstu.extractor.repository;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
import ru.ulstu.extractor.model.Repository;
|
||||||
|
|
||||||
|
public interface RepositoryRepository extends JpaRepository<Repository, Integer> {
|
||||||
|
Repository findByUrl(String url);
|
||||||
|
}
|
@ -1,32 +1,13 @@
|
|||||||
package ru.ulstu.extractor.service;
|
package ru.ulstu.extractor.service;
|
||||||
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import ru.ulstu.extractor.model.Commit;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class FilteringService {
|
public class FilteringService {
|
||||||
|
|
||||||
private final GitRepositoryService gitRepositoryService;
|
private final IndexService indexService;
|
||||||
|
|
||||||
public FilteringService(GitRepositoryService gitRepositoryService) {
|
|
||||||
this.gitRepositoryService = gitRepositoryService;
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Commit> getCommits(String urlRepo) {
|
|
||||||
return getCommits(urlRepo, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<Commit> getCommits(String urlRepo, String filterCommitMessage) {
|
public FilteringService(IndexService indexService) {
|
||||||
if (filterCommitMessage != null) {
|
this.indexService = indexService;
|
||||||
return getCommits(urlRepo)
|
|
||||||
.stream()
|
|
||||||
.filter(commit -> commit.getMessage().contains(filterCommitMessage))
|
|
||||||
.collect(Collectors.toList());
|
|
||||||
} else {
|
|
||||||
return getCommits(urlRepo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,63 @@
|
|||||||
|
package ru.ulstu.extractor.service;
|
||||||
|
|
||||||
|
import com.sun.istack.NotNull;
|
||||||
|
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||||
|
import org.springframework.data.domain.Page;
|
||||||
|
import org.springframework.data.domain.Pageable;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import ru.ulstu.extractor.model.Author;
|
||||||
|
import ru.ulstu.extractor.model.Branch;
|
||||||
|
import ru.ulstu.extractor.model.Commit;
|
||||||
|
import ru.ulstu.extractor.model.Repository;
|
||||||
|
import ru.ulstu.extractor.repository.AuthorRepository;
|
||||||
|
import ru.ulstu.extractor.repository.BranchRepository;
|
||||||
|
import ru.ulstu.extractor.repository.CommitRepository;
|
||||||
|
import ru.ulstu.extractor.repository.RepositoryRepository;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class IndexService {
|
||||||
|
private final GitRepositoryService gitRepositoryService;
|
||||||
|
private final RepositoryRepository repositoryRepository;
|
||||||
|
private final BranchRepository branchRepository;
|
||||||
|
private final CommitRepository commitRepository;
|
||||||
|
private final AuthorRepository authorRepository;
|
||||||
|
|
||||||
|
|
||||||
|
public IndexService(GitRepositoryService gitRepositoryService,
|
||||||
|
RepositoryRepository repositoryRepository,
|
||||||
|
BranchRepository branchRepository,
|
||||||
|
CommitRepository commitRepository,
|
||||||
|
AuthorRepository authorRepository) {
|
||||||
|
this.gitRepositoryService = gitRepositoryService;
|
||||||
|
this.repositoryRepository = repositoryRepository;
|
||||||
|
this.branchRepository = branchRepository;
|
||||||
|
this.commitRepository = commitRepository;
|
||||||
|
this.authorRepository = authorRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void index(@NotNull String repositoryUrl, @NotNull String branchName) throws GitAPIException, IOException {
|
||||||
|
Repository repository = repositoryRepository.findByUrl(repositoryUrl);
|
||||||
|
if (repository == null) {
|
||||||
|
repositoryRepository.save(new Repository(repositoryUrl));
|
||||||
|
}
|
||||||
|
Branch branch = branchRepository.findByRepositoryAndName(repository, branchName);
|
||||||
|
if (branch == null) {
|
||||||
|
branchRepository.save(new Branch(repository, branchName));
|
||||||
|
}
|
||||||
|
List<Commit> commits = gitRepositoryService.getCommits(repositoryUrl, branchName);
|
||||||
|
branch.setCommits(commits);
|
||||||
|
branchRepository.save(branch);
|
||||||
|
gitRepositoryService.remove(repositoryUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<Author> getRepositoryAuthors(@NotNull String repositoryUrl, @NotNull String branchName) {
|
||||||
|
return authorRepository.findByRepositoryAndBranch(repositoryRepository.findByUrl(repositoryUrl), branchName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Page<Commit> getCommits(@NotNull String repositoryUrl, @NotNull String branchName, Pageable pageable) {
|
||||||
|
return commitRepository.findByRepositoryAndBranch(pageable, repositoryRepository.findByUrl(repositoryUrl), branchName);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<?xml version="1.1" encoding="UTF-8" standalone="no"?>
|
||||||
|
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
|
||||||
|
<changeSet author="orion" id="20210329-120000-1">
|
||||||
|
<addColumn tableName="repository">
|
||||||
|
<column name="version" type="integer"/>
|
||||||
|
</addColumn>
|
||||||
|
<addColumn tableName="branch">
|
||||||
|
<column name="version" type="integer"/>
|
||||||
|
</addColumn>
|
||||||
|
<addColumn tableName="commit">
|
||||||
|
<column name="version" type="integer"/>
|
||||||
|
</addColumn>
|
||||||
|
<addColumn tableName="author">
|
||||||
|
<column name="version" type="integer"/>
|
||||||
|
</addColumn>
|
||||||
|
</changeSet>
|
||||||
|
<changeSet author="orion" id="20210329-120000-2">
|
||||||
|
<addColumn tableName="file_change">
|
||||||
|
<column name="version" type="integer"/>
|
||||||
|
</addColumn>
|
||||||
|
<addColumn tableName="line_change">
|
||||||
|
<column name="version" type="integer"/>
|
||||||
|
</addColumn>
|
||||||
|
</changeSet>
|
||||||
|
<changeSet author="orion" id="20210329-120000-3" objectQuotingStrategy="QUOTE_ALL_OBJECTS">
|
||||||
|
<createTable tableName="hibernate_sequences">
|
||||||
|
<column name="sequence_name" type="VARCHAR(255)">
|
||||||
|
<constraints nullable="false"/>
|
||||||
|
</column>
|
||||||
|
<column name="sequence_next_hi_value" type="BIGINT"/>
|
||||||
|
<column name="next_val" type="bigint"/>
|
||||||
|
</createTable>
|
||||||
|
<addPrimaryKey columnNames="sequence_name" constraintName="hibernate_sequences_pkey"
|
||||||
|
tableName="hibernate_sequences"/>
|
||||||
|
</changeSet>
|
||||||
|
</databaseChangeLog>
|
Loading…
Reference in New Issue