github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/store/nbs/gc_copier.go (about)

     1  // Copyright 2020 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 nbs
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"path"
    21  	"strings"
    22  )
    23  
    24  type gcErrAccum map[string]error
    25  
    26  var _ error = gcErrAccum{}
    27  
    28  func (ea gcErrAccum) add(path string, err error) {
    29  	ea[path] = err
    30  }
    31  
    32  func (ea gcErrAccum) isEmpty() bool {
    33  	return len(ea) == 0
    34  }
    35  
    36  func (ea gcErrAccum) Error() string {
    37  	var sb strings.Builder
    38  	sb.WriteString("error garbage collecting the following files:")
    39  	for filePath, err := range ea {
    40  		sb.WriteString(fmt.Sprintf("\t%s: %s", filePath, err.Error()))
    41  	}
    42  	return sb.String()
    43  }
    44  
    45  type gcCopier struct {
    46  	writer *CmpChunkTableWriter
    47  }
    48  
    49  func newGarbageCollectionCopier() (*gcCopier, error) {
    50  	writer, err := NewCmpChunkTableWriter("")
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	return &gcCopier{writer}, nil
    55  }
    56  
    57  func (gcc *gcCopier) addChunk(ctx context.Context, c CompressedChunk) error {
    58  	return gcc.writer.AddCmpChunk(c)
    59  }
    60  
    61  func (gcc *gcCopier) copyTablesToDir(ctx context.Context, destDir string) ([]tableSpec, error) {
    62  	filename, err := gcc.writer.Finish()
    63  	if err != nil {
    64  		return nil, err
    65  	}
    66  
    67  	filepath := path.Join(destDir, filename)
    68  	err = gcc.writer.FlushToFile(filepath)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	addr, err := parseAddr(filename)
    74  	if err != nil {
    75  		return nil, err
    76  	}
    77  
    78  	return []tableSpec{
    79  		tableSpec{
    80  			name:       addr,
    81  			chunkCount: gcc.writer.ChunkCount(),
    82  		},
    83  	}, nil
    84  }