fix(rdb): add id column for file info, sharing info tables

This commit is contained in:
hexxa 2022-10-08 11:28:04 +08:00 committed by Hexxa
parent 033c2bc316
commit 2b50c84273
8 changed files with 100 additions and 46 deletions

View file

@ -15,16 +15,18 @@ import (
func (st *SQLiteStore) getFileInfo(ctx context.Context, itemPath string) (*db.FileInfo, error) {
var infoStr string
fInfo := &db.FileInfo{}
var id uint64
var isDir bool
var size int64
var shareId string
err := st.db.QueryRowContext(
ctx,
`select is_dir, size, share_id, info
`select id, is_dir, size, share_id, info
from t_file_info
where path=?`,
itemPath,
).Scan(
&id,
&isDir,
&size,
&shareId,
@ -41,6 +43,7 @@ func (st *SQLiteStore) getFileInfo(ctx context.Context, itemPath string) (*db.Fi
if err != nil {
return nil, err
}
fInfo.Id = id
fInfo.IsDir = isDir
fInfo.Size = size
fInfo.ShareID = shareId
@ -69,7 +72,7 @@ func (st *SQLiteStore) ListFileInfos(ctx context.Context, itemPaths []string) (m
rows, err := st.db.QueryContext(
ctx,
fmt.Sprintf(
`select path, is_dir, size, share_id, info
`select id, path, is_dir, size, share_id, info
from t_file_info
where path in (%s)
`,
@ -85,11 +88,12 @@ func (st *SQLiteStore) ListFileInfos(ctx context.Context, itemPaths []string) (m
var fInfoStr, itemPath, shareId string
var isDir bool
var size int64
var id uint64
fInfos := map[string]*db.FileInfo{}
for rows.Next() {
fInfo := &db.FileInfo{}
err = rows.Scan(&itemPath, &isDir, &size, &shareId, &fInfoStr)
err = rows.Scan(&id, &itemPath, &isDir, &size, &shareId, &fInfoStr)
if err != nil {
return nil, err
}
@ -98,6 +102,7 @@ func (st *SQLiteStore) ListFileInfos(ctx context.Context, itemPaths []string) (m
if err != nil {
return nil, err
}
fInfo.Id = id
fInfo.IsDir = isDir
fInfo.Size = size
fInfo.ShareID = shareId
@ -111,7 +116,7 @@ func (st *SQLiteStore) ListFileInfos(ctx context.Context, itemPaths []string) (m
return fInfos, nil
}
func (st *SQLiteStore) addFileInfo(ctx context.Context, userId uint64, itemPath string, info *db.FileInfo) error {
func (st *SQLiteStore) addFileInfo(ctx context.Context, infoId, userId uint64, itemPath string, info *db.FileInfo) error {
infoStr, err := json.Marshal(info)
if err != nil {
return err
@ -126,24 +131,24 @@ func (st *SQLiteStore) addFileInfo(ctx context.Context, userId uint64, itemPath
_, err = st.db.ExecContext(
ctx,
`insert into t_file_info (
path, user, location, parent, name,
id, path, user, location, parent, name,
is_dir, size, share_id, info
)
values (
?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?,
?, ?, ?, ?
)`,
itemPath, userId, location, dirPath, itemName,
infoId, itemPath, userId, location, dirPath, itemName,
info.IsDir, info.Size, info.ShareID, infoStr,
)
return err
}
func (st *SQLiteStore) AddFileInfo(ctx context.Context, userId uint64, itemPath string, info *db.FileInfo) error {
func (st *SQLiteStore) AddFileInfo(ctx context.Context, infoId, userId uint64, itemPath string, info *db.FileInfo) error {
st.Lock()
defer st.Unlock()
err := st.addFileInfo(ctx, userId, itemPath, info)
err := st.addFileInfo(ctx, infoId, userId, itemPath, info)
if err != nil {
return err
}
@ -260,7 +265,7 @@ func (st *SQLiteStore) MoveFileInfo(ctx context.Context, userId uint64, oldPath,
if err != nil {
return err
}
return st.addFileInfo(ctx, userId, newPath, info)
return st.addFileInfo(ctx, info.Id, userId, newPath, info)
}
func getLocation(itemPath string) (string, error) {

View file

@ -78,7 +78,7 @@ func (st *SQLiteStore) GetSharingDir(ctx context.Context, hashID string) (string
return sharedPath, nil
}
func (st *SQLiteStore) AddSharing(ctx context.Context, userId uint64, dirPath string) error {
func (st *SQLiteStore) AddSharing(ctx context.Context, infoId, userId uint64, dirPath string) error {
st.Lock()
defer st.Unlock()
@ -109,14 +109,17 @@ func (st *SQLiteStore) AddSharing(ctx context.Context, userId uint64, dirPath st
_, err = st.db.ExecContext(
ctx,
`insert into t_file_info (
path, user, location, parent, name,
id, path, user,
location, parent, name,
is_dir, size, share_id, info
)
values (
?, ?, ?, ?, ?,
?, ?, ?,
?, ?, ?,
?, ?, ?, ?
)`,
dirPath, userId, location, parentPath, name,
infoId, dirPath, userId,
location, parentPath, name,
true, 0, shareID, infoStr,
)
return err

View file

@ -8,21 +8,21 @@ import (
"github.com/ihexxa/quickshare/src/db"
)
func (st *SQLiteStore) addUploadInfoOnly(ctx context.Context, userId uint64, tmpPath, filePath string, fileSize int64) error {
func (st *SQLiteStore) addUploadInfoOnly(ctx context.Context, uploadId, userId uint64, tmpPath, filePath string, fileSize int64) error {
_, err := st.db.ExecContext(
ctx,
`insert into t_file_uploading (
real_path, tmp_path, user, size, uploaded
id, real_path, tmp_path, user, size, uploaded
)
values (
?, ?, ?, ?, ?
?, ?, ?, ?, ?, ?
)`,
filePath, tmpPath, userId, fileSize, 0,
uploadId, filePath, tmpPath, userId, fileSize, 0,
)
return err
}
func (st *SQLiteStore) AddUploadInfos(ctx context.Context, userId uint64, tmpPath, filePath string, info *db.FileInfo) error {
func (st *SQLiteStore) AddUploadInfos(ctx context.Context, uploadId, userId uint64, tmpPath, filePath string, info *db.FileInfo) error {
st.Lock()
defer st.Unlock()
@ -46,7 +46,7 @@ func (st *SQLiteStore) AddUploadInfos(ctx context.Context, userId uint64, tmpPat
return err
}
return st.addUploadInfoOnly(ctx, userId, tmpPath, filePath, info.Size)
return st.addUploadInfoOnly(ctx, uploadId, userId, tmpPath, filePath, info.Size)
}
func (st *SQLiteStore) DelUploadingInfos(ctx context.Context, userId uint64, realPath string) error {
@ -86,7 +86,7 @@ func (st *SQLiteStore) delUploadInfoOnly(ctx context.Context, userId uint64, fil
return err
}
func (st *SQLiteStore) MoveUploadingInfos(ctx context.Context, userId uint64, uploadPath, itemPath string) error {
func (st *SQLiteStore) MoveUploadingInfos(ctx context.Context, infoId, userId uint64, uploadPath, itemPath string) error {
st.Lock()
defer st.Unlock()
@ -98,7 +98,7 @@ func (st *SQLiteStore) MoveUploadingInfos(ctx context.Context, userId uint64, up
if err != nil {
return err
}
return st.addFileInfo(ctx, userId, itemPath, &db.FileInfo{
return st.addFileInfo(ctx, infoId, userId, itemPath, &db.FileInfo{
Size: size,
})
}

View file

@ -20,7 +20,7 @@ type SQLite struct {
}
func NewSQLite(dbPath string) (*SQLite, error) {
db, err := sql.Open("sqlite3", fmt.Sprintf("%s?_synchronous=FULL", dbPath))
db, err := sql.Open("sqlite3", fmt.Sprintf("%s?_sync=FULL&_locking=EXCLUSIVE", dbPath))
if err != nil {
return nil, err
}
@ -148,6 +148,7 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
_, err := st.db.ExecContext(
ctx,
`create table if not exists t_file_info (
id bigint not null,
path varchar not null,
user bigint not null,
location varchar not null,
@ -157,13 +158,21 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
size bigint not null,
share_id varchar not null,
info varchar not null,
primary key(path)
primary key(id)
)`,
)
if err != nil {
return err
}
_, err = st.db.ExecContext(
ctx,
`create index if not exists t_file_path on t_file_info (path, location)`,
)
if err != nil {
return err
}
_, err = st.db.ExecContext(
ctx,
`create index if not exists t_file_share on t_file_info (share_id, location)`,
@ -175,12 +184,13 @@ func (st *SQLiteStore) InitFileTables(ctx context.Context) error {
_, err = st.db.ExecContext(
ctx,
`create table if not exists t_file_uploading (
id bigint not null,
real_path varchar not null,
tmp_path varchar not null unique,
user bigint not null,
size bigint not null,
uploaded bigint not null,
primary key(real_path)
primary key(id)
)`,
)
if err != nil {