github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/fkconstrain/diffitr.go (about)

     1  // Copyright 2021 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 fkconstrain
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"fmt"
    21  	"time"
    22  
    23  	"github.com/dolthub/dolt/go/libraries/doltcore/diff"
    24  	"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
    25  	"github.com/dolthub/dolt/go/libraries/doltcore/schema"
    26  	nomsdiff "github.com/dolthub/dolt/go/store/diff"
    27  	"github.com/dolthub/dolt/go/store/types"
    28  )
    29  
    30  type mapIterAsRowDiffer struct {
    31  	itr types.MapIterator
    32  	ctx context.Context
    33  }
    34  
    35  // Start starts the RowDiffer.
    36  func (mitr mapIterAsRowDiffer) Start(_ context.Context, _, _ types.Map) {}
    37  
    38  // GetDiffs returns the requested number of diff.Differences, or times out.
    39  func (mitr mapIterAsRowDiffer) GetDiffs(_ int, _ time.Duration) ([]*nomsdiff.Difference, bool, error) {
    40  	k, v, err := mitr.itr.Next(mitr.ctx)
    41  
    42  	if err != nil {
    43  		return nil, false, err
    44  	}
    45  
    46  	if k == nil {
    47  		return nil, false, nil
    48  	}
    49  
    50  	return []*nomsdiff.Difference{{
    51  		ChangeType:  types.DiffChangeAdded,
    52  		OldValue:    nil,
    53  		NewValue:    v,
    54  		NewKeyValue: k,
    55  		KeyValue:    k,
    56  	}}, true, nil
    57  }
    58  
    59  // Close closes the RowDiffer.
    60  func (mitr mapIterAsRowDiffer) Close() error {
    61  	return nil
    62  }
    63  
    64  func getDiffItr(ctx context.Context, parentCommitRoot, root *doltdb.RootValue, tblName string) (diff.RowDiffer, error) {
    65  	parentOK := true
    66  	pTbl, pSch, err := getTableAndSchema(ctx, parentCommitRoot, tblName)
    67  
    68  	if errors.Is(err, doltdb.ErrTableNotFound) {
    69  		parentOK = false
    70  	} else if err != nil {
    71  		return nil, err
    72  	}
    73  
    74  	tbl, sch, err := getTableAndSchema(ctx, root, tblName)
    75  
    76  	if err != nil {
    77  		return nil, err
    78  	}
    79  
    80  	rowData, err := tbl.GetRowData(ctx)
    81  
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  
    86  	if !parentOK {
    87  		itr, err := rowData.Iterator(ctx)
    88  
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  
    93  		return mapIterAsRowDiffer{itr: itr, ctx: ctx}, nil
    94  	}
    95  
    96  	pRowData, err := pTbl.GetRowData(ctx)
    97  
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	rd := diff.NewRowDiffer(ctx, pSch, sch, 1024)
   103  	rd.Start(ctx, pRowData, rowData)
   104  
   105  	return rd, nil
   106  }
   107  
   108  func getTableAndSchema(ctx context.Context, root *doltdb.RootValue, tableName string) (*doltdb.Table, schema.Schema, error) {
   109  	tbl, _, ok, err := root.GetTableInsensitive(ctx, tableName)
   110  
   111  	if err != nil {
   112  		return nil, nil, err
   113  	} else if !ok {
   114  		return nil, nil, fmt.Errorf("%w: %s", doltdb.ErrTableNotFound, tableName)
   115  	}
   116  
   117  	sch, err := tbl.GetSchema(ctx)
   118  
   119  	if err != nil {
   120  		return nil, nil, err
   121  	}
   122  
   123  	return tbl, sch, nil
   124  }