This repository has been archived by the owner on May 11, 2019. It is now read-only.
/
local.go
83 lines (63 loc) · 1.38 KB
/
local.go
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
80
81
82
83
package storage
import (
"sync"
)
// -------------------------------------------------
func NewLocalStorage() *keyValueStorage {
return newKeyValueStorage(&localStorageDriver{
// Lock
Lock: &sync.Mutex{},
// Table
Users: make(map[string]string),
})
}
type localStorageDriver struct {
Lock *sync.Mutex
// Map{userID => userJson}
Users map[string]string
}
func (s *localStorageDriver) Set(userID, userJson string) error {
s.Lock.Lock()
defer s.Lock.Unlock()
s.Users[userID] = userJson
return nil
}
func (s *localStorageDriver) Lookup(userID string) (string, bool, error) {
s.Lock.Lock()
defer s.Lock.Unlock()
userJson, ok := s.Users[userID]
return userJson, ok, nil
}
func (s *localStorageDriver) Index(name string) keyValueIndex {
return NewIndex()
}
// -------------------------------------------------
func NewIndex() *Index {
return &Index{
&sync.Mutex{},
make(map[string]string),
}
}
// Index is a helper struct
type Index struct {
Lock *sync.Mutex
Data map[string]string
}
func (i *Index) Put(key, value string) error {
i.Lock.Lock()
defer i.Lock.Unlock()
i.Data[key] = value
return nil
}
func (i *Index) Remove(key string) error {
i.Lock.Lock()
defer i.Lock.Unlock()
delete(i.Data, key)
return nil
}
func (i *Index) Lookup(key string) (string, bool, error) {
i.Lock.Lock()
defer i.Lock.Unlock()
value, ok := i.Data[key]
return value, ok, nil
}