github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/internal/reserve/olditems.go (about)

     1  // Copyright 2024 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 reserve
     6  
     7  import (
     8  	"encoding/binary"
     9  	"path"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/storage"
    12  	"github.com/ethersphere/bee/v2/pkg/swarm"
    13  )
    14  
    15  // BatchRadiusItemV1 allows iteration of the chunks with respect to bin and batchID.
    16  // Used for batch evictions of certain bins.
    17  type BatchRadiusItemV1 struct {
    18  	Bin     uint8
    19  	BatchID []byte
    20  	Address swarm.Address
    21  	BinID   uint64
    22  }
    23  
    24  func (b *BatchRadiusItemV1) Namespace() string {
    25  	return "batchRadius"
    26  }
    27  
    28  func (b *BatchRadiusItemV1) ID() string {
    29  	return string(b.BatchID) + string(b.Bin) + b.Address.ByteString()
    30  }
    31  
    32  func (b *BatchRadiusItemV1) String() string {
    33  	return path.Join(b.Namespace(), b.ID())
    34  }
    35  
    36  func (b *BatchRadiusItemV1) Clone() storage.Item {
    37  	if b == nil {
    38  		return nil
    39  	}
    40  	return &BatchRadiusItemV1{
    41  		Bin:     b.Bin,
    42  		BatchID: copyBytes(b.BatchID),
    43  		Address: b.Address.Clone(),
    44  		BinID:   b.BinID,
    45  	}
    46  }
    47  
    48  const batchRadiusItemSizeV1 = 1 + swarm.HashSize + swarm.HashSize + 8
    49  
    50  func (b *BatchRadiusItemV1) Marshal() ([]byte, error) {
    51  
    52  	if b.Address.IsZero() {
    53  		return nil, errMarshalInvalidAddress
    54  	}
    55  
    56  	buf := make([]byte, batchRadiusItemSizeV1)
    57  
    58  	i := 0
    59  
    60  	buf[i] = b.Bin
    61  	i += 1
    62  
    63  	copy(buf[i:i+swarm.HashSize], b.BatchID)
    64  	i += swarm.HashSize
    65  
    66  	copy(buf[i:i+swarm.HashSize], b.Address.Bytes())
    67  	i += swarm.HashSize
    68  
    69  	binary.BigEndian.PutUint64(buf[i:i+8], b.BinID)
    70  
    71  	return buf, nil
    72  }
    73  
    74  func (b *BatchRadiusItemV1) Unmarshal(buf []byte) error {
    75  
    76  	if len(buf) != batchRadiusItemSizeV1 {
    77  		return errUnmarshalInvalidSize
    78  	}
    79  
    80  	i := 0
    81  	b.Bin = buf[i]
    82  	i += 1
    83  
    84  	b.BatchID = copyBytes(buf[i : i+swarm.HashSize])
    85  	i += swarm.HashSize
    86  
    87  	b.Address = swarm.NewAddress(buf[i : i+swarm.HashSize]).Clone()
    88  	i += swarm.HashSize
    89  
    90  	b.BinID = binary.BigEndian.Uint64(buf[i : i+8])
    91  
    92  	return nil
    93  }
    94  
    95  // ChunkBinItemV1 allows for iterating on ranges of bin and binIDs for chunks.
    96  // BinIDs come in handy when syncing the reserve contents with other peers.
    97  type ChunkBinItemV1 struct {
    98  	Bin       uint8
    99  	BinID     uint64
   100  	Address   swarm.Address
   101  	BatchID   []byte
   102  	ChunkType swarm.ChunkType
   103  }
   104  
   105  func (c *ChunkBinItemV1) Namespace() string {
   106  	return "chunkBin"
   107  }
   108  
   109  func (c *ChunkBinItemV1) ID() string {
   110  	return binIDToString(c.Bin, c.BinID)
   111  }
   112  
   113  func (c *ChunkBinItemV1) String() string {
   114  	return path.Join(c.Namespace(), c.ID())
   115  }
   116  
   117  func (c *ChunkBinItemV1) Clone() storage.Item {
   118  	if c == nil {
   119  		return nil
   120  	}
   121  	return &ChunkBinItemV1{
   122  		Bin:       c.Bin,
   123  		BinID:     c.BinID,
   124  		Address:   c.Address.Clone(),
   125  		BatchID:   copyBytes(c.BatchID),
   126  		ChunkType: c.ChunkType,
   127  	}
   128  }
   129  
   130  const chunkBinItemSizeV1 = 1 + 8 + swarm.HashSize + swarm.HashSize + 1
   131  
   132  func (c *ChunkBinItemV1) Marshal() ([]byte, error) {
   133  
   134  	if c.Address.IsZero() {
   135  		return nil, errMarshalInvalidAddress
   136  	}
   137  
   138  	buf := make([]byte, chunkBinItemSizeV1)
   139  	i := 0
   140  
   141  	buf[i] = c.Bin
   142  	i += 1
   143  
   144  	binary.BigEndian.PutUint64(buf[i:i+8], c.BinID)
   145  	i += 8
   146  
   147  	copy(buf[i:i+swarm.HashSize], c.Address.Bytes())
   148  	i += swarm.HashSize
   149  
   150  	copy(buf[i:i+swarm.HashSize], c.BatchID)
   151  	i += swarm.HashSize
   152  
   153  	buf[i] = uint8(c.ChunkType)
   154  
   155  	return buf, nil
   156  }
   157  
   158  func (c *ChunkBinItemV1) Unmarshal(buf []byte) error {
   159  
   160  	if len(buf) != chunkBinItemSizeV1 {
   161  		return errUnmarshalInvalidSize
   162  	}
   163  
   164  	i := 0
   165  	c.Bin = buf[i]
   166  	i += 1
   167  
   168  	c.BinID = binary.BigEndian.Uint64(buf[i : i+8])
   169  	i += 8
   170  
   171  	c.Address = swarm.NewAddress(buf[i : i+swarm.HashSize]).Clone()
   172  	i += swarm.HashSize
   173  
   174  	c.BatchID = copyBytes(buf[i : i+swarm.HashSize])
   175  	i += swarm.HashSize
   176  
   177  	c.ChunkType = swarm.ChunkType(buf[i])
   178  
   179  	return nil
   180  }