2021-06-14 07:52:55 +05:30
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
package db
|
2021-06-14 07:52:55 +05:30
|
|
|
|
|
|
|
import (
|
2022-05-20 19:38:52 +05:30
|
|
|
"context"
|
2021-06-14 07:52:55 +05:30
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ResourceIndex represents a resource index which could be used as issue/release and others
|
|
|
|
// We can create different tables i.e. issue_index, release_index and etc.
|
|
|
|
type ResourceIndex struct {
|
2021-08-25 14:12:51 +05:30
|
|
|
GroupID int64 `xorm:"pk"`
|
2021-06-14 07:52:55 +05:30
|
|
|
MaxIndex int64 `xorm:"index"`
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
// UpsertResourceIndex the function will not return until it acquires the lock or receives an error.
|
2022-06-13 15:07:59 +05:30
|
|
|
func UpsertResourceIndex(ctx context.Context, tableName string, groupID int64) (err error) {
|
2021-06-14 07:52:55 +05:30
|
|
|
// An atomic UPSERT operation (INSERT/UPDATE) is the only operation
|
|
|
|
// that ensures that the key is actually locked.
|
|
|
|
switch {
|
|
|
|
case setting.Database.UseSQLite3 || setting.Database.UsePostgreSQL:
|
2022-06-13 15:07:59 +05:30
|
|
|
_, err = Exec(ctx, fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
|
2021-06-14 07:52:55 +05:30
|
|
|
"VALUES (?,1) ON CONFLICT (group_id) DO UPDATE SET max_index = %s.max_index+1",
|
|
|
|
tableName, tableName), groupID)
|
|
|
|
case setting.Database.UseMySQL:
|
2022-06-13 15:07:59 +05:30
|
|
|
_, err = Exec(ctx, fmt.Sprintf("INSERT INTO %s (group_id, max_index) "+
|
2021-06-14 07:52:55 +05:30
|
|
|
"VALUES (?,1) ON DUPLICATE KEY UPDATE max_index = max_index+1", tableName),
|
|
|
|
groupID)
|
|
|
|
case setting.Database.UseMSSQL:
|
|
|
|
// https://weblogs.sqlteam.com/dang/2009/01/31/upsert-race-condition-with-merge/
|
2022-06-13 15:07:59 +05:30
|
|
|
_, err = Exec(ctx, fmt.Sprintf("MERGE %s WITH (HOLDLOCK) as target "+
|
2021-06-14 07:52:55 +05:30
|
|
|
"USING (SELECT ? AS group_id) AS src "+
|
|
|
|
"ON src.group_id = target.group_id "+
|
|
|
|
"WHEN MATCHED THEN UPDATE SET target.max_index = target.max_index+1 "+
|
|
|
|
"WHEN NOT MATCHED THEN INSERT (group_id, max_index) "+
|
|
|
|
"VALUES (src.group_id, 1);", tableName),
|
|
|
|
groupID)
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("database type not supported")
|
|
|
|
}
|
2022-06-20 15:32:49 +05:30
|
|
|
return err
|
2021-06-14 07:52:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrResouceOutdated represents an error when request resource outdated
|
|
|
|
ErrResouceOutdated = errors.New("resource outdated")
|
|
|
|
// ErrGetResourceIndexFailed represents an error when resource index retries 3 times
|
|
|
|
ErrGetResourceIndexFailed = errors.New("get resource index failed")
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-09-23 16:20:06 +05:30
|
|
|
// MaxDupIndexAttempts max retry times to create index
|
|
|
|
MaxDupIndexAttempts = 3
|
2021-06-14 07:52:55 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// GetNextResourceIndex retried 3 times to generate a resource index
|
|
|
|
func GetNextResourceIndex(tableName string, groupID int64) (int64, error) {
|
2021-09-23 16:20:06 +05:30
|
|
|
for i := 0; i < MaxDupIndexAttempts; i++ {
|
2021-06-14 07:52:55 +05:30
|
|
|
idx, err := getNextResourceIndex(tableName, groupID)
|
|
|
|
if err == ErrResouceOutdated {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return idx, nil
|
|
|
|
}
|
|
|
|
return 0, ErrGetResourceIndexFailed
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
// DeleteResouceIndex delete resource index
|
2022-05-20 19:38:52 +05:30
|
|
|
func DeleteResouceIndex(ctx context.Context, tableName string, groupID int64) error {
|
|
|
|
_, err := Exec(ctx, fmt.Sprintf("DELETE FROM %s WHERE group_id=?", tableName), groupID)
|
2021-06-14 07:52:55 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// getNextResourceIndex return the next index
|
|
|
|
func getNextResourceIndex(tableName string, groupID int64) (int64, error) {
|
2022-06-13 15:07:59 +05:30
|
|
|
ctx, commiter, err := TxContext()
|
|
|
|
if err != nil {
|
2021-06-14 07:52:55 +05:30
|
|
|
return 0, err
|
|
|
|
}
|
2022-06-13 15:07:59 +05:30
|
|
|
defer commiter.Close()
|
2021-06-14 07:52:55 +05:30
|
|
|
var preIdx int64
|
2022-06-13 15:07:59 +05:30
|
|
|
if _, err := GetEngine(ctx).SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ?", tableName), groupID).Get(&preIdx); err != nil {
|
2021-06-14 07:52:55 +05:30
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:07:59 +05:30
|
|
|
if err := UpsertResourceIndex(ctx, tableName, groupID); err != nil {
|
2021-06-14 07:52:55 +05:30
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var curIdx int64
|
2022-06-13 15:07:59 +05:30
|
|
|
has, err := GetEngine(ctx).SQL(fmt.Sprintf("SELECT max_index FROM %s WHERE group_id = ? AND max_index=?", tableName), groupID, preIdx+1).Get(&curIdx)
|
2021-06-14 07:52:55 +05:30
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return 0, ErrResouceOutdated
|
|
|
|
}
|
2022-06-13 15:07:59 +05:30
|
|
|
if err := commiter.Commit(); err != nil {
|
2021-06-14 07:52:55 +05:30
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return curIdx, nil
|
|
|
|
}
|