orm优化
This commit is contained in:
@ -41,7 +41,8 @@ final class ResumeAllCmd<T extends AbsTaskEntity> extends AbsNormalCmd<T> {
|
|||||||
private AbsTask createTask(DownloadEntity entity) {
|
private AbsTask createTask(DownloadEntity entity) {
|
||||||
AbsTask task = mQueue.getTask(entity);
|
AbsTask task = mQueue.getTask(entity);
|
||||||
if (task == null) {
|
if (task == null) {
|
||||||
DownloadTaskEntity taskEntity = new DownloadTaskEntity(entity);
|
DownloadTaskEntity taskEntity = new DownloadTaskEntity();
|
||||||
|
taskEntity.entity = entity;
|
||||||
task = mQueue.createTask(mTargetName, taskEntity);
|
task = mQueue.createTask(mTargetName, taskEntity);
|
||||||
} else {
|
} else {
|
||||||
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
|
Log.w(TAG, "添加命令执行失败,【该任务已经存在】");
|
||||||
|
@ -32,7 +32,8 @@ public class DownloadGroupTarget
|
|||||||
this.mEntity = entity;
|
this.mEntity = entity;
|
||||||
this.mTargetName = targetName;
|
this.mTargetName = targetName;
|
||||||
this.mUrls = urls;
|
this.mUrls = urls;
|
||||||
mTaskEntity = new DownloadGroupTaskEntity(entity);
|
mTaskEntity = new DownloadGroupTaskEntity();
|
||||||
|
mTaskEntity.entity = entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,10 +25,6 @@ public class DownloadGroupTaskEntity extends AbsTaskEntity<DownloadGroupEntity>
|
|||||||
|
|
||||||
@OneToOne(table = DownloadGroupEntity.class, key = "groupName") public DownloadGroupEntity entity;
|
@OneToOne(table = DownloadGroupEntity.class, key = "groupName") public DownloadGroupEntity entity;
|
||||||
|
|
||||||
public DownloadGroupTaskEntity(DownloadGroupEntity entity) {
|
|
||||||
this.entity = entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public DownloadGroupEntity getEntity() {
|
@Override public DownloadGroupEntity getEntity() {
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
@ -31,7 +31,8 @@ public class DownloadTarget extends
|
|||||||
DownloadTarget(DownloadEntity entity, String targetName) {
|
DownloadTarget(DownloadEntity entity, String targetName) {
|
||||||
mEntity = entity;
|
mEntity = entity;
|
||||||
mTargetName = targetName;
|
mTargetName = targetName;
|
||||||
mTaskEntity = new DownloadTaskEntity(entity);
|
mTaskEntity = new DownloadTaskEntity();
|
||||||
|
mTaskEntity.entity = mEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -29,10 +29,6 @@ public class DownloadTaskEntity extends AbsTaskEntity<DownloadEntity> {
|
|||||||
public DownloadTaskEntity() {
|
public DownloadTaskEntity() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public DownloadTaskEntity(DownloadEntity entity) {
|
|
||||||
this.entity = entity;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override public DownloadEntity getEntity() {
|
@Override public DownloadEntity getEntity() {
|
||||||
return entity;
|
return entity;
|
||||||
}
|
}
|
||||||
|
@ -242,7 +242,8 @@ public class DownloadGroupUtil implements IDownloadUtil {
|
|||||||
if (taskEntity != null) {
|
if (taskEntity != null) {
|
||||||
return taskEntity;
|
return taskEntity;
|
||||||
}
|
}
|
||||||
taskEntity = new DownloadTaskEntity(entity);
|
taskEntity = new DownloadTaskEntity();
|
||||||
|
taskEntity.entity = entity;
|
||||||
taskEntity.headers = mTaskEntity.headers;
|
taskEntity.headers = mTaskEntity.headers;
|
||||||
taskEntity.requestEnum = mTaskEntity.requestEnum;
|
taskEntity.requestEnum = mTaskEntity.requestEnum;
|
||||||
taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey;
|
taskEntity.redirectUrlKey = mTaskEntity.redirectUrlKey;
|
||||||
|
@ -117,8 +117,8 @@ class FileInfoThread implements Runnable {
|
|||||||
if (onFileInfoListener != null) {
|
if (onFileInfoListener != null) {
|
||||||
onFileInfoListener.onComplete(mEntity.getDownloadUrl(), code);
|
onFileInfoListener.onComplete(mEntity.getDownloadUrl(), code);
|
||||||
}
|
}
|
||||||
mEntity.update();
|
mEntity.save();
|
||||||
mTaskEntity.update();
|
mTaskEntity.save();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@ package com.arialyy.aria.core.inf;
|
|||||||
import com.arialyy.aria.core.RequestEnum;
|
import com.arialyy.aria.core.RequestEnum;
|
||||||
import com.arialyy.aria.orm.DbEntity;
|
import com.arialyy.aria.orm.DbEntity;
|
||||||
import com.arialyy.aria.orm.Ignore;
|
import com.arialyy.aria.orm.Ignore;
|
||||||
|
import com.arialyy.aria.orm.Primary;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
@ -29,7 +30,7 @@ public abstract class AbsTaskEntity<ENTITY extends AbsEntity> extends DbEntity {
|
|||||||
/**
|
/**
|
||||||
* Task实体对应的key
|
* Task实体对应的key
|
||||||
*/
|
*/
|
||||||
public String key;
|
@Primary public String key = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* http 请求头
|
* http 请求头
|
||||||
|
@ -53,6 +53,7 @@ public class DownloadGroupTaskQueue
|
|||||||
if (!TextUtils.isEmpty(targetName)) {
|
if (!TextUtils.isEmpty(targetName)) {
|
||||||
task = (DownloadGroupTask) TaskFactory.getInstance()
|
task = (DownloadGroupTask) TaskFactory.getInstance()
|
||||||
.createTask(targetName, entity, DownloadGroupSchedulers.getInstance());
|
.createTask(targetName, entity, DownloadGroupSchedulers.getInstance());
|
||||||
|
entity.key = entity.getEntity().getGroupName();
|
||||||
mCachePool.putTask(task);
|
mCachePool.putTask(task);
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "target name 为 null!!");
|
Log.e(TAG, "target name 为 null!!");
|
||||||
|
@ -118,6 +118,7 @@ public class DownloadTaskQueue
|
|||||||
if (!TextUtils.isEmpty(target)) {
|
if (!TextUtils.isEmpty(target)) {
|
||||||
task = (DownloadTask) TaskFactory.getInstance()
|
task = (DownloadTask) TaskFactory.getInstance()
|
||||||
.createTask(target, entity, DownloadSchedulers.getInstance());
|
.createTask(target, entity, DownloadSchedulers.getInstance());
|
||||||
|
entity.key = entity.getEntity().getDownloadUrl();
|
||||||
mCachePool.putTask(task);
|
mCachePool.putTask(task);
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "target name 为 null!!");
|
Log.e(TAG, "target name 为 null!!");
|
||||||
|
@ -54,12 +54,12 @@ public class UploadTaskQueue extends AbsTaskQueue<UploadTask, UploadTaskEntity,
|
|||||||
return AriaManager.getInstance(AriaManager.APP).getUploadConfig().oldMaxTaskNum;
|
return AriaManager.getInstance(AriaManager.APP).getUploadConfig().oldMaxTaskNum;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Override public UploadTask createTask(String targetName, UploadTaskEntity entity) {
|
@Override public UploadTask createTask(String targetName, UploadTaskEntity entity) {
|
||||||
UploadTask task = null;
|
UploadTask task = null;
|
||||||
if (!TextUtils.isEmpty(targetName)) {
|
if (!TextUtils.isEmpty(targetName)) {
|
||||||
task = (UploadTask) TaskFactory.getInstance()
|
task = (UploadTask) TaskFactory.getInstance()
|
||||||
.createTask(targetName, entity, UploadSchedulers.getInstance());
|
.createTask(targetName, entity, UploadSchedulers.getInstance());
|
||||||
|
entity.key = entity.getEntity().getFilePath();
|
||||||
mCachePool.putTask(task);
|
mCachePool.putTask(task);
|
||||||
} else {
|
} else {
|
||||||
Log.e(TAG, "target name 为 null是!!");
|
Log.e(TAG, "target name 为 null是!!");
|
||||||
|
@ -86,7 +86,7 @@ public class UploadReceiver extends AbsReceiver<UploadEntity> {
|
|||||||
for (UploadEntity entity : allEntity) {
|
for (UploadEntity entity : allEntity) {
|
||||||
if (entity.getState() == IEntity.STATE_RUNNING) {
|
if (entity.getState() == IEntity.STATE_RUNNING) {
|
||||||
stopCmds.add(
|
stopCmds.add(
|
||||||
CommonUtil.createCmd(targetName, new UploadTaskEntity(entity), NormalCmdFactory.TASK_STOP));
|
CommonUtil.createCmd(targetName, new UploadTaskEntity(), NormalCmdFactory.TASK_STOP));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
AriaManager.getInstance(AriaManager.APP).setCmds(stopCmds).exe();
|
AriaManager.getInstance(AriaManager.APP).setCmds(stopCmds).exe();
|
||||||
|
@ -27,7 +27,8 @@ public class UploadTarget extends AbsNormalTarget<UploadTarget, UploadEntity, Up
|
|||||||
UploadTarget(UploadEntity entity, String targetName) {
|
UploadTarget(UploadEntity entity, String targetName) {
|
||||||
this.mEntity = entity;
|
this.mEntity = entity;
|
||||||
this.mTargetName = targetName;
|
this.mTargetName = targetName;
|
||||||
mTaskEntity = new UploadTaskEntity(entity);
|
mTaskEntity = new UploadTaskEntity();
|
||||||
|
mTaskEntity.entity = entity;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -19,8 +19,10 @@ package com.arialyy.aria.orm;
|
|||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
import com.arialyy.aria.util.CommonUtil;
|
import com.arialyy.aria.util.CommonUtil;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Type;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by lyy on 2015/11/2.
|
* Created by lyy on 2015/11/2.
|
||||||
@ -156,7 +158,7 @@ public class DbEntity {
|
|||||||
|
|
||||||
private void updateRowID() {
|
private void updateRowID() {
|
||||||
try {
|
try {
|
||||||
Field[] fields = CommonUtil.getFields(getClass());
|
List<Field> fields = CommonUtil.getAllFields(getClass());
|
||||||
List<String> where = new ArrayList<>();
|
List<String> where = new ArrayList<>();
|
||||||
List<String> values = new ArrayList<>();
|
List<String> values = new ArrayList<>();
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
@ -165,7 +167,16 @@ public class DbEntity {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
where.add(field.getName());
|
where.add(field.getName());
|
||||||
values.add(field.get(this) + "");
|
Type type = field.getType();
|
||||||
|
if (SqlHelper.isOneToOne(field)) {
|
||||||
|
values.add(SqlHelper.getOneToOneParams(field));
|
||||||
|
} else if (type == List.class) {
|
||||||
|
values.add(SqlHelper.getListElementParams(field));
|
||||||
|
} else if (type == Map.class) {
|
||||||
|
values.add(SqlHelper.map2Str((Map<String, String>) field.get(this)));
|
||||||
|
} else {
|
||||||
|
values.add(field.get(this) + "");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DbEntity entity = findData(getClass(), where.toArray(new String[where.size()]),
|
DbEntity entity = findData(getClass(), where.toArray(new String[where.size()]),
|
||||||
values.toArray(new String[values.size()]));
|
values.toArray(new String[values.size()]));
|
||||||
|
@ -51,10 +51,6 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
private static final int FIND_ALL_DATA = 5;
|
private static final int FIND_ALL_DATA = 5;
|
||||||
private static final int DEL_DATA = 6;
|
private static final int DEL_DATA = 6;
|
||||||
|
|
||||||
private static final String MAP_FIELD = "map$$";
|
|
||||||
private static final String LIST_FIELD = "list$$";
|
|
||||||
private static final String GENERIC_FIELD = "Generic$$";
|
|
||||||
|
|
||||||
private static volatile SqlHelper INSTANCE = null;
|
private static volatile SqlHelper INSTANCE = null;
|
||||||
|
|
||||||
static SqlHelper init(Context context) {
|
static SqlHelper init(Context context) {
|
||||||
@ -380,7 +376,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
/**
|
/**
|
||||||
* 获取一对一参数
|
* 获取一对一参数
|
||||||
*/
|
*/
|
||||||
private static String getOneToOneParams(Field field) {
|
static String getOneToOneParams(Field field) {
|
||||||
OneToOne oneToOne = field.getAnnotation(OneToOne.class);
|
OneToOne oneToOne = field.getAnnotation(OneToOne.class);
|
||||||
if (oneToOne == null) {
|
if (oneToOne == null) {
|
||||||
throw new IllegalArgumentException("@OneToOne注解的对象必须要有@Primary注解的字段");
|
throw new IllegalArgumentException("@OneToOne注解的对象必须要有@Primary注解的字段");
|
||||||
@ -393,7 +389,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
*
|
*
|
||||||
* @param field list反射字段
|
* @param field list反射字段
|
||||||
*/
|
*/
|
||||||
private static String getListElementParams(Field field) {
|
static String getListElementParams(Field field) {
|
||||||
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
|
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
|
||||||
if (oneToMany == null) {
|
if (oneToMany == null) {
|
||||||
throw new IllegalArgumentException("List中元素必须被@OneToMany注解");
|
throw new IllegalArgumentException("List中元素必须被@OneToMany注解");
|
||||||
@ -600,15 +596,26 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
throw new IllegalArgumentException("List中的元素对象必须需要@Primary注解的字段");
|
throw new IllegalArgumentException("List中的元素对象必须需要@Primary注解的字段");
|
||||||
}
|
}
|
||||||
//list字段保存的数据
|
//list字段保存的数据
|
||||||
|
int kc = cursor.getColumnIndex(primaryKey);
|
||||||
String params = cursor.getString(column);
|
String params = cursor.getString(column);
|
||||||
|
String primaryData = cursor.getString(kc);
|
||||||
|
if (TextUtils.isEmpty(primaryData)) continue;
|
||||||
|
List<T> list = findForeignData(db, primaryData, params);
|
||||||
|
if (list == null) continue;
|
||||||
field.set(entity, findForeignData(db, primaryKey, params));
|
field.set(entity, findForeignData(db, primaryKey, params));
|
||||||
} else if (isOneToOne(field)) {
|
} else if (isOneToOne(field)) {
|
||||||
String primaryKey = getPrimaryName(clazz);
|
String primaryKey = getPrimaryName(clazz);
|
||||||
if (TextUtils.isEmpty(primaryKey)) {
|
if (TextUtils.isEmpty(primaryKey)) {
|
||||||
throw new IllegalArgumentException("@OneToOne的注解对象必须需要@Primary注解的字段");
|
throw new IllegalArgumentException("@OneToOne的注解对象必须需要@Primary注解的字段");
|
||||||
}
|
}
|
||||||
|
int kc = cursor.getColumnIndex(primaryKey);
|
||||||
String params = cursor.getString(column);
|
String params = cursor.getString(column);
|
||||||
field.set(entity, findForeignData(db, primaryKey, params).get(0));
|
String primaryData = cursor.getString(kc);
|
||||||
|
if (TextUtils.isEmpty(primaryData) || primaryData.equalsIgnoreCase("null")) continue;
|
||||||
|
List<T> list = findForeignData(db, primaryData, params);
|
||||||
|
if (list != null && list.size() > 1) {
|
||||||
|
field.set(entity, list.get(0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
|
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
|
||||||
@ -632,7 +639,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
*/
|
*/
|
||||||
private static <T extends DbEntity> List<T> findForeignData(SQLiteDatabase db, String primary,
|
private static <T extends DbEntity> List<T> findForeignData(SQLiteDatabase db, String primary,
|
||||||
String childParams) {
|
String childParams) {
|
||||||
String[] params = childParams.split("$$");
|
String[] params = childParams.split("\\$\\$");
|
||||||
return findData(db, params[0], params[1] + "=?", primary);
|
return findData(db, params[0], params[1] + "=?", primary);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -643,10 +650,13 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
private static Map<String, String> str2Map(String str) {
|
private static Map<String, String> str2Map(String str) {
|
||||||
String[] element = str.split(",");
|
|
||||||
Map<String, String> map = new HashMap<>();
|
Map<String, String> map = new HashMap<>();
|
||||||
|
if (TextUtils.isEmpty(str)) {
|
||||||
|
return map;
|
||||||
|
}
|
||||||
|
String[] element = str.split(",");
|
||||||
for (String data : element) {
|
for (String data : element) {
|
||||||
String[] s = data.split("$");
|
String[] s = data.split("\\$");
|
||||||
map.put(s[0], s[1]);
|
map.put(s[0], s[1]);
|
||||||
}
|
}
|
||||||
return map;
|
return map;
|
||||||
@ -658,7 +668,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
* {@code Map<String, String>}
|
* {@code Map<String, String>}
|
||||||
* </pre>
|
* </pre>
|
||||||
*/
|
*/
|
||||||
private static String map2Str(Map<String, String> map) {
|
static String map2Str(Map<String, String> map) {
|
||||||
StringBuilder sb = new StringBuilder();
|
StringBuilder sb = new StringBuilder();
|
||||||
Set<String> keys = map.keySet();
|
Set<String> keys = map.keySet();
|
||||||
for (String key : keys) {
|
for (String key : keys) {
|
||||||
@ -696,7 +706,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
/**
|
/**
|
||||||
* 判断是否一对多注解
|
* 判断是否一对多注解
|
||||||
*/
|
*/
|
||||||
private static boolean isOneToMany(Field field) {
|
static boolean isOneToMany(Field field) {
|
||||||
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
|
OneToMany oneToMany = field.getAnnotation(OneToMany.class);
|
||||||
return oneToMany != null;
|
return oneToMany != null;
|
||||||
}
|
}
|
||||||
@ -704,7 +714,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
/**
|
/**
|
||||||
* 判断是否是一对一注解
|
* 判断是否是一对一注解
|
||||||
*/
|
*/
|
||||||
private static boolean isOneToOne(Field field) {
|
static boolean isOneToOne(Field field) {
|
||||||
OneToOne oneToOne = field.getAnnotation(OneToOne.class);
|
OneToOne oneToOne = field.getAnnotation(OneToOne.class);
|
||||||
return oneToOne != null;
|
return oneToOne != null;
|
||||||
}
|
}
|
||||||
@ -712,7 +722,7 @@ final class SqlHelper extends SQLiteOpenHelper {
|
|||||||
/**
|
/**
|
||||||
* 判断是否是主键
|
* 判断是否是主键
|
||||||
*/
|
*/
|
||||||
private static boolean isPrimary(Field field) {
|
static boolean isPrimary(Field field) {
|
||||||
Primary pk = field.getAnnotation(Primary.class);
|
Primary pk = field.getAnnotation(Primary.class);
|
||||||
return pk != null;
|
return pk != null;
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,7 @@ public class UploadTaskEntity extends AbsTaskEntity<UploadEntity> {
|
|||||||
*/
|
*/
|
||||||
public Map<String, String> formFields = new HashMap<>();
|
public Map<String, String> formFields = new HashMap<>();
|
||||||
|
|
||||||
public UploadTaskEntity(UploadEntity entity) {
|
public UploadTaskEntity() {
|
||||||
this.entity = entity;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override public UploadEntity getEntity() {
|
@Override public UploadEntity getEntity() {
|
||||||
|
Reference in New Issue
Block a user