mirror of
https://github.com/black-ant/Ant-Browser.git
synced 2026-07-14 18:48:55 +08:00
channel: master version: 1.0.0 source-ref: master published-at-utc: 2026-03-13T15:19:28Z
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
package launchcode
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
)
|
|
|
|
// MemoryLaunchCodeDAO 基于内存的 LaunchCodeDAO 实现,仅用于测试
|
|
type MemoryLaunchCodeDAO struct {
|
|
mu sync.RWMutex
|
|
profileToCode map[string]string
|
|
codeToProfile map[string]string
|
|
}
|
|
|
|
// NewMemoryLaunchCodeDAO 创建内存 DAO
|
|
func NewMemoryLaunchCodeDAO() *MemoryLaunchCodeDAO {
|
|
return &MemoryLaunchCodeDAO{
|
|
profileToCode: make(map[string]string),
|
|
codeToProfile: make(map[string]string),
|
|
}
|
|
}
|
|
|
|
func (d *MemoryLaunchCodeDAO) FindProfileId(code string) (string, error) {
|
|
d.mu.RLock()
|
|
defer d.mu.RUnlock()
|
|
profileId, ok := d.codeToProfile[code]
|
|
if !ok {
|
|
return "", fmt.Errorf("launch code not found: %s", code)
|
|
}
|
|
return profileId, nil
|
|
}
|
|
|
|
func (d *MemoryLaunchCodeDAO) FindCode(profileId string) (string, error) {
|
|
d.mu.RLock()
|
|
defer d.mu.RUnlock()
|
|
code, ok := d.profileToCode[profileId]
|
|
if !ok {
|
|
return "", fmt.Errorf("profile not found: %s", profileId)
|
|
}
|
|
return code, nil
|
|
}
|
|
|
|
func (d *MemoryLaunchCodeDAO) Upsert(profileId, code string) error {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
// 清理旧 code 的反向映射
|
|
if oldCode, ok := d.profileToCode[profileId]; ok {
|
|
delete(d.codeToProfile, oldCode)
|
|
}
|
|
d.profileToCode[profileId] = code
|
|
d.codeToProfile[code] = profileId
|
|
return nil
|
|
}
|
|
|
|
func (d *MemoryLaunchCodeDAO) Delete(profileId string) error {
|
|
d.mu.Lock()
|
|
defer d.mu.Unlock()
|
|
if code, ok := d.profileToCode[profileId]; ok {
|
|
delete(d.codeToProfile, code)
|
|
delete(d.profileToCode, profileId)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *MemoryLaunchCodeDAO) LoadAll() (map[string]string, error) {
|
|
d.mu.RLock()
|
|
defer d.mu.RUnlock()
|
|
result := make(map[string]string, len(d.profileToCode))
|
|
for k, v := range d.profileToCode {
|
|
result[k] = v
|
|
}
|
|
return result, nil
|
|
}
|