summaryrefslogtreecommitdiff
path: root/plugins/memory/kv.go
blob: c13c2314abb20b0e6f3d8d8b480e1859744d0609 (plain)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package memory

import (
	"strings"
	"sync"
	"time"

	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/plugins/config"
	"github.com/spiral/roadrunner/v2/plugins/kv"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	kvv1 "github.com/spiral/roadrunner/v2/proto/kv/v1beta"
)

type Driver struct {
	clearMu sync.RWMutex
	heap    sync.Map
	// stop is used to stop keys GC and close boltdb connection
	stop chan struct{}
	log  logger.Logger
	cfg  *Config
}

func NewInMemoryDriver(log logger.Logger, key string, cfgPlugin config.Configurer, stop chan struct{}) (kv.Storage, error) {
	const op = errors.Op("new_in_memory_driver")

	d := &Driver{
		stop: stop,
		log:  log,
	}

	err := cfgPlugin.UnmarshalKey(key, &d.cfg)
	if err != nil {
		return nil, errors.E(op, err)
	}

	d.cfg.InitDefaults()

	go d.gc()

	return d, nil
}

func (s *Driver) Has(keys ...string) (map[string]bool, error) {
	const op = errors.Op("in_memory_plugin_has")
	if keys == nil {
		return nil, errors.E(op, errors.NoKeys)
	}
	m := make(map[string]bool)
	for i := range keys {
		keyTrimmed := strings.TrimSpace(keys[i])
		if keyTrimmed == "" {
			return nil, errors.E(op, errors.EmptyKey)
		}

		if _, ok := s.heap.Load(keys[i]); ok {
			m[keys[i]] = true
		}
	}

	return m, nil
}

func (s *Driver) Get(key string) ([]byte, error) {
	const op = errors.Op("in_memory_plugin_get")
	// to get cases like "  "
	keyTrimmed := strings.TrimSpace(key)
	if keyTrimmed == "" {
		return nil, errors.E(op, errors.EmptyKey)
	}

	if data, exist := s.heap.Load(key); exist {
		// here might be a panic
		// but data only could be a string, see Set function
		return data.(*kvv1.Item).Value, nil
	}
	return nil, nil
}

func (s *Driver) MGet(keys ...string) (map[string][]byte, error) {
	const op = errors.Op("in_memory_plugin_mget")
	if keys == nil {
		return nil, errors.E(op, errors.NoKeys)
	}

	// should not be empty keys
	for i := range keys {
		keyTrimmed := strings.TrimSpace(keys[i])
		if keyTrimmed == "" {
			return nil, errors.E(op, errors.EmptyKey)
		}
	}

	m := make(map[string][]byte, len(keys))

	for i := range keys {
		if value, ok := s.heap.Load(keys[i]); ok {
			m[keys[i]] = value.(*kvv1.Item).Value
		}
	}

	return m, nil
}

func (s *Driver) Set(items ...*kvv1.Item) error {
	const op = errors.Op("in_memory_plugin_set")
	if items == nil {
		return errors.E(op, errors.NoKeys)
	}

	for i := range items {
		if items[i] == nil {
			continue
		}
		// TTL is set
		if items[i].Timeout != "" {
			// check the TTL in the item
			_, err := time.Parse(time.RFC3339, items[i].Timeout)
			if err != nil {
				return err
			}
		}

		s.heap.Store(items[i].Key, items[i])
	}
	return nil
}

// MExpire sets the expiration time to the key
// If key already has the expiration time, it will be overwritten
func (s *Driver) MExpire(items ...*kvv1.Item) error {
	const op = errors.Op("in_memory_plugin_mexpire")
	for i := range items {
		if items[i] == nil {
			continue
		}
		if items[i].Timeout == "" || strings.TrimSpace(items[i].Key) == "" {
			return errors.E(op, errors.Str("should set timeout and at least one key"))
		}

		// if key exist, overwrite it value
		if pItem, ok := s.heap.LoadAndDelete(items[i].Key); ok {
			// check that time is correct
			_, err := time.Parse(time.RFC3339, items[i].Timeout)
			if err != nil {
				return errors.E(op, err)
			}
			tmp := pItem.(*kvv1.Item)
			// guess that t is in the future
			// in memory is just FOR TESTING PURPOSES
			// LOGIC ISN'T IDEAL
			s.heap.Store(items[i].Key, &kvv1.Item{
				Key:     items[i].Key,
				Value:   tmp.Value,
				Timeout: items[i].Timeout,
			})
		}
	}

	return nil
}

func (s *Driver) TTL(keys ...string) (map[string]string, error) {
	const op = errors.Op("in_memory_plugin_ttl")
	if keys == nil {
		return nil, errors.E(op, errors.NoKeys)
	}

	// should not be empty keys
	for i := range keys {
		keyTrimmed := strings.TrimSpace(keys[i])
		if keyTrimmed == "" {
			return nil, errors.E(op, errors.EmptyKey)
		}
	}

	m := make(map[string]string, len(keys))

	for i := range keys {
		if item, ok := s.heap.Load(keys[i]); ok {
			m[keys[i]] = item.(*kvv1.Item).Timeout
		}
	}
	return m, nil
}

func (s *Driver) Delete(keys ...string) error {
	const op = errors.Op("in_memory_plugin_delete")
	if keys == nil {
		return errors.E(op, errors.NoKeys)
	}

	// should not be empty keys
	for i := range keys {
		keyTrimmed := strings.TrimSpace(keys[i])
		if keyTrimmed == "" {
			return errors.E(op, errors.EmptyKey)
		}
	}

	for i := range keys {
		s.heap.Delete(keys[i])
	}
	return nil
}

func (s *Driver) Clear() error {
	s.clearMu.Lock()
	s.heap = sync.Map{}
	s.clearMu.Unlock()

	return nil
}

// ================================== PRIVATE ======================================

func (s *Driver) gc() {
	ticker := time.NewTicker(time.Duration(s.cfg.Interval) * time.Second)
	for {
		select {
		case <-s.stop:
			ticker.Stop()
			return
		case now := <-ticker.C:
			// mutes needed to clear the map
			s.clearMu.RLock()

			// check every second
			s.heap.Range(func(key, value interface{}) bool {
				v := value.(*kvv1.Item)
				if v.Timeout == "" {
					return true
				}

				t, err := time.Parse(time.RFC3339, v.Timeout)
				if err != nil {
					return false
				}

				if now.After(t) {
					s.log.Debug("key deleted", "key", key)
					s.heap.Delete(key)
				}
				return true
			})

			s.clearMu.RUnlock()
		}
	}
}