github.com/demonoid81/containerd@v1.3.4/metadata/bolt.go (about)

     1  /*
     2     Copyright The containerd Authors.
     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 metadata
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  	bolt "go.etcd.io/bbolt"
    24  )
    25  
    26  type transactionKey struct{}
    27  
    28  // WithTransactionContext returns a new context holding the provided
    29  // bolt transaction. Functions which require a bolt transaction will
    30  // first check to see if a transaction is already created on the
    31  // context before creating their own.
    32  func WithTransactionContext(ctx context.Context, tx *bolt.Tx) context.Context {
    33  	return context.WithValue(ctx, transactionKey{}, tx)
    34  }
    35  
    36  type transactor interface {
    37  	View(fn func(*bolt.Tx) error) error
    38  	Update(fn func(*bolt.Tx) error) error
    39  }
    40  
    41  // view gets a bolt db transaction either from the context
    42  // or starts a new one with the provided bolt database.
    43  func view(ctx context.Context, db transactor, fn func(*bolt.Tx) error) error {
    44  	tx, ok := ctx.Value(transactionKey{}).(*bolt.Tx)
    45  	if !ok {
    46  		return db.View(fn)
    47  	}
    48  	return fn(tx)
    49  }
    50  
    51  // update gets a writable bolt db transaction either from the context
    52  // or starts a new one with the provided bolt database.
    53  func update(ctx context.Context, db transactor, fn func(*bolt.Tx) error) error {
    54  	tx, ok := ctx.Value(transactionKey{}).(*bolt.Tx)
    55  	if !ok {
    56  		return db.Update(fn)
    57  	} else if !tx.Writable() {
    58  		return errors.Wrap(bolt.ErrTxNotWritable, "unable to use transaction from context")
    59  	}
    60  	return fn(tx)
    61  }