github.com/MetalBlockchain/metalgo@v1.11.9/api/admin/key_value_reader.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package admin
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/MetalBlockchain/metalgo/database"
    10  )
    11  
    12  var _ database.KeyValueReader = (*KeyValueReader)(nil)
    13  
    14  type KeyValueReader struct {
    15  	client Client
    16  }
    17  
    18  func NewKeyValueReader(client Client) *KeyValueReader {
    19  	return &KeyValueReader{
    20  		client: client,
    21  	}
    22  }
    23  
    24  func (r *KeyValueReader) Has(key []byte) (bool, error) {
    25  	_, err := r.client.DBGet(context.Background(), key)
    26  	if err == database.ErrNotFound {
    27  		return false, nil
    28  	}
    29  	return err == nil, err
    30  }
    31  
    32  func (r *KeyValueReader) Get(key []byte) ([]byte, error) {
    33  	return r.client.DBGet(context.Background(), key)
    34  }