Skip to content
Permalink
Browse files

fix bug #407

移除`DownloadGroupEntity`字段`groupHash`的主键约束,`DownloadEntity`字段`groupHash`的外键约束,`TaskRecord`字段`dGroupHash`的外键约束
优化关联查询的性能
  • Loading branch information...
AriaLyy committed Jun 11, 2019
1 parent b670e3e commit 8f8f8fa7d7fe89f9eeb5ab50402d303a331cd35f
Showing with 277 additions and 236 deletions.
  1. +13 −19 Aria/src/main/java/com/arialyy/aria/core/common/AbsFileer.java
  2. +35 −9 Aria/src/main/java/com/arialyy/aria/core/common/AbsThreadTask.java
  3. +1 −1 Aria/src/main/java/com/arialyy/aria/core/common/BaseListener.java
  4. +5 −3 Aria/src/main/java/com/arialyy/aria/core/common/NormalFileer.java
  5. +2 −6 Aria/src/main/java/com/arialyy/aria/core/common/TaskRecord.java
  6. +0 −1 Aria/src/main/java/com/arialyy/aria/core/common/http/HttpDelegate.java
  7. +10 −6 Aria/src/main/java/com/arialyy/aria/core/download/AbsGroupConfigHandler.java
  8. +2 −6 Aria/src/main/java/com/arialyy/aria/core/download/DownloadEntity.java
  9. +1 −1 Aria/src/main/java/com/arialyy/aria/core/download/DownloadGroupTask.java
  10. +6 −7 Aria/src/main/java/com/arialyy/aria/core/download/DownloadTask.java
  11. +15 −0 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveDelegate.java
  12. +9 −4 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveDownloadUtil.java
  13. +2 −11 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8LiveLoader.java
  14. +13 −0 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8TaskConfig.java
  15. +7 −0 Aria/src/main/java/com/arialyy/aria/core/download/m3u8/M3U8VodLoader.java
  16. +3 −3 Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupEntity.java
  17. +2 −2 Aria/src/main/java/com/arialyy/aria/core/inf/AbsGroupTask.java
  18. +2 −2 Aria/src/main/java/com/arialyy/aria/core/inf/AbsNormalTask.java
  19. +3 −4 Aria/src/main/java/com/arialyy/aria/core/inf/AbsTask.java
  20. +39 −1 Aria/src/main/java/com/arialyy/aria/core/manager/ThreadTaskManager.java
  21. +2 −1 Aria/src/main/java/com/arialyy/aria/orm/DBConfig.java
  22. +81 −141 Aria/src/main/java/com/arialyy/aria/orm/DelegateFind.java
  23. +14 −7 Aria/src/main/java/com/arialyy/aria/orm/SqlHelper.java
  24. +5 −0 DEV_LOG.md
  25. +1 −1 app/src/main/java/com/arialyy/simple/core/download/m3u8/M3U8VodModule.java
  26. +1 −0 app/src/main/java/com/arialyy/simple/core/download/multi_download/FileListAdapter.java
  27. +3 −0 app/src/main/java/com/arialyy/simple/core/download/multi_download/MultiTaskActivity.java
@@ -15,18 +15,15 @@
*/
package com.arialyy.aria.core.common;

import android.content.Context;
import android.os.Looper;
import com.arialyy.aria.core.AriaManager;
import android.util.SparseArray;
import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.core.inf.IEventListener;
import com.arialyy.aria.core.manager.ThreadTaskManager;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.CommonUtil;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@@ -40,10 +37,9 @@
protected IEventListener mListener;
protected TASK_WRAPPER mTaskWrapper;
protected ENTITY mEntity;
protected Context mContext;
protected File mTempFile; //文件
protected File mTempFile;

private Map<Integer, AbsThreadTask> mTask = new HashMap<>();
private SparseArray<AbsThreadTask> mTask = new SparseArray<>();
private ScheduledThreadPoolExecutor mTimer;

/**
@@ -58,7 +54,6 @@ protected AbsFileer(IEventListener listener, TASK_WRAPPER wrapper) {
mListener = listener;
mTaskWrapper = wrapper;
mEntity = mTaskWrapper.getEntity();
mContext = AriaManager.APP;
TAG = CommonUtil.getClassName(getClass());
}

@@ -82,7 +77,7 @@ public ENTITY getEntity() {
return mEntity;
}

public Map<Integer, AbsThreadTask> getTaskList() {
public SparseArray<AbsThreadTask> getTaskList() {
return mTask;
}

@@ -92,10 +87,8 @@ public ENTITY getEntity() {
private void resetState() {
closeTimer();
if (mTask != null && mTask.size() != 0) {
for (AbsThreadTask task : mTask.values()) {
if (task != null) {
task.breakTask();
}
for (int i = 0; i < mTask.size(); i++) {
mTask.valueAt(i).breakTask();
}
mTask.clear();
}
@@ -152,10 +145,9 @@ private synchronized void startTimer() {
|| mStateManager.isStop()
|| mStateManager.isCancel()
|| mStateManager.isFail()
|| !isRunning()) {
if (mStateManager.isComplete() || mStateManager.isFail()) {
ThreadTaskManager.getInstance().removeTaskThread(mTaskWrapper.getKey());
}
|| !isRunning()
|| isBreak()) {
ThreadTaskManager.getInstance().removeTaskThread(mTaskWrapper.getKey());
closeTimer();
} else if (mStateManager.getCurrentProgress() >= 0) {
mListener.onProgress(mStateManager.getCurrentProgress());
@@ -209,7 +201,8 @@ public synchronized void cancel() {
closeTimer();
isCancel = true;
onCancel();
for (AbsThreadTask task : mTask.values()) {
for (int i = 0; i < mTask.size(); i++) {
AbsThreadTask task = mTask.valueAt(i);
if (task != null && !task.isThreadComplete()) {
task.cancel();
}
@@ -229,7 +222,8 @@ public synchronized void stop() {
isStop = true;
onStop();
if (mStateManager.isComplete()) return;
for (AbsThreadTask task : mTask.values()) {
for (int i = 0; i < mTask.size(); i++) {
AbsThreadTask task = mTask.valueAt(i);
if (task != null && !task.isThreadComplete()) {
task.stop();
}
@@ -33,6 +33,7 @@
import com.arialyy.aria.exception.BaseException;
import com.arialyy.aria.util.ALog;
import com.arialyy.aria.util.BufferedRandomAccessFile;
import com.arialyy.aria.util.CommonUtil;
import com.arialyy.aria.util.ErrorHelp;
import com.arialyy.aria.util.NetUtils;
import java.io.File;
@@ -197,6 +198,7 @@ void breakTask() {
void sendState(int state, @Nullable Bundle bundle) {
Message msg = mStateHandler.obtainMessage();
msg.what = state;
msg.obj = this;
if (bundle != null) {
msg.setData(bundle);
}
@@ -242,7 +244,7 @@ protected boolean checkBlock() {
blockFile.delete();
ALog.i(TAG, String.format("删除分块【%s】成功", blockFile.getName()));
}
retryThis(isBreak());
retryBlockTask(isBreak());
return false;
}
return true;
@@ -316,7 +318,7 @@ public void cancel() {
* @param subCurrentLocation 当前线程下载进度
* @param ex 异常信息
*/
protected void fail(final long subCurrentLocation, BaseException ex) {
protected void fail(long subCurrentLocation, BaseException ex) {
fail(subCurrentLocation, ex, true);
}

@@ -331,10 +333,11 @@ protected void fail(final long subCurrentLocation, BaseException ex, boolean nee
}
if (mTaskWrapper.getRequestType() == ITaskWrapper.M3U8_VOD) {
writeConfig(false, 0);
retryM3U8Peer(needRetry);
} else {
if (mTaskWrapper.isSupportBP()) {
writeConfig(false, subCurrentLocation);
retryThis(needRetry && mConfig.startThreadNum != 1);
retryBlockTask(needRetry && mConfig.startThreadNum != 1);
} else {
ALog.e(TAG, String.format("任务【%s】执行失败", getFileName()));
ErrorHelp.saveError(TAG, "", ALog.getExceptionString(ex));
@@ -344,30 +347,53 @@ protected void fail(final long subCurrentLocation, BaseException ex, boolean nee
}

/**
* 重试当前线程,如果其中一条线程已经下载失败,则任务该任务下载失败,并且停止该任务的所有线程
* 重试ts分片
*/
private void retryM3U8Peer(boolean needRetry) {
if (!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry) {
ALog.w(TAG, String.format("任务【%s】重试失败,网络未连接", getFileName()));
sendFailMsg(null);
return;
}
if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected(AriaManager.APP)
|| isNotNetRetry) && !isBreak()) {
ALog.w(TAG, String.format("任务【%s】正在重试", getFileName()));
mFailTimes++;
mConfig.tempFile.delete();
CommonUtil.createFile(mConfig.tempFile.getPath());
ThreadTaskManager.getInstance().retryThread(this);
} else {
sendFailMsg(null);
}
}

/**
* 重试分块线程,如果其中一条线程已经下载失败,则任务该任务下载失败,并且停止该任务的所有线程
*
* @param needRetry 是否可以重试
*/
private void retryThis(boolean needRetry) {
private void retryBlockTask(boolean needRetry) {
if (!NetUtils.isConnected(AriaManager.APP) && !isNotNetRetry) {
ALog.w(TAG, String.format("任务【%s】重试失败,网络未连接", getFileName()));
sendFailMsg(null);
return;
}
if (mFailTimes < RETRY_NUM && needRetry && (NetUtils.isConnected(AriaManager.APP)
|| isNotNetRetry) && !isBreak()) {
ALog.w(TAG, String.format("任务【%s】正在重试", getFileName()));
mFailTimes++;
handleRetryRecord();
ThreadTaskManager.getInstance().retryThread(AbsThreadTask.this);
handleBlockRecord();
ThreadTaskManager.getInstance().retryThread(this);
} else {
sendFailMsg(null);
}
}

/**
* 处理线程重试的记录,只有多线程任务才会执行
* 处理线程重试的分块记录,只有多线程任务才会执行
* 如果是以前版本{@link BufferedRandomAccessFile}创建的下载,那么 record.startLocation不用修改
*/
private void handleRetryRecord() {
private void handleBlockRecord() {
if (mConfig.isBlock) {
// 默认线程分块长度
File temp = mConfig.tempFile;
@@ -35,7 +35,7 @@
import java.lang.ref.WeakReference;

public abstract class BaseListener<ENTITY extends AbsEntity, TASK_WRAPPER extends AbsTaskWrapper<ENTITY>,
TASK extends AbsTask<ENTITY, TASK_WRAPPER>>
TASK extends AbsTask<TASK_WRAPPER>>
implements IEventListener {
protected static String TAG;
private static final int RUN_SAVE_INTERVAL = 5 * 1000; //5s保存一次下载中的进度
@@ -55,7 +55,8 @@ protected NormalFileer(IEventListener listener, TASK_WRAPPER wrapper) {
*/
@Override
public void setMaxSpeed(int maxSpeed) {
for (AbsThreadTask task : getTaskList().values()) {
for (int i = 0; i < getTaskList().size(); i++) {
AbsThreadTask task = getTaskList().valueAt(i);
if (task != null && mStartThreadNum > 0) {
task.setMaxSpeed(maxSpeed / mStartThreadNum);
}
@@ -168,8 +169,9 @@ private void startThreadTask() {
} else {
mListener.onStart(mStateManager.getCurrentProgress());
}
for (AbsThreadTask task : getTaskList().values()) {
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), task);

for (int i = 0; i < getTaskList().size(); i++) {
ThreadTaskManager.getInstance().startThread(mTaskWrapper.getKey(), getTaskList().valueAt(i));
}
}

@@ -15,13 +15,10 @@
*/
package com.arialyy.aria.core.common;

import com.arialyy.aria.core.download.DownloadGroupEntity;
import com.arialyy.aria.orm.ActionPolicy;
import com.arialyy.aria.orm.DbEntity;
import com.arialyy.aria.orm.annotation.Foreign;
import com.arialyy.aria.orm.annotation.Ignore;
import com.arialyy.aria.orm.annotation.NoNull;
import com.arialyy.aria.orm.annotation.Primary;
import com.arialyy.aria.orm.annotation.Unique;
import java.util.List;

/**
@@ -44,7 +41,7 @@
/**
* 任务文件路径
*/
@Primary
@Unique
public String filePath;

/**
@@ -67,7 +64,6 @@
/**
* 下载任务组名
*/
@Foreign(parent = DownloadGroupEntity.class, column = "groupHash", onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
public String dGroupHash;

/**
@@ -25,7 +25,6 @@
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.core.inf.IHttpFileLenAdapter;
import com.arialyy.aria.core.inf.IHttpHeaderDelegate;
import com.arialyy.aria.core.inf.ITargetHandler;
import com.arialyy.aria.util.ALog;
import java.net.Proxy;
import java.util.Collection;
@@ -78,17 +78,15 @@ void reChangeDirPath(String newDirPath) {
ALog.d(TAG, String.format("修改新路径为:%s", newDirPath));
List<DTaskWrapper> subTasks = mWrapper.getSubTaskWrapper();
if (subTasks != null && !subTasks.isEmpty()) {
List<DownloadEntity> des = new ArrayList<>();
for (DTaskWrapper dte : subTasks) {
DownloadEntity de = dte.getEntity();
String oldPath = de.getDownloadPath();
String oldPath = de.getFilePath();
String newPath = newDirPath + "/" + de.getFileName();
File file = new File(oldPath);
if (file.exists()) {
file.renameTo(new File(newPath));
}
de.setDownloadPath(newPath);
des.add(de);
de.setFilePath(newPath);
}
}
}
@@ -103,12 +101,18 @@ void reChangeDirPath(String newDirPath) {
ALog.e(TAG, "文件夹路径不能为null");
return false;
} else if (!mDirPathTemp.startsWith("/")) {
ALog.e(TAG, "文件夹路径【" + mDirPathTemp + "】错误");
ALog.e(TAG, String.format("文件夹路径【%s】错误", mDirPathTemp));
return false;
}
File file = new File(mDirPathTemp);
if (file.isFile()) {
ALog.e(TAG, "路径【" + mDirPathTemp + "】是文件,请设置文件夹路径");
ALog.e(TAG, String.format("路径【%s】是文件,请设置文件夹路径", mDirPathTemp));
return false;
}

if ((getEntity().getDirPath() == null || !getEntity().getDirPath().equals(mDirPathTemp))
&& DbEntity.checkDataExist(DownloadGroupEntity.class, "dirPath=?", mDirPathTemp)) {
ALog.e(TAG, String.format("文件夹路径【%s】已被其它任务占用,请重新设置文件夹路径", mDirPathTemp));
return false;
}

@@ -21,23 +21,19 @@
import android.text.TextUtils;
import com.arialyy.aria.core.inf.AbsNormalEntity;
import com.arialyy.aria.core.inf.AbsTaskWrapper;
import com.arialyy.aria.orm.ActionPolicy;
import com.arialyy.aria.orm.annotation.Foreign;
import com.arialyy.aria.orm.annotation.Primary;
import com.arialyy.aria.orm.annotation.Unique;
import com.arialyy.aria.util.CommonUtil;

/**
* Created by lyy on 2015/12/25.
* 下载实体
*/
public class DownloadEntity extends AbsNormalEntity implements Parcelable {
@Primary private String downloadPath; //保存路径
@Unique private String downloadPath; //保存路径

/**
* 所属任务组
*/
@Foreign(parent = DownloadGroupEntity.class, column = "groupHash",
onUpdate = ActionPolicy.CASCADE, onDelete = ActionPolicy.CASCADE)
private String groupHash;

/**
@@ -32,7 +32,7 @@
* Created by AriaL on 2017/6/27.
* 任务组任务
*/
public class DownloadGroupTask extends AbsGroupTask<DownloadGroupEntity, DGTaskWrapper> {
public class DownloadGroupTask extends AbsGroupTask<DGTaskWrapper> {

private DownloadGroupTask(DGTaskWrapper taskWrapper, Handler outHandler) {
mTaskWrapper = taskWrapper;

0 comments on commit 8f8f8fa

Please sign in to comment.
You can’t perform that action at this time.