Skip to content

Commit 20573b3

Browse files
Extract duplicate code (#93)
* Extract duplicate code * Fix the comment
1 parent 0103c5a commit 20573b3

File tree

1 file changed

+25
-32
lines changed
  • nebula-algorithm/src/main/scala/com/vesoft/nebula/algorithm/config

1 file changed

+25
-32
lines changed

nebula-algorithm/src/main/scala/com/vesoft/nebula/algorithm/config/Configs.scala

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,20 @@ package com.vesoft.nebula.algorithm.config
88
import java.io.File
99
import java.nio.file.Files
1010
import org.apache.log4j.Logger
11+
1112
import scala.collection.JavaConverters._
1213
import com.typesafe.config.{Config, ConfigFactory}
14+
import com.vesoft.nebula.algorithm.config.Configs.readConfig
15+
1316
import scala.collection.mutable
1417

1518
/**
1619
* sparkConfig is used to submit spark application, such as graph algorithm
1720
*/
1821
object SparkConfigEntry {
1922
def apply(config: Config): SparkConfigEntry = {
20-
val map = mutable.Map[String, String]()
21-
val sparkConfig = config.getObject("spark")
22-
for (key <- sparkConfig.unwrapped().keySet().asScala) {
23-
val sparkKey = s"spark.${key}"
24-
if (config.getAnyRef(sparkKey).isInstanceOf[String]) {
25-
val sparkValue = config.getString(sparkKey)
26-
map += sparkKey -> sparkValue
27-
} else {
28-
for (subKey <- config.getObject(sparkKey).unwrapped().keySet().asScala) {
29-
val key = s"${sparkKey}.${subKey}"
30-
val sparkValue = config.getString(key)
31-
map += key -> sparkValue
32-
}
33-
}
34-
}
35-
SparkConfigEntry(map.toMap)
23+
val map = readConfig(config, "spark")
24+
SparkConfigEntry(map)
3625
}
3726
}
3827

@@ -41,22 +30,8 @@ object SparkConfigEntry {
4130
*/
4231
object AlgorithmConfigEntry {
4332
def apply(config: Config): AlgorithmConfigEntry = {
44-
val map = mutable.Map[String, String]()
45-
val algoConfig = config.getObject("algorithm")
46-
for (key <- algoConfig.unwrapped().keySet().asScala) {
47-
val algorithmKey = s"algorithm.${key}"
48-
if (config.getAnyRef(algorithmKey).isInstanceOf[String]) {
49-
val algorithmValue = config.getString(algorithmKey)
50-
map += algorithmKey -> algorithmValue
51-
} else {
52-
for (subkey <- config.getObject(algorithmKey).unwrapped().keySet().asScala) {
53-
val key = s"${algorithmKey}.${subkey}"
54-
val value = config.getString(key)
55-
map += key -> value
56-
}
57-
}
58-
}
59-
AlgorithmConfigEntry(map.toMap)
33+
val map = readConfig(config, "algorithm")
34+
AlgorithmConfigEntry(map)
6035
}
6136
}
6237

@@ -365,6 +340,24 @@ object Configs {
365340
}
366341
parser.parse(args, Argument())
367342
}
343+
344+
def readConfig(config: Config, name: String): Map[String, String] = {
345+
val map = mutable.Map[String, String]()
346+
val configObject = config.getObject(name)
347+
for (key <- configObject.unwrapped().keySet().asScala) {
348+
val refinedKey = s"$name.$key"
349+
config.getAnyRef(refinedKey) match {
350+
case stringValue: String => map += refinedKey -> stringValue
351+
case _ =>
352+
for (subKey <- config.getObject(refinedKey).unwrapped().keySet().asScala) {
353+
val refinedSubKey = s"$refinedKey.$subKey"
354+
val refinedSubValue = config.getString(refinedSubKey)
355+
map += refinedSubKey -> refinedSubValue
356+
}
357+
}
358+
}
359+
map.toMap
360+
}
368361
}
369362

370363
object AlgoConstants {

0 commit comments

Comments
 (0)