Skip to content

Commit d398c5b

Browse files
committed
Fixing code style.
And refactoring to bring the search function to under 50 lines.
1 parent 40e8743 commit d398c5b

File tree

3 files changed

+47
-41
lines changed

3 files changed

+47
-41
lines changed

src/main/scala/de/upb/cs/swt/delphi/cli/Config.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ case class Config(server: String = sys.env.getOrElse("DELPHI_SERVER", "https://d
3333
query : String = "",
3434
limit : Option[Int] = None,
3535
id : String = "",
36-
timeout : Int = 10,
36+
timeout : Option[Int] = None,
3737
args: List[String] = List(),
3838
opts: List[String] = List()) {
3939

src/main/scala/de/upb/cs/swt/delphi/cli/DelphiCLI.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package de.upb.cs.swt.delphi.cli
1818

1919
import akka.actor.ActorSystem
2020
import akka.http.scaladsl.Http
21-
import akka.stream.ActorMaterializer
2221
import de.upb.cs.swt.delphi.cli.commands.{RetrieveCommand, SearchCommand, TestCommand}
2322

2423
import scala.concurrent.duration.Duration
@@ -65,7 +64,7 @@ object DelphiCLI extends App {
6564
opt[String]("csv").action((x, c) => c.copy(csv = x)).text("Path to the output .csv file (overwrites existing file)"),
6665
opt[Int]("limit").action((x, c) => c.copy(limit = Some(x))).text("The maximal number of results returned."),
6766
opt[Unit](name="list").action((_, c) => c.copy(list = true)).text("Output results as list (raw option overrides this)"),
68-
opt[Int]("timeout").action((x, c) => c.copy(timeout = x)).text("Timeout in seconds.")
67+
opt[Int]("timeout").action((x, c) => c.copy(timeout = Some(x))).text("Timeout in seconds.")
6968
)
7069
}
7170
}

src/main/scala/de/upb/cs/swt/delphi/cli/commands/SearchCommand.scala

Lines changed: 45 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@
1616

1717
package de.upb.cs.swt.delphi.cli.commands
1818

19-
import java.util.concurrent.TimeoutException
20-
import java.util.concurrent.TimeUnit
19+
import java.util.concurrent.{TimeUnit, TimeoutException}
2120

2221
import akka.actor.ActorSystem
2322
import akka.http.scaladsl.Http
@@ -33,10 +32,13 @@ import de.upb.cs.swt.delphi.cli.artifacts.SearchResultJson._
3332
import spray.json.DefaultJsonProtocol
3433

3534
import scala.concurrent.duration._
36-
import scala.concurrent.{Await, Future}
37-
import scala.util.{Failure, Success}
35+
import scala.concurrent.{Await, ExecutionContextExecutor, Future}
36+
import scala.util.{Failure, Success, Try}
3837

3938
object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProtocol {
39+
40+
final var SEARCH_TIMEOUT: Int = 10
41+
4042
/**
4143
* Executes the command implementation
4244
*
@@ -59,47 +61,52 @@ object SearchCommand extends Command with SprayJsonSupport with DefaultJsonProto
5961
Http().singleRequest(HttpRequest(uri = searchUri, method = HttpMethods.POST, entity = entity))
6062
}
6163

62-
try {
63-
val response = Await.result(responseFuture, Duration(config.timeout + " seconds"))
64-
val resultFuture: Future[String] = response match {
65-
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
66-
entity.dataBytes.runFold(ByteString(""))(_ ++ _).map { body =>
67-
body.utf8String
68-
}
69-
case resp@HttpResponse(code, _, _, _) => {
70-
error(config)("Request failed, response code: " + code)
71-
resp.discardEntityBytes()
72-
Future("")
64+
Try(Await.result(responseFuture, Duration(config.timeout.getOrElse(SEARCH_TIMEOUT) + " seconds"))).
65+
map(response => parseResponse(response, config, start)(ec, materializer)).
66+
recover {
67+
case e : TimeoutException => {
68+
error(config)("The query timed out after " + (System.nanoTime() - start).nanos.toUnit(TimeUnit.SECONDS) +
69+
" seconds. To set a longer timeout, use the --timeout option.")
70+
Failure(e)
7371
}
7472
}
73+
}
7574

76-
val result = Await.result(resultFuture, Duration.Inf)
77-
78-
val took = (System.nanoTime() - start).nanos.toUnit(TimeUnit.SECONDS)
75+
private def parseResponse(response: HttpResponse, config: Config, start: Long)
76+
(implicit ec: ExecutionContextExecutor, materializer: ActorMaterializer): Unit = {
7977

80-
if (config.raw || result.equals("")) {
81-
reportResult(config)(result)
78+
val resultFuture: Future[String] = response match {
79+
case HttpResponse(StatusCodes.OK, headers, entity, _) =>
80+
entity.dataBytes.runFold(ByteString(""))(_ ++ _).map { body =>
81+
body.utf8String
82+
}
83+
case resp@HttpResponse(code, _, _, _) => {
84+
error(config)("Request failed, response code: " + code)
85+
resp.discardEntityBytes()
86+
Future("")
8287
}
88+
}
89+
90+
val result = Await.result(resultFuture, Duration.Inf)
8391

84-
if(!(config.raw || result.equals("")) || !config.csv.equals("")) {
85-
val unmarshalledFuture = Unmarshal(result).to[List[SearchResult]]
86-
87-
val processFuture = unmarshalledFuture.transform {
88-
case Success(unmarshalled) => {
89-
processResults(config, unmarshalled, took)
90-
Success(unmarshalled)
91-
}
92-
case Failure(e) => {
93-
error(config)(result)
94-
Failure(e)
95-
}
92+
val took = (System.nanoTime() - start).nanos.toUnit(TimeUnit.SECONDS)
93+
94+
if (config.raw || result.equals("")) {
95+
reportResult(config)(result)
96+
}
97+
98+
if(!(config.raw || result.equals("")) || !config.csv.equals("")) {
99+
val unmarshalledFuture = Unmarshal(result).to[List[SearchResult]]
100+
101+
val processFuture = unmarshalledFuture.transform {
102+
case Success(unmarshalled) => {
103+
processResults(config, unmarshalled, took)
104+
Success(unmarshalled)
105+
}
106+
case Failure(e) => {
107+
error(config)(result)
108+
Failure(e)
96109
}
97-
}
98-
} catch {
99-
case e : TimeoutException => {
100-
error(config)("The query timed out after " + (System.nanoTime() - start).nanos.toUnit(TimeUnit.SECONDS) +
101-
" seconds. To set a longer timeout, use the --timeout option.")
102-
Failure(e)
103110
}
104111
}
105112
}

0 commit comments

Comments
 (0)