forked from scttnlsn/queued
/
memory_store.go
54 lines (42 loc) · 857 Bytes
/
memory_store.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
package queued
// Iterator
type MemoryIterator struct {
}
func (it *MemoryIterator) NextRecord() (*Record, bool) {
return nil, false
}
// Store
type MemoryStore struct {
id int
records map[int]*Record
}
func NewMemoryStore() *MemoryStore {
records := make(map[int]*Record)
return &MemoryStore{
id: 0,
records: records,
}
}
func (s *MemoryStore) Get(id int) (*Record, error) {
if record, ok := s.records[id]; ok {
return record, nil
} else {
return nil, nil
}
}
func (s *MemoryStore) Put(record *Record) error {
record.id = s.id + 1
s.records[record.id] = record
s.id = record.id
return nil
}
func (s *MemoryStore) Remove(id int) error {
delete(s.records, id)
return nil
}
func (s *MemoryStore) Iterator() Iterator {
return &MemoryIterator{}
}
func (s *MemoryStore) Drop() {
s.records = make(map[int]*Record)
}