github.com/ledgerwatch/erigon-lib@v1.0.0/kv/temporal/historyv2/storage_changeset.go (about)

     1  /*
     2     Copyright 2022 Erigon contributors
     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 historyv2
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/binary"
    22  	"errors"
    23  	"fmt"
    24  	"sort"
    25  
    26  	"github.com/ledgerwatch/erigon-lib/common/length"
    27  	"github.com/ledgerwatch/erigon-lib/kv"
    28  )
    29  
    30  var (
    31  	ErrNotFound = errors.New("not found")
    32  )
    33  
    34  func NewStorageChangeSet() *ChangeSet {
    35  	return &ChangeSet{
    36  		Changes: make([]Change, 0),
    37  		keyLen:  length.Addr + length.Hash + length.Incarnation,
    38  	}
    39  }
    40  
    41  func EncodeStorage(blockN uint64, s *ChangeSet, f func(k, v []byte) error) error {
    42  	sort.Sort(s)
    43  	keyPart := length.Addr + length.Incarnation
    44  	for _, cs := range s.Changes {
    45  		newK := make([]byte, length.BlockNum+keyPart)
    46  		binary.BigEndian.PutUint64(newK, blockN)
    47  		copy(newK[8:], cs.Key[:keyPart])
    48  		newV := make([]byte, 0, length.Hash+len(cs.Value))
    49  		newV = append(append(newV, cs.Key[keyPart:]...), cs.Value...)
    50  		if err := f(newK, newV); err != nil {
    51  			return err
    52  		}
    53  	}
    54  	return nil
    55  }
    56  
    57  func DecodeStorage(dbKey, dbValue []byte) (uint64, []byte, []byte, error) {
    58  	blockN := binary.BigEndian.Uint64(dbKey)
    59  	if len(dbValue) < length.Hash {
    60  		return 0, nil, nil, fmt.Errorf("storage changes purged for block %d", blockN)
    61  	}
    62  	k := make([]byte, length.Addr+length.Incarnation+length.Hash)
    63  	dbKey = dbKey[length.BlockNum:] // remove BlockN bytes
    64  	copy(k, dbKey)
    65  	copy(k[len(dbKey):], dbValue[:length.Hash])
    66  	v := dbValue[length.Hash:]
    67  	if len(v) == 0 {
    68  		v = nil
    69  	}
    70  
    71  	return blockN, k, v, nil
    72  }
    73  
    74  func FindStorage(c kv.CursorDupSort, blockNumber uint64, k []byte) ([]byte, error) {
    75  	addWithInc, loc := k[:length.Addr+length.Incarnation], k[length.Addr+length.Incarnation:]
    76  	seek := make([]byte, length.BlockNum+length.Addr+length.Incarnation)
    77  	binary.BigEndian.PutUint64(seek, blockNumber)
    78  	copy(seek[8:], addWithInc)
    79  	v, err := c.SeekBothRange(seek, loc)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	if !bytes.HasPrefix(v, loc) {
    84  		return nil, ErrNotFound
    85  	}
    86  	return v[length.Hash:], nil
    87  }