增加ftp服务器标志
This commit is contained in:
@@ -73,22 +73,7 @@ public class RecordHandler implements IRecordHandler {
|
||||
mAdapter.onPre();
|
||||
mTaskRecord = DbDataHelper.getTaskRecord(getFilePath(), mEntity.getTaskType());
|
||||
if (mTaskRecord == null) {
|
||||
if (!new File(getFilePath()).exists()){
|
||||
FileUtil.createFile(getFilePath());
|
||||
}
|
||||
initRecord(true);
|
||||
} else {
|
||||
File file = new File(mTaskRecord.filePath);
|
||||
if (!file.exists()) {
|
||||
ALog.w(TAG, String.format("文件【%s】不存在,重新分配线程区间", mTaskRecord.filePath));
|
||||
DbEntity.deleteData(ThreadRecord.class, "taskKey=?", mTaskRecord.filePath);
|
||||
mTaskRecord.threadRecords.clear();
|
||||
mTaskRecord.threadNum = mAdapter.initTaskThreadNum();
|
||||
initRecord(false);
|
||||
} else if (mTaskRecord.threadRecords == null || mTaskRecord.threadRecords.isEmpty()) {
|
||||
mTaskRecord.threadNum = mAdapter.initTaskThreadNum();
|
||||
initRecord(false);
|
||||
}
|
||||
}
|
||||
mAdapter.handlerTaskRecord(mTaskRecord);
|
||||
}
|
||||
|
||||
@@ -149,28 +149,29 @@ public class RecordHelper {
|
||||
* 处理单线程的任务的记录
|
||||
*/
|
||||
public void handleSingleThreadRecord() {
|
||||
File file = new File(mTaskRecord.filePath);
|
||||
// mTaskRecord.isBlock是为了兼容以前的文件格式
|
||||
File file = new File(
|
||||
mTaskRecord.isBlock ? String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, 0)
|
||||
: mTaskRecord.filePath);
|
||||
ThreadRecord tr = mTaskRecord.threadRecords.get(0);
|
||||
if (!file.exists()) {
|
||||
ALog.w(TAG, String.format("文件【%s】不存在,任务将重新开始", file.getPath()));
|
||||
tr.startLocation = 0;
|
||||
tr.isComplete = false;
|
||||
tr.endLocation = mWrapper.getEntity().getFileSize();
|
||||
} else if (mTaskRecord.isBlock) {
|
||||
if (file.length() > mWrapper.getEntity().getFileSize()) {
|
||||
ALog.i(TAG, String.format("文件【%s】错误,任务重新开始", file.getPath()));
|
||||
FileUtil.deleteFile(file);
|
||||
tr.startLocation = 0;
|
||||
} else if (file.length() > mWrapper.getEntity().getFileSize()) {
|
||||
ALog.i(TAG, String.format("文件【%s】错误,任务重新开始", file.getPath()));
|
||||
FileUtil.deleteFile(file);
|
||||
tr.startLocation = 0;
|
||||
tr.isComplete = false;
|
||||
tr.endLocation = mWrapper.getEntity().getFileSize();
|
||||
} else if (file.length() == mWrapper.getEntity().getFileSize()) {
|
||||
tr.isComplete = true;
|
||||
} else {
|
||||
if (file.length() != tr.startLocation) {
|
||||
ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length()));
|
||||
tr.startLocation = file.length();
|
||||
tr.isComplete = false;
|
||||
tr.endLocation = mWrapper.getEntity().getFileSize();
|
||||
} else if (file.length() == mWrapper.getEntity().getFileSize()) {
|
||||
tr.isComplete = true;
|
||||
} else {
|
||||
if (file.length() != tr.startLocation) {
|
||||
ALog.i(TAG, String.format("修正【%s】的进度记录为:%s", file.getPath(), file.length()));
|
||||
tr.startLocation = file.length();
|
||||
tr.isComplete = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
mWrapper.setNewTask(false);
|
||||
|
||||
@@ -202,6 +202,11 @@ public class ThreadStateManager implements IThreadState {
|
||||
* @return {@code true} 合并成功,{@code false}合并失败
|
||||
*/
|
||||
private boolean mergeFile() {
|
||||
if (mTaskRecord.threadNum == 1) {
|
||||
File partFile = new File(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, 0));
|
||||
return partFile.renameTo(new File(mTaskRecord.filePath));
|
||||
}
|
||||
|
||||
List<String> partPath = new ArrayList<>();
|
||||
for (int i = 0, len = mTaskRecord.threadNum; i < len; i++) {
|
||||
partPath.add(String.format(IRecordHandler.SUB_PATH, mTaskRecord.filePath, i));
|
||||
|
||||
@@ -34,7 +34,7 @@ class DBConfig {
|
||||
static boolean DEBUG = false;
|
||||
static Map<String, Class<? extends DbEntity>> mapping = new LinkedHashMap<>();
|
||||
static String DB_NAME;
|
||||
static int VERSION = 57;
|
||||
static int VERSION = 58;
|
||||
|
||||
/**
|
||||
* 是否将数据库保存在Sd卡,{@code true} 是
|
||||
|
||||
@@ -28,6 +28,7 @@ import com.arialyy.aria.util.ALog;
|
||||
import java.io.File;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
@@ -163,7 +164,49 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
for (String tableName : tables) {
|
||||
Class<? extends DbEntity> clazz = DBConfig.mapping.get(tableName);
|
||||
if (SqlUtil.tableExists(db, clazz)) {
|
||||
//修改表名为中介表名
|
||||
// ----------- 1、获取旧表字段、新表字段
|
||||
Cursor columnC =
|
||||
db.rawQuery(String.format("PRAGMA table_info(%s)", tableName), null);
|
||||
|
||||
// 获取新表的所有字段名称
|
||||
List<String> newTabColumns = SqlUtil.getColumns(clazz);
|
||||
// 获取旧表的所有字段名称
|
||||
List<String> oldTabColumns = new ArrayList<>();
|
||||
|
||||
while (columnC.moveToNext()) {
|
||||
String columnName = columnC.getString(columnC.getColumnIndex("name"));
|
||||
oldTabColumns.add(columnName);
|
||||
}
|
||||
columnC.close();
|
||||
|
||||
// ----------- 2、为防止字段增加失败的情况,先给旧表增加字段
|
||||
List<String> newAddColum = getNewColumn(newTabColumns, oldTabColumns);
|
||||
// 删除重命名的字段
|
||||
Map<String, String > modifyMap = null;
|
||||
if (modifyColumns != null){
|
||||
modifyMap = modifyColumns.get(tableName);
|
||||
if (modifyMap != null){
|
||||
Iterator<String> it = newAddColum.iterator();
|
||||
while (it.hasNext()){
|
||||
String s = it.next();
|
||||
if (modifyMap.get(s) != null){
|
||||
it.remove();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 给旧表增加字段,防止新增字段失败
|
||||
if (newAddColum.size() > 0){
|
||||
String sql = "ALTER TABLE %s ADD COLUMN %s %s";
|
||||
for (String nc : newAddColum){
|
||||
String temp = String.format(sql, tableName, nc, SqlUtil.getColumnTypeByFieldName(clazz, nc));
|
||||
ALog.d(TAG, "添加表字段的sql:" + temp);
|
||||
db.execSQL(temp);
|
||||
}
|
||||
}
|
||||
|
||||
// ----------- 3、将旧表备份下,并创建新表
|
||||
String alertSql = String.format("ALTER TABLE %s RENAME TO %s_temp", tableName, tableName);
|
||||
db.execSQL(alertSql);
|
||||
|
||||
@@ -176,28 +219,15 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
long count = cursor.getLong(0);
|
||||
cursor.close();
|
||||
|
||||
// 复制数据
|
||||
// ----------- 4、将旧表数据复制到新表
|
||||
if (count > 0) {
|
||||
Cursor columnC =
|
||||
db.rawQuery(String.format("PRAGMA table_info(%s_temp)", tableName), null);
|
||||
|
||||
// 获取新表的所有字段名称
|
||||
List<String> newTabColumns = SqlUtil.getColumns(clazz);
|
||||
// 获取旧表的所有字段名称
|
||||
List<String> oldTabColumns = new ArrayList<>();
|
||||
|
||||
while (columnC.moveToNext()) {
|
||||
String columnName = columnC.getString(columnC.getColumnIndex("name"));
|
||||
oldTabColumns.add(columnName);
|
||||
}
|
||||
columnC.close();
|
||||
|
||||
// 旧表需要删除的字段,删除旧表有而新表没的字段
|
||||
List<String> diffTab = getDiffColumn(newTabColumns, oldTabColumns);
|
||||
StringBuilder params = new StringBuilder();
|
||||
|
||||
// 需要修改的列名映射表
|
||||
Map<String, String> modifyMap = null;
|
||||
//Map<String, String> modifyMap = null;
|
||||
if (modifyColumns != null) {
|
||||
modifyMap = modifyColumns.get(tableName);
|
||||
}
|
||||
@@ -227,11 +257,11 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
String insertSql =
|
||||
String.format("INSERT INTO %s (%s) SELECT %s FROM %s_temp", tableName, newParamStr,
|
||||
oldParamStr, tableName);
|
||||
ALog.d(TAG, "insertSql = " + insertSql);
|
||||
ALog.d(TAG, "恢复数据的sql:" + insertSql);
|
||||
|
||||
db.execSQL(insertSql);
|
||||
}
|
||||
//删除中介表
|
||||
// ----------- 5、删除备份的表
|
||||
SqlUtil.dropTable(db, tableName + "_temp");
|
||||
} else {
|
||||
SqlUtil.createTable(db, clazz);
|
||||
@@ -258,6 +288,19 @@ final class SqlHelper extends SQLiteOpenHelper {
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取新增字段
|
||||
*
|
||||
* @param newTab 新表字段
|
||||
* @param oldTab 就表字段
|
||||
* @return 新表有而旧表没的字段
|
||||
*/
|
||||
private List<String> getNewColumn(List<String> newTab, List<String> oldTab) {
|
||||
List<String> temp = new ArrayList<>(newTab);
|
||||
temp.removeAll(oldTab);
|
||||
return temp;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给TaskRecord 增加任务类型
|
||||
*/
|
||||
|
||||
@@ -240,28 +240,13 @@ final class SqlUtil {
|
||||
continue;
|
||||
}
|
||||
Class<?> type = field.getType();
|
||||
sb.append(field.getName());
|
||||
if (type == String.class || type.isEnum()) {
|
||||
sb.append(" VARCHAR");
|
||||
} else if (type == int.class || type == Integer.class) {
|
||||
sb.append(" INTEGER");
|
||||
} else if (type == float.class || type == Float.class) {
|
||||
sb.append(" FLOAT");
|
||||
} else if (type == double.class || type == Double.class) {
|
||||
sb.append(" DOUBLE");
|
||||
} else if (type == long.class || type == Long.class) {
|
||||
sb.append(" BIGINT");
|
||||
} else if (type == boolean.class || type == Boolean.class) {
|
||||
sb.append(" BOOLEAN");
|
||||
} else if (type == java.util.Date.class || type == java.sql.Date.class) {
|
||||
sb.append(" DATA");
|
||||
} else if (type == byte.class || type == Byte.class) {
|
||||
sb.append(" BLOB");
|
||||
} else if (type == Map.class || type == List.class) {
|
||||
sb.append(" TEXT");
|
||||
} else {
|
||||
String columnType = getColumnType(type);
|
||||
if (columnType == null) {
|
||||
continue;
|
||||
}
|
||||
sb.append(field.getName());
|
||||
sb.append(" ").append(columnType);
|
||||
|
||||
if (SqlUtil.isPrimary(field)) {
|
||||
Primary pk = field.getAnnotation(Primary.class);
|
||||
sb.append(" PRIMARY KEY");
|
||||
@@ -315,10 +300,51 @@ final class SqlUtil {
|
||||
|
||||
String str = sb.toString();
|
||||
str = str.substring(0, str.length() - 1) + ");";
|
||||
ALog.d(TAG, "创建表的sql:" + str);
|
||||
db.execSQL(str);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据字段名获取字段类型
|
||||
*/
|
||||
static String getColumnTypeByFieldName(Class tabClass, String fieldName) {
|
||||
List<Field> fields = CommonUtil.getAllFields(tabClass);
|
||||
for (Field field : fields) {
|
||||
if (field.getName().equals(fieldName)) {
|
||||
return getColumnType(field.getType());
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取字段类型
|
||||
*/
|
||||
static String getColumnType(Class fieldtype) {
|
||||
if (fieldtype == String.class || fieldtype.isEnum()) {
|
||||
return "VARCHAR";
|
||||
} else if (fieldtype == int.class || fieldtype == Integer.class) {
|
||||
return "INTEGER";
|
||||
} else if (fieldtype == float.class || fieldtype == Float.class) {
|
||||
return "FLOAT";
|
||||
} else if (fieldtype == double.class || fieldtype == Double.class) {
|
||||
return "DOUBLE";
|
||||
} else if (fieldtype == long.class || fieldtype == Long.class) {
|
||||
return "BIGINT";
|
||||
} else if (fieldtype == boolean.class || fieldtype == Boolean.class) {
|
||||
return "BOOLEAN";
|
||||
} else if (fieldtype == java.util.Date.class || fieldtype == java.sql.Date.class) {
|
||||
return "DATA";
|
||||
} else if (fieldtype == byte.class || fieldtype == Byte.class) {
|
||||
return "BLOB";
|
||||
} else if (fieldtype == Map.class || fieldtype == List.class) {
|
||||
return "TEXT";
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* URL编码字符串
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user