{"action":"created","comment":{"url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments/38581510","id":38581510,"diff_hunk":"@@ -17,53 +17,114 @@\n  * along with this program; if not, write to the Free Software Foundation,\n  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n  */\n-\n package org.sonar.server.source;\n \n-import org.apache.commons.lang.ObjectUtils;\n-import org.elasticsearch.common.collect.Lists;\n-import org.sonar.api.server.ServerSide;\n-import org.sonar.server.source.index.SourceLineDoc;\n-import org.sonar.server.source.index.SourceLineIndex;\n-\n-import javax.annotation.Nullable;\n+import com.google.common.base.Function;\n+import com.google.common.base.Functions;\n+import com.google.common.base.Optional;\n+import com.google.common.base.Preconditions;\n+import com.google.common.base.Predicate;\n+import com.google.common.collect.FluentIterable;\n+import javax.annotation.Nonnull;\n+import org.sonar.db.DbClient;\n+import org.sonar.db.DbSession;\n+import org.sonar.db.protobuf.DbFileSources;\n+import org.sonar.db.source.FileSourceDto;\n \n-import java.util.List;\n-\n-@ServerSide\n public class SourceService {\n \n-  private final HtmlSourceDecorator sourceDecorator;\n-  private final SourceLineIndex sourceLineIndex;\n+  private final DbClient dbClient;\n+  private final HtmlSourceDecorator htmlDecorator;\n \n-  public SourceService(HtmlSourceDecorator sourceDecorator, SourceLineIndex sourceLineIndex) {\n-    this.sourceDecorator = sourceDecorator;\n-    this.sourceLineIndex = sourceLineIndex;\n+  public SourceService(DbClient dbClient, HtmlSourceDecorator htmlDecorator) {\n+    this.dbClient = dbClient;\n+    this.htmlDecorator = htmlDecorator;\n   }\n \n   /**\n-   * Raw lines of source file.\n+   * Returns a range of lines as raw db data. User permission is not verified.\n+   * @param from starts from 1\n+   * @param toInclusive starts from 1, must be greater than or equal param {@code from}\n    */\n-  public List<String> getLinesAsTxt(String fileUuid, @Nullable Integer fromParam, @Nullable Integer toParam) {\n-    int from = (Integer) ObjectUtils.defaultIfNull(fromParam, 1);\n-    int to = (Integer) ObjectUtils.defaultIfNull(toParam, Integer.MAX_VALUE);\n-    List<String> lines = Lists.newArrayList();\n-    for (SourceLineDoc lineDoc : sourceLineIndex.getLines(fileUuid, from, to)) {\n-      lines.add(lineDoc.source());\n-    }\n-    return lines;\n+  public Optional<Iterable<DbFileSources.Line>> getLines(DbSession dbSession, String fileUuid, int from, int toInclusive) {\n+    return getLines(dbSession, fileUuid, from, toInclusive, Functions.<DbFileSources.Line>identity());\n+  }\n+\n+  /**\n+   * Returns a range of lines as raw text.\n+   * @see #getLines(DbSession, String, int, int)\n+   */\n+  public Optional<Iterable<String>> getLinesAsRawText(DbSession dbSession, String fileUuid, int from, int toInclusive) {\n+    return getLines(dbSession, fileUuid, from, toInclusive, LineToRaw.INSTANCE);\n+  }\n+\n+  public Optional<Iterable<String>> getLinesAsHtml(DbSession dbSession, String fileUuid, int from, int toInclusive) {\n+    return getLines(dbSession, fileUuid, from, toInclusive, lineToHtml());\n   }\n \n   /**\n-   * Decorated lines of source file.\n+   * Returns a single line from a source file. {@code Optional.absent()} is returned if the\n+   * file or the line do not exist.\n+   * @param line starts from 1\n    */\n-  public List<String> getLinesAsHtml(String fileUuid, @Nullable Integer fromParam, @Nullable Integer toParam) {\n-    int from = (Integer) ObjectUtils.defaultIfNull(fromParam, 1);\n-    int to = (Integer) ObjectUtils.defaultIfNull(toParam, Integer.MAX_VALUE);\n-    List<String> lines = Lists.newArrayList();\n-    for (SourceLineDoc lineDoc : sourceLineIndex.getLines(fileUuid, from, to)) {\n-      lines.add(sourceDecorator.getDecoratedSourceAsHtml(lineDoc.source(), lineDoc.highlighting(), lineDoc.symbols()));\n+  public Optional<DbFileSources.Line> getLine(DbSession dbSession, String fileUuid, int line) {\n+    verifyLine(line);\n+    FileSourceDto dto = dbClient.fileSourceDao().selectSourceByFileUuid(dbSession, fileUuid);\n+    if (dto == null) {\n+      return Optional.absent();\n+    }\n+    DbFileSources.Data data = dto.getSourceData();\n+    return FluentIterable.from(data.getLinesList())\n+      .filter(new IsGreaterOrEqualThanLine(line))\n+      .first();\n+  }\n+\n+  private <E> Optional<Iterable<E>> getLines(DbSession dbSession, String fileUuid, int from, int toInclusive, Function<DbFileSources.Line, E> function) {\n+    verifyLine(from);\n+    Preconditions.checkArgument(toInclusive >= from, String.format(\"Line number must greater than or equal to %d, got %d\", from, toInclusive));\n+    FileSourceDto dto = dbClient.fileSourceDao().selectSourceByFileUuid(dbSession, fileUuid);\n+    if (dto == null) {\n+      return Optional.absent();\n+    }\n+    DbFileSources.Data data = dto.getSourceData();\n+    return Optional.of((Iterable<E>) FluentIterable.from(data.getLinesList())\n+      .filter(new IsGreaterOrEqualThanLine(from))\n+      .limit(toInclusive - from + 1)\n+      .transform(function));\n+  }\n+\n+  private void verifyLine(int line) {","path":"server/sonar-server/src/main/java/org/sonar/server/source/SourceService.java","position":112,"original_position":112,"commit_id":"404af5d2a8c32df03aeada3b59b684062cb530f9","original_commit_id":"404af5d2a8c32df03aeada3b59b684062cb530f9","user":{"login":"SonarTech","id":1842438,"avatar_url":"https://avatars.githubusercontent.com/u/1842438?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarTech","html_url":"https://github.com/SonarTech","followers_url":"https://api.github.com/users/SonarTech/followers","following_url":"https://api.github.com/users/SonarTech/following{/other_user}","gists_url":"https://api.github.com/users/SonarTech/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarTech/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarTech/subscriptions","organizations_url":"https://api.github.com/users/SonarTech/orgs","repos_url":"https://api.github.com/users/SonarTech/repos","events_url":"https://api.github.com/users/SonarTech/events{/privacy}","received_events_url":"https://api.github.com/users/SonarTech/received_events","type":"User","site_admin":false},"body":"![MAJOR](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/severity-major.png) Make \"verifyLine\" a \"static\" method. [![rule](https://raw.githubusercontent.com/SonarCommunity/sonar-github/master/images/rule.png)](http://nemo.sonarqube.org/coding_rules#rule_key=squid%3AS2325)\n","created_at":"2015-09-02T20:40:55Z","updated_at":"2015-09-02T20:40:55Z","html_url":"https://github.com/SonarSource/sonarqube/pull/490#discussion_r38581510","pull_request_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490","_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments/38581510"},"html":{"href":"https://github.com/SonarSource/sonarqube/pull/490#discussion_r38581510"},"pull_request":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490"}}},"pull_request":{"url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490","id":44037612,"html_url":"https://github.com/SonarSource/sonarqube/pull/490","diff_url":"https://github.com/SonarSource/sonarqube/pull/490.diff","patch_url":"https://github.com/SonarSource/sonarqube/pull/490.patch","issue_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/490","number":490,"state":"open","locked":false,"title":"SONAR-6813 drop ES index sourceLines","user":{"login":"simonbrandhof","id":414010,"avatar_url":"https://avatars.githubusercontent.com/u/414010?v=3","gravatar_id":"","url":"https://api.github.com/users/simonbrandhof","html_url":"https://github.com/simonbrandhof","followers_url":"https://api.github.com/users/simonbrandhof/followers","following_url":"https://api.github.com/users/simonbrandhof/following{/other_user}","gists_url":"https://api.github.com/users/simonbrandhof/gists{/gist_id}","starred_url":"https://api.github.com/users/simonbrandhof/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simonbrandhof/subscriptions","organizations_url":"https://api.github.com/users/simonbrandhof/orgs","repos_url":"https://api.github.com/users/simonbrandhof/repos","events_url":"https://api.github.com/users/simonbrandhof/events{/privacy}","received_events_url":"https://api.github.com/users/simonbrandhof/received_events","type":"User","site_admin":false},"body":"Dropping the Elasticsearch index \"sourceLines\" requires to refactor the \nway to compute the metric \"days_since_last_commit\". This metric\nis currently replaced by the new \"last_commit_date\" (see SONAR-6824).\n","created_at":"2015-09-02T20:21:55Z","updated_at":"2015-09-02T20:40:56Z","closed_at":null,"merged_at":null,"merge_commit_sha":null,"assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490/commits","review_comments_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490/comments","review_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments{/number}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/490/comments","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/404af5d2a8c32df03aeada3b59b684062cb530f9","head":{"label":"SonarSource:feature/drop-source-index","ref":"feature/drop-source-index","sha":"404af5d2a8c32df03aeada3b59b684062cb530f9","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":1222504,"name":"sonarqube","full_name":"SonarSource/sonarqube","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonarqube","description":"Put your technical debt under control","fork":false,"url":"https://api.github.com/repos/SonarSource/sonarqube","forks_url":"https://api.github.com/repos/SonarSource/sonarqube/forks","keys_url":"https://api.github.com/repos/SonarSource/sonarqube/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonarqube/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonarqube/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonarqube/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonarqube/events","assignees_url":"https://api.github.com/repos/SonarSource/sonarqube/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonarqube/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonarqube/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonarqube/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonarqube/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonarqube/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonarqube/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonarqube/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonarqube/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonarqube/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonarqube/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonarqube/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonarqube/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonarqube/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonarqube/merges","archive_url":"https://api.github.com/repos/SonarSource/sonarqube/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonarqube/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonarqube/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonarqube/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonarqube/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonarqube/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonarqube/releases{/id}","created_at":"2011-01-05T11:05:17Z","updated_at":"2015-09-02T14:26:02Z","pushed_at":"2015-09-02T20:21:55Z","git_url":"git://github.com/SonarSource/sonarqube.git","ssh_url":"git@github.com:SonarSource/sonarqube.git","clone_url":"https://github.com/SonarSource/sonarqube.git","svn_url":"https://github.com/SonarSource/sonarqube","homepage":"http://www.sonarqube.org","size":750378,"stargazers_count":718,"watchers_count":718,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":377,"mirror_url":null,"open_issues_count":9,"forks":377,"open_issues":9,"watchers":718,"default_branch":"master"}},"base":{"label":"SonarSource:master","ref":"master","sha":"5c07e5e99a1fc9ec6b0a4db8fddc785a1e885b79","user":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"repo":{"id":1222504,"name":"sonarqube","full_name":"SonarSource/sonarqube","owner":{"login":"SonarSource","id":545988,"avatar_url":"https://avatars.githubusercontent.com/u/545988?v=3","gravatar_id":"","url":"https://api.github.com/users/SonarSource","html_url":"https://github.com/SonarSource","followers_url":"https://api.github.com/users/SonarSource/followers","following_url":"https://api.github.com/users/SonarSource/following{/other_user}","gists_url":"https://api.github.com/users/SonarSource/gists{/gist_id}","starred_url":"https://api.github.com/users/SonarSource/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SonarSource/subscriptions","organizations_url":"https://api.github.com/users/SonarSource/orgs","repos_url":"https://api.github.com/users/SonarSource/repos","events_url":"https://api.github.com/users/SonarSource/events{/privacy}","received_events_url":"https://api.github.com/users/SonarSource/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/SonarSource/sonarqube","description":"Put your technical debt under control","fork":false,"url":"https://api.github.com/repos/SonarSource/sonarqube","forks_url":"https://api.github.com/repos/SonarSource/sonarqube/forks","keys_url":"https://api.github.com/repos/SonarSource/sonarqube/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SonarSource/sonarqube/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SonarSource/sonarqube/teams","hooks_url":"https://api.github.com/repos/SonarSource/sonarqube/hooks","issue_events_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/events{/number}","events_url":"https://api.github.com/repos/SonarSource/sonarqube/events","assignees_url":"https://api.github.com/repos/SonarSource/sonarqube/assignees{/user}","branches_url":"https://api.github.com/repos/SonarSource/sonarqube/branches{/branch}","tags_url":"https://api.github.com/repos/SonarSource/sonarqube/tags","blobs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SonarSource/sonarqube/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SonarSource/sonarqube/git/refs{/sha}","trees_url":"https://api.github.com/repos/SonarSource/sonarqube/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SonarSource/sonarqube/statuses/{sha}","languages_url":"https://api.github.com/repos/SonarSource/sonarqube/languages","stargazers_url":"https://api.github.com/repos/SonarSource/sonarqube/stargazers","contributors_url":"https://api.github.com/repos/SonarSource/sonarqube/contributors","subscribers_url":"https://api.github.com/repos/SonarSource/sonarqube/subscribers","subscription_url":"https://api.github.com/repos/SonarSource/sonarqube/subscription","commits_url":"https://api.github.com/repos/SonarSource/sonarqube/commits{/sha}","git_commits_url":"https://api.github.com/repos/SonarSource/sonarqube/git/commits{/sha}","comments_url":"https://api.github.com/repos/SonarSource/sonarqube/comments{/number}","issue_comment_url":"https://api.github.com/repos/SonarSource/sonarqube/issues/comments{/number}","contents_url":"https://api.github.com/repos/SonarSource/sonarqube/contents/{+path}","compare_url":"https://api.github.com/repos/SonarSource/sonarqube/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SonarSource/sonarqube/merges","archive_url":"https://api.github.com/repos/SonarSource/sonarqube/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SonarSource/sonarqube/downloads","issues_url":"https://api.github.com/repos/SonarSource/sonarqube/issues{/number}","pulls_url":"https://api.github.com/repos/SonarSource/sonarqube/pulls{/number}","milestones_url":"https://api.github.com/repos/SonarSource/sonarqube/milestones{/number}","notifications_url":"https://api.github.com/repos/SonarSource/sonarqube/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SonarSource/sonarqube/labels{/name}","releases_url":"https://api.github.com/repos/SonarSource/sonarqube/releases{/id}","created_at":"2011-01-05T11:05:17Z","updated_at":"2015-09-02T14:26:02Z","pushed_at":"2015-09-02T20:21:55Z","git_url":"git://github.com/SonarSource/sonarqube.git","ssh_url":"git@github.com:SonarSource/sonarqube.git","clone_url":"https://github.com/SonarSource/sonarqube.git","svn_url":"https://github.com/SonarSource/sonarqube","homepage":"http://www.sonarqube.org","size":750378,"stargazers_count":718,"watchers_count":718,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":377,"mirror_url":null,"open_issues_count":9,"forks":377,"open_issues":9,"watchers":718,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490"},"html":{"href":"https://github.com/SonarSource/sonarqube/pull/490"},"issue":{"href":"https://api.github.com/repos/SonarSource/sonarqube/issues/490"},"comments":{"href":"https://api.github.com/repos/SonarSource/sonarqube/issues/490/comments"},"review_comments":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490/comments"},"review_comment":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/SonarSource/sonarqube/pulls/490/commits"},"statuses":{"href":"https://api.github.com/repos/SonarSource/sonarqube/statuses/404af5d2a8c32df03aeada3b59b684062cb530f9"}}}}