Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed shell error loop on UNIQUE violation #1080

Merged
merged 2 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions go/libraries/doltcore/doltdb/index_editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,18 +70,18 @@ func (indexEd *IndexEditor) Flush(ctx context.Context) error {
indexEd.flushMutex.Lock()
defer indexEd.flushMutex.Unlock()

// We have to ensure that the edit accumulator is closed, otherwise it will cause a memory leak
defer indexEd.ed.Close() // current edit accumulator is captured by defer

if indexEd.idx.IsUnique() {
for _, numOfKeys := range indexEd.keyCount {
if numOfKeys > 1 {
indexEd.reset(indexEd.data)
return fmt.Errorf("UNIQUE constraint violation on index: %s", indexEd.idx.Name())
}
}
indexEd.keyCount = make(map[hash.Hash]int64)
}

// We have to ensure that the edit accumulator is closed, otherwise it will cause a memory leak
defer indexEd.ed.Close() // current edit accumulator is captured by defer

accEdits, err := indexEd.ed.FinishedEditing()
if err != nil {
indexEd.reset(indexEd.data)
Expand Down
35 changes: 35 additions & 0 deletions go/libraries/doltcore/doltdb/index_editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,38 @@ func TestIndexEditorWriteAfterFlush(t *testing.T) {
require.NoError(t, err)
assert.True(t, sameIndexData.Equals(newIndexData))
}

func TestIndexEditorFlushClearsUniqueError(t *testing.T) {
format := types.Format_7_18
db, err := dbfactory.MemFactory{}.CreateDB(context.Background(), format, nil, nil)
require.NoError(t, err)
colColl, err := schema.NewColCollection(
schema.NewColumn("pk", 0, types.IntKind, true),
schema.NewColumn("v1", 1, types.IntKind, false))
require.NoError(t, err)
tableSch, err := schema.SchemaFromCols(colColl)
require.NoError(t, err)
index, err := tableSch.Indexes().AddIndexByColNames("idx_unq", []string{"v1"}, schema.IndexProperties{IsUnique: true, Comment: ""})
require.NoError(t, err)
indexSch := index.Schema()
emptyMap, err := types.NewMap(context.Background(), db)
require.NoError(t, err)

indexEditor := NewIndexEditor(index, emptyMap)
dRow, err := row.New(format, indexSch, row.TaggedValues{
0: types.Int(1),
1: types.Int(1),
})
require.NoError(t, err)
require.NoError(t, indexEditor.UpdateIndex(context.Background(), nil, dRow))
dRow, err = row.New(format, indexSch, row.TaggedValues{
0: types.Int(2),
1: types.Int(1),
})
require.NoError(t, err)
require.NoError(t, indexEditor.UpdateIndex(context.Background(), nil, dRow))
err = indexEditor.Flush(context.Background())
require.Error(t, err)
err = indexEditor.Flush(context.Background())
require.NoError(t, err)
}