github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/libraries/doltcore/diff/diff.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 diff
    16  
    17  import (
    18  	"context"
    19  	"time"
    20  
    21  	"github.com/dolthub/go-mysql-server/sql"
    22  
    23  	"github.com/dolthub/dolt/go/store/diff"
    24  	"github.com/dolthub/dolt/go/store/types"
    25  )
    26  
    27  // ChangeType is an enum that represents the type of change in a diff
    28  type ChangeType int
    29  
    30  const (
    31  	// None is no change
    32  	None ChangeType = iota
    33  	// Added is the ChangeType value for a row that was newly added (In new, but not in old)
    34  	Added
    35  	// Removed is the ChangeTypeProp value for a row that was newly deleted (In old, but not in new)
    36  	Removed
    37  	// ModifiedOld is the ChangeType value for the row which represents the old value of the row before it was changed.
    38  	ModifiedOld
    39  	// ModifiedNew is the ChangeType value for the row which represents the new value of the row after it was changed.
    40  	ModifiedNew
    41  )
    42  
    43  // Mode is an enum that represents the presentation of a diff
    44  type Mode int
    45  
    46  const (
    47  	ModeRow     Mode = 0
    48  	ModeLine    Mode = 1
    49  	ModeInPlace Mode = 2
    50  	ModeContext Mode = 3
    51  )
    52  
    53  type RowDiffer interface {
    54  	// Start starts the RowDiffer.
    55  	Start(ctx context.Context, from, to types.Map)
    56  
    57  	// StartWithRange starts the RowDiffer with the specified range
    58  	StartWithRange(ctx context.Context, from, to types.Map, start types.Value, inRange types.ValueInRange)
    59  
    60  	// GetDiffs returns the requested number of diff.Differences, or times out.
    61  	GetDiffs(numDiffs int, timeout time.Duration) ([]*diff.Difference, bool, error)
    62  
    63  	// GetDiffsWithFilter returns the requested number of filtered diff.Differences, or times out.
    64  	GetDiffsWithFilter(numDiffs int, timeout time.Duration, filterByChangeType types.DiffChangeType) ([]*diff.Difference, bool, error)
    65  
    66  	// Close closes the RowDiffer.
    67  	Close() error
    68  }
    69  
    70  // SqlRowDiffWriter knows how to write diff rows for a table to an arbitrary format and destination.
    71  type SqlRowDiffWriter interface {
    72  	// WriteRow writes the diff row given, of the diff type provided. colDiffTypes is guaranteed to be the same length as
    73  	// the input row.
    74  	WriteRow(ctx context.Context, row sql.Row, diffType ChangeType, colDiffTypes []ChangeType) error
    75  
    76  	// WriteCombinedRow writes the diff of the rows given as a single, combined row.
    77  	WriteCombinedRow(ctx context.Context, oldRow, newRow sql.Row, mode Mode) error
    78  
    79  	// Close finalizes the work of this writer.
    80  	Close(ctx context.Context) error
    81  }
    82  
    83  // SchemaDiffWriter knows how to write SQL DDL statements for a schema diff for a table to an arbitrary format and
    84  // destination.
    85  type SchemaDiffWriter interface {
    86  	// WriteSchemaDiff writes the schema diff given (a SQL statement) and returns any error. A single table may have
    87  	// many SQL statements for a single diff. WriteSchemaDiff will be called before any row diffs via |WriteRow|
    88  	WriteSchemaDiff(schemaDiffStatement string) error
    89  	// Close finalizes the work of this writer.
    90  	Close() error
    91  }