github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/blobstore/blobstore.go (about)

     1  // Copyright 2023 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package blobstore
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"io"
    21  )
    22  
    23  // Blobstore is an interface for storing and retrieving blobs of data by key
    24  type Blobstore interface {
    25  	// Path returns this blobstore's path.
    26  	Path() (path string)
    27  
    28  	// Exists returns true if a blob keyed by |key| exists.
    29  	Exists(ctx context.Context, key string) (ok bool, err error)
    30  
    31  	// Get returns a byte range of from the blob keyed by |key|, and the latest store version.
    32  	Get(ctx context.Context, key string, br BlobRange) (rc io.ReadCloser, version string, err error)
    33  
    34  	// Put creates a new blob from |reader| keyed by |key|, it returns the latest store version.
    35  	Put(ctx context.Context, key string, totalSize int64, reader io.Reader) (version string, err error)
    36  
    37  	// CheckAndPut updates the blob keyed by |key| using a check-and-set on |expectedVersion|.
    38  	CheckAndPut(ctx context.Context, expectedVersion, key string, totalSize int64, reader io.Reader) (version string, err error)
    39  
    40  	// Concatenate creates a new blob named |key| by concatenating |sources|.
    41  	Concatenate(ctx context.Context, key string, sources []string) (version string, err error)
    42  }
    43  
    44  // GetBytes is a utility method calls bs.Get and handles reading the data from the returned
    45  // io.ReadCloser and closing it.
    46  func GetBytes(ctx context.Context, bs Blobstore, key string, br BlobRange) ([]byte, string, error) {
    47  	rc, ver, err := bs.Get(ctx, key, br)
    48  
    49  	if err != nil || rc == nil {
    50  		return nil, ver, err
    51  	}
    52  
    53  	defer rc.Close()
    54  	data, err := io.ReadAll(rc)
    55  
    56  	if err != nil {
    57  		return nil, "", err
    58  	}
    59  
    60  	return data, ver, nil
    61  }
    62  
    63  // PutBytes is a utility method calls bs.Put by wrapping the supplied []byte in an io.Reader
    64  func PutBytes(ctx context.Context, bs Blobstore, key string, data []byte) (string, error) {
    65  	reader := bytes.NewReader(data)
    66  	return bs.Put(ctx, key, int64(len(data)), reader)
    67  }