优化数据库查询
1、去掉了RenderEntity表的主键 2、去掉了ReferenceEntity表的主键和 与RenderEntity表的关联外键 3、去掉了上面两个表的set,map集合改为int,String基础字段 4、对properties字段进行了压缩 5、将properties表中的linkpid字段移到RenderEntity中 6、查询语句修改
This commit is contained in:
@@ -1,7 +1,11 @@
|
||||
package com.navinfo.collect.library.data.entity
|
||||
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.navinfo.collect.library.utils.GeometryTools
|
||||
import com.navinfo.collect.library.utils.GeometryToolsKt
|
||||
import com.navinfo.collect.library.utils.StrZipUtil
|
||||
import io.realm.RealmDictionary
|
||||
import io.realm.RealmObject
|
||||
import io.realm.RealmSet
|
||||
@@ -20,9 +24,12 @@ open class ReferenceEntity() : RealmObject() {
|
||||
@Ignore
|
||||
lateinit var name: String //要素名
|
||||
lateinit var table: String //要素表名
|
||||
var propertiesDb: String = ""
|
||||
var code: String = "0" // 要素编码
|
||||
|
||||
@Ignore
|
||||
var zoomMin: Int = 18 //显示最小级别
|
||||
|
||||
@Ignore
|
||||
var zoomMax: Int = 23 //显示最大级别
|
||||
var taskId: Int = 0 //任务ID
|
||||
@@ -68,8 +75,21 @@ open class ReferenceEntity() : RealmObject() {
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
@Ignore
|
||||
var properties: RealmDictionary<String> = RealmDictionary()
|
||||
get() {
|
||||
if (propertiesDb.isNotEmpty() && field.isEmpty()) {
|
||||
try {
|
||||
val gson = Gson()
|
||||
val type = object : TypeToken<RealmDictionary<String>>() {}.type
|
||||
field = gson.fromJson(StrZipUtil.uncompress(propertiesDb), type)
|
||||
} catch (e: Exception) {
|
||||
Log.e("jingo","ReferenceEntity 转 properties $e")
|
||||
}
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
@Ignore
|
||||
var tileX: RealmSet<Int> = RealmSet() // x方向的tile编码
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.navinfo.collect.library.data.entity
|
||||
|
||||
import android.os.Parcelable
|
||||
import android.util.Log
|
||||
import com.google.gson.Gson
|
||||
import com.google.gson.reflect.TypeToken
|
||||
import com.navinfo.collect.library.system.Constant
|
||||
@@ -30,8 +31,7 @@ open class RenderEntity() : RealmObject(), Parcelable {
|
||||
lateinit var name: String //要素名
|
||||
lateinit var table: String //要素表名
|
||||
var code: String = "0" // 要素编码
|
||||
// var geometryDb: String = ""
|
||||
|
||||
var propertiesDb: String = ""
|
||||
var geometry: String =
|
||||
"" // 要素渲染参考的geometry,该数据可能会在导入预处理环节被修改,原始geometry会保存在properties的geometry字段下
|
||||
get() {
|
||||
@@ -73,7 +73,7 @@ open class RenderEntity() : RealmObject(), Parcelable {
|
||||
try {
|
||||
field = GeometryTools.createGeometry(geometry)
|
||||
} catch (e: Exception) {
|
||||
|
||||
Log.e("jingo","RenderEntity 转 wkt失败 $e")
|
||||
}
|
||||
}
|
||||
return field
|
||||
@@ -85,16 +85,15 @@ open class RenderEntity() : RealmObject(), Parcelable {
|
||||
if (propertiesDb != null && propertiesDb.isNotEmpty() && field.isEmpty()) {
|
||||
try {
|
||||
val gson = Gson()
|
||||
val type = object : TypeToken<List<RealmDictionary<String>>>() {}.type
|
||||
val type = object : TypeToken<RealmDictionary<String>>() {}.type
|
||||
field = gson.fromJson(StrZipUtil.uncompress(propertiesDb), type)
|
||||
} catch (e: Exception) {
|
||||
|
||||
Log.e("jingo","RenderEntity 转 properties $e")
|
||||
}
|
||||
}
|
||||
return field
|
||||
}
|
||||
|
||||
var propertiesDb: String = ""
|
||||
|
||||
@Ignore
|
||||
var tileX: RealmSet<Int> = RealmSet() // x方向的tile编码
|
||||
@@ -117,19 +116,4 @@ open class RenderEntity() : RealmObject(), Parcelable {
|
||||
constructor(name: String) : this() {
|
||||
this.name = name
|
||||
}
|
||||
|
||||
companion object {
|
||||
object LinkTable {
|
||||
//道路linkId
|
||||
const val linkPid = "linkPid"
|
||||
}
|
||||
|
||||
object LimitTable {
|
||||
const val linkPid = "linkPid"
|
||||
}
|
||||
|
||||
object KindCodeTable {
|
||||
const val linkPid = "linkPid"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.navinfo.collect.library.data.entity
|
||||
|
||||
import android.os.Parcelable
|
||||
import com.navinfo.collect.library.utils.GeometryTools
|
||||
import com.navinfo.collect.library.utils.GeometryToolsKt
|
||||
import io.realm.RealmDictionary
|
||||
import io.realm.RealmObject
|
||||
import io.realm.RealmSet
|
||||
import io.realm.annotations.Ignore
|
||||
import io.realm.annotations.Index
|
||||
import kotlinx.parcelize.Parcelize
|
||||
import org.locationtech.jts.geom.Geometry
|
||||
|
||||
/**
|
||||
* 渲染要素对应的实体
|
||||
* */
|
||||
@Parcelize
|
||||
open class RenderEntity1() : RealmObject(), Parcelable {
|
||||
lateinit var name: String //要素名
|
||||
lateinit var table: String //要素表名
|
||||
var code: String = "0" // 要素编码
|
||||
var geometry: String = ""
|
||||
var propertiesDb: String = ""
|
||||
var tileXMin: Int = 0
|
||||
var tileXMax: Int = 0
|
||||
var tileYMin: Int = 0
|
||||
var tileYMax: Int = 0
|
||||
var taskId: Int = 0 //任务ID
|
||||
var zoomMin: Int = 18 //显示最小级别
|
||||
var zoomMax: Int = 23 //显示最大级别
|
||||
var enable: Int = 0 // 默认0不是显示 1为渲染显示 2为常显
|
||||
var catchEnable: Int = 0 // 0不捕捉 1捕捉
|
||||
|
||||
var linkPid: String = "" // RenderEntity关联的linkPid集合(可能会关联多个)
|
||||
var linkRelation: LinkRelation? = null
|
||||
|
||||
constructor(name: String) : this() {
|
||||
this.name = name
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,8 @@ public class OMDBReferenceDataSource implements ITileDataSource {
|
||||
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||
@Override
|
||||
public void query(MapTile tile, ITileDataSink mapDataSink) {
|
||||
if(MapParamUtils.getTaskConfig() == null)
|
||||
return;
|
||||
// 获取tile对应的坐标范围
|
||||
if (tile.zoomLevel >= Constant.OMDB_MIN_ZOOM && tile.zoomLevel <= Constant.DATA_ZOOM) {
|
||||
Realm realm = Realm.getInstance(MapParamUtils.getTaskConfig());
|
||||
|
||||
@@ -64,16 +64,12 @@ public class OMDBTileDataSource implements ITileDataSource {
|
||||
@RequiresApi(api = Build.VERSION_CODES.N)
|
||||
@Override
|
||||
public void query(MapTile tile, ITileDataSink mapDataSink) {
|
||||
if(MapParamUtils.getTaskConfig() == null)
|
||||
return;
|
||||
// 获取tile对应的坐标范围
|
||||
if (tile.zoomLevel >= Constant.OMDB_MIN_ZOOM && tile.zoomLevel <= Constant.DATA_ZOOM) {
|
||||
Realm realm = null;
|
||||
int threadCode = Thread.currentThread().hashCode();
|
||||
// MapPosition pos = new MapPosition();
|
||||
// viewport.getMapPosition(pos);
|
||||
// DataObject newDataObject = new DataObject();
|
||||
// newDataObject.zoom = tile.zoomLevel;
|
||||
// newDataObject.threadCode = threadCode;
|
||||
// newDataObject.lonLat = pos.getX() + "," + pos.getY();
|
||||
synchronized (realmObjectList) {
|
||||
int configCode = MapParamUtils.getTaskConfig().hashCode();
|
||||
for (RealmObject object : realmObjectList) {
|
||||
@@ -96,7 +92,6 @@ public class OMDBTileDataSource implements ITileDataSource {
|
||||
realmObjectList.add(o);
|
||||
}
|
||||
}
|
||||
// Log.e("jingo", " " + Realm.getDefaultInstance().hashCode() + " " + Realm.getInstance(MapParamUtils.getTaskConfig()).hashCode());
|
||||
|
||||
// Realm realm = Realm.getInstance(MapParamUtils.getTaskConfig());
|
||||
RealmQuery<RenderEntity> realmQuery = realm.where(RenderEntity.class);
|
||||
@@ -112,7 +107,6 @@ public class OMDBTileDataSource implements ITileDataSource {
|
||||
yEnd = (int) (yEnd << m);
|
||||
}
|
||||
|
||||
final int currentTileX = xStart;
|
||||
if (isUpdate) {
|
||||
realm.refresh();
|
||||
isUpdate = false;
|
||||
@@ -124,18 +118,9 @@ public class OMDBTileDataSource implements ITileDataSource {
|
||||
} else {
|
||||
sql += " and enable>=0";
|
||||
}
|
||||
|
||||
realmQuery.rawPredicate(sql);
|
||||
if (MapParamUtils.getDataLayerEnum() != null) {
|
||||
MapParamUtils.getDataLayerEnum().getSql();
|
||||
}
|
||||
|
||||
// realmQuery.greaterThanOrEqualTo("tileXMin", xStart);
|
||||
// realmQuery.lessThanOrEqualTo("tileXMax", xEnd);
|
||||
// realmQuery.greaterThanOrEqualTo("tileYMin", yStart);
|
||||
// realmQuery.lessThanOrEqualTo("tileYMax", yEnd);
|
||||
// realmQuery.like("geometry","116.31509664888955 39.83318797612014 0");
|
||||
// 筛选不显示的数据
|
||||
// 筛选不显示的数据
|
||||
if (Constant.HAD_LAYER_INVISIABLE_ARRAY != null && Constant.HAD_LAYER_INVISIABLE_ARRAY.length > 0) {
|
||||
realmQuery.beginGroup();
|
||||
for (String type : Constant.HAD_LAYER_INVISIABLE_ARRAY) {
|
||||
@@ -144,43 +129,13 @@ public class OMDBTileDataSource implements ITileDataSource {
|
||||
realmQuery.endGroup();
|
||||
}
|
||||
long time = System.currentTimeMillis();
|
||||
List<RenderEntity> listResult = realmQuery/*.distinct("id")*/.findAll();
|
||||
List<RenderEntity> listResult = realmQuery.findAll();
|
||||
long newTime = System.currentTimeMillis() - time;
|
||||
|
||||
Log.e("jingo", "当前OMDBTileDataSource " + Thread.currentThread().hashCode() + " 当前realm " + realm.hashCode() + " 查询耗时" + newTime );
|
||||
Log.e("jingo", "当前OMDBTileDataSource " + Thread.currentThread().hashCode() + " 当前realm " + realm.hashCode() + " 查询耗时" + newTime + " 条数" + listResult.size());
|
||||
// 数据记录的tile号是以正外接tile号列表,此处过滤并未与当前tile相交的数据
|
||||
if (!listResult.isEmpty()) {
|
||||
Polygon tilePolygon = GeometryTools.getTilePolygon(tile);
|
||||
// System.out.println("第一条数据的最小x值:" + listResult.get(0).getTileX().stream().min(Integer::compare).get());
|
||||
// System.out.println("当前tile的:" + listResult.get(0).getTileX().stream().min(Integer::compare).get());
|
||||
// synchronized (dataObjectList) {
|
||||
// int index = -1;
|
||||
// for (int i = 0; i < dataObjectList.size(); i++) {
|
||||
// DataObject dataObject = dataObjectList.get(i);
|
||||
// if (dataObject.threadCode == newDataObject.threadCode) {
|
||||
// index = i;
|
||||
// } else if (dataObject.zoom == tile.zoomLevel && dataObject.lonLat.equals(newDataObject.lonLat)) {
|
||||
// listResult = listResult.stream().filter((RenderEntity renderEntity) -> {
|
||||
// for (String id : dataObject.listIds) {
|
||||
// if (id.equals(renderEntity.getId())) {
|
||||
// return false;
|
||||
// }
|
||||
// }
|
||||
// return renderEntity.getWkt().intersects(tilePolygon);
|
||||
// })
|
||||
// /*过滤数据,只有最小x(屏幕的最小x或数据的最小x会被渲染,跨Tile的其他数据不再重复渲染)*/
|
||||
//// .filter((RenderEntity renderEntity) -> MercatorProjection.longitudeToTileX(viewport.fromScreenPoint(0,0).getLongitude(), (byte) Constant.DATA_ZOOM) == currentTileX || renderEntity.getTileX().stream().min(Integer::compare).get() == currentTileX)
|
||||
// .collect(Collectors.toList());
|
||||
// }
|
||||
// }
|
||||
// if (index > -1) {
|
||||
// dataObjectList.remove(index);
|
||||
// }
|
||||
// for (RenderEntity renderEntity : listResult) {
|
||||
// newDataObject.listIds.add(renderEntity.getId());
|
||||
// }
|
||||
// dataObjectList.add(newDataObject);
|
||||
// }
|
||||
listResult = listResult.stream().filter((RenderEntity renderEntity) ->
|
||||
renderEntity.getWkt().intersects(tilePolygon)
|
||||
)
|
||||
@@ -192,12 +147,10 @@ public class OMDBTileDataSource implements ITileDataSource {
|
||||
} else {
|
||||
mapDataSink.completed(QueryResult.SUCCESS);
|
||||
}
|
||||
|
||||
// realm.close();
|
||||
} else {
|
||||
mapDataSink.completed(QueryResult.SUCCESS);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -35,6 +35,7 @@ public class RealmDBTileDataSource implements ITileDataSource {
|
||||
public void query(MapTile tile, ITileDataSink mapDataSink) {
|
||||
// 获取tile对应的坐标范围
|
||||
if (tile.zoomLevel>=15&&tile.zoomLevel<=Constant.OVER_ZOOM) {
|
||||
Log.e("jingo","RealmDBTileDataSource RealmDBTileDataSource RealmDBTileDataSource");
|
||||
// int m = Constant.OVER_ZOOM-tile.zoomLevel;
|
||||
// int xStart = (int)tile.tileX<<m;
|
||||
// int xEnd = (int)((tile.tileX+1)<<m);
|
||||
|
||||
Reference in New Issue
Block a user