数据库重构

v4
laoyuyu 2 years ago
parent ac9f9eb6b8
commit b1abda97d6
  1. 2
      PublicComponent/src/main/java/com/arialyy/aria/core/service/DbService.kt
  2. 8
      PublicComponent/src/main/java/com/arialyy/aria/orm/DuaDb.kt
  3. 12
      PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DEntityDao.kt
  4. 81
      PublicComponent/src/main/java/com/arialyy/aria/orm/dao/DGEntityDao.kt
  5. 67
      PublicComponent/src/main/java/com/arialyy/aria/orm/dao/RecordDao.kt
  6. 12
      PublicComponent/src/main/java/com/arialyy/aria/orm/dao/UEntityDao.kt
  7. 54
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/BlockRecord.kt
  8. 9
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DEntity.kt
  9. 17
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DGEntity.kt
  10. 18
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/DGSubRelation.kt
  11. 61
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/MEntity.kt
  12. 40
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/MKeyInfo.kt
  13. 42
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/RecordRelation.kt
  14. 35
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/TaskRecord.kt
  15. 2
      PublicComponent/src/main/java/com/arialyy/aria/orm/entity/UEntity.kt

@ -27,7 +27,7 @@ import com.arialyy.aria.util.ReflectionUtil
* @Description
* @Date 19:36 AM 2023/1/16
**/
open class DbService : IService {
internal class DbService : IService {
private var duaDb: DuaDb? = null
/**

@ -19,10 +19,14 @@ import androidx.room.Database
import androidx.room.RoomDatabase
import com.arialyy.aria.orm.dao.DEntityDao
import com.arialyy.aria.orm.dao.UEntityDao
import com.arialyy.aria.orm.entiry.UEntity
import com.arialyy.aria.orm.entity.BlockRecord
import com.arialyy.aria.orm.entity.DGEntity
import com.arialyy.aria.orm.entity.MEntity
import com.arialyy.aria.orm.entity.TaskRecord
import com.arialyy.aria.orm.entity.UEntity
@Database(
entities = [DbEntity::class, UEntity::class],
entities = [DbEntity::class, UEntity::class, DGEntity::class, MEntity::class, TaskRecord::class, BlockRecord::class],
version = 1
)
abstract class DuaDb : RoomDatabase() {

@ -19,8 +19,9 @@ import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.arialyy.aria.orm.entiry.DEntity
import com.arialyy.aria.orm.entity.DEntity
/**
* @Author laoyuyu
@ -29,11 +30,16 @@ import com.arialyy.aria.orm.entiry.DEntity
**/
@Dao
interface DEntityDao {
@Transaction
@Query("SELECT * FROM DEntity")
suspend fun getDEntityList():List<DEntity>
@Query("SELECT * FROM DEntity WHERE :dId=dId")
suspend fun queryDEntityById(dId: String): DEntity
suspend fun getDEntityById(did: String): DEntity
@Query("SELECT * FROM DEntity WHERE :sourceUrl=sourceUrl")
suspend fun queryDEntityBySource(sourceUrl: String): DEntity
suspend fun getDEntityBySource(sourceUrl: String): DEntity
@Insert
suspend fun insert(dEntity: DEntity)

@ -0,0 +1,81 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.arialyy.aria.orm.entity.DEntity
import com.arialyy.aria.orm.entity.DGEntity
import com.arialyy.aria.orm.entity.DGSubRelation
/**
* @Author laoyuyu
* @Description
* @Date 8:55 AM 2023/1/19
**/
@Dao
interface DGEntityDao {
@Transaction
@Query("SELECT * FROM DGEntity")
suspend fun getDGEntityList(): List<DGSubRelation>
@Transaction
@Query("SELECT * FROM DGEntity WHERE :gid=gid")
suspend fun getDGEntityByGid(gid: Int): DGSubRelation
@Transaction
@Query("SELECT * FROM DGEntity WHERE :gid=gid")
suspend fun getDGList(): List<DGSubRelation>
@Insert
suspend fun insertSubList(subList: List<DEntity>)
@Update
suspend fun update(dgEntity: DGEntity)
@Delete
suspend fun deleteSubList(subList: List<DEntity>)
@Delete
@Deprecated(
"please use ",
ReplaceWith("delete(dgEntity)", "com.arialyy.aria.orm.dao.DGEntityDao.delete")
)
suspend fun deleteDg(dgEntity: DGEntity)
@Insert
@Deprecated(
"please use ",
ReplaceWith("insert(dgEntity)", "com.arialyy.aria.orm.dao.DGEntityDao.insert")
)
suspend fun insertDg(dgEntity: DGEntity)
@Transaction
suspend fun delete(dgEntity: DGEntity) {
deleteSubList(dgEntity.subList)
deleteDg(dgEntity)
}
suspend fun insert(dgEntity: DGEntity) {
insertDg(dgEntity)
insertSubList(dgEntity.subList)
}
}

@ -0,0 +1,67 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.dao
import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.arialyy.aria.orm.entity.BlockRecord
import com.arialyy.aria.orm.entity.RecordRelation
import com.arialyy.aria.orm.entity.TaskRecord
/**
* @Author laoyuyu
* @Description
* @Date 10:34 AM 2023/1/19
**/
@Dao
interface RecordDao {
@Transaction
@Query("SELECT * FROM TaskRecord WHERE :key=taskKey")
suspend fun getTaskRecordByKey(key: String): RecordRelation
@Insert
@Deprecated(
"please use ",
ReplaceWith(
"deleteTaskRecord(taskRecord)",
"com.arialyy.aria.orm.dao.RecordDao.insert"
)
)
suspend fun insertTaskRecord(taskRecord: TaskRecord)
@Insert
suspend fun insertSubList(blockList: List<BlockRecord>)
@Update
suspend fun update(record: TaskRecord)
@Delete
suspend fun deleteTaskRecord(record: TaskRecord)
@Delete
suspend fun deleteBlockRecord(blockRecord: List<BlockRecord>)
@Transaction
suspend fun insert(taskRecord: TaskRecord) {
insertTaskRecord(taskRecord)
insertSubList(taskRecord.blockList)
}
}

@ -19,8 +19,9 @@ import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.Update
import com.arialyy.aria.orm.entiry.UEntity
import com.arialyy.aria.orm.entity.UEntity
/**
* @Author laoyuyu
@ -29,11 +30,16 @@ import com.arialyy.aria.orm.entiry.UEntity
**/
@Dao
interface UEntityDao {
@Transaction
@Query("SELECT * FROM DEntity")
suspend fun getUEntityList(): List<UEntity>
@Query("SELECT * FROM DEntity WHERE :uId=uId")
suspend fun queryUEntityById(uId: String): UEntity
suspend fun getUEntityById(uId: String): UEntity
@Query("SELECT * FROM UEntity WHERE :filePath=filePath")
suspend fun queryUEntityBySource(filePath: String): UEntity
suspend fun getUEntityBySource(filePath: String): UEntity
@Insert
suspend fun insert(uEntity: UEntity)

@ -0,0 +1,54 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entity
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.ForeignKey.Companion.CASCADE
import androidx.room.PrimaryKey
/**
* @Author laoyuyu
* @Description
* @Date 10:16 AM 2023/1/19
**/
@Entity(
foreignKeys = [ForeignKey(
entity = TaskRecord::class,
parentColumns = ["tId"],
childColumns = ["tId"],
onDelete = CASCADE
)]
)
data class BlockRecord(
@PrimaryKey(autoGenerate = true) val bId: Int = 0,
val tId: Int,
/**
* 开始位置
*/
val startLocation: Long = 0,
/**
* 结束位置
*/
val endLocation: Long = 0,
val blockSize: Long = 0,
val isComplete: Boolean = false
)

@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entiry
package com.arialyy.aria.orm.entity
import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
@ -24,7 +25,9 @@ import androidx.room.PrimaryKey
*/
@Entity(indices = [Index(value = ["sourceUrl", "savePath"])])
data class DEntity(
@PrimaryKey(autoGenerate = true) val dId: Int = 0,
@PrimaryKey(autoGenerate = true) val did: Int = 0,
val parentId: Int = -1,
/**
* file source url
@ -39,6 +42,8 @@ data class DEntity(
*/
var ext: String? = null,
val isSub: Boolean = false,
val createTime: Long,
val updateTime: Long

@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entiry
package com.arialyy.aria.orm.entity
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
import androidx.room.TypeConverters
import com.arialyy.aria.orm.DGUrlConverter
@ -25,17 +27,11 @@ import com.arialyy.aria.orm.DGUrlConverter
* @Description
* @Date 4:32 PM 2023/1/16
**/
@Entity
@Entity(indices = [Index(value = ["savePath"])])
@TypeConverters(DGUrlConverter::class)
data class DGEntity(
@PrimaryKey(autoGenerate = true) val dgId: Int = 0,
/**
* 组合任务等hash为 为子任务地址相加的url的Md5
* ftpdir为ftpdir下载地址
*/
val groupHash: String,
/**
* 任务组别名
*/
@ -59,4 +55,7 @@ data class DGEntity(
val createTime: Long,
val updateTime: Long
)
) {
@Ignore
internal var subList: MutableList<DEntity> = mutableListOf()
}

@ -13,16 +13,24 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entiry
package com.arialyy.aria.orm.entity
import androidx.room.Embedded
import androidx.room.Relation
data class DGSubList(
data class DGSubRelation(
@Embedded val dgEntity: DGEntity,
) {
@Relation(
parentColumn = "dgId",
entityColumn = "dId"
entityColumn = "parentId"
)
val subEntity: List<DEntity>
)
var subList: List<DEntity> = emptyList()
set(value) {
dgEntity.subList.apply {
clear()
addAll(value)
}
field = value
}
}

@ -0,0 +1,61 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entity
import androidx.room.Entity
import androidx.room.Index
import androidx.room.PrimaryKey
@Entity(indices = [Index(value = ["sourceUrl", "savePath"])])
data class MEntity(
@PrimaryKey(autoGenerate = true) val mId: Int = 0,
val keyId: Int = -1,
/**
* file source url
*/
val sourceUrl: String,
/**
* file save path
*/
val savePath: String,
/**
* extended Information
*/
var ext: String? = null,
/**
* 当前peer的位置
*/
val peerIndex: Int = 0,
/**
* peer总数
*/
private var peerNum: Int = 0,
/**
* 是否是直播true 直播
*/
val isLive: Boolean = false,
/**
* 缓存目录
*/
val cacheDir: String? = null
)

@ -0,0 +1,40 @@
package com.arialyy.aria.orm.entity
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity
data class MKeyInfo(
@PrimaryKey(autoGenerate = true) val kId: Int,
/**
* 加密key保存地址
*/
val keyPath: String,
/**
* 加密key的下载地址
*/
val keyUrl: String,
/**
* 加密算法
*/
val method: String,
/**
* key的iv值
*/
val iv: String,
/**
* key的格式可能为空
*/
val keyFormat: String? = null,
/**
* key的格式版本默认为1如果是多个版本使用"/"分隔"1", "1/2", or "1/2/5"
*/
val keyFormatVersion: String = "1",
)

@ -0,0 +1,42 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entity
import androidx.room.Embedded
import androidx.room.Relation
/**
* @Author laoyuyu
* @Description
* @Date 10:27 AM 2023/1/19
**/
data class RecordRelation(
@Embedded
val taskRecord: TaskRecord
) {
@Relation(
parentColumn = "tId",
entityColumn = "tId"
)
var blockRecordList: List<BlockRecord> = emptyList()
set(value) {
taskRecord.blockList.apply {
clear()
addAll(value)
}
field = value
}
}

@ -0,0 +1,35 @@
/*
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entity
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.Index
import androidx.room.PrimaryKey
@Entity(indices = [Index(value = ["taskKey"])])
data class TaskRecord(
@PrimaryKey(autoGenerate = true) val tId: Int = 0,
val taskKey: String,
val filePath: String,
val taskType: Int,
val fileLen: Long,
val blockNum: Int,
val blockSize: Long
) {
@Ignore
internal var blockList: MutableList<BlockRecord> = mutableListOf()
}

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.arialyy.aria.orm.entiry
package com.arialyy.aria.orm.entity
import androidx.room.Entity
import androidx.room.Index
Loading…
Cancel
Save