github.com/ethersphere/bee/v2@v2.2.0/pkg/storage/inmemchunkstore/inmemchunkstore.go (about)

     1  // Copyright 2022 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package inmemchunkstore
     6  
     7  import (
     8  	"context"
     9  	"sync"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/storage"
    12  	"github.com/ethersphere/bee/v2/pkg/swarm"
    13  )
    14  
    15  type ChunkStore struct {
    16  	mu     sync.Mutex
    17  	chunks map[string]chunkCount
    18  }
    19  
    20  type chunkCount struct {
    21  	chunk swarm.Chunk
    22  	count int
    23  }
    24  
    25  func New() *ChunkStore {
    26  	return &ChunkStore{
    27  		chunks: make(map[string]chunkCount),
    28  	}
    29  }
    30  
    31  func (c *ChunkStore) Get(_ context.Context, addr swarm.Address) (swarm.Chunk, error) {
    32  	c.mu.Lock()
    33  	defer c.mu.Unlock()
    34  
    35  	chunk, ok := c.chunks[addr.ByteString()]
    36  	if !ok {
    37  		return nil, storage.ErrNotFound
    38  	}
    39  	return chunk.chunk, nil
    40  }
    41  
    42  func (c *ChunkStore) Put(_ context.Context, ch swarm.Chunk) error {
    43  	c.mu.Lock()
    44  	defer c.mu.Unlock()
    45  
    46  	chunkCount, ok := c.chunks[ch.Address().ByteString()]
    47  	if !ok {
    48  		chunkCount.chunk = swarm.NewChunk(ch.Address(), ch.Data()).WithStamp(ch.Stamp())
    49  	}
    50  	chunkCount.count++
    51  	c.chunks[ch.Address().ByteString()] = chunkCount
    52  
    53  	return nil
    54  }
    55  
    56  func (c *ChunkStore) Has(_ context.Context, addr swarm.Address) (bool, error) {
    57  	c.mu.Lock()
    58  	defer c.mu.Unlock()
    59  
    60  	_, exists := c.chunks[addr.ByteString()]
    61  
    62  	return exists, nil
    63  }
    64  
    65  func (c *ChunkStore) Delete(_ context.Context, addr swarm.Address) error {
    66  	c.mu.Lock()
    67  	defer c.mu.Unlock()
    68  
    69  	chunkCount := c.chunks[addr.ByteString()]
    70  	chunkCount.count--
    71  	if chunkCount.count <= 0 {
    72  		delete(c.chunks, addr.ByteString())
    73  	} else {
    74  		c.chunks[addr.ByteString()] = chunkCount
    75  	}
    76  
    77  	return nil
    78  }
    79  
    80  func (c *ChunkStore) Replace(_ context.Context, ch swarm.Chunk) error {
    81  	c.mu.Lock()
    82  	defer c.mu.Unlock()
    83  
    84  	chunkCount := c.chunks[ch.Address().ByteString()]
    85  	chunkCount.chunk = ch
    86  	c.chunks[ch.Address().ByteString()] = chunkCount
    87  	return nil
    88  }
    89  
    90  func (c *ChunkStore) Iterate(_ context.Context, fn storage.IterateChunkFn) error {
    91  	c.mu.Lock()
    92  	defer c.mu.Unlock()
    93  
    94  	for _, chunkCount := range c.chunks {
    95  		stop, err := fn(chunkCount.chunk)
    96  		if err != nil {
    97  			return err
    98  		}
    99  		if stop {
   100  			return nil
   101  		}
   102  	}
   103  
   104  	return nil
   105  }
   106  
   107  func (c *ChunkStore) Close() error {
   108  	return nil
   109  }