github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/chunks/chunk.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  // This file incorporates work covered by the following copyright and
    16  // permission notice:
    17  //
    18  // Copyright 2016 Attic Labs, Inc. All rights reserved.
    19  // Licensed under the Apache License, version 2.0:
    20  // http://www.apache.org/licenses/LICENSE-2.0
    21  
    22  // Package chunks provides facilities for representing, storing, and fetching content-addressed chunks of Noms data.
    23  package chunks
    24  
    25  import (
    26  	"bytes"
    27  
    28  	"github.com/dolthub/dolt/go/store/d"
    29  	"github.com/dolthub/dolt/go/store/hash"
    30  )
    31  
    32  // Chunk is a unit of stored data in noms
    33  type Chunk struct {
    34  	r    hash.Hash
    35  	data []byte
    36  }
    37  
    38  var EmptyChunk = NewChunk([]byte{})
    39  
    40  func (c Chunk) Hash() hash.Hash {
    41  	return c.r
    42  }
    43  
    44  func (c Chunk) Data() []byte {
    45  	return c.data
    46  }
    47  
    48  func (c Chunk) ToChunk() (Chunk, error) {
    49  	return c, nil
    50  }
    51  
    52  func (c Chunk) IsEmpty() bool {
    53  	return len(c.data) == 0
    54  }
    55  
    56  // NewChunk creates a new Chunk backed by data. This means that the returned Chunk has ownership of this slice of memory.
    57  func NewChunk(data []byte) Chunk {
    58  	r := hash.Of(data)
    59  	return Chunk{r, data}
    60  }
    61  
    62  // NewChunkWithHash creates a new chunk with a known hash. The hash is not re-calculated or verified. This should obviously only be used in cases where the caller already knows the specified hash is correct.
    63  func NewChunkWithHash(r hash.Hash, data []byte) Chunk {
    64  	return Chunk{r, data}
    65  }
    66  
    67  // ChunkWriter wraps an io.WriteCloser, additionally providing the ability to grab the resulting Chunk for all data written through the interface. Calling Chunk() or Close() on an instance disallows further writing.
    68  type ChunkWriter struct {
    69  	buffer *bytes.Buffer
    70  	c      Chunk
    71  }
    72  
    73  func NewChunkWriter() *ChunkWriter {
    74  	b := &bytes.Buffer{}
    75  	return &ChunkWriter{
    76  		buffer: b,
    77  	}
    78  }
    79  
    80  func (w *ChunkWriter) Write(data []byte) (int, error) {
    81  	if w.buffer == nil {
    82  		d.Panic("Write() cannot be called after Hash() or Close().")
    83  	}
    84  	size, err := w.buffer.Write(data)
    85  	d.Chk.NoError(err)
    86  	return size, nil
    87  }
    88  
    89  // Chunk() closes the writer and returns the resulting Chunk.
    90  func (w *ChunkWriter) Chunk() Chunk {
    91  	d.Chk.NoError(w.Close())
    92  	return w.c
    93  }
    94  
    95  // Close() closes computes the hash and Puts it into the ChunkSink Note: The Write() method never returns an error. Instead, like other noms interfaces, errors are reported via panic.
    96  func (w *ChunkWriter) Close() error {
    97  	if w.buffer == nil {
    98  		return nil
    99  	}
   100  
   101  	w.c = NewChunk(w.buffer.Bytes())
   102  	w.buffer = nil
   103  	return nil
   104  }