Skip to content

Commit f197cd7

Browse files
author
Vladimir Kotal
authored
add API endpoint to refresh path descriptions (#2327)
fixes #2233
1 parent 6630f04 commit f197cd7

File tree

6 files changed

+104
-38
lines changed

6 files changed

+104
-38
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/configuration/Configuration.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import java.io.IOException;
3939
import java.io.InputStream;
4040
import java.io.OutputStream;
41+
import java.nio.file.Path;
42+
import java.nio.file.Paths;
4143
import java.util.ArrayList;
4244
import java.util.Collections;
4345
import java.util.HashMap;
@@ -233,7 +235,7 @@ public final class Configuration {
233235
* The name of the eftar file relative to the <var>DATA_ROOT</var>, which
234236
* contains definition tags.
235237
*/
236-
public static final String EFTAR_DTAGS_FILE = "index/dtags.eftar";
238+
public static final String EFTAR_DTAGS_NAME = "dtags.eftar";
237239
private transient File dtagsEftar = null;
238240

239241
/**
@@ -1221,13 +1223,20 @@ public String getForbiddenIncludeFileContent(boolean force) {
12211223
}
12221224

12231225
/**
1224-
* Get the eftar file, which contains definition tags.
1226+
* @return path to the file holding compiled path descriptions for the web application
1227+
*/
1228+
public Path getDtagsEftarPath() {
1229+
return Paths.get(getDataRoot(), "index", EFTAR_DTAGS_NAME);
1230+
}
1231+
1232+
/**
1233+
* Get the eftar file, which contains definition tags for path descriptions.
12251234
*
12261235
* @return {@code null} if there is no such file, the file otherwise.
12271236
*/
12281237
public File getDtagsEftar() {
12291238
if (dtagsEftar == null) {
1230-
File tmp = new File(getDataRoot() + "/" + EFTAR_DTAGS_FILE);
1239+
File tmp = getDtagsEftarPath().toFile();
12311240
if (tmp.canRead()) {
12321241
dtagsEftar = tmp;
12331242
}

opengrok-indexer/src/main/java/org/opengrok/indexer/web/EftarFile.java

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@
2626
import java.io.BufferedOutputStream;
2727
import java.io.BufferedReader;
2828
import java.io.DataOutputStream;
29+
import java.io.File;
2930
import java.io.FileNotFoundException;
3031
import java.io.FileOutputStream;
3132
import java.io.FileReader;
3233
import java.io.IOException;
3334
import java.io.RandomAccessFile;
35+
import java.io.StringReader;
3436
import java.util.Map;
3537
import java.util.StringTokenizer;
3638
import java.util.TreeMap;
37-
import java.util.logging.Level;
3839
import java.util.logging.Logger;
3940

4041
import org.opengrok.indexer.logger.LoggerFactory;
@@ -200,12 +201,23 @@ private void traverse(Node n) {
200201
}
201202
private Node root;
202203

203-
public void readInput(String tagsPath) throws IOException {
204-
try (BufferedReader r = new BufferedReader(new FileReader(tagsPath))) {
204+
public void readInput(File inputFile) throws IOException {
205+
try (BufferedReader r = new BufferedReader(new FileReader(inputFile))) {
205206
readInput(r);
206207
}
207208
}
208209

210+
public void readInput(String input) throws IOException {
211+
try (BufferedReader r = new BufferedReader(new StringReader(input))) {
212+
readInput(r);
213+
}
214+
}
215+
216+
/**
217+
* Reads the input into interim representation. Can be called multiple times.
218+
* @param r reader
219+
* @throws IOException
220+
*/
209221
private void readInput(BufferedReader r) throws IOException {
210222
if (root == null) {
211223
root = new Node(1, null);
@@ -240,32 +252,13 @@ public void write(String outPath) throws FileNotFoundException, IOException {
240252
}
241253
}
242254

243-
public void create(String[] args) throws IOException, FileNotFoundException {
244-
for (int i = 0; i < args.length - 1; i++) {
245-
readInput(args[i]);
246-
}
247-
write(args[args.length - 1]);
255+
public void create(File inputFile, String outputPath) throws IOException {
256+
readInput(inputFile);
257+
write(outputPath);
248258
}
249259

250-
/**
251-
* Main method is used to generate eftar file from the path description
252-
* file in the run scripts.
253-
*
254-
* @param args Input files and output file
255-
*/
256-
@SuppressWarnings("PMD.SystemPrintln")
257-
public static void main(String[] args) {
258-
if (args.length < 2) {
259-
System.err.println("Usage inputFile [inputFile ...] outputFile");
260-
System.exit(1);
261-
}
262-
263-
try {
264-
EftarFile ef = new EftarFile();
265-
ef.create(args);
266-
} catch (IOException e) {
267-
LOGGER.log(Level.WARNING, "EftarFile error", e);
268-
}
260+
public void create(String input, String outputPath) throws IOException, FileNotFoundException {
261+
readInput(input);
262+
write(outputPath);
269263
}
270-
271264
}

opengrok-indexer/src/main/java/org/opengrok/indexer/web/EftarFileReader.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public int getChildOffset() {
142142
public EftarFileReader(String file) throws FileNotFoundException {
143143
this(new File(file));
144144
}
145+
145146
public EftarFileReader(File file) throws FileNotFoundException {
146147
f = new RandomAccessFile(file, "r");
147148
isOpen = true;
@@ -174,6 +175,12 @@ public String getChildTag(FNode fn, String name) throws IOException {
174175
return null;
175176
}
176177

178+
/**
179+
* Get description for path
180+
* @param path path relative to source root
181+
* @return path description string
182+
* @throws IOException
183+
*/
177184
public String get(String path) throws IOException {
178185
StringTokenizer toks = new StringTokenizer(path, "/");
179186
f.seek(0);

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ public class EftarFileTest {
4545
public EftarFileTest() {
4646
}
4747

48+
private final static String PATH_STRING = "/path";
49+
4850
@BeforeClass
4951
public static void setUpClass() throws Exception {
5052
tsv = File.createTempFile("paths", ".tsv");
@@ -55,7 +57,7 @@ public static void setUpClass() throws Exception {
5557
out = new PrintWriter(new FileWriter(tsv));
5658
StringBuilder sb = new StringBuilder();
5759
for (int ii = 0; ii < 100; ii++) {
58-
sb.append("/path");
60+
sb.append(PATH_STRING);
5961
sb.append(Integer.toString(ii));
6062
out.print(sb.toString());
6163
out.print("\tDescription ");
@@ -65,13 +67,13 @@ public static void setUpClass() throws Exception {
6567
} finally {
6668
try { out.close(); } catch (Exception e) { }
6769
}
68-
//create eftar files
69-
String[] args = new String[2];
70-
args[0] = tsv.getAbsolutePath();
71-
args[1] = eftar.getAbsolutePath();
70+
71+
// Create eftar files.
72+
String inputFile = tsv.getAbsolutePath();
73+
String outputFile = eftar.getAbsolutePath();
7274

7375
EftarFile ef = new EftarFile();
74-
ef.create(args);
76+
ef.create(new File(inputFile), outputFile);
7577
}
7678

7779
@AfterClass
@@ -109,7 +111,7 @@ private void searchEftarFile(EftarFileReader er) throws IOException {
109111
match.append("Description ");
110112
int offset = match.length();
111113
for (int ii = 0; ii < 100; ii++) {
112-
sb.append("/path");
114+
sb.append(PATH_STRING);
113115
sb.append(Integer.toString(ii));
114116
match.setLength(offset);
115117
match.append(Integer.toString(ii));

opengrok-web/src/main/java/org/opengrok/web/api/v1/controller/SystemController.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323
package org.opengrok.web.api.v1.controller;
2424

2525
import org.opengrok.indexer.configuration.RuntimeEnvironment;
26+
import org.opengrok.indexer.web.EftarFile;
2627
import org.opengrok.web.api.v1.suggester.provider.service.SuggesterService;
2728

2829
import javax.inject.Inject;
2930
import javax.ws.rs.Consumes;
31+
import javax.ws.rs.POST;
3032
import javax.ws.rs.PUT;
3133
import javax.ws.rs.Path;
3234
import javax.ws.rs.core.MediaType;
35+
import java.io.IOException;
3336
import java.util.Collections;
3437

3538
@Path("/system")
@@ -53,4 +56,12 @@ public void refresh(final String project) {
5356
public void reloadIncludes() {
5457
env.reloadIncludeFiles(env.getConfiguration());
5558
}
59+
60+
@POST
61+
@Path("/pathdesc")
62+
@Consumes(MediaType.TEXT_PLAIN)
63+
public void loadPathDescriptions(final String input) throws IOException {
64+
EftarFile ef = new EftarFile();
65+
ef.create(input, env.getConfiguration().getDtagsEftarPath().toString());
66+
}
5667
}

opengrok-web/src/test/java/org/opengrok/web/api/v1/controller/SystemControllerTest.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,24 @@
88
import org.opengrok.indexer.configuration.Configuration;
99
import org.opengrok.indexer.configuration.RuntimeEnvironment;
1010
import org.opengrok.indexer.util.IOUtils;
11+
import org.opengrok.indexer.web.EftarFileReader;
1112
import org.opengrok.web.api.v1.RestApp;
1213

1314
import javax.ws.rs.client.Entity;
1415
import javax.ws.rs.core.Application;
1516
import javax.ws.rs.core.Response;
1617

1718
import java.io.File;
19+
import java.io.FileNotFoundException;
1820
import java.io.IOException;
1921
import java.io.PrintWriter;
2022
import java.nio.file.Files;
2123
import java.nio.file.Path;
24+
import java.nio.file.Paths;
2225

2326
import static org.junit.Assert.assertEquals;
2427
import static org.junit.Assert.assertNotEquals;
28+
import static org.junit.Assert.assertTrue;
2529

2630
public class SystemControllerTest extends JerseyTest {
2731

@@ -75,4 +79,44 @@ public void testIncludeReload() throws IOException {
7579
// Cleanup
7680
IOUtils.removeRecursive(includeRootPath);
7781
}
82+
83+
@Test
84+
public void testDtagsEftarReload() throws IOException {
85+
// The output file will be located in a directory under data root so create it first.
86+
Path dataRoot = Files.createTempDirectory("api_dtags_test");
87+
env.setDataRoot(dataRoot.toString());
88+
Paths.get(dataRoot.toString(), "index").toFile().mkdir();
89+
90+
// Create path descriptions string.
91+
StringBuilder sb = new StringBuilder();
92+
String[][] descriptions = {
93+
{ "/path1", "foo foo" },
94+
{ "/path2", "bar bar" }
95+
};
96+
97+
for (int i = 0; i < descriptions.length; i++) {
98+
sb.append(descriptions[i][0]);
99+
sb.append("\t");
100+
sb.append(descriptions[i][1]);
101+
sb.append("\n");
102+
}
103+
String input = sb.toString();
104+
105+
// Reload the contents via API call.
106+
Response r = target("system")
107+
.path("pathdesc")
108+
.request().post(Entity.text(input));
109+
assertEquals(Response.Status.NO_CONTENT.getStatusCode(), r.getStatus());
110+
111+
// Check
112+
Path eftarPath = env.getConfiguration().getDtagsEftarPath();
113+
assertTrue(eftarPath.toFile().exists());
114+
EftarFileReader er = new EftarFileReader(eftarPath.toString());
115+
for (int i = 0; i < descriptions.length; i++) {
116+
assertEquals(descriptions[i][1], er.get(descriptions[i][0]));
117+
}
118+
119+
// Cleanup
120+
IOUtils.removeRecursive(dataRoot);
121+
}
78122
}

0 commit comments

Comments
 (0)