github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/doltdb/durable/conflict_index.go (about)

     1  // Copyright 2022 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 durable
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  
    21  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    22  	"github.com/dolthub/dolt/go/store/hash"
    23  	"github.com/dolthub/dolt/go/store/prolly/tree"
    24  	"github.com/dolthub/dolt/go/store/types"
    25  )
    26  
    27  type ConflictIndex interface {
    28  	HashOf() (hash.Hash, error)
    29  	Count() uint64
    30  	Format() *types.NomsBinFormat
    31  }
    32  
    33  // RefFromConflictIndex persists |idx| and returns the types.Ref targeting it.
    34  func RefFromConflictIndex(ctx context.Context, vrw types.ValueReadWriter, idx ConflictIndex) (types.Ref, error) {
    35  	switch idx.Format() {
    36  	case types.Format_LD_1:
    37  		return refFromNomsValue(ctx, vrw, idx.(nomsConflictIndex).index)
    38  
    39  	case types.Format_DOLT:
    40  		return types.Ref{}, fmt.Errorf("__DOLT__ conflicts should be stored in ArtifactIndex")
    41  
    42  	default:
    43  		return types.Ref{}, errNbfUnknown
    44  	}
    45  }
    46  
    47  // NewEmptyConflictIndex returns an ConflictIndex with no rows.
    48  func NewEmptyConflictIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, oursSch, theirsSch, baseSch schema.Schema) (ConflictIndex, error) {
    49  	switch vrw.Format() {
    50  	case types.Format_LD_1:
    51  		m, err := types.NewMap(ctx, vrw)
    52  		if err != nil {
    53  			return nil, err
    54  		}
    55  		return ConflictIndexFromNomsMap(m, vrw), nil
    56  
    57  	case types.Format_DOLT:
    58  		return nil, fmt.Errorf("__DOLT__ conflicts should be stored in ArtifactIndex")
    59  
    60  	default:
    61  		return nil, errNbfUnknown
    62  	}
    63  }
    64  
    65  func ConflictIndexFromNomsMap(m types.Map, vrw types.ValueReadWriter) ConflictIndex {
    66  	return nomsConflictIndex{
    67  		index: m,
    68  		vrw:   vrw,
    69  	}
    70  }
    71  
    72  func NomsMapFromConflictIndex(i ConflictIndex) types.Map {
    73  	return i.(nomsConflictIndex).index
    74  }
    75  
    76  func conflictIndexFromRef(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, ourSch, theirSch, baseSch schema.Schema, r types.Ref) (ConflictIndex, error) {
    77  	return conflictIndexFromAddr(ctx, vrw, ns, ourSch, theirSch, baseSch, r.TargetHash())
    78  }
    79  
    80  func conflictIndexFromAddr(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, ourSch, theirSch, baseSch schema.Schema, addr hash.Hash) (ConflictIndex, error) {
    81  	v, err := vrw.ReadValue(ctx, addr)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	switch vrw.Format() {
    87  	case types.Format_LD_1:
    88  		return ConflictIndexFromNomsMap(v.(types.Map), vrw), nil
    89  
    90  	case types.Format_DOLT:
    91  		return nil, fmt.Errorf("__DOLT__ conflicts should be stored in ArtifactIndex")
    92  
    93  	default:
    94  		return nil, errNbfUnknown
    95  	}
    96  }
    97  
    98  type nomsConflictIndex struct {
    99  	index types.Map
   100  	vrw   types.ValueReadWriter
   101  }
   102  
   103  func (i nomsConflictIndex) HashOf() (hash.Hash, error) {
   104  	return i.index.Hash(i.vrw.Format())
   105  }
   106  
   107  func (i nomsConflictIndex) Count() uint64 {
   108  	return i.index.Len()
   109  }
   110  
   111  func (i nomsConflictIndex) Format() *types.NomsBinFormat {
   112  	return i.vrw.Format()
   113  }