Skip to content

Commit 95c1e33

Browse files
authored
directory listing API (#4308)
fixes #3047 fixes #3738
1 parent dcb7712 commit 95c1e33

File tree

16 files changed

+672
-130
lines changed

16 files changed

+672
-130
lines changed

apiary.apib

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,41 @@ The `Content-type` header of the reply will be set accordingly.
197197
}
198198

199199

200+
201+
## Directory listing [/list{?path}]
202+
203+
### get directory entries [GET]
204+
205+
+ Parameters
206+
+ path (string) - path of file/directory to get listing for, relative to source root, starting with /
207+
208+
+ Response 200 (application/json)
209+
+ Body
210+
211+
[
212+
{
213+
"path": "/lucene/.github",
214+
"numLines": 178,
215+
"loc": 125,
216+
"date": 1673456294670,
217+
"description": null,
218+
"pathDescription": "",
219+
"isDirectory": true,
220+
"size": null
221+
},
222+
{
223+
"path": "/lucene/.asf.yaml",
224+
"numLines": 25,
225+
"loc": 21,
226+
"date": 1673456294670,
227+
"description": null,
228+
"pathDescription": "",
229+
"isDirectory": false,
230+
"size": 574
231+
}
232+
]
233+
234+
200235
## Include files reload [/system/includes/reload]
201236

202237
### reloads all include files for web application [PUT]

opengrok-indexer/src/main/java/org/opengrok/indexer/history/HistoryGuru.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,13 +712,14 @@ public boolean hasAnnotationCacheForFile(File file) {
712712
}
713713

714714
/**
715-
* Get the last modified times and descriptions for all files and subdirectories in the specified directory.
715+
* Get the last modified times and descriptions for all files and subdirectories in the specified directory
716+
* and set it into the entries provided.
716717
* @param directory the directory whose files to check
717718
* @param entries list of {@link DirectoryEntry} instances
718719
* @return whether to fall back to file system based time stamps if the date is {@code null}
719720
* @throws org.opengrok.indexer.history.CacheException if history cannot be retrieved
720721
*/
721-
public boolean getLastHistoryEntries(File directory, List<DirectoryEntry> entries) throws CacheException {
722+
public boolean fillLastHistoryEntries(File directory, List<DirectoryEntry> entries) throws CacheException {
722723

723724
if (!env.isUseHistoryCacheForDirectoryListing()) {
724725
LOGGER.log(Level.FINEST, "using history cache to retrieve last modified times for ''{0}}'' is disabled",

opengrok-indexer/src/main/java/org/opengrok/indexer/search/DirectoryEntry.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@
3535
public class DirectoryEntry {
3636

3737
private final File file;
38-
private final NullableNumLinesLOC extra;
38+
private NullableNumLinesLOC extra;
3939

4040
private String description;
4141

42+
private String pathDescription;
43+
4244
private Date date;
4345

4446
/**
@@ -77,6 +79,10 @@ public NullableNumLinesLOC getExtra() {
7779
return extra;
7880
}
7981

82+
public void setExtra(NullableNumLinesLOC extra) {
83+
this.extra = extra;
84+
}
85+
8086
public String getDescription() {
8187
return description;
8288
}
@@ -92,4 +98,12 @@ public Date getDate() {
9298
public void setDate(Date date) {
9399
this.date = date;
94100
}
101+
102+
public String getPathDescription() {
103+
return pathDescription;
104+
}
105+
106+
public void setPathDescription(String pathDescription) {
107+
this.pathDescription = pathDescription;
108+
}
95109
}

opengrok-indexer/src/main/java/org/opengrok/indexer/search/DirectoryExtraReader.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.search;
@@ -59,8 +59,7 @@ public class DirectoryExtraReader {
5959
* @return a list of results, limited to 2000 values
6060
* @throws IOException if an error occurs searching the index
6161
*/
62-
public List<NullableNumLinesLOC> search(IndexSearcher searcher, String path)
63-
throws IOException {
62+
public List<NullableNumLinesLOC> search(IndexSearcher searcher, String path) throws IOException {
6463
if (searcher == null) {
6564
throw new IllegalArgumentException("`searcher' is null");
6665
}
@@ -87,9 +86,7 @@ public List<NullableNumLinesLOC> search(IndexSearcher searcher, String path)
8786
"search.latency", new String[]{"category", "extra",
8887
"outcome", hits.scoreDocs.length > 0 ? "success" : "empty"});
8988

90-
List<NullableNumLinesLOC> results = processHits(searcher, hits);
91-
92-
return results;
89+
return processHits(searcher, hits);
9390
}
9491

9592
private List<NullableNumLinesLOC> processHits(IndexSearcher searcher, TopDocs hits)

opengrok-indexer/src/main/java/org/opengrok/indexer/util/FileExtraZipper.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@
1818
*/
1919

2020
/*
21+
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2122
* Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2223
*/
2324
package org.opengrok.indexer.util;
2425

2526
import java.io.File;
26-
import java.util.ArrayList;
2727
import java.util.HashMap;
2828
import java.util.List;
2929
import java.util.Map;
30-
import java.util.stream.Collectors;
3130

3231
import org.opengrok.indexer.analysis.NullableNumLinesLOC;
3332
import org.opengrok.indexer.search.DirectoryEntry;
@@ -41,29 +40,23 @@ public class FileExtraZipper {
4140

4241
/**
4342
* Merge the specified lists by looking up a possible entry in
44-
* {@code extras} for every element in {@code files}.
45-
* @param dir the files' directory
46-
* @param files the file names
47-
* @param extras some OpenGrok-analyzed extra metadata
48-
* @return a list of the same size as {@code files}
43+
* {@code extras} for every element in {@code entries}.
44+
*
45+
* @param entries list of {@link DirectoryEntry} instances
46+
* @param extras some OpenGrok-analyzed extra metadata
4947
*/
50-
public List<DirectoryEntry> zip(File dir, List<String> files, List<NullableNumLinesLOC> extras) {
48+
public void zip(List<DirectoryEntry> entries, List<NullableNumLinesLOC> extras) {
5149

5250
if (extras == null) {
53-
return files.stream().map(f -> new DirectoryEntry(new File(dir, f))).collect(Collectors.toList());
51+
return;
5452
}
5553

5654
Map<String, NullableNumLinesLOC> byName = indexExtraByName(extras);
5755

58-
List<DirectoryEntry> result = new ArrayList<>(files.size());
59-
for (String file : files) {
60-
File fileobj = new File(dir, file);
61-
NullableNumLinesLOC extra = findExtra(byName, fileobj);
62-
DirectoryEntry entry = new DirectoryEntry(fileobj, extra);
63-
result.add(entry);
56+
for (DirectoryEntry entry : entries) {
57+
NullableNumLinesLOC extra = findExtra(byName, entry.getFile());
58+
entry.setExtra(extra);
6459
}
65-
66-
return result;
6760
}
6861

6962
private NullableNumLinesLOC findExtra(Map<String, NullableNumLinesLOC> byName, File fileobj) {

opengrok-indexer/src/test/java/org/opengrok/indexer/history/HistoryGuruTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ void testGetLastHistoryEntriesWrtMergeCommits(boolean isMergeCommitsEnabled) thr
452452
}
453453
boolean useHistoryCacheForDirectoryListingOrig = env.isUseHistoryCacheForDirectoryListing();
454454
env.setUseHistoryCacheForDirectoryListing(true);
455-
boolean fallback = instance.getLastHistoryEntries(repositoryRoot, directoryEntries);
455+
boolean fallback = instance.fillLastHistoryEntries(repositoryRoot, directoryEntries);
456456
if (isMergeCommitsEnabled) {
457457
assertFalse(fallback);
458458
} else {

opengrok-indexer/src/test/java/org/opengrok/indexer/web/DummyHttpServletRequest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,17 @@ public String getPathTranslated() {
182182
throw new UnsupportedOperationException("Not supported yet.");
183183
}
184184

185+
private String contextPath;
186+
187+
public void setContextPath(String contextPath) {
188+
this.contextPath = contextPath;
189+
}
190+
185191
@Override
186192
public String getContextPath() {
193+
if (contextPath != null) {
194+
return contextPath;
195+
}
187196
throw new UnsupportedOperationException("Not supported yet.");
188197
}
189198

@@ -224,9 +233,18 @@ public StringBuffer getRequestURL() {
224233

225234
@Override
226235
public String getServletPath() {
236+
if (servletPath != null) {
237+
return servletPath;
238+
}
227239
throw new UnsupportedOperationException("Not supported yet.");
228240
}
229241

242+
private String servletPath;
243+
244+
public void setServletPath(String path) {
245+
this.servletPath = path;
246+
}
247+
230248
@Override
231249
public HttpSession getSession(boolean bln) {
232250
if (bln) {

0 commit comments

Comments
 (0)