Hunter0x7c7
2022-08-11 a82f9cb69f63aaeba40c024960deda7d75b9fece
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package transientstorageimpl
 
//go:generate go run github.com/v2fly/v2ray-core/v5/common/errors/errorgen
 
import (
    "context"
    "strings"
    "sync"
 
    "github.com/v2fly/v2ray-core/v5/features/extension/storage"
)
 
func NewScopedTransientStorageImpl() storage.ScopedTransientStorage {
    return &scopedTransientStorageImpl{scopes: map[string]storage.ScopedTransientStorage{}, values: map[string]interface{}{}}
}
 
type scopedTransientStorageImpl struct {
    access sync.Mutex
    scopes map[string]storage.ScopedTransientStorage
    values map[string]interface{}
}
 
func (s *scopedTransientStorageImpl) ScopedTransientStorage() {
    panic("implement me")
}
 
func (s *scopedTransientStorageImpl) Put(ctx context.Context, key string, value interface{}) error {
    s.access.Lock()
    defer s.access.Unlock()
    s.values[key] = value
    return nil
}
 
func (s *scopedTransientStorageImpl) Get(ctx context.Context, key string) (interface{}, error) {
    s.access.Lock()
    defer s.access.Unlock()
    sw, ok := s.values[key]
    if !ok {
        return nil, newError("unable to find ")
    }
    return sw, nil
}
 
func (s *scopedTransientStorageImpl) List(ctx context.Context, keyPrefix string) ([]string, error) {
    s.access.Lock()
    defer s.access.Unlock()
    var ret []string
    for key := range s.values {
        if strings.HasPrefix(key, keyPrefix) {
            ret = append(ret, key)
        }
    }
    return ret, nil
}
 
func (s *scopedTransientStorageImpl) Clear(ctx context.Context) {
    s.access.Lock()
    defer s.access.Unlock()
    s.values = map[string]interface{}{}
}
 
func (s *scopedTransientStorageImpl) NarrowScope(ctx context.Context, key string) (storage.ScopedTransientStorage, error) {
    s.access.Lock()
    defer s.access.Unlock()
    sw, ok := s.scopes[key]
    if !ok {
        scope := NewScopedTransientStorageImpl()
        s.scopes[key] = scope
        return scope, nil
    }
    return sw, nil
}
 
func (s *scopedTransientStorageImpl) DropScope(ctx context.Context, key string) error {
    s.access.Lock()
    defer s.access.Unlock()
    delete(s.scopes, key)
    return nil
}