github.com/ethersphere/bee/v2@v2.2.0/pkg/statestore/storeadapter/migration.go (about)

     1  // Copyright 2023 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 storeadapter
     6  
     7  import (
     8  	"strings"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/puller"
    11  	"github.com/ethersphere/bee/v2/pkg/storage"
    12  	"github.com/ethersphere/bee/v2/pkg/storage/migration"
    13  )
    14  
    15  func allSteps(st storage.Store) migration.Steps {
    16  	return map[uint64]migration.StepFn{
    17  		1: epochMigration(st),
    18  		2: deletePrefix(st, puller.IntervalPrefix),
    19  		3: deletePrefix(st, puller.IntervalPrefix),
    20  		4: deletePrefix(st, "blocklist"),
    21  		5: deletePrefix(st, "batchstore"),
    22  		6: deletePrefix(st, puller.IntervalPrefix),
    23  		7: deletePrefix(st, puller.IntervalPrefix),
    24  		8: deletePrefix(st, puller.IntervalPrefix),
    25  	}
    26  }
    27  
    28  func deletePrefix(s storage.Store, prefix string) migration.StepFn {
    29  	return func() error {
    30  		store := &StateStorerAdapter{s}
    31  		return store.Iterate(prefix, func(key, val []byte) (stop bool, err error) {
    32  			return false, store.Delete(string(key))
    33  		})
    34  	}
    35  }
    36  
    37  func epochMigration(s storage.Store) migration.StepFn {
    38  
    39  	return func() error {
    40  
    41  		var deleteEntries = []string{
    42  			"statestore_schema",
    43  			"tags",
    44  			puller.IntervalPrefix,
    45  			"kademlia-counters",
    46  			"addressbook",
    47  			"batch",
    48  		}
    49  
    50  		return s.Iterate(storage.Query{
    51  			Factory: func() storage.Item { return &rawItem{&proxyItem{obj: []byte(nil)}} },
    52  		}, func(res storage.Result) (stop bool, err error) {
    53  			if strings.HasPrefix(res.ID, stateStoreNamespace) {
    54  				return false, nil
    55  			}
    56  			for _, e := range deleteEntries {
    57  				if strings.HasPrefix(res.ID, e) {
    58  					_ = s.Delete(&rawItem{&proxyItem{key: res.ID}})
    59  					return false, nil
    60  				}
    61  			}
    62  
    63  			item := res.Entry.(*rawItem)
    64  			item.key = res.ID
    65  			item.ns = stateStoreNamespace
    66  			if err := s.Put(item); err != nil {
    67  				return true, err
    68  			}
    69  			_ = s.Delete(&rawItem{&proxyItem{key: res.ID}})
    70  			return false, nil
    71  		})
    72  	}
    73  }