修复上3.3.9依然存在的数据库长度问题,修复删除记录的问题 https://github.com/AriaLyy/Aria/issues/181

pull/330/head
AriaLyy 7 years ago
parent d341f9dd9f
commit a903180491
  1. 2
      Aria/src/main/java/com/arialyy/aria/core/inf/AbsTarget.java
  2. 23
      Aria/src/main/java/com/arialyy/aria/core/manager/TEManager.java
  3. 2
      Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  4. 10
      Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  5. 23
      Aria/src/main/java/com/arialyy/aria/orm/SqlUtil.java
  6. 3
      app/src/main/java/com/arialyy/simple/download/SingleTaskActivity.java

@ -24,6 +24,7 @@ import com.arialyy.aria.core.command.normal.NormalCmdFactory;
import com.arialyy.aria.core.common.RequestEnum; import com.arialyy.aria.core.common.RequestEnum;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.manager.TEManager;
import com.arialyy.aria.core.upload.UploadTaskEntity; import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
@ -50,6 +51,7 @@ public abstract class AbsTarget<TARGET extends AbsTarget, ENTITY extends AbsEnti
cancel(); cancel();
} else { } else {
mTaskEntity.deleteData(); mTaskEntity.deleteData();
TEManager.getInstance().removeTEntity(mEntity.getKey());
} }
} }

@ -15,6 +15,7 @@
*/ */
package com.arialyy.aria.core.manager; package com.arialyy.aria.core.manager;
import android.support.v4.util.LruCache;
import com.arialyy.aria.core.download.DownloadGroupTaskEntity; import com.arialyy.aria.core.download.DownloadGroupTaskEntity;
import com.arialyy.aria.core.download.DownloadTaskEntity; import com.arialyy.aria.core.download.DownloadTaskEntity;
import com.arialyy.aria.core.inf.AbsEntity; import com.arialyy.aria.core.inf.AbsEntity;
@ -23,8 +24,6 @@ import com.arialyy.aria.core.upload.UploadTaskEntity;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil; import com.arialyy.aria.util.CommonUtil;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import java.util.concurrent.locks.ReentrantLock;
@ -35,7 +34,7 @@ import java.util.concurrent.locks.ReentrantLock;
public class TEManager { public class TEManager {
private static final String TAG = "TaskManager"; private static final String TAG = "TaskManager";
private static volatile TEManager INSTANCE = null; private static volatile TEManager INSTANCE = null;
private Map<String, AbsTaskEntity> map = new ConcurrentHashMap<>(); private LruCache<String, AbsTaskEntity> cache = new LruCache<>(1024);
private Lock lock; private Lock lock;
public static TEManager getInstance() { public static TEManager getInstance() {
@ -61,7 +60,7 @@ public class TEManager {
final Lock lock = this.lock; final Lock lock = this.lock;
lock.lock(); lock.lock();
try { try {
AbsTaskEntity tEntity = map.get(convertKey(key)); AbsTaskEntity tEntity = cache.get(convertKey(key));
if (tEntity == null || tEntity.getClass() != clazz) { if (tEntity == null || tEntity.getClass() != clazz) {
ITEntityFactory factory = chooseFactory(clazz); ITEntityFactory factory = chooseFactory(clazz);
if (factory == null) { if (factory == null) {
@ -69,7 +68,7 @@ public class TEManager {
return null; return null;
} }
tEntity = factory.create(key); tEntity = factory.create(key);
map.put(convertKey(key), tEntity); cache.put(convertKey(key), tEntity);
} }
return (TE) tEntity; return (TE) tEntity;
} finally { } finally {
@ -87,7 +86,7 @@ public class TEManager {
lock.lock(); lock.lock();
try { try {
String groupName = CommonUtil.getMd5Code(urls); String groupName = CommonUtil.getMd5Code(urls);
AbsTaskEntity tEntity = map.get(convertKey(groupName)); AbsTaskEntity tEntity = cache.get(convertKey(groupName));
if (tEntity == null || tEntity.getClass() != clazz) { if (tEntity == null || tEntity.getClass() != clazz) {
IGTEntityFactory factory = chooseGroupFactory(clazz); IGTEntityFactory factory = chooseGroupFactory(clazz);
if (factory == null) { if (factory == null) {
@ -95,7 +94,7 @@ public class TEManager {
return null; return null;
} }
tEntity = factory.create(groupName, urls); tEntity = factory.create(groupName, urls);
map.put(convertKey(groupName), tEntity); cache.put(convertKey(groupName), tEntity);
} }
return (TE) tEntity; return (TE) tEntity;
} finally { } finally {
@ -112,7 +111,7 @@ public class TEManager {
final Lock lock = this.lock; final Lock lock = this.lock;
lock.lock(); lock.lock();
try { try {
AbsTaskEntity tEntity = map.get(convertKey(absEntity.getKey())); AbsTaskEntity tEntity = cache.get(convertKey(absEntity.getKey()));
if (tEntity == null || tEntity.getClass() != clazz) { if (tEntity == null || tEntity.getClass() != clazz) {
ITEntityFactory factory = chooseFactory(clazz); ITEntityFactory factory = chooseFactory(clazz);
if (factory == null) { if (factory == null) {
@ -120,7 +119,7 @@ public class TEManager {
return null; return null;
} }
tEntity = factory.create(absEntity); tEntity = factory.create(absEntity);
map.put(convertKey(absEntity.getKey()), tEntity); cache.put(convertKey(absEntity.getKey()), tEntity);
} }
return (TE) tEntity; return (TE) tEntity;
} finally { } finally {
@ -153,7 +152,7 @@ public class TEManager {
final Lock lock = this.lock; final Lock lock = this.lock;
lock.lock(); lock.lock();
try { try {
AbsTaskEntity tEntity = map.get(convertKey(key)); AbsTaskEntity tEntity = cache.get(convertKey(key));
if (tEntity == null) { if (tEntity == null) {
return null; return null;
} else { } else {
@ -177,7 +176,7 @@ public class TEManager {
final Lock lock = this.lock; final Lock lock = this.lock;
lock.lock(); lock.lock();
try { try {
return map.put(convertKey(te.key), te) != null; return cache.put(convertKey(te.key), te) != null;
} finally { } finally {
lock.unlock(); lock.unlock();
} }
@ -190,7 +189,7 @@ public class TEManager {
final Lock lock = this.lock; final Lock lock = this.lock;
lock.lock(); lock.lock();
try { try {
return map.remove(convertKey(key)); return cache.remove(convertKey(key));
} finally { } finally {
lock.unlock(); lock.unlock();
} }

@ -34,7 +34,7 @@ import java.util.Map;
class DBConfig { class DBConfig {
static Map<String, Class> mapping = new HashMap<>(); static Map<String, Class> mapping = new HashMap<>();
static String DB_NAME; static String DB_NAME;
static int VERSION = 20; static int VERSION = 21;
static { static {
if (TextUtils.isEmpty(DB_NAME)) { if (TextUtils.isEmpty(DB_NAME)) {

@ -21,6 +21,7 @@ import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.util.LruCache;
import android.text.TextUtils; import android.text.TextUtils;
import com.arialyy.aria.core.AriaManager; import com.arialyy.aria.core.AriaManager;
import com.arialyy.aria.util.ALog; import com.arialyy.aria.util.ALog;
@ -49,6 +50,7 @@ final class SqlHelper extends SQLiteOpenHelper {
private static final int DEL_DATA = 6; private static final int DEL_DATA = 6;
private static volatile SqlHelper INSTANCE = null; private static volatile SqlHelper INSTANCE = null;
private static LruCache<Integer, DbEntity> mDataCache = new LruCache<>(1024);
static SqlHelper init(Context context) { static SqlHelper init(Context context) {
if (INSTANCE == null) { if (INSTANCE == null) {
@ -328,6 +330,7 @@ final class SqlHelper extends SQLiteOpenHelper {
db = checkDb(db); db = checkDb(db);
Class<?> clazz = dbEntity.getClass(); Class<?> clazz = dbEntity.getClass();
List<Field> fields = CommonUtil.getAllFields(clazz); List<Field> fields = CommonUtil.getAllFields(clazz);
DbEntity cacheEntity = mDataCache.get(dbEntity.hashCode());
if (fields != null && fields.size() > 0) { if (fields != null && fields.size() > 0) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
sb.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET "); sb.append("UPDATE ").append(CommonUtil.getClassName(dbEntity)).append(" SET ");
@ -337,8 +340,12 @@ final class SqlHelper extends SQLiteOpenHelper {
if (SqlUtil.ignoreField(field)) { if (SqlUtil.ignoreField(field)) {
continue; continue;
} }
sb.append(i > 0 ? ", " : "");
try { try {
if (cacheEntity != null && field.get(dbEntity) == field.get(cacheEntity)) {
continue;
}
sb.append(i > 0 ? ", " : "");
String value; String value;
sb.append(field.getName()).append("='"); sb.append(field.getName()).append("='");
Type type = field.getType(); Type type = field.getType();
@ -656,6 +663,7 @@ final class SqlHelper extends SQLiteOpenHelper {
} }
} }
entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid")); entity.rowID = cursor.getInt(cursor.getColumnIndex("rowid"));
mDataCache.put(entity.hashCode(), entity);
entitys.add(entity); entitys.add(entity);
} }
closeCursor(cursor); closeCursor(cursor);

@ -110,10 +110,19 @@ final class SqlUtil {
if (TextUtils.isEmpty(str)) { if (TextUtils.isEmpty(str)) {
return map; return map;
} }
boolean isDecode = false;
if (str.endsWith("_&_decode_&_")) {
isDecode = true;
str = str.substring(0, str.length() - 12);
}
String[] element = str.split(","); 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]); if (isDecode) {
map.put(CommonUtil.decryptBASE64(s[0]), CommonUtil.decryptBASE64(s[1]));
} else {
map.put(s[0], s[1]);
}
} }
return map; return map;
} }
@ -128,10 +137,18 @@ final class SqlUtil {
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) {
sb.append(key).append("$").append(map.get(key)).append(","); sb.append(CommonUtil.encryptBASE64(key))
.append("$")
.append(CommonUtil.encryptBASE64(map.get(key)))
.append(",");
} }
String str = sb.toString(); String str = sb.toString();
return TextUtils.isEmpty(str) ? str : str.substring(0, str.length() - 1); str = TextUtils.isEmpty(str) ? str : str.substring(0, str.length() - 1);
//3.3.10版本之前没有decode,需要加标志
if (map.size() != 0) {
str += "_&_decode_&_";
}
return str;
} }
/** /**

@ -209,7 +209,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
//Aria.download(this).load(DOWNLOAD_URL).removeRecord(); //Aria.download(this).load(DOWNLOAD_URL).removeRecord();
break; break;
case R.id.cancel: case R.id.cancel:
Aria.download(this).load(DOWNLOAD_URL).cancel(); //Aria.download(this).load(DOWNLOAD_URL).cancel();
Aria.download(this).load(DOWNLOAD_URL).removeRecord();
break; break;
} }
} }

Loading…
Cancel
Save