-
Notifications
You must be signed in to change notification settings - Fork 1
/
memtable.go
59 lines (51 loc) · 1.14 KB
/
memtable.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
// Package memtable contains implementation of key-value store
package memtable
import (
"sync"
"time"
)
// KVRow individual row in db
type KVRow struct {
Key string
Value string
createdAt int64
}
// KVStore DB memory map
type KVStore struct {
data map[string]KVRow
mux sync.Mutex
}
// Get value for a key
func (s *KVStore) Get(key string) (string, error) {
if s.data[key] != (KVRow{}) {
return s.data[key].Value, nil
}
return "", ErrKeyNotFound
}
// Create to save data
func (s *KVStore) Create(key, value string) (string, error) {
s.mux.Lock()
defer s.mux.Unlock()
s.data[key] = KVRow{key, value, time.Now().Unix()}
return "Inserted 1", nil
}
// Delete row data by key
func (s *KVStore) Delete(key string) (string, error) {
s.mux.Lock()
defer s.mux.Unlock()
if found, _ := s.Get(key); len(found) == 0 {
return "Deleted 0", ErrKeyNotFound
}
delete(s.data, key)
return "Deleted 1", nil
}
// Singleton KVStore instance
var once sync.Once
var store *KVStore
// NewDB returns a singleton KvStore instance
func NewDB() (store *KVStore) {
once.Do(func() {
store = &KVStore{data: make(map[string]KVRow)}
})
return store
}