@SuppressLint("Range") public SendFrameTimeMtc selectFrameTime(long sceneStartTime) { SendFrameTimeMtc sendFrameTimeMtc = new SendFrameTimeMtc(); Cursor cursor = mRDB.query(TABLE_FRAMETIME, null, "scene_start_time=?", new String[]{String.valueOf(sceneStartTime)}, null, null, "_id", null); int batch = 0; int curBatch = 0; long frameTime = 0; long curFrameTime = 0; if (cursor.getCount() > 0) { int batchIndex = cursor.getColumnIndex("batch"); int frameTimeIndex = cursor.getColumnIndex("frame_time"); SendFrameTimeEntity sendFrameTimeEntity = new SendFrameTimeEntity(); while (cursor.moveToNext()) { curFrameTime = cursor.getLong(frameTimeIndex); curBatch = cursor.getInt(batchIndex); if (sendFrameTimeEntity.getTs() == 0) { /** * 首次重置批次 */ sendFrameTimeEntity.setTs(curFrameTime); batch = curBatch; } else { if (batch == curBatch) { sendFrameTimeEntity.getFtList().add(curFrameTime - frameTime); frameTime = curFrameTime; } else { // 批次不同,重置 sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); sendFrameTimeEntity = new SendFrameTimeEntity(); sendFrameTimeEntity.setTs(curFrameTime); batch = curBatch; } } } sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); } return sendFrameTimeMtc; }
时间: 2024-02-10 11:34:38 浏览: 117
这段代码是一个方法,用于从数据库中查询某个场景的帧时间信息,并返回一个包含该场景所有帧时间的对象 SendFrameTimeMtc。该方法首先通过传入的场景开始时间,从数据库表 TABLE_FRAMETIME 中查询该场景的所有帧时间信息。然后,它会遍历查询结果,对于每一个批次的帧时间,将其时间差添加到 SendFrameTimeEntity 对象的 FtlList 中,并将该对象加入到 SendFrameTimeMtc 对象的 SendFrames 中。如果遍历过程中发现当前帧时间和上一次查询到的帧时间不在同一个批次,则会将上一个 SendFrameTimeEntity 对象加入到 SendFrameTimeMtc 中,并重置 SendFrameTimeEntity 对象的时间戳和批次号。最后,返回 SendFrameTimeMtc 对象,其中包含了该场景所有帧时间的信息。
相关问题
@SuppressLint("Range") public SendFrameTimeMtc selectFrameTime(long sceneStartTime) { SendFrameTimeMtc sendFrameTimeMtc = new SendFrameTimeMtc(); Cursor cursor = mRDB.query(TABLE_FRAMETIME, null, "scene_start_time=?", new String[]{String.valueOf(sceneStartTime)}, null, null, "_id", null); int batch = 0; long frameTime = 0; long curFrameTime = 0; if (cursor.getCount() > 0){ SendFrameTimeEntity sendFrameTimeEntity = new SendFrameTimeEntity(); while (cursor.moveToNext()) { if (sendFrameTimeEntity.getTs() == 0){ /** * 首次重置批次 */ frameTime = cursor.getLong(cursor.getColumnIndex("frame_time")); sendFrameTimeEntity.setTs(frameTime); batch = cursor.getInt(cursor.getColumnIndex("batch")); }else { if (batch == cursor.getInt(cursor.getColumnIndex("batch"))){ curFrameTime = cursor.getLong(cursor.getColumnIndex("frame_time")); sendFrameTimeEntity.getFtList().add(curFrameTime-frameTime); frameTime = curFrameTime; }else { // 批次不同,重置 sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); sendFrameTimeEntity = new SendFrameTimeEntity(); frameTime = cursor.getLong(cursor.getColumnIndex("frame_time")); sendFrameTimeEntity.setTs(frameTime); batch = cursor.getInt(cursor.getColumnIndex("batch")); } } } sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity); } return sendFrameTimeMtc; }
这段代码看起来已经比较简洁,但是还是可以进行一些优化:
1. 可以使用 try-with-resources 语句来自动关闭 cursor,避免忘记关闭而导致的资源泄漏。
2. 可以将 cursor.getColumnIndex() 方法的调用移到循环外部,避免在循环中重复调用,提升效率。
3. 可以将重复的代码抽取成方法,提高代码的复用性和可读性。
下面是优化后的代码:
```
@SuppressLint("Range")
public SendFrameTimeMtc selectFrameTime(long sceneStartTime) {
SendFrameTimeMtc sendFrameTimeMtc = new SendFrameTimeMtc();
SendFrameTimeEntity sendFrameTimeEntity = new SendFrameTimeEntity();
try (Cursor cursor = mRDB.query(TABLE_FRAMETIME, null, "scene_start_time=?", new String[]{String.valueOf(sceneStartTime)}, null, null, "_id", null)) {
int batchIndex = cursor.getColumnIndex("batch");
int frameTimeIndex = cursor.getColumnIndex("frame_time");
while (cursor.moveToNext()) {
int batch = cursor.getInt(batchIndex);
long frameTime = cursor.getLong(frameTimeIndex);
if (sendFrameTimeEntity.getTs() == 0) {
/** 首次重置批次 */
sendFrameTimeEntity.setTs(frameTime);
sendFrameTimeEntity.setBatch(batch);
} else {
if (batch == sendFrameTimeEntity.getBatch()) {
long curFrameTime = frameTime;
sendFrameTimeEntity.getFtList().add(curFrameTime - sendFrameTimeEntity.getTs());
sendFrameTimeEntity.setTs(curFrameTime);
} else {
// 批次不同,重置
sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity);
sendFrameTimeEntity = new SendFrameTimeEntity();
sendFrameTimeEntity.setTs(frameTime);
sendFrameTimeEntity.setBatch(batch);
}
}
}
sendFrameTimeMtc.getSendFrames().add(sendFrameTimeEntity);
}
return sendFrameTimeMtc;
}
```
注意:由于无法运行代码,以上修改可能存在错误,请仔细检查修改后的代码是否正确。
优化此段代码 @SuppressLint("Range") public JSONArray selectApmList(Long sceneStartTime) { JSONArray apmJsonArray = new JSONArray(); String selection = "scene_start_time=?"; String[] selectionArgs = {String.valueOf(sceneStartTime)}; String orderBy = "_id"; Cursor cursor = mRDB.query(TABLE_PERFORMANCE, null, selection, selectionArgs, null, null, orderBy, null); if (cursor.getCount() > 0) { int tsIndex = cursor.getColumnIndex("ts"); int pssIndex = cursor.getColumnIndex("pss"); int cpuIndex = cursor.getColumnIndex("cpu"); int currentIndex = cursor.getColumnIndex("current"); int voltageIndex = cursor.getColumnIndex("voltage"); int batteryPercentIndex = cursor.getColumnIndex("battery_percent"); while (cursor.moveToNext()) { JSONObject apmEntity = new JSONObject(); JsonUtils.put(apmEntity, ConfigConst.send_apmTsCollect, cursor.getLong(tsIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmPss, cursor.getLong(pssIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmCpu, cursor.getLong(cpuIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmCurrent, cursor.getLong(currentIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmVoltage, cursor.getLong(voltageIndex)); JsonUtils.put(apmEntity, ConfigConst.send_apmBatteryPercent, cursor.getLong(batteryPercentIndex)); apmJsonArray.put(apmEntity); } return apmJsonArray; } return apmJsonArray; }
可以考虑使用以下方式进行优化:
1. 将查询操作放在子线程中执行,避免在主线程中执行数据库操作导致UI卡顿;
2. 使用缓存机制,将查询结果缓存起来,避免重复查询数据库;
3. 对于查询结果为空的情况,可以直接返回空的JSONArray,无需返回一个空的JSONArray对象;
4. 对于Cursor对象,最好在使用完之后进行关闭操作,以释放资源。
优化后的代码如下所示:
@SuppressLint("Range")
public void selectApmList(final Long sceneStartTime, final Callback<JSONArray> callback) {
new Thread(new Runnable() {
@Override
public void run() {
JSONArray apmJsonArray = new JSONArray();
String selection = "scene_start_time=?";
String[] selectionArgs = {String.valueOf(sceneStartTime)};
String orderBy = "_id";
Cursor cursor = mRDB.query(TABLE_PERFORMANCE, null, selection, selectionArgs, null,
null, orderBy, null);
if (cursor != null && cursor.getCount() > 0 && cursor.moveToFirst()) {
int tsIndex = cursor.getColumnIndex("ts");
int pssIndex = cursor.getColumnIndex("pss");
int cpuIndex = cursor.getColumnIndex("cpu");
int currentIndex = cursor.getColumnIndex("current");
int voltageIndex = cursor.getColumnIndex("voltage");
int batteryPercentIndex = cursor.getColumnIndex("battery_percent");
do {
JSONObject apmEntity = new JSONObject();
JsonUtils.put(apmEntity, ConfigConst.send_apmTsCollect, cursor.getLong(tsIndex));
JsonUtils.put(apmEntity, ConfigConst.send_apmPss, cursor.getLong(pssIndex));
JsonUtils.put(apmEntity, ConfigConst.send_apmCpu, cursor.getLong(cpuIndex));
JsonUtils.put(apmEntity, ConfigConst.send_apmCurrent, cursor.getLong(currentIndex));
JsonUtils.put(apmEntity, ConfigConst.send_apmVoltage, cursor.getLong(voltageIndex));
JsonUtils.put(apmEntity, ConfigConst.send_apmBatteryPercent,
cursor.getLong(batteryPercentIndex));
apmJsonArray.put(apmEntity);
} while (cursor.moveToNext());
callback.onSuccess(apmJsonArray);
} else {
callback.onSuccess(null);
}
if (cursor != null) {
cursor.close();
}
}
}).start();
}
其中,Callback是自定义的回调接口,用于在查询完成后返回结果。
阅读全文