mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Implement new limit/offset rule for git
Directories shall no longer be taken into account for limit/offset calculations.
This commit is contained in:
@@ -306,7 +306,7 @@ public final class BrowseCommandBuilder
|
|||||||
* severe performance implications. Reading a repository with thousands of files in one folder
|
* severe performance implications. Reading a repository with thousands of files in one folder
|
||||||
* can generate a huge load for a longer time.
|
* can generate a huge load for a longer time.
|
||||||
*
|
*
|
||||||
* @param limit The maximal number of files this request shall return.
|
* @param limit The maximal number of files this request shall return (directories are <b>not</b> counted).
|
||||||
*
|
*
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
@@ -318,8 +318,10 @@ public final class BrowseCommandBuilder
|
|||||||
/**
|
/**
|
||||||
* Proceed the list from the given number on (zero based).
|
* Proceed the list from the given number on (zero based).
|
||||||
*
|
*
|
||||||
* @param offset The number of the entry, the result should start with (zero based).
|
* @param offset The number of the file, the result should start with (zero based).
|
||||||
* All preceding entries will be omitted.
|
* All preceding files will be omitted. Directories are <b>not</b>
|
||||||
|
* counted. Therefore directories are only listed in results without
|
||||||
|
* offset.
|
||||||
* @since 2.0.0
|
* @since 2.0.0
|
||||||
*/
|
*/
|
||||||
public BrowseCommandBuilder setOffset(int offset) {
|
public BrowseCommandBuilder setOffset(int offset) {
|
||||||
|
|||||||
@@ -264,25 +264,28 @@ public class GitBrowseCommand extends AbstractGitCommand
|
|||||||
private void convertToFileObject(FileObject parent, org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, ObjectId revId, List<TreeEntry> entries) throws IOException {
|
private void convertToFileObject(FileObject parent, org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, ObjectId revId, List<TreeEntry> entries) throws IOException {
|
||||||
List<FileObject> files = Lists.newArrayList();
|
List<FileObject> files = Lists.newArrayList();
|
||||||
Iterator<TreeEntry> entryIterator = entries.iterator();
|
Iterator<TreeEntry> entryIterator = entries.iterator();
|
||||||
while (entryIterator.hasNext() && ++resultCount <= request.getLimit() + request.getOffset())
|
boolean hasNext;
|
||||||
|
while ((hasNext = entryIterator.hasNext()) && resultCount < request.getLimit() + request.getOffset())
|
||||||
{
|
{
|
||||||
TreeEntry entry = entryIterator.next();
|
TreeEntry entry = entryIterator.next();
|
||||||
FileObject fileObject = createFileObject(repo, request, revId, entry);
|
FileObject fileObject = createFileObject(repo, request, revId, entry);
|
||||||
|
|
||||||
|
if (!fileObject.isDirectory()) {
|
||||||
|
++resultCount;
|
||||||
|
}
|
||||||
|
|
||||||
if (request.isRecursive() && fileObject.isDirectory()) {
|
if (request.isRecursive() && fileObject.isDirectory()) {
|
||||||
convertToFileObject(fileObject, repo, request, revId, entry.getChildren());
|
convertToFileObject(fileObject, repo, request, revId, entry.getChildren());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (resultCount > request.getOffset()) {
|
if (resultCount > request.getOffset() || fileObject.isDirectory()) {
|
||||||
files.add(fileObject);
|
files.add(fileObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
parent.setChildren(files);
|
parent.setChildren(files);
|
||||||
|
|
||||||
if (resultCount > request.getLimit() + request.getOffset()) {
|
parent.setTruncated(hasNext);
|
||||||
parent.setTruncated(true);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Optional<TreeEntry> createTree(String path, TreeEntry parent, org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, TreeWalk treeWalk) throws IOException {
|
private Optional<TreeEntry> createTree(String path, TreeEntry parent, org.eclipse.jgit.lib.Repository repo, BrowseCommandRequest request, TreeWalk treeWalk) throws IOException {
|
||||||
|
|||||||
@@ -242,21 +242,37 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase {
|
|||||||
@Test
|
@Test
|
||||||
public void testBrowseLimit() throws IOException {
|
public void testBrowseLimit() throws IOException {
|
||||||
BrowseCommandRequest request = new BrowseCommandRequest();
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
request.setLimit(2);
|
request.setLimit(1);
|
||||||
FileObject root = createCommand()
|
FileObject root = createCommand()
|
||||||
.getBrowserResult(request).getFile();
|
.getBrowserResult(request).getFile();
|
||||||
assertNotNull(root);
|
assertNotNull(root);
|
||||||
|
|
||||||
Collection<FileObject> foList = root.getChildren();
|
Collection<FileObject> foList = root.getChildren();
|
||||||
|
|
||||||
|
assertThat(foList).extracting("name").containsExactly("c", "a.txt");
|
||||||
assertThat(foList).hasSize(2);
|
assertThat(foList).hasSize(2);
|
||||||
assertTrue(root.isTruncated());
|
assertTrue("result should be marked as trunctated", root.isTruncated());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBrowseLimitWithoutTruncation() throws IOException {
|
||||||
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
|
request.setLimit(3);
|
||||||
|
FileObject root = createCommand()
|
||||||
|
.getBrowserResult(request).getFile();
|
||||||
|
assertNotNull(root);
|
||||||
|
|
||||||
|
Collection<FileObject> foList = root.getChildren();
|
||||||
|
|
||||||
|
assertThat(foList).extracting("name").containsExactly("c", "a.txt", "b.txt", "f.txt");
|
||||||
|
assertThat(foList).hasSize(4);
|
||||||
|
assertFalse("result should not be marked as trunctated", root.isTruncated());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testBrowseOffset() throws IOException {
|
public void testBrowseOffset() throws IOException {
|
||||||
BrowseCommandRequest request = new BrowseCommandRequest();
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
request.setLimit(2);
|
request.setLimit(1);
|
||||||
request.setOffset(2);
|
request.setOffset(2);
|
||||||
FileObject root = createCommand()
|
FileObject root = createCommand()
|
||||||
.getBrowserResult(request).getFile();
|
.getBrowserResult(request).getFile();
|
||||||
@@ -264,15 +280,15 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase {
|
|||||||
|
|
||||||
Collection<FileObject> foList = root.getChildren();
|
Collection<FileObject> foList = root.getChildren();
|
||||||
|
|
||||||
assertThat(foList).extracting("name").contains("b.txt", "f.txt");
|
assertThat(foList).extracting("name").contains("f.txt");
|
||||||
assertFalse(root.isTruncated());
|
assertFalse("result should not be marked as trunctated", root.isTruncated());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRecursiveLimit() throws IOException {
|
public void testRecursiveLimit() throws IOException {
|
||||||
BrowseCommandRequest request = new BrowseCommandRequest();
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
|
|
||||||
request.setLimit(4);
|
request.setLimit(3);
|
||||||
request.setRecursive(true);
|
request.setRecursive(true);
|
||||||
|
|
||||||
FileObject root = createCommand().getBrowserResult(request).getFile();
|
FileObject root = createCommand().getBrowserResult(request).getFile();
|
||||||
@@ -295,7 +311,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase {
|
|||||||
public void testRecursiveLimitInSubDir() throws IOException {
|
public void testRecursiveLimitInSubDir() throws IOException {
|
||||||
BrowseCommandRequest request = new BrowseCommandRequest();
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
|
|
||||||
request.setLimit(2);
|
request.setLimit(1);
|
||||||
request.setRecursive(true);
|
request.setRecursive(true);
|
||||||
|
|
||||||
FileObject root = createCommand().getBrowserResult(request).getFile();
|
FileObject root = createCommand().getBrowserResult(request).getFile();
|
||||||
@@ -318,7 +334,7 @@ public class GitBrowseCommandTest extends AbstractGitCommandTestBase {
|
|||||||
public void testRecursiveOffset() throws IOException {
|
public void testRecursiveOffset() throws IOException {
|
||||||
BrowseCommandRequest request = new BrowseCommandRequest();
|
BrowseCommandRequest request = new BrowseCommandRequest();
|
||||||
|
|
||||||
request.setOffset(2);
|
request.setOffset(1);
|
||||||
request.setRecursive(true);
|
request.setRecursive(true);
|
||||||
|
|
||||||
FileObject root = createCommand().getBrowserResult(request).getFile();
|
FileObject root = createCommand().getBrowserResult(request).getFile();
|
||||||
|
|||||||
Reference in New Issue
Block a user