github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/migration/step_02.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 migration
     6  
     7  import (
     8  	"context"
     9  	"time"
    10  
    11  	storage "github.com/ethersphere/bee/v2/pkg/storage"
    12  	"github.com/ethersphere/bee/v2/pkg/storer/internal/cache"
    13  	"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
    14  	"github.com/ethersphere/bee/v2/pkg/swarm"
    15  )
    16  
    17  // step_02 migrates the cache to the new format.
    18  // the old cacheEntry item has the same key, but the value is different. So only
    19  // a Put is needed.
    20  func step_02(st transaction.Storage) func() error {
    21  
    22  	return func() error {
    23  
    24  		trx, done := st.NewTransaction(context.Background())
    25  		defer done()
    26  
    27  		var entries []*cache.CacheEntryItem
    28  		err := trx.IndexStore().Iterate(
    29  			storage.Query{
    30  				Factory:      func() storage.Item { return &cache.CacheEntryItem{} },
    31  				ItemProperty: storage.QueryItemID,
    32  			},
    33  			func(res storage.Result) (bool, error) {
    34  				entry := &cache.CacheEntryItem{
    35  					Address:         swarm.NewAddress([]byte(res.ID)),
    36  					AccessTimestamp: time.Now().UnixNano(),
    37  				}
    38  				entries = append(entries, entry)
    39  				return false, nil
    40  			},
    41  		)
    42  		if err != nil {
    43  			return err
    44  		}
    45  
    46  		for _, entry := range entries {
    47  			err := trx.IndexStore().Put(entry)
    48  			if err != nil {
    49  				return err
    50  			}
    51  		}
    52  
    53  		return trx.Commit()
    54  	}
    55  
    56  }