优化数据库查询

This commit is contained in:
squallzhjch
2023-10-25 10:17:16 +08:00
parent 9cf39d476c
commit c7122376cf
19 changed files with 1301 additions and 840 deletions

View File

@@ -21,16 +21,7 @@ import java.util.*
* */
@Parcelize
open class LinkRelation() : RealmObject(), Parcelable {
@PrimaryKey
var linkPid:String = UUID.randomUUID().toString()
@Index
var linkPid:String = ""
var sNodeId: String? = null
@Index
var eNodeId: String? = null
var direct: Int = 0
constructor(direct: Int) : this() {
this.direct = direct
}
}

View File

@@ -14,21 +14,25 @@ import java.util.*
* 渲染要素对应的实体
* */
open class ReferenceEntity() : RealmObject() {
@PrimaryKey
var id: String = UUID.randomUUID().toString() // id
var renderEntityId: String = "" // 参考的renderEntity的Id
// @PrimaryKey
// var id: Int = 0 // id
// var renderEntityId: Int = 0 // 参考的renderEntity的Id
@Ignore
lateinit var name: String //要素名
lateinit var table: String //要素表名
var code: String = "0" // 要素编码
@Ignore
var zoomMin: Int = 18 //显示最小级别
@Ignore
var zoomMax: Int = 23 //显示最大级别
var taskId: Int = 0 //任务ID
var enable:Int = 0 // 默认0不是显示 1为渲染显示
var tileXMin:Int =0
var tileXMax:Int = 0
var tileYMin:Int =0
var tileYMax:Int = 0
var geometry: String = "" // 要素渲染参考的geometry该数据可能会在导入预处理环节被修改原始geometry会保存在properties的geometry字段下
var enable: Int = 0 // 默认0不是显示 1为渲染显示
var tileXMin: Int = 0
var tileXMax: Int = 0
var tileYMin: Int = 0
var tileYMax: Int = 0
var geometry: String =
"" // 要素渲染参考的geometry该数据可能会在导入预处理环节被修改原始geometry会保存在properties的geometry字段下
get() {
wkt = GeometryTools.createGeometry(field)
return field
@@ -64,11 +68,16 @@ open class ReferenceEntity() : RealmObject() {
}
return field
}
@Ignore
var properties: RealmDictionary<String> = RealmDictionary()
@Ignore
var tileX: RealmSet<Int> = RealmSet() // x方向的tile编码
@Ignore
var tileY: RealmSet<Int> = RealmSet() // y方向的tile编码
constructor(name: String): this() {
constructor(name: String) : this() {
this.name = name
}
}

View File

@@ -1,9 +1,12 @@
package com.navinfo.collect.library.data.entity
import android.os.Parcelable
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.navinfo.collect.library.system.Constant
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
@@ -15,24 +18,35 @@ import org.locationtech.jts.geom.Coordinate
import org.locationtech.jts.geom.Geometry
import org.oscim.core.MercatorProjection
import java.util.*
import java.util.zip.GZIPInputStream
/**
* 渲染要素对应的实体
* */
@Parcelize
open class RenderEntity() : RealmObject(), Parcelable {
@PrimaryKey
var id: String = UUID.randomUUID().toString() // id
// @PrimaryKey
// var id: String = UUID.randomUUID().toString() // id
lateinit var name: String //要素名
lateinit var table: String //要素表名
var code: String = "0" // 要素编码
var geometry: String = "" // 要素渲染参考的geometry该数据可能会在导入预处理环节被修改原始geometry会保存在properties的geometry字段下
// var geometryDb: String = ""
var geometry: String =
"" // 要素渲染参考的geometry该数据可能会在导入预处理环节被修改原始geometry会保存在properties的geometry字段下
get() {
wkt = GeometryTools.createGeometry(field)
return field
}
// get() {
// if (geometryDb != null && geometryDb.isNotEmpty() && field.isEmpty()) {
// field = StrZipUtil.uncompress(geometryDb)
// }
// return field
// }
set(value) {
field = value
// geometryDb = StrZipUtil.compress(value)
// 根据geometry自动计算当前要素的x-tile和y-tile
GeometryToolsKt.getTileXByGeometry(value, tileX)
@@ -64,22 +78,40 @@ open class RenderEntity() : RealmObject(), Parcelable {
}
return field
}
@Ignore
var properties: RealmDictionary<String> = RealmDictionary()
get() {
if (propertiesDb != null && propertiesDb.isNotEmpty() && field.isEmpty()) {
try {
val gson = Gson()
val type = object : TypeToken<List<RealmDictionary<String>>>() {}.type
field = gson.fromJson(StrZipUtil.uncompress(propertiesDb), type)
} catch (e: Exception) {
}
}
return field
}
var propertiesDb: String = ""
@Ignore
var tileX: RealmSet<Int> = RealmSet() // x方向的tile编码
@Ignore
var tileY: RealmSet<Int> = RealmSet() // y方向的tile编码
var tileXMin:Int =0
var tileXMax:Int = 0
var tileYMin:Int =0
var tileYMax:Int = 0
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捕捉
@Index
lateinit var linkPid: String // RenderEntity关联的linkPid集合(可能会关联多个)
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() {

View File

@@ -0,0 +1,40 @@
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
}
}

View File

@@ -110,13 +110,12 @@ public class OMDBReferenceDataSource implements ITileDataSource {
@Override
public void cancel() {
if (Realm.getDefaultInstance().isInTransaction()) {
Realm.getDefaultInstance().cancelTransaction();
}
// if (Realm.getDefaultInstance().isInTransaction()) {
// Realm.getDefaultInstance().cancelTransaction();
// }
}
public void update() {
isUpdate = true;
Log.e("qj", Thread.currentThread().getName());
}
}

View File

@@ -207,13 +207,12 @@ public class OMDBTileDataSource implements ITileDataSource {
@Override
public void cancel() {
if (Realm.getDefaultInstance().isInTransaction()) {
Realm.getDefaultInstance().cancelTransaction();
}
// if (Realm.getDefaultInstance().isInTransaction()) {
// Realm.getDefaultInstance().cancelTransaction();
// }
}
public void update() {
isUpdate = true;
Log.e("qj", Thread.currentThread().getName());
}
}

View File

@@ -27,7 +27,7 @@ public class OMDBTileSource extends RealmDBTileSource {
@Override
public OpenResult open() {
Log.d("qj", Realm.getDefaultInstance().where(RenderEntity.class).findAll().size()+"open安装数量");
// Log.d("qj", Realm.getDefaultInstance().where(RenderEntity.class).findAll().size()+"open安装数量");
return OpenResult.SUCCESS;
}

View File

@@ -35,27 +35,28 @@ public class RealmDBTileDataSource implements ITileDataSource {
public void query(MapTile tile, ITileDataSink mapDataSink) {
// 获取tile对应的坐标范围
if (tile.zoomLevel>=15&&tile.zoomLevel<=Constant.OVER_ZOOM) {
int m = Constant.OVER_ZOOM-tile.zoomLevel;
int xStart = (int)tile.tileX<<m;
int xEnd = (int)((tile.tileX+1)<<m);
int yStart = (int)tile.tileY<<m;
int yEnd = (int)((tile.tileY+1)<<m);
RealmQuery<GeometryFeatureEntity> realmQuery = Realm.getDefaultInstance().where(GeometryFeatureEntity.class)
.rawPredicate("tileX>="+xStart+" and tileX<="+xEnd+" and tileY>="+yStart+" and tileY<="+yEnd);
// 筛选不显示的数据
if (Constant.HAD_LAYER_INVISIABLE_ARRAY!=null&&Constant.HAD_LAYER_INVISIABLE_ARRAY.length>0) {
realmQuery.beginGroup();
for (String type: Constant.HAD_LAYER_INVISIABLE_ARRAY) {
realmQuery.notEqualTo("name", type);
}
realmQuery.endGroup();
}
List<GeometryFeatureEntity> listResult = realmQuery.distinct("id").findAll();
mThreadLocalDecoders.get().decode(tile, mapDataSink, listResult);
mapDataSink.completed(QueryResult.SUCCESS);
Realm.getDefaultInstance().close();
// int m = Constant.OVER_ZOOM-tile.zoomLevel;
// int xStart = (int)tile.tileX<<m;
// int xEnd = (int)((tile.tileX+1)<<m);
// int yStart = (int)tile.tileY<<m;
// int yEnd = (int)((tile.tileY+1)<<m);
//
// RealmQuery<GeometryFeatureEntity> realmQuery = Realm.getDefaultInstance().where(GeometryFeatureEntity.class)
// .rawPredicate("tileX>="+xStart+" and tileX<="+xEnd+" and tileY>="+yStart+" and tileY<="+yEnd);
// // 筛选不显示的数据
// if (Constant.HAD_LAYER_INVISIABLE_ARRAY!=null&&Constant.HAD_LAYER_INVISIABLE_ARRAY.length>0) {
// realmQuery.beginGroup();
// for (String type: Constant.HAD_LAYER_INVISIABLE_ARRAY) {
// realmQuery.notEqualTo("name", type);
// }
// realmQuery.endGroup();
// }
// List<GeometryFeatureEntity> listResult = realmQuery.distinct("id").findAll();
// mThreadLocalDecoders.get().decode(tile, mapDataSink, listResult);
// mapDataSink.completed(QueryResult.SUCCESS);
// Realm.getDefaultInstance().close();
// Log.d("RealmDBTileDataSource", "tile:"+tile.getBoundingBox().toString());
mapDataSink.completed(QueryResult.SUCCESS);
} else {
mapDataSink.completed(QueryResult.SUCCESS);
}
@@ -68,8 +69,8 @@ public class RealmDBTileDataSource implements ITileDataSource {
@Override
public void cancel() {
if (Realm.getInstance(RealmUtils.getInstance().getRealmConfiguration()).isInTransaction()) {
Realm.getInstance(RealmUtils.getInstance().getRealmConfiguration()).cancelTransaction();
}
// if (Realm.getInstance(RealmUtils.getInstance().getRealmConfiguration()).isInTransaction()) {
// Realm.getInstance(RealmUtils.getInstance().getRealmConfiguration()).cancelTransaction();
// }
}
}

View File

@@ -0,0 +1,282 @@
package com.navinfo.collect.library.utils
import sun.misc.BASE64Decoder
import sun.misc.BASE64Encoder
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.io.IOException
import java.util.*
import java.util.zip.*
object StrZipUtil {
/**
* @param input 需要压缩的字符串
* @return 压缩后的字符串
* @throws IOException IO
*/
fun compress(input: String): String {
if (input.isEmpty()) {
return input
}
try {
val out = ByteArrayOutputStream()
val gzipOs = GZIPOutputStream(out)
gzipOs.write(input.toByteArray())
gzipOs.close()
return BASE64Encoder().encode(out.toByteArray())
} catch (e: Exception) {
return input
}
}
/**
* @param zippedStr 压缩后的字符串
* @return 解压缩后的
* @throws IOException IO
*/
fun uncompress(zippedStr: String): String {
if (zippedStr.isEmpty()) {
return zippedStr
}
try {
val out = ByteArrayOutputStream()
val `in` = ByteArrayInputStream(
BASE64Decoder().decodeBuffer(zippedStr)
)
val gzipIs = GZIPInputStream(`in`)
val buffer = ByteArray(256)
var n: Int
while (gzipIs.read(buffer).also { n = it } >= 0) {
out.write(buffer, 0, n)
}
// toString()使用平台默认编码也可以显式的指定如toString("GBK")
return out.toString()
} catch (e: Exception) {
return zippedStr
}
}
/***
* 压缩GZip
*
* @param data
* @return
*/
fun gZip(data: ByteArray?): ByteArray? {
var b: ByteArray? = null
try {
val bos = ByteArrayOutputStream()
val gzip = GZIPOutputStream(bos)
gzip.write(data)
gzip.finish()
gzip.close()
b = bos.toByteArray()
bos.close()
} catch (ex: java.lang.Exception) {
ex.printStackTrace()
}
return b
}
/***
* 解压GZip
*
* @param data
* @return
*/
fun unGZip(data: ByteArray?): ByteArray? {
var b: ByteArray? = null
try {
val bis = ByteArrayInputStream(data)
val gzip = GZIPInputStream(bis)
val buf = ByteArray(1024)
var num = -1
val baos = ByteArrayOutputStream()
while (gzip.read(buf, 0, buf.size).also { num = it } != -1) {
baos.write(buf, 0, num)
}
b = baos.toByteArray()
baos.flush()
baos.close()
gzip.close()
bis.close()
} catch (ex: java.lang.Exception) {
ex.printStackTrace()
}
return b
}
/***
* 压缩Zip
*
* @param data
* @return
*/
fun zip(data: ByteArray): ByteArray? {
var b: ByteArray? = null
try {
val bos = ByteArrayOutputStream()
val zip = ZipOutputStream(bos)
val entry = ZipEntry("zip")
entry.size = data.size.toLong()
zip.putNextEntry(entry)
zip.write(data)
zip.closeEntry()
zip.close()
b = bos.toByteArray()
bos.close()
} catch (ex: java.lang.Exception) {
ex.printStackTrace()
}
return b
}
/***
* 解压Zip
*
* @param data
* @return
*/
fun unZip(data: ByteArray?): ByteArray? {
var b: ByteArray? = null
try {
val bis = ByteArrayInputStream(data)
val zip = ZipInputStream(bis)
while (zip.nextEntry != null) {
val buf = ByteArray(1024)
var num = -1
val baos = ByteArrayOutputStream()
while (zip.read(buf, 0, buf.size).also { num = it } != -1) {
baos.write(buf, 0, num)
}
b = baos.toByteArray()
baos.flush()
baos.close()
}
zip.close()
bis.close()
} catch (ex: java.lang.Exception) {
ex.printStackTrace()
}
return b
}
// /***
// * 压缩BZip2
// *
// * @param data
// * @return
// */
// public static byte[] bZip2(byte[] data) {
// byte[] b = null;
// try {
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);
// bzip2.write(data);
// bzip2.flush();
// bzip2.close();
// b = bos.toByteArray();
// bos.close();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return b;
// }
// /***
// * 解压BZip2
// *
// * @param data
// * @return
// */
// public static byte[] unBZip2(byte[] data) {
// byte[] b = null;
// try {
// ByteArrayInputStream bis = new ByteArrayInputStream(data);
// CBZip2InputStream bzip2 = new CBZip2InputStream(bis);
// byte[] buf = new byte[1024];
// int num = -1;
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// while ((num = bzip2.read(buf, 0, buf.length)) != -1) {
// baos.write(buf, 0, num);
// }
// b = baos.toByteArray();
// baos.flush();
// baos.close();
// bzip2.close();
// bis.close();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return b;
// }
// /***
// * 压缩BZip2
// *
// * @param data
// * @return
// */
// public static byte[] bZip2(byte[] data) {
// byte[] b = null;
// try {
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);
// bzip2.write(data);
// bzip2.flush();
// bzip2.close();
// b = bos.toByteArray();
// bos.close();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return b;
// }
// /***
// * 解压BZip2
// *
// * @param data
// * @return
// */
// public static byte[] unBZip2(byte[] data) {
// byte[] b = null;
// try {
// ByteArrayInputStream bis = new ByteArrayInputStream(data);
// CBZip2InputStream bzip2 = new CBZip2InputStream(bis);
// byte[] buf = new byte[1024];
// int num = -1;
// ByteArrayOutputStream baos = new ByteArrayOutputStream();
// while ((num = bzip2.read(buf, 0, buf.length)) != -1) {
// baos.write(buf, 0, num);
// }
// b = baos.toByteArray();
// baos.flush();
// baos.close();
// bzip2.close();
// bis.close();
// } catch (Exception ex) {
// ex.printStackTrace();
// }
// return b;
// }
/**
* 把字节数组转换成16进制字符串
*
* @param bArray
* @return
*/
fun bytesToHexString(bArray: ByteArray): String? {
val sb = StringBuffer(bArray.size)
var sTemp: String
for (i in bArray.indices) {
sTemp = Integer.toHexString(0xFF and bArray[i].toInt())
if (sTemp.length < 2) sb.append(0)
sb.append(sTemp.uppercase(Locale.getDefault()))
}
return sb.toString()
}
}