fix: 修改realm插入大量数据OOM问题

This commit is contained in:
2023-09-06 09:31:07 +08:00
parent b1e5e30880
commit ab266bce3e
2 changed files with 46 additions and 48 deletions

View File

@@ -177,7 +177,7 @@ class ImportOMDBHelper @AssistedInject constructor(
it.name == currentConfig.table it.name == currentConfig.table
} }
// 将listResult数据插入到Realm数据库中 // 将listResult数据插入到Realm数据库中
val listResult = mutableListOf<RenderEntity>() // val listResult = mutableListOf<RenderEntity>()
currentConfig?.let { currentConfig?.let {
val list = FileIOUtils.readFile2List(txtFile, "UTF-8") val list = FileIOUtils.readFile2List(txtFile, "UTF-8")
Log.d("ImportOMDBHelper", "开始解析:${txtFile?.name}") Log.d("ImportOMDBHelper", "开始解析:${txtFile?.name}")
@@ -332,28 +332,28 @@ class ImportOMDBHelper @AssistedInject constructor(
} }
} }
//交限增加相同LinkIn与LinkOut过滤原则 // //交限增加相同LinkIn与LinkOut过滤原则
if (renderEntity.code == DataCodeEnum.OMDB_RESTRICTION.code) { // if (renderEntity.code == DataCodeEnum.OMDB_RESTRICTION.code) {
if (renderEntity.properties.containsKey("linkIn") && renderEntity.properties.containsKey( // if (renderEntity.properties.containsKey("linkIn") && renderEntity.properties.containsKey(
"linkOut" // "linkOut"
) // )
) { // ) {
var linkIn = renderEntity.properties["linkIn"] // var linkIn = renderEntity.properties["linkIn"]
var linkOut = renderEntity.properties["linkOut"] // var linkOut = renderEntity.properties["linkOut"]
if (linkIn != null && linkOut != null) { // if (linkIn != null && linkOut != null) {
var checkMsg = "$linkIn$linkOut" // var checkMsg = "$linkIn$linkOut"
if (resHashMap.containsKey(checkMsg)) { // if (resHashMap.containsKey(checkMsg)) {
Log.e( // Log.e(
"qj", // "qj",
"${renderEntity.name}==过滤交限linkin与linkout相同且存在多条数据" // "${renderEntity.name}==过滤交限linkin与linkout相同且存在多条数据"
) // )
continue // continue
} else { // } else {
resHashMap.put(checkMsg, renderEntity) // resHashMap.put(checkMsg, renderEntity)
} // }
} // }
} // }
} // }
//遍历判断只显示与任务Link相关的任务数据 //遍历判断只显示与任务Link相关的任务数据
if (currentConfig.checkLinkId) { if (currentConfig.checkLinkId) {
@@ -619,17 +619,17 @@ class ImportOMDBHelper @AssistedInject constructor(
renderEntity.properties["startTime"] = "null" renderEntity.properties["startTime"] = "null"
} }
} }
listResult.add(renderEntity) // listResult.add(renderEntity)
realm.insert(renderEntity) realm.insert(renderEntity)
} }
} }
} }
} }
// 如果当前解析的是OMDB_RD_LINK数据将其缓存在预处理类中以便后续处理其他要素时使用 // // 如果当前解析的是OMDB_RD_LINK数据将其缓存在预处理类中以便后续处理其他要素时使用
if (currentConfig.table == "OMDB_RD_LINK") { // if (currentConfig.table == "OMDB_RD_LINK") {
importConfig.preProcess.cacheRdLink = // importConfig.preProcess.cacheRdLink =
listResult.associateBy { it.properties["linkPid"] } // listResult.associateBy { it.properties["linkPid"] }
} // }
realm.commitTransaction() realm.commitTransaction()
// 1个文件发送一次flow流 // 1个文件发送一次flow流
emit("${++processIndex}/${tableNum}") emit("${++processIndex}/${tableNum}")
@@ -637,8 +637,6 @@ class ImportOMDBHelper @AssistedInject constructor(
} catch (e: Exception) { } catch (e: Exception) {
realm.cancelTransaction() realm.cancelTransaction()
throw e throw e
} finally {
realm.close()
} }
} }
emit("finish") emit("finish")

View File

@@ -20,23 +20,23 @@ class ImportPreProcess {
val defaultTranslateDistance = 3.0 val defaultTranslateDistance = 3.0
val testFlag:Boolean = true val testFlag:Boolean = true
fun checkCircleRoad(renderEntity: RenderEntity): Boolean { fun checkCircleRoad(renderEntity: RenderEntity): Boolean {
val linkInId = renderEntity.properties["linkIn"] // val linkInId = renderEntity.properties["linkIn"]
val linkOutId = renderEntity.properties["linkOut"] // val linkOutId = renderEntity.properties["linkOut"]
// 根据linkIn和linkOut获取对应的link数据 // // 根据linkIn和linkOut获取对应的link数据
val linkInEntity = cacheRdLink[linkInId] // val linkInEntity = cacheRdLink[linkInId]
val linkOutEntity = cacheRdLink[linkOutId] // val linkOutEntity = cacheRdLink[linkOutId]
Log.d( // Log.d(
"checkCircleRoad", // "checkCircleRoad",
"LinkInEntity: ${linkInId}- ${linkInEntity?.properties?.get("snodePid")}LinkOutEntity: ${linkOutId}- ${ // "LinkInEntity: ${linkInId}- ${linkInEntity?.properties?.get("snodePid")}LinkOutEntity: ${linkOutId}- ${
linkOutEntity?.properties?.get("enodePid") // linkOutEntity?.properties?.get("enodePid")
}" // }"
) // )
// 查询linkIn的sNode和linkOut的eNode是否相同如果相同认为数据是环形路口返回false // // 查询linkIn的sNode和linkOut的eNode是否相同如果相同认为数据是环形路口返回false
if (linkInEntity != null && linkOutEntity != null) { // if (linkInEntity != null && linkOutEntity != null) {
if (linkInEntity.properties["snodePid"] == linkOutEntity.properties["enodePid"] || linkInEntity.properties["enodePid"] == linkOutEntity.properties["snodePid"] || linkInEntity.properties["snodePid"] == linkOutEntity.properties["snodePid"] || linkInEntity.properties["enodePid"] == linkOutEntity.properties["enodePid"]) { // if (linkInEntity.properties["snodePid"] == linkOutEntity.properties["enodePid"] || linkInEntity.properties["enodePid"] == linkOutEntity.properties["snodePid"] || linkInEntity.properties["snodePid"] == linkOutEntity.properties["snodePid"] || linkInEntity.properties["enodePid"] == linkOutEntity.properties["enodePid"]) {
return false // return false
} // }
} // }
return true return true
} }