github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/blobstore/blobstore.go (about) 1 // Copyright 2019 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 "io/ioutil" 22 ) 23 24 // Blobstore is an interface for storing and retrieving blobs of data by key 25 type Blobstore interface { 26 Exists(ctx context.Context, key string) (bool, error) 27 Get(ctx context.Context, key string, br BlobRange) (io.ReadCloser, string, error) 28 Put(ctx context.Context, key string, reader io.Reader) (string, error) 29 CheckAndPut(ctx context.Context, expectedVersion, key string, reader io.Reader) (string, error) 30 } 31 32 // GetBytes is a utility method calls bs.Get and handles reading the data from the returned 33 // io.ReadCloser and closing it. 34 func GetBytes(ctx context.Context, bs Blobstore, key string, br BlobRange) ([]byte, string, error) { 35 rc, ver, err := bs.Get(ctx, key, br) 36 37 if err != nil || rc == nil { 38 return nil, ver, err 39 } 40 41 defer rc.Close() 42 data, err := ioutil.ReadAll(rc) 43 44 if err != nil { 45 return nil, "", err 46 } 47 48 return data, ver, nil 49 } 50 51 // PutBytes is a utility method calls bs.Put by wrapping the supplied []byte in an io.Reader 52 func PutBytes(ctx context.Context, bs Blobstore, key string, data []byte) (string, error) { 53 reader := bytes.NewReader(data) 54 return bs.Put(ctx, key, reader) 55 }