github.com/attic-labs/noms@v0.0.0-20210827224422-e5fa29d95e8b/cmd/noms/noms_blob_put.go (about)

     1  // Copyright 2016 Attic Labs, Inc. All rights reserved.
     2  // Licensed under the Apache License, version 2.0:
     3  // http://www.apache.org/licenses/LICENSE-2.0
     4  
     5  package main
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  	"io"
    11  	"os"
    12  
    13  	"github.com/attic-labs/noms/go/config"
    14  	"github.com/attic-labs/noms/go/d"
    15  	"github.com/attic-labs/noms/go/types"
    16  	"github.com/attic-labs/noms/go/util/profile"
    17  )
    18  
    19  func nomsBlobPut(filePath string, dbPath string, concurrency int) int {
    20  	info, err := os.Stat(filePath)
    21  	if err != nil {
    22  		d.CheckError(errors.New("couldn't stat file"))
    23  	}
    24  
    25  	defer profile.MaybeStartProfile().Stop()
    26  
    27  	fileSize := info.Size()
    28  	chunkSize := fileSize / int64(concurrency)
    29  	if chunkSize < (1 << 20) {
    30  		chunkSize = 1 << 20
    31  	}
    32  
    33  	readers := make([]io.Reader, fileSize/chunkSize)
    34  	for i := 0; i < len(readers); i++ {
    35  		r, err := os.Open(filePath)
    36  		d.CheckErrorNoUsage(err)
    37  		defer r.Close()
    38  		r.Seek(int64(i)*chunkSize, 0)
    39  		limit := chunkSize
    40  		if i == len(readers)-1 {
    41  			limit += fileSize % chunkSize // adjust size of last slice to include the final bytes.
    42  		}
    43  		lr := io.LimitReader(r, limit)
    44  		readers[i] = lr
    45  	}
    46  
    47  	cfg := config.NewResolver()
    48  	db, err := cfg.GetDatabase(dbPath)
    49  	if err != nil {
    50  		fmt.Fprintf(os.Stderr, "Could not open database: %s\n", err)
    51  		return 1
    52  	}
    53  	defer db.Close()
    54  
    55  	blob := types.NewBlob(db, readers...)
    56  	ref := db.WriteValue(blob)
    57  	db.Flush()
    58  	fmt.Printf("#%s\n", ref.TargetHash())
    59  	return 0
    60  }