Skip to content

Commit

Permalink
Cleanup calls to NewEmptyIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
nicktobey committed Nov 26, 2024
1 parent 00d6c1d commit f375111
Show file tree
Hide file tree
Showing 19 changed files with 46 additions and 32 deletions.
2 changes: 1 addition & 1 deletion go/cmd/dolt/commands/schcmds/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func putEmptyTableWithSchema(ctx context.Context, tblName string, root doltdb.Ro
return nil, errhand.BuildDError("error: failed to get table.").AddCause(err).Build()
}

empty, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), sch, false)
empty, err := durable.NewEmptyPrimaryIndex(ctx, root.VRW(), root.NodeStore(), sch)
if err != nil {
return nil, errhand.BuildDError("error: failed to get table.").AddCause(err).Build()
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/commit_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func TestAsyncPushOnWrite(t *testing.T) {
assert.NoError(t, err)

tSchema := createTestSchema(t)
rowData, err := durable.NewEmptyIndex(ctx, ddb.vrw, ddb.ns, tSchema, false)
rowData, err := durable.NewEmptyPrimaryIndex(ctx, ddb.vrw, ddb.ns, tSchema)
require.NoError(t, err)
tbl, err := CreateTestTable(ddb.vrw, ddb.ns, tSchema, rowData)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions go/libraries/doltcore/doltdb/doltdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func CreateTestTable(vrw types.ValueReadWriter, ns tree.NodeStore, tSchema schem

func createTestRowData(t *testing.T, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema) durable.Index {
if types.Format_Default == types.Format_DOLT {
idx, err := durable.NewEmptyIndex(context.Background(), vrw, ns, sch, false)
idx, err := durable.NewEmptyPrimaryIndex(context.Background(), vrw, ns, sch)
require.NoError(t, err)
return idx
}
Expand Down Expand Up @@ -303,7 +303,7 @@ func TestLDNoms(t *testing.T) {

ctx := context.Background()
tSchema := createTestSchema(t)
rowData, err := durable.NewEmptyIndex(ctx, ddb.vrw, ddb.ns, tSchema, false)
rowData, err := durable.NewEmptyPrimaryIndex(ctx, ddb.vrw, ddb.ns, tSchema)
if err != nil {
t.Fatal("Failed to create new empty index")
}
Expand Down
30 changes: 22 additions & 8 deletions go/libraries/doltcore/doltdb/durable/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/dolthub/go-mysql-server/sql/expression/function/vector"
"io"
"strings"

Expand Down Expand Up @@ -123,8 +124,17 @@ func indexFromAddr(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeS
}
}

// NewEmptyIndex returns an index with no rows.
func NewEmptyIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, isKeylessSecondary bool) (Index, error) {
func NewEmptyPrimaryIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema) (Index, error) {
return newEmptyIndex(ctx, vrw, ns, sch, false, false)
}

func NewEmptyIndexFromSchemaIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, idx schema.Index) (Index, error) {
sch := idx.Schema()
return newEmptyIndex(ctx, vrw, ns, sch, idx.IsVector(), schema.IsKeyless(sch))
}

// newEmptyIndex returns an index with no rows.
func newEmptyIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, isVector bool, isKeylessSecondary bool) (Index, error) {
switch vrw.Format() {
case types.Format_LD_1:
m, err := types.NewMap(ctx, vrw)
Expand All @@ -138,17 +148,21 @@ func NewEmptyIndex(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeS
if isKeylessSecondary {
kd = prolly.AddHashToSchema(kd)
}
m, err := prolly.NewMapFromTuples(ctx, ns, kd, vd)
if err != nil {
return nil, err
}
return IndexFromProllyMap(m), nil
return NewEmptyProllyIndex(ctx, ns, kd, vd)

default:
return nil, errNbfUnknown
}
}

func NewEmptyProllyIndex(ctx context.Context, ns tree.NodeStore, kd, vd val.TupleDesc) (Index, error) {
m, err := prolly.NewMapFromTuples(ctx, ns, kd, vd)
if err != nil {
return nil, err
}
return IndexFromProllyMap(m), nil
}

type nomsIndex struct {
index types.Map
vrw types.ValueReadWriter
Expand Down Expand Up @@ -393,7 +407,7 @@ func NewIndexSetWithEmptyIndexes(ctx context.Context, vrw types.ValueReadWriter,
return nil, err
}
for _, index := range sch.Indexes().AllIndexes() {
empty, err := NewEmptyIndex(ctx, vrw, ns, index.Schema(), false)
empty, err := NewEmptyIndexFromSchemaIndex(ctx, vrw, ns, index)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/root_val.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ func (root *rootValue) putTable(ctx context.Context, tName TableName, ref types.
func CreateEmptyTable(ctx context.Context, root RootValue, tName TableName, sch schema.Schema) (RootValue, error) {
ns := root.NodeStore()
vrw := root.VRW()
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
empty, err := durable.NewEmptyPrimaryIndex(ctx, vrw, ns, sch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/doltdb/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func NewTableFromDurable(table durable.Table) *Table {
}

func NewEmptyTable(ctx context.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema) (*Table, error) {
rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
rows, err := durable.NewEmptyPrimaryIndex(ctx, vrw, ns, sch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/dtestutils/testcommands/multienv.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func createTestTable(dEnv *env.DoltEnv, tableName string, sch schema.Schema) err
vrw := dEnv.DoltDB.ValueReadWriter()
ns := dEnv.DoltDB.NodeStore()

idx, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
idx, err := durable.NewEmptyPrimaryIndex(ctx, vrw, ns, sch)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/merge/fulltext_rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ func purgeFulltextTableData(ctx *sql.Context, root doltdb.RootValue, tableNames
if err != nil {
return nil, err
}
rows, err := durable.NewEmptyIndex(ctx, tbl.ValueReadWriter(), tbl.NodeStore(), sch, false)
rows, err := durable.NewEmptyPrimaryIndex(ctx, tbl.ValueReadWriter(), tbl.NodeStore(), sch)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions go/libraries/doltcore/merge/violations_fk.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func GetForeignKeyViolations(ctx context.Context, newRoot, baseRoot doltdb.RootV
return err
}
// Parent does not exist in the ancestor so we use an empty map
emptyIdx, err := durable.NewEmptyIndex(ctx, postParent.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Schema, false)
emptyIdx, err := durable.NewEmptyPrimaryIndex(ctx, postParent.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Schema)
if err != nil {
return err
}
Expand All @@ -129,7 +129,7 @@ func GetForeignKeyViolations(ctx context.Context, newRoot, baseRoot doltdb.RootV
return err
}
// Child does not exist in the ancestor so we use an empty map
emptyIdx, err := durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Schema, false)
emptyIdx, err := durable.NewEmptyPrimaryIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Schema)
if err != nil {
return err
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func parentFkConstraintViolations(
}
var idx durable.Index
if empty {
idx, err = durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Schema, false)
idx, err = durable.NewEmptyIndexFromSchemaIndex(ctx, postChild.Table.ValueReadWriter(), postParent.Table.NodeStore(), postParent.Index)
if err != nil {
return err
}
Expand Down Expand Up @@ -405,7 +405,7 @@ func childFkConstraintViolations(
}
var idx durable.Index
if empty {
idx, err = durable.NewEmptyIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Schema, false)
idx, err = durable.NewEmptyIndexFromSchemaIndex(ctx, postChild.Table.ValueReadWriter(), postChild.Table.NodeStore(), postChild.Index)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/alterschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func makePeopleTable(ctx context.Context, dEnv *env.DoltEnv) (*env.DoltEnv, erro
if err != nil {
return nil, err
}
rows, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), sch, false)
rows, err := durable.NewEmptyPrimaryIndex(ctx, root.VRW(), root.NodeStore(), sch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func CreateTestTable(t *testing.T, dEnv *env.DoltEnv, tableName string, sch sche
vrw := dEnv.DoltDB.ValueReadWriter()
ns := dEnv.DoltDB.NodeStore()

rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
rows, err := durable.NewEmptyPrimaryIndex(ctx, vrw, ns, sch)
require.NoError(t, err)
tbl, err := doltdb.NewTable(ctx, vrw, ns, sch, rows, nil, nil)
require.NoError(t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ func (itr *prollyConflictRowIter) loadTableMaps(ctx *sql.Context, baseHash, thei

var idx durable.Index
if !ok {
idx, err = durable.NewEmptyIndex(ctx, itr.vrw, itr.ns, itr.ourSch, false)
idx, err = durable.NewEmptyPrimaryIndex(ctx, itr.vrw, itr.ns, itr.ourSch)
} else {
idx, err = baseTbl.GetRowData(ctx)
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/dtables/diff_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ func tableData(ctx *sql.Context, tbl *doltdb.Table, ddb *doltdb.DoltDB) (durable
var err error

if tbl == nil {
data, err = durable.NewEmptyIndex(ctx, ddb.ValueReadWriter(), ddb.NodeStore(), schema.EmptySchema, false)
data, err = durable.NewEmptyPrimaryIndex(ctx, ddb.ValueReadWriter(), ddb.NodeStore(), schema.EmptySchema)
if err != nil {
return nil, nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions go/libraries/doltcore/sqle/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ func emptyFulltextTable(
return nil, nil, err
}

empty, err := durable.NewEmptyIndex(ctx, dt.ValueReadWriter(), dt.NodeStore(), doltSchema, false)
empty, err := durable.NewEmptyPrimaryIndex(ctx, dt.ValueReadWriter(), dt.NodeStore(), doltSchema)
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -1041,7 +1041,7 @@ func (t *WritableDoltTable) truncate(
}

for _, idx := range sch.Indexes().AllIndexes() {
empty, err := durable.NewEmptyIndex(ctx, table.ValueReadWriter(), table.NodeStore(), idx.Schema(), false)
empty, err := durable.NewEmptyIndexFromSchemaIndex(ctx, table.ValueReadWriter(), table.NodeStore(), idx)
if err != nil {
return nil, err
}
Expand All @@ -1065,7 +1065,7 @@ func (t *WritableDoltTable) truncate(
}
}

empty, err := durable.NewEmptyIndex(ctx, table.ValueReadWriter(), table.NodeStore(), sch, false)
empty, err := durable.NewEmptyPrimaryIndex(ctx, table.ValueReadWriter(), table.NodeStore(), sch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/temp_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func NewTempTable(
vrw := ddb.ValueReadWriter()
ns := ddb.NodeStore()

idx, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
idx, err := durable.NewEmptyPrimaryIndex(ctx, vrw, ns, sch)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func CreateEmptyTestTable(dEnv *env.DoltEnv, tableName string, sch schema.Schema
vrw := dEnv.DoltDB.ValueReadWriter()
ns := dEnv.DoltDB.NodeStore()

rows, err := durable.NewEmptyIndex(ctx, vrw, ns, sch, false)
rows, err := durable.NewEmptyPrimaryIndex(ctx, vrw, ns, sch)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
// single prolly tree materialization by presorting the index keys in an
// intermediate file format.
func BuildProllyIndexExternal(ctx *sql.Context, vrw types.ValueReadWriter, ns tree.NodeStore, sch schema.Schema, tableName string, idx schema.Index, primary prolly.Map, uniqCb DupEntryCb) (durable.Index, error) {
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, idx.Schema(), schema.IsKeyless(sch))
empty, err := durable.NewEmptyIndexFromSchemaIndex(ctx, vrw, ns, idx.Schema())
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion go/libraries/doltcore/table/editor/creation/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func BuildUniqueProllyIndex(
primary prolly.Map,
cb DupEntryCb,
) (durable.Index, error) {
empty, err := durable.NewEmptyIndex(ctx, vrw, ns, idx.Schema(), schema.IsKeyless(sch))
empty, err := durable.NewEmptyIndexFromSchemaIndex(ctx, vrw, ns, idx)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func TestEndToEnd(t *testing.T) {
root, err := dEnv.WorkingRoot(ctx)
require.NoError(t, err)

empty, err := durable.NewEmptyIndex(ctx, root.VRW(), root.NodeStore(), tt.sch, false)
empty, err := durable.NewEmptyPrimaryIndex(ctx, root.VRW(), root.NodeStore(), tt.sch)
require.NoError(t, err)

indexes, err := durable.NewIndexSet(ctx, root.VRW(), root.NodeStore())
Expand Down

0 comments on commit f375111

Please sign in to comment.