memory.go (4642B)
1 // Copyright 2026 Shivakumar (Certichain) 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package storage 16 17 import ( 18 "bytes" 19 "maps" 20 "slices" 21 "strings" 22 "sync" 23 ) 24 25 // memoryStore is an in-memory implementation of the Store interface. 26 // It is intended for testing and should not be used in production. 27 type memoryStore struct { 28 mu sync.RWMutex 29 data map[string][]byte 30 closed bool 31 } 32 33 // NewMemoryStore creates a new instance of an in-memory store. 34 // It is intended for testing and should not be used in production. 35 func NewMemoryStore() Store { 36 return &memoryStore{ 37 data: make(map[string][]byte), 38 } 39 } 40 41 // Get retrieves a value from the map. 42 func (m *memoryStore) Get(key []byte) ([]byte, error) { 43 m.mu.RLock() 44 defer m.mu.RUnlock() 45 46 if m.closed { 47 return nil, ErrClosed 48 } 49 if len(key) == 0 { 50 return nil, ErrEmptyKey 51 } 52 53 val, ok := m.data[string(key)] 54 if !ok { 55 return nil, ErrNotFound 56 } 57 58 // Return a clone to mimic Badger's ValueCopy behavior and ensure immutability. 59 return bytes.Clone(val), nil 60 } 61 62 // Set stores a value in the map. 63 func (m *memoryStore) Set(key, value []byte) error { 64 m.mu.Lock() 65 defer m.mu.Unlock() 66 67 if m.closed { 68 return ErrClosed 69 } 70 if len(key) == 0 { 71 return ErrEmptyKey 72 } 73 if len(value) == 0 { 74 return ErrEmptyValue 75 } 76 77 // Store a clone to prevent the caller from modifying the data later. 78 m.data[string(key)] = bytes.Clone(value) 79 80 return nil 81 } 82 83 // Delete removes a key from the map. 84 func (m *memoryStore) Delete(key []byte) error { 85 m.mu.Lock() 86 defer m.mu.Unlock() 87 88 if m.closed { 89 return ErrClosed 90 } 91 if len(key) == 0 { 92 return ErrEmptyKey 93 } 94 95 delete(m.data, string(key)) 96 return nil 97 } 98 99 // Has checks if a key exists in the map. 100 func (m *memoryStore) Has(key []byte) (bool, error) { 101 m.mu.RLock() 102 defer m.mu.RUnlock() 103 104 if m.closed { 105 return false, ErrClosed 106 } 107 if len(key) == 0 { 108 return false, ErrEmptyKey 109 } 110 111 _, ok := m.data[string(key)] 112 return ok, nil 113 } 114 115 // Iterate simulates prefix iteration by sorting map keys. 116 func (m *memoryStore) Iterate(prefix []byte, fn func(key, value []byte) error) error { 117 m.mu.RLock() 118 if m.closed { 119 m.mu.RUnlock() 120 return ErrClosed 121 } 122 123 // Extract keys that match the prefix. 124 var keys []string 125 prefixStr := string(prefix) 126 for k := range m.data { 127 if strings.HasPrefix(k, prefixStr) { 128 keys = append(keys, k) 129 } 130 } 131 slices.Sort(keys) 132 m.mu.RUnlock() 133 134 // Execute callback on sorted keys. 135 for _, k := range keys { 136 m.mu.RLock() 137 if m.closed { 138 m.mu.RUnlock() 139 return ErrClosed 140 } 141 val, ok := m.data[k] 142 if !ok { 143 // Key might have been deleted between the lock cycles. 144 m.mu.RUnlock() 145 continue 146 } 147 // Safe clones for the callback. 148 cpKey := []byte(k) 149 cpVal := bytes.Clone(val) 150 m.mu.RUnlock() 151 152 if err := fn(cpKey, cpVal); err != nil { 153 return err 154 } 155 } 156 157 return nil 158 } 159 160 // NewBatch returns a new memoryBatch for performing atomic write operations. 161 func (m *memoryStore) NewBatch() (Batch, error) { 162 m.mu.RLock() 163 defer m.mu.RUnlock() 164 165 if m.closed { 166 return nil, ErrClosed 167 } 168 169 return &memoryBatch{ 170 store: m, 171 staging: make(map[string][]byte), 172 }, nil 173 } 174 175 // Close marks the store as closed. 176 func (m *memoryStore) Close() error { 177 m.mu.Lock() 178 defer m.mu.Unlock() 179 180 m.closed = true 181 return nil 182 } 183 184 // memoryBatch is an in-memory implementation of the Batch interface. 185 type memoryBatch struct { 186 store *memoryStore 187 staging map[string][]byte 188 closed bool 189 } 190 191 // Set stages a key-value pair in the batch. 192 func (b *memoryBatch) Set(key, value []byte) error { 193 if b.closed { 194 return ErrClosed 195 } 196 if len(key) == 0 { 197 return ErrEmptyKey 198 } 199 b.staging[string(key)] = bytes.Clone(value) 200 return nil 201 } 202 203 // Commit persists all staged operations to the store atomically. 204 func (b *memoryBatch) Commit() error { 205 if b.closed { 206 return ErrClosed 207 } 208 209 b.store.mu.Lock() 210 defer b.store.mu.Unlock() 211 212 if b.store.closed { 213 return ErrClosed 214 } 215 216 // Atomically apply all staged writes to the main map 217 maps.Copy(b.store.data, b.staging) 218 219 b.closed = true 220 return nil 221 } 222 223 // Discard releases resources associated with the batch. 224 func (b *memoryBatch) Discard() { 225 b.closed = true 226 b.staging = nil 227 }