k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/checkpointmanager/testing/util.go (about) 1 /* 2 Copyright 2017 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testing 18 19 import ( 20 "fmt" 21 "sync" 22 ) 23 24 // MemStore is an implementation of CheckpointStore interface which stores checkpoint in memory. 25 type MemStore struct { 26 mem map[string][]byte 27 sync.Mutex 28 } 29 30 // NewMemStore returns an instance of MemStore 31 func NewMemStore() *MemStore { 32 return &MemStore{mem: make(map[string][]byte)} 33 } 34 35 // Write writes the data to the store 36 func (mstore *MemStore) Write(key string, data []byte) error { 37 mstore.Lock() 38 defer mstore.Unlock() 39 mstore.mem[key] = data 40 return nil 41 } 42 43 // Read returns data read from store 44 func (mstore *MemStore) Read(key string) ([]byte, error) { 45 mstore.Lock() 46 defer mstore.Unlock() 47 data, ok := mstore.mem[key] 48 if !ok { 49 return nil, fmt.Errorf("checkpoint is not found") 50 } 51 return data, nil 52 } 53 54 // Delete deletes data from the store 55 func (mstore *MemStore) Delete(key string) error { 56 mstore.Lock() 57 defer mstore.Unlock() 58 delete(mstore.mem, key) 59 return nil 60 } 61 62 // List returns all the keys from the store 63 func (mstore *MemStore) List() ([]string, error) { 64 mstore.Lock() 65 defer mstore.Unlock() 66 keys := make([]string, 0) 67 for key := range mstore.mem { 68 keys = append(keys, key) 69 } 70 return keys, nil 71 }