forked from botopolis/bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
brain.go
112 lines (98 loc) · 2.73 KB
/
brain.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package bot
import (
"errors"
"fmt"
"reflect"
"sort"
"sync"
)
// Store is what the brain saves information in for longer term
// recollection. Brain loads what it needs to in memory, but in
// order to have persistence between runs, we can use a store.
type Store interface {
// Get is called on Brain.Get() when the key doesn't exist in memory
Get(key string, object interface{}) error
// Set is always called on Brain.Set()
Set(key string, object interface{}) error
// Delete is always called on Brain.Delete()
Delete(key string) error
}
// Brain is our data store. It defaults to just saving values in memory,
// but can be given a Store via Brain.SetStore(), to which values are written.
// Brain is threadsafe - and assumes the same of Store.
type Brain struct {
store Store
mu sync.RWMutex
cache map[string]interface{}
}
var errNullStore = errors.New("Nullstore contains no values")
type nullStore struct{}
func (n nullStore) Get(string, interface{}) error { return errNullStore }
func (n nullStore) Set(string, interface{}) error { return errNullStore }
func (n nullStore) Delete(string) error { return errNullStore }
// NewBrain creates a Brain
func NewBrain() *Brain {
return &Brain{
store: nullStore{},
cache: make(map[string]interface{}),
}
}
// SetStore assigns a Store to Brain
func (b *Brain) SetStore(s Store) { b.store = s }
// Get retrieves a value from the store and sets it to the interface
// It tries the memory store first, and falls back to Brain's Store
func (b *Brain) Get(key string, i interface{}) error {
b.mu.RLock()
defer b.mu.RUnlock()
if d, ok := b.cache[key]; ok {
return copyInterface(d, i)
}
if err := b.store.Get(key, i); err != nil {
return err
}
b.cache[key] = i
return nil
}
// Set assigns the given value in memory and to Brain's Store
func (b *Brain) Set(key string, i interface{}) error {
b.mu.Lock()
defer b.mu.Unlock()
b.cache[key] = i
return b.store.Set(key, i)
}
// Delete removes the given key from memory and Brain's Store
func (b *Brain) Delete(key string) error {
b.mu.Lock()
defer b.mu.Unlock()
delete(b.cache, key)
return b.store.Delete(key)
}
// Keys returns all the keys in the Brain's cache.
func (b *Brain) Keys() []string {
b.mu.Lock()
defer b.mu.Unlock()
var keys []string
for k := range b.cache {
keys = append(keys, k)
}
sort.Strings(keys)
return keys
}
func ifaceValue(i interface{}) reflect.Value {
v := reflect.ValueOf(i)
if v.Kind() == reflect.Ptr {
return reflect.Indirect(v)
}
return v
}
func copyInterface(from, to interface{}) error {
if to == nil {
return nil
}
fromVal, toVal := ifaceValue(from), ifaceValue(to)
if fromVal.Kind() != toVal.Kind() {
return fmt.Errorf("Can't assign %T to %T", from, to)
}
toVal.Set(fromVal)
return nil
}