github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/internal/stampindex/oldstampindex.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 stampindex
     6  
     7  import (
     8  	"encoding/binary"
     9  	"fmt"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/storage"
    12  	"github.com/ethersphere/bee/v2/pkg/storage/storageutil"
    13  	"github.com/ethersphere/bee/v2/pkg/storer/internal"
    14  	"github.com/ethersphere/bee/v2/pkg/swarm"
    15  )
    16  
    17  // ItemV1 is an store.Item that represents data relevant to stamp.
    18  type ItemV1 struct {
    19  	// Keys.
    20  	namespace  []byte // The namespace of other related item.
    21  	BatchID    []byte
    22  	StampIndex []byte
    23  
    24  	// Values.
    25  	StampTimestamp   []byte
    26  	ChunkAddress     swarm.Address
    27  	ChunkIsImmutable bool
    28  }
    29  
    30  // ID implements the storage.Item interface.
    31  func (i ItemV1) ID() string {
    32  	return fmt.Sprintf("%s/%s/%s", string(i.namespace), string(i.BatchID), string(i.StampIndex))
    33  }
    34  
    35  // Namespace implements the storage.Item interface.
    36  func (i ItemV1) Namespace() string {
    37  	return "stampIndex"
    38  }
    39  
    40  // Marshal implements the storage.Item interface.
    41  func (i ItemV1) Marshal() ([]byte, error) {
    42  	switch {
    43  	case len(i.namespace) == 0:
    44  		return nil, errStampItemMarshalScopeInvalid
    45  	case len(i.BatchID) != swarm.HashSize:
    46  		return nil, errStampItemMarshalBatchIDInvalid
    47  	case len(i.StampIndex) != swarm.StampIndexSize:
    48  		return nil, errStampItemMarshalBatchIndexInvalid
    49  	}
    50  
    51  	buf := make([]byte, 8+len(i.namespace)+swarm.HashSize+swarm.StampIndexSize+swarm.StampTimestampSize+swarm.HashSize)
    52  
    53  	l := 0
    54  	binary.LittleEndian.PutUint64(buf[l:l+8], uint64(len(i.namespace)))
    55  	l += 8
    56  	copy(buf[l:l+len(i.namespace)], i.namespace)
    57  	l += len(i.namespace)
    58  	copy(buf[l:l+swarm.HashSize], i.BatchID)
    59  	l += swarm.HashSize
    60  	copy(buf[l:l+swarm.StampIndexSize], i.StampIndex)
    61  	l += swarm.StampIndexSize
    62  	copy(buf[l:l+swarm.StampTimestampSize], i.StampTimestamp)
    63  	l += swarm.StampTimestampSize
    64  	copy(buf[l:l+swarm.HashSize], internal.AddressBytesOrZero(i.ChunkAddress))
    65  	return buf, nil
    66  }
    67  
    68  // Unmarshal implements the storage.Item interface.
    69  func (i *ItemV1) Unmarshal(bytes []byte) error {
    70  	if len(bytes) < 8 {
    71  		return errStampItemUnmarshalInvalidSize
    72  	}
    73  	nsLen := int(binary.LittleEndian.Uint64(bytes))
    74  	if len(bytes) != 8+nsLen+swarm.HashSize+swarm.StampIndexSize+swarm.StampTimestampSize+swarm.HashSize {
    75  		return errStampItemUnmarshalInvalidSize
    76  	}
    77  
    78  	ni := new(ItemV1)
    79  	l := 8
    80  	ni.namespace = append(make([]byte, 0, nsLen), bytes[l:l+nsLen]...)
    81  	l += nsLen
    82  	ni.BatchID = append(make([]byte, 0, swarm.HashSize), bytes[l:l+swarm.HashSize]...)
    83  	l += swarm.HashSize
    84  	ni.StampIndex = append(make([]byte, 0, swarm.StampIndexSize), bytes[l:l+swarm.StampIndexSize]...)
    85  	l += swarm.StampIndexSize
    86  	ni.StampTimestamp = append(make([]byte, 0, swarm.StampTimestampSize), bytes[l:l+swarm.StampTimestampSize]...)
    87  	l += swarm.StampTimestampSize
    88  	ni.ChunkAddress = internal.AddressOrZero(bytes[l : l+swarm.HashSize])
    89  	*i = *ni
    90  	return nil
    91  }
    92  
    93  // Clone  implements the storage.Item interface.
    94  func (i *ItemV1) Clone() storage.Item {
    95  	if i == nil {
    96  		return nil
    97  	}
    98  	return &ItemV1{
    99  		namespace:        append([]byte(nil), i.namespace...),
   100  		BatchID:          append([]byte(nil), i.BatchID...),
   101  		StampIndex:       append([]byte(nil), i.StampIndex...),
   102  		StampTimestamp:   append([]byte(nil), i.StampTimestamp...),
   103  		ChunkAddress:     i.ChunkAddress.Clone(),
   104  		ChunkIsImmutable: i.ChunkIsImmutable,
   105  	}
   106  }
   107  
   108  // String implements the fmt.Stringer interface.
   109  func (i ItemV1) String() string {
   110  	return storageutil.JoinFields(i.Namespace(), i.ID())
   111  }
   112  
   113  func (i *ItemV1) SetNamespace(ns []byte) {
   114  	i.namespace = ns
   115  }