Skip to content

Commit

Permalink
noot
Browse files Browse the repository at this point in the history
  • Loading branch information
elee1766 committed Aug 15, 2024
1 parent 5e8b325 commit 506a629
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion memorystorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ func (s *memoryStorage) Exists(ctx context.Context, key string) bool {

// Store saves value at key.
func (s *memoryStorage) Store(_ context.Context, key string, value []byte) error {
s.mu.Lock()
defer s.mu.Unlock()
s.m[key] = storageEntry{
i: KeyInfo{
Key: key,
Expand All @@ -71,6 +73,8 @@ func (s *memoryStorage) Store(_ context.Context, key string, value []byte) error

// Load retrieves the value at key.
func (s *memoryStorage) Load(_ context.Context, key string) ([]byte, error) {
s.mu.Lock()
defer s.mu.Unlock()
val, ok := s.m[key]
if !ok {
return nil, os.ErrNotExist
Expand All @@ -80,12 +84,20 @@ func (s *memoryStorage) Load(_ context.Context, key string) ([]byte, error) {

// Delete deletes the value at key.
func (s *memoryStorage) Delete(_ context.Context, key string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.m, key)
return nil
}

// List returns all keys that match prefix.
func (s *memoryStorage) List(ctx context.Context, prefix string, recursive bool) ([]string, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.list(ctx, prefix, recursive)
}

func (s *memoryStorage) list(ctx context.Context, prefix string, recursive bool) ([]string, error) {
var keyList []string

keys := make([]string, 0, len(s.m))
Expand All @@ -104,7 +116,7 @@ func (s *memoryStorage) List(ctx context.Context, prefix string, recursive bool)
// If current key is a directory
if recursive && k != trimmedKey {
// Recursively traverse all child directories
childKeys, err := s.List(ctx, fullPathKey, recursive)
childKeys, err := s.list(ctx, fullPathKey, recursive)
if err != nil {
return keyList, err
}
Expand All @@ -119,6 +131,8 @@ func (s *memoryStorage) List(ctx context.Context, prefix string, recursive bool)

// Stat returns information about key.
func (s *memoryStorage) Stat(_ context.Context, key string) (KeyInfo, error) {
s.mu.Lock()
defer s.mu.Unlock()
val, ok := s.m[key]
if !ok {
return KeyInfo{}, os.ErrNotExist
Expand Down

0 comments on commit 506a629

Please sign in to comment.