github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/pinstore.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 storer
     6  
     7  import (
     8  	"context"
     9  	"fmt"
    10  	"time"
    11  
    12  	storage "github.com/ethersphere/bee/v2/pkg/storage"
    13  	"github.com/ethersphere/bee/v2/pkg/storer/internal"
    14  	pinstore "github.com/ethersphere/bee/v2/pkg/storer/internal/pinning"
    15  	"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
    16  	"github.com/ethersphere/bee/v2/pkg/swarm"
    17  )
    18  
    19  // NewCollection is the implementation of the PinStore.NewCollection method.
    20  func (db *DB) NewCollection(ctx context.Context) (PutterSession, error) {
    21  	var (
    22  		pinningPutter internal.PutterCloserWithReference
    23  		err           error
    24  	)
    25  	err = db.storage.Run(ctx, func(store transaction.Store) error {
    26  		pinningPutter, err = pinstore.NewCollection(store.IndexStore())
    27  		if err != nil {
    28  			return fmt.Errorf("pinstore.NewCollection: %w", err)
    29  		}
    30  		return nil
    31  	})
    32  	if err != nil {
    33  		return nil, err
    34  	}
    35  
    36  	return &putterSession{
    37  		Putter: putterWithMetrics{
    38  			storage.PutterFunc(
    39  				func(ctx context.Context, chunk swarm.Chunk) error {
    40  					unlock := db.Lock(uploadsLock)
    41  					defer unlock()
    42  					return db.storage.Run(ctx, func(s transaction.Store) error {
    43  						return pinningPutter.Put(ctx, s, chunk)
    44  					})
    45  				},
    46  			),
    47  			db.metrics,
    48  			"pinstore",
    49  		},
    50  		done: func(address swarm.Address) error {
    51  			unlock := db.Lock(uploadsLock)
    52  			defer unlock()
    53  			return db.storage.Run(ctx, func(s transaction.Store) error {
    54  				return pinningPutter.Close(s.IndexStore(), address)
    55  			})
    56  		},
    57  		cleanup: func() error {
    58  			unlock := db.Lock(uploadsLock)
    59  			defer unlock()
    60  			return pinningPutter.Cleanup(db.storage)
    61  		},
    62  	}, nil
    63  }
    64  
    65  // DeletePin is the implementation of the PinStore.DeletePin method.
    66  func (db *DB) DeletePin(ctx context.Context, root swarm.Address) (err error) {
    67  	dur := captureDuration(time.Now())
    68  	defer func() {
    69  		db.metrics.MethodCallsDuration.WithLabelValues("pinstore", "DeletePin").Observe(dur())
    70  		if err == nil {
    71  			db.metrics.MethodCalls.WithLabelValues("pinstore", "DeletePin", "success").Inc()
    72  		} else {
    73  			db.metrics.MethodCalls.WithLabelValues("pinstore", "DeletePin", "failure").Inc()
    74  		}
    75  	}()
    76  
    77  	unlock := db.Lock(uploadsLock)
    78  	defer unlock()
    79  
    80  	return pinstore.DeletePin(ctx, db.storage, root)
    81  }
    82  
    83  // Pins is the implementation of the PinStore.Pins method.
    84  func (db *DB) Pins() (address []swarm.Address, err error) {
    85  	dur := captureDuration(time.Now())
    86  	defer func() {
    87  		db.metrics.MethodCallsDuration.WithLabelValues("pinstore", "Pins").Observe(dur())
    88  		if err == nil {
    89  			db.metrics.MethodCalls.WithLabelValues("pinstore", "Pins", "success").Inc()
    90  		} else {
    91  			db.metrics.MethodCalls.WithLabelValues("pinstore", "Pins", "failure").Inc()
    92  		}
    93  	}()
    94  
    95  	return pinstore.Pins(db.storage.IndexStore())
    96  }
    97  
    98  // HasPin is the implementation of the PinStore.HasPin method.
    99  func (db *DB) HasPin(root swarm.Address) (has bool, err error) {
   100  	dur := captureDuration(time.Now())
   101  	defer func() {
   102  		db.metrics.MethodCallsDuration.WithLabelValues("pinstore", "HasPin").Observe(dur())
   103  		if err == nil {
   104  			db.metrics.MethodCalls.WithLabelValues("pinstore", "HasPin", "success").Inc()
   105  		} else {
   106  			db.metrics.MethodCalls.WithLabelValues("pinstore", "HasPin", "failure").Inc()
   107  		}
   108  	}()
   109  
   110  	return pinstore.HasPin(db.storage.IndexStore(), root)
   111  }
   112  
   113  func (db *DB) IteratePinCollection(root swarm.Address, iterateFn func(swarm.Address) (bool, error)) error {
   114  	return pinstore.IterateCollection(db.storage.IndexStore(), root, iterateFn)
   115  }