Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_text/tests/detect_corpus/go/interfaces.txt

1.1 KiB, 1 run

created by r1870400018:11980, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1package storage
2
3import (
4 "errors"
5 "sync"
6)
7
8var ErrNotFound = errors.New("key not found")
9
10type Store interface {
11 Get(key string) ([]byte, error)
12 Put(key string, value []byte) error
13 Delete(key string) error
14 Keys() []string
15}
16
17type MemStore struct {
18 mu sync.RWMutex
19 data map[string][]byte
20}
21
22func NewMemStore() *MemStore {
23 return &MemStore{data: make(map[string][]byte)}
24}
25
26func (m *MemStore) Get(key string) ([]byte, error) {
27 m.mu.RLock()
28 defer m.mu.RUnlock()
29 val, ok := m.data[key]
30 if !ok {
31 return nil, ErrNotFound
32 }
33 cp := make([]byte, len(val))
34 copy(cp, val)
35 return cp, nil
36}
37
38func (m *MemStore) Put(key string, value []byte) error {
39 m.mu.Lock()
40 defer m.mu.Unlock()
41 cp := make([]byte, len(value))
42 copy(cp, value)
43 m.data[key] = cp
44 return nil
45}
46
47func (m *MemStore) Delete(key string) error {
48 m.mu.Lock()
49 defer m.mu.Unlock()
50 if _, ok := m.data[key]; !ok {
51 return ErrNotFound
52 }
53 delete(m.data, key)
54 return nil
55}
56
57func (m *MemStore) Keys() []string {
58 m.mu.RLock()
59 defer m.mu.RUnlock()
60 keys := make([]string, 0, len(m.data))
61 for k := range m.data {
62 keys = append(keys, k)
63 }
64 return keys
65}