\db_path style

This commit is contained in:
Burgess Leo
2025-05-22 17:21:44 +08:00
parent 3347abe02f
commit d2a3a7b5b5
9 changed files with 301 additions and 111 deletions

View File

@@ -183,40 +183,50 @@ def sort_fragments_by_start_byte(file_extents_list: list) -> list:
if __name__ == "__main__":
test_files = [
r"Y:\CloudMusic\AGA - MIZU.mp3",
r"Y:\CloudMusic\AGA - 一.mp3",
r"Y:\CloudMusic\Aaron Zigman - Main Title.mp3",
r"Y:\CloudMusic\Anson Seabra - Keep Your Head Up Princess.mp3",
r"Y:\CloudMusic\Anthony Keyrouz,Romy Wave - Something Just Like This (feat. Romy Wave).mp3",
r"Y:\CloudMusic\Ava Max - Sweet but Psycho.mp3",
r"Y:\CloudMusic\Cecilia Cheung - Turn Into Fireworks and Fall for You.mp3",
r"Y:\CloudMusic\Color Music Choir - Something Just Like This (Live).mp3"
r"CloudMusic\AGA - MIZU.mp3",
r"CloudMusic\AGA - 一.mp3",
r"CloudMusic\Aaron Zigman - Main Title.mp3",
r"CloudMusic\Anson Seabra - Keep Your Head Up Princess.mp3",
r"CloudMusic\Anthony Keyrouz,Romy Wave - Something Just Like This (feat. Romy Wave).mp3",
r"CloudMusic\Ava Max - Sweet but Psycho.mp3",
r"CloudMusic\Cecilia Cheung - Turn Into Fireworks and Fall for You.mp3",
r"CloudMusic\Color Music Choir - Something Just Like This (Live).mp3"
]
test_files_sort = [
{'absolute_path': 'Y:\\CloudMusic\\AGA - MIZU.mp3', 'name': 'AGA - MIZU.mp3', 'path_id': 6, 'extent_count': 1,
{'absolute_path': 'CloudMusic\\AGA - MIZU.mp3', 'name': 'AGA - MIZU.mp3', 'path_id': 6, 'extent_count': 1,
'fragments': [{'start_byte': 694849536, 'length': 8126464}]},
{'absolute_path': 'Y:\\CloudMusic\\AGA - 一.mp3', 'name': 'AGA - 一.mp3', 'path_id': 7, 'extent_count': 2,
{'absolute_path': 'CloudMusic\\AGA - 一.mp3', 'name': 'AGA - 一.mp3', 'path_id': 7, 'extent_count': 2,
'fragments': [{'start_byte': 702976000, 'length': 10870784}, {'start_byte': 23162880, 'length': 69632}]},
{'absolute_path': 'Y:\\CloudMusic\\Aaron Zigman - Main Title.mp3', 'name': 'Aaron Zigman - Main Title.mp3',
{'absolute_path': 'CloudMusic\\Aaron Zigman - Main Title.mp3', 'name': 'Aaron Zigman - Main Title.mp3',
'path_id': 5, 'extent_count': 1, 'fragments': [{'start_byte': 687685632, 'length': 7163904}]},
{'absolute_path': 'Y:\\CloudMusic\\Anson Seabra - Keep Your Head Up Princess.mp3',
{'absolute_path': 'CloudMusic\\Anson Seabra - Keep Your Head Up Princess.mp3',
'name': 'Anson Seabra - Keep Your Head Up Princess.mp3', 'path_id': 8, 'extent_count': 1,
'fragments': [{'start_byte': 713846784, 'length': 7970816}]},
{'absolute_path': 'Y:\\CloudMusic\\Anthony Keyrouz,Romy Wave - Something Just Like This (feat. Romy Wave).mp3',
{'absolute_path': 'CloudMusic\\Anthony Keyrouz,Romy Wave - Something Just Like This (feat. Romy Wave).mp3',
'name': 'Anthony Keyrouz,Romy Wave - Something Just Like This (feat. Romy Wave).mp3', 'path_id': 9,
'extent_count': 1, 'fragments': [{'start_byte': 721817600, 'length': 9179136}]},
{'absolute_path': 'Y:\\CloudMusic\\Ava Max - Sweet but Psycho.mp3', 'name': 'Ava Max - Sweet but Psycho.mp3',
{'absolute_path': 'CloudMusic\\Ava Max - Sweet but Psycho.mp3', 'name': 'Ava Max - Sweet but Psycho.mp3',
'path_id': 10, 'extent_count': 1, 'fragments': [{'start_byte': 731000832, 'length': 7938048}]},
{'absolute_path': 'Y:\\CloudMusic\\Cecilia Cheung - Turn Into Fireworks and Fall for You.mp3',
{'absolute_path': 'CloudMusic\\Cecilia Cheung - Turn Into Fireworks and Fall for You.mp3',
'name': 'Cecilia Cheung - Turn Into Fireworks and Fall for You.mp3', 'path_id': 11, 'extent_count': 1,
'fragments': [{'start_byte': 738938880, 'length': 6791168}]},
{'absolute_path': 'Y:\\CloudMusic\\Color Music Choir - Something Just Like This (Live).mp3',
{'absolute_path': 'CloudMusic\\Color Music Choir - Something Just Like This (Live).mp3',
'name': 'Color Music Choir - Something Just Like This (Live).mp3', 'path_id': 12, 'extent_count': 1,
'fragments': [{'start_byte': 745730048, 'length': 6193152}]}]
# path_info = GetFilesDBPathInfo(files_path=test_files)
# file_extents_data = GetFilesDBNodeInfo(path_records=path_info)
result = sort_fragments_by_start_byte(test_files_sort)
print(result)
# for item in result:
# print(item)
path_info = GetFilesDBPathInfo(files_path=test_files)
file_extents_data = GetFilesDBNodeInfo(path_records=path_info)
# 根据文件片段先后排序
single_fragment_result = sort_fragments_by_start_byte(file_extents_data)
# 模拟多文件片段,根据文件片段先后排序
multi_fragment_result = sort_fragments_by_start_byte(test_files_sort)
print("单文件片段排序结果:")
for item in single_fragment_result:
print(item)
print("\n多文件片段排序结果:")
for item in multi_fragment_result:
print(item)

View File

@@ -0,0 +1,154 @@
import sqlite3
from files_sort import GetFilesDBNodeInfo
def GetFolderID(
folder_path: str,
db_path: str = "../src/db_ntfs_info.db",
table_name: str = "db_path"
) -> int | None:
"""
根据文件夹路径,查询数据库中该文件夹对应的 ID。
:param folder_path: 文件夹路径(如 r"CloudMusic\\"
:param db_path: 数据库文件路径
:param table_name: 要查询的数据表名称,默认为 'db_path'
:return: 成功则返回 IDint失败返回 None
"""
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
try:
# 使用 table_name 构建 SQL 查询
sql = f"SELECT ID FROM {table_name} WHERE Path = ?"
cursor.execute(sql, (folder_path,))
result = cursor.fetchone()
if result:
return result[0]
else:
print(f"未找到路径:{folder_path} 在表 {table_name}")
return None
except sqlite3.Error as e:
print(f"数据库操作失败:{e}")
return None
finally:
conn.close()
def GetNodeFragmentsByParentID(
parent_id: int,
db_path: str = "../src/db_ntfs_info.db",
table_name: str = "db_node"
) -> list:
"""
根据 ParentID 查询 db_node 表中对应的文件/子目录的分片信息。
:param parent_id: 父节点 ID
:param db_path: 数据库文件路径
:param table_name: 数据表名称
:return: 包含 PathID、分片数量及分片信息的列表
"""
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
# 构建查询语句(根据分片字段)
sql = f"""
SELECT
PathID, ExtentCount,
extent1_Location, extent1_Length,
extent2_Location, extent2_Length,
extent3_Location, extent3_Length,
extent4_Location, extent4_Length
FROM {table_name}
WHERE ParentID = ?
"""
try:
cursor.execute(sql, (parent_id,))
rows = cursor.fetchall()
except Exception as e:
print(f"数据库查询失败:{e}")
return []
results = []
for row in rows:
path_id = row[0]
extent_count = row[1]
fragments = []
for i in range(4):
location = row[2 + i * 2]
length = row[3 + i * 2]
if location is not None and length is not None and length > 0:
fragments.append({
'start_byte': location,
'length': length
})
results.append({
'path_id': path_id,
'extent_count': extent_count,
'fragments': fragments
})
conn.close()
return results
def GetSubPathsByParentID(
parent_id: int,
db_path: str = "../src/db_ntfs_info.db",
table_name: str = "db_path"
) -> list:
"""
根据 ParentID 查询 db_path 表中对应的子项(文件/文件夹)。
:param parent_id: 父节点 ID
:param db_path: 数据库文件路径
:param table_name: 数据表名称
:return: 包含 ID、Path、Name 的字典列表
"""
conn = sqlite3.connect(db_path)
cursor = conn.cursor()
sql = f"""
SELECT ID, Path, Name
FROM {table_name}
WHERE ParentID = ?
"""
try:
cursor.execute(sql, (parent_id,))
rows = cursor.fetchall()
except Exception as e:
print(f"数据库查询失败:{e}")
return []
results = []
for row in rows:
item = {
'id': row[0],
'absolute_path': row[1], # 因为 Path 已经是完整格式(如 CloudMusic\\AGA - MIZU.mp3
'name': row[2]
}
results.append(item)
conn.close()
return results
if __name__ == "__main__":
test_folder_path = "pictures"
parent_id_test = GetFolderID(test_folder_path)
# node_data = GetNodeFragmentsByParentID(parent_id_test)
path_data = GetSubPathsByParentID(parent_id_test)
node_data = GetFilesDBNodeInfo(path_records=path_data)
for data in node_data:
print(data)