-
Notifications
You must be signed in to change notification settings - Fork 0
/
memdb.go
49 lines (40 loc) · 935 Bytes
/
memdb.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
package memdb
import (
"sync"
"github.com/tchajed/specious-db/db"
)
// Database is an in-memory, non-persistent database mainly for testing purposes.
type Database struct {
l *sync.Mutex
m map[db.Key]db.Value
}
// Get looks up a key.
func (s Database) Get(k db.Key) db.MaybeValue {
s.l.Lock()
defer s.l.Unlock()
val, ok := s.m[k]
if !ok {
return db.NoValue
}
return db.SomeValue(val)
}
// Put stores data in the database.
func (s *Database) Put(k db.Key, v db.Value) {
s.l.Lock()
defer s.l.Unlock()
s.m[k] = v
}
// Delete deletes a key.
func (s *Database) Delete(k db.Key) {
s.l.Lock()
defer s.l.Unlock()
delete(s.m, k)
}
// Close does nothing
func (s *Database) Close() {}
// Compact does nothing. In-memory databases are always compact.
func (s *Database) Compact() {}
// New creates an empty in-memory database.
func New() *Database {
return &Database{l: new(sync.Mutex), m: make(map[db.Key]db.Value)}
}