github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/libkb/memdb.go (about) 1 package libkb 2 3 import ( 4 "errors" 5 "fmt" 6 "sync" 7 8 lru "github.com/hashicorp/golang-lru" 9 ) 10 11 type MemDb struct { 12 sync.Mutex 13 lru *lru.Cache 14 } 15 16 var _ LocalDb = (*MemDb)(nil) 17 18 func NewMemDb(size int) *MemDb { 19 c, _ := lru.New(size) 20 return &MemDb{ 21 lru: c, 22 } 23 } 24 25 func (m *MemDb) Open() error { return nil } 26 func (m *MemDb) Stats() string { return "" } 27 func (m *MemDb) CompactionStats() (bool, bool, error) { 28 return false, false, nil 29 } 30 func (m *MemDb) ForceOpen() error { return nil } 31 func (m *MemDb) Close() error { 32 m.lru.Purge() 33 return nil 34 } 35 func (m *MemDb) Nuke() (string, error) { 36 m.lru.Purge() 37 return "", nil 38 } 39 func (m *MemDb) Clean(force bool) error { 40 return nil 41 } 42 func (m *MemDb) OpenTransaction() (res LocalDbTransaction, err error) { 43 return res, errors.New("not implemented") 44 } 45 46 func (m *MemDb) Put(id DbKey, aliases []DbKey, value []byte) error { 47 m.Lock() 48 defer m.Unlock() 49 m.lru.Add(id, value) 50 for _, a := range aliases { 51 m.lru.Add(a, value) 52 } 53 return nil 54 } 55 56 func (m *MemDb) Delete(id DbKey) error { 57 m.Lock() 58 defer m.Unlock() 59 m.lru.Remove(id) 60 return nil 61 } 62 63 func (m *MemDb) Get(id DbKey) ([]byte, bool, error) { 64 m.Lock() 65 defer m.Unlock() 66 val, ok := m.lru.Get(id) 67 if !ok { 68 return nil, false, nil 69 } 70 return val.([]byte), true, nil 71 } 72 73 func (m *MemDb) Lookup(alias DbKey) ([]byte, bool, error) { 74 return m.Get(alias) 75 } 76 77 func (m *MemDb) KeysWithPrefixes(prefixes ...[]byte) (DBKeySet, error) { 78 return nil, fmt.Errorf("unimplemented on memdb") 79 }