mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-03 03:55:58 +01:00
Don't use cache library immediately.
This commit is contained in:
@@ -33,8 +33,6 @@ object MyBuild extends Build {
|
|||||||
"org.apache.commons" % "commons-compress" % "1.5",
|
"org.apache.commons" % "commons-compress" % "1.5",
|
||||||
"com.typesafe.slick" %% "slick" % "1.0.1",
|
"com.typesafe.slick" %% "slick" % "1.0.1",
|
||||||
"com.h2database" % "h2" % "1.3.171",
|
"com.h2database" % "h2" % "1.3.171",
|
||||||
"com.google.guava" % "guava" % "14.0.1",
|
|
||||||
"com.google.code.findbugs" % "jsr305" % "2.0.1",
|
|
||||||
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
|
"ch.qos.logback" % "logback-classic" % "1.0.6" % "runtime",
|
||||||
"org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "container",
|
"org.eclipse.jetty" % "jetty-webapp" % "8.1.8.v20121106" % "container",
|
||||||
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
|
"org.eclipse.jetty.orbit" % "javax.servlet" % "3.0.0.v201112011016" % "container;provided;test" artifacts (Artifact("javax.servlet", "jar", "jar"))
|
||||||
|
|||||||
@@ -8,16 +8,14 @@ import org.eclipse.jgit.treewalk.TreeWalk
|
|||||||
import org.eclipse.jgit.revwalk.RevWalk
|
import org.eclipse.jgit.revwalk.RevWalk
|
||||||
import scala.collection.mutable.ListBuffer
|
import scala.collection.mutable.ListBuffer
|
||||||
import org.eclipse.jgit.lib.FileMode
|
import org.eclipse.jgit.lib.FileMode
|
||||||
import com.google.common.cache.{CacheLoader, CacheBuilder}
|
|
||||||
import java.util.concurrent.TimeUnit
|
|
||||||
import model.Issue
|
import model.Issue
|
||||||
|
|
||||||
class IndexController extends IndexControllerBase
|
class IndexController extends IndexControllerBase
|
||||||
with RepositoryService with AccountService with SystemSettingsService with ActivityService
|
with RepositoryService with AccountService with SystemSettingsService with ActivityService with IssuesService
|
||||||
with ReferrerAuthenticator
|
with ReferrerAuthenticator
|
||||||
|
|
||||||
trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
||||||
with SystemSettingsService with ActivityService
|
with SystemSettingsService with ActivityService with IssuesService
|
||||||
with ReferrerAuthenticator =>
|
with ReferrerAuthenticator =>
|
||||||
|
|
||||||
val searchForm = mapping(
|
val searchForm = mapping(
|
||||||
@@ -54,7 +52,7 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
val SearchResult(files, issues) = cache.get(repository.owner, repository.name, query)
|
val SearchResult(files, issues) = searchRepository(repository.owner, repository.name, query)
|
||||||
|
|
||||||
target.toLowerCase match {
|
target.toLowerCase match {
|
||||||
case "issue" =>
|
case "issue" =>
|
||||||
@@ -83,6 +81,51 @@ trait IndexControllerBase extends ControllerBase { self: RepositoryService
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
case class SearchResult(
|
||||||
|
files: List[(String, String)],
|
||||||
|
issues: List[(Issue, Int, String)]
|
||||||
|
)
|
||||||
|
|
||||||
|
def searchRepository(owner: String, repository: String, query: String): SearchResult = {
|
||||||
|
val issues = if(query.isEmpty) Nil else searchIssuesByKeyword(owner, repository, query)
|
||||||
|
val files = if(query.isEmpty) Nil else searchRepositoryFiles(owner, repository, query)
|
||||||
|
SearchResult(files, issues)
|
||||||
|
}
|
||||||
|
|
||||||
|
private def searchRepositoryFiles(owner: String, repository: String, query: String): List[(String, String)] = {
|
||||||
|
JGitUtil.withGit(getRepositoryDir(owner, repository)){ git =>
|
||||||
|
val revWalk = new RevWalk(git.getRepository)
|
||||||
|
val objectId = git.getRepository.resolve("HEAD")
|
||||||
|
val revCommit = revWalk.parseCommit(objectId)
|
||||||
|
val treeWalk = new TreeWalk(git.getRepository)
|
||||||
|
treeWalk.setRecursive(true)
|
||||||
|
treeWalk.addTree(revCommit.getTree)
|
||||||
|
|
||||||
|
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||||
|
val list = new ListBuffer[(String, String)]
|
||||||
|
|
||||||
|
while (treeWalk.next()) {
|
||||||
|
if(treeWalk.getFileMode(0) != FileMode.TREE){
|
||||||
|
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
|
||||||
|
if(FileUtil.isText(bytes)){
|
||||||
|
val text = new String(bytes, "UTF-8")
|
||||||
|
val lowerText = text.toLowerCase
|
||||||
|
val indices = keywords.map(lowerText.indexOf _)
|
||||||
|
if(!indices.exists(_ < 0)){
|
||||||
|
list.append((treeWalk.getPathString, text))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
treeWalk.release
|
||||||
|
revWalk.release
|
||||||
|
|
||||||
|
list.toList
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
private def getHighlightText(content: String, query: String): (String, Int) = {
|
private def getHighlightText(content: String, query: String): (String, Int) = {
|
||||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||||
val lowerText = content.toLowerCase
|
val lowerText = content.toLowerCase
|
||||||
@@ -116,58 +159,6 @@ case class FileSearchResult(
|
|||||||
highlightLineNumber: Int)
|
highlightLineNumber: Int)
|
||||||
|
|
||||||
object RepositorySearch extends IssuesService {
|
object RepositorySearch extends IssuesService {
|
||||||
|
|
||||||
val CodeLimit = 10
|
val CodeLimit = 10
|
||||||
val IssueLimit = 10
|
val IssueLimit = 10
|
||||||
|
|
||||||
case class SearchResult(
|
|
||||||
files: List[(String, String)],
|
|
||||||
issues: List[(Issue, Int, String)]
|
|
||||||
)
|
|
||||||
|
|
||||||
val cache = CacheBuilder.newBuilder()
|
|
||||||
.maximumSize(100)
|
|
||||||
.expireAfterWrite(10, TimeUnit.MINUTES)
|
|
||||||
.build(
|
|
||||||
new CacheLoader[(String, String, String), SearchResult]() {
|
|
||||||
override def load(key: (String, String, String)) = {
|
|
||||||
val (owner, repository, query) = key
|
|
||||||
val issues = if(query.isEmpty) Nil else searchIssuesByKeyword(owner, repository, query)
|
|
||||||
val files = if(query.isEmpty) Nil else searchRepositoryFiles(owner, repository, query)
|
|
||||||
SearchResult(files, issues)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
private def searchRepositoryFiles(owner: String, repository: String, query: String): List[(String, String)] = {
|
|
||||||
JGitUtil.withGit(getRepositoryDir(owner, repository)){ git =>
|
|
||||||
val revWalk = new RevWalk(git.getRepository)
|
|
||||||
val objectId = git.getRepository.resolve("HEAD")
|
|
||||||
val revCommit = revWalk.parseCommit(objectId)
|
|
||||||
val treeWalk = new TreeWalk(git.getRepository)
|
|
||||||
treeWalk.setRecursive(true)
|
|
||||||
treeWalk.addTree(revCommit.getTree)
|
|
||||||
|
|
||||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
|
||||||
val list = new ListBuffer[(String, String)]
|
|
||||||
|
|
||||||
while (treeWalk.next()) {
|
|
||||||
if(treeWalk.getFileMode(0) != FileMode.TREE){
|
|
||||||
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
|
|
||||||
if(FileUtil.isText(bytes)){
|
|
||||||
val text = new String(bytes, "UTF-8")
|
|
||||||
val lowerText = text.toLowerCase
|
|
||||||
val indices = keywords.map(lowerText.indexOf _)
|
|
||||||
if(!indices.exists(_ < 0)){
|
|
||||||
list.append((treeWalk.getPathString, text))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
treeWalk.release
|
|
||||||
revWalk.release
|
|
||||||
|
|
||||||
list.toList
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user