From 314cf3461d102baa9aa8036d3a800aab0bd6d044 Mon Sep 17 00:00:00 2001 From: Travis Date: Fri, 12 Mar 2021 16:32:32 -0600 Subject: [PATCH] Cache BitDepth on bsiGroup during index.Open. Before this change, we were only caching the BitDepth on the field.options. This was ok as long as applyOptions() was called after that. But unfortunately, during startup, applyOptions() was called prior to that being set. So with this commit, we explicitly set the value in bsiGroup.BitDepth as well. --- field.go | 17 +++++++++++++++++ field_internal_test.go | 20 ++++++++++++++++++++ index.go | 6 +++--- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/field.go b/field.go index a0243c458..b26144289 100644 --- a/field.go +++ b/field.go @@ -730,6 +730,23 @@ func (f *Field) bitDepth() (uint64, error) { return maxBitDepth, nil } +// cacheBitDepth is used by Index.setFieldBitDepths() to updated the in-memory +// bitDepth values for each field and its bsiGroup. +func (f *Field) cacheBitDepth(bd uint64) error { + // Get the assocated bsiGroup so that its bitDepth can be updated as well. + bsig := f.bsiGroup(f.name) + + f.mu.Lock() + defer f.mu.Unlock() + + f.options.BitDepth = bd + if bsig != nil { + bsig.BitDepth = bd + } + + return nil +} + // openViews opens and initializes the views inside the field. func (f *Field) openViews() error { view2shards := f.idx.fieldView2shard.getViewsForField(f.name) diff --git a/field_internal_test.go b/field_internal_test.go index 3705b321d..c154151fc 100644 --- a/field_internal_test.go +++ b/field_internal_test.go @@ -950,6 +950,16 @@ func TestField_SaveMeta(t *testing.T) { t.Fatalf("expected BitDepth after set to be: %d, got: %d", expBitDepth, f.options.BitDepth) } + tx2 := f.idx.holder.txf.NewTx(Txo{Index: f.idx, Field: f.Field, Shard: 0}) + defer tx2.Rollback() + if rslt, ok, err := f.Value(tx2, colID); err != nil { + t.Fatal(err) + } else if !ok { + t.Fatal("expected Value() to return exists = true") + } else if rslt != val { + t.Fatalf("expected value to be: %d, got: %d", val, rslt) + } + // Reload field and verify that it is persisted. if err := f.Reopen(); err != nil { t.Fatal(err) @@ -958,4 +968,14 @@ func TestField_SaveMeta(t *testing.T) { if f.options.BitDepth != expBitDepth { t.Fatalf("expected BitDepth after reopen to be: %d, got: %d", expBitDepth, f.options.BitDepth) } + + tx3 := f.idx.holder.txf.NewTx(Txo{Index: f.idx, Field: f.Field, Shard: 0}) + defer tx3.Rollback() + if rslt, ok, err := f.Value(tx3, colID); err != nil { + t.Fatal(err) + } else if !ok { + t.Fatal("expected Value() after reopen to return exists = true") + } else if rslt != val { + t.Fatalf("expected value after reopen to be: %d, got: %d", val, rslt) + } } diff --git a/index.go b/index.go index 6bec27db8..d83e1db53 100644 --- a/index.go +++ b/index.go @@ -438,9 +438,9 @@ func (i *Index) setFieldBitDepths() error { if err != nil { return errors.Wrapf(err, "getting bit depth for field: %s", name) } - f.mu.Lock() - f.options.BitDepth = bd - f.mu.Unlock() + if err := f.cacheBitDepth(bd); err != nil { + return errors.Wrapf(err, "caching field bitDepth: %d", bd) + } } return nil }