From 766e942d8129600b417cbf163cffc1928b81cd16 Mon Sep 17 00:00:00 2001 From: Seebs Date: Mon, 19 Sep 2022 15:22:10 -0500 Subject: [PATCH] continue removing Tx parameters to view-type functions A few view functions were taking a Tx, which had to be shard-specific, but that's sort of awkward -- the view is inherently not shard-specific, so it should be handling sharding internally. There were also a couple of remaining obsolete checks for whether a Tx was nil, at least two of which were in contexts where it absolutely can't be. Remove all of them, and also the function itself. --- field.go | 61 ++++++++++++---------------------------------------- fragment.go | 13 +---------- txfactory.go | 3 --- util.go | 14 ------------ view.go | 20 ++++++++++++----- 5 files changed, 30 insertions(+), 81 deletions(-) diff --git a/field.go b/field.go index d87f0f3e1..b0e23b76c 100644 --- a/field.go +++ b/field.go @@ -271,7 +271,9 @@ func OptFieldTypeTimestamp(epoch time.Time, timeUnit string) FieldOption { // scale = -2: // min : [-922337203685477580800, -100] // GAPs: [-99, -1], [-199, -101] ... [-922337203685477580799, -922337203685477580701] -// 0 +// +// 0 +// // max : [100, 922337203685477580700] // GAPs: [1, 99], [101, 199] ... [922337203685477580601, 922337203685477580699] // @@ -282,7 +284,6 @@ func OptFieldTypeTimestamp(epoch time.Time, timeUnit string) FieldOption { // // min : [-922337203685477580800, -922337203685477580800+(2^64)] // max : [922337203685477580700-(2^64), 922337203685477580700] -// func OptFieldTypeDecimal(scale int64, minmax ...pql.Decimal) FieldOption { return func(fo *FieldOptions) error { if fo.Type != "" { @@ -1258,8 +1259,6 @@ func (f *Field) MutexCheck(ctx context.Context, qcx *Qcx, details bool, limit in // SetBit sets a bit on a view within the field. func (f *Field) SetBit(qcx *Qcx, rowID, colID uint64, t *time.Time) (changed bool, err error) { - tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: f.idx, Shard: colID / ShardWidth}) - defer finisher(&err) viewName := viewStandard if !f.options.NoStandardView { // Retrieve view. Exit if it doesn't exist. @@ -1269,7 +1268,7 @@ func (f *Field) SetBit(qcx *Qcx, rowID, colID uint64, t *time.Time) (changed boo } // Set non-time bit. - if v, err := view.setBit(tx, rowID, colID); err != nil { + if v, err := view.setBit(qcx, rowID, colID); err != nil { return changed, errors.Wrap(err, "setting on view") } else if v { changed = v @@ -1288,7 +1287,7 @@ func (f *Field) SetBit(qcx *Qcx, rowID, colID uint64, t *time.Time) (changed boo return changed, errors.Wrapf(err, "creating view %s", subname) } - if c, err := view.setBit(tx, rowID, colID); err != nil { + if c, err := view.setBit(qcx, rowID, colID); err != nil { return changed, errors.Wrapf(err, "setting on view %s", subname) } else if c { changed = true @@ -1300,8 +1299,6 @@ func (f *Field) SetBit(qcx *Qcx, rowID, colID uint64, t *time.Time) (changed boo // ClearBit clears a bit within the field. func (f *Field) ClearBit(qcx *Qcx, rowID, colID uint64) (changed bool, err error) { - tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: f.idx, Shard: colID / ShardWidth}) - defer finisher(&err) viewName := viewStandard // Retrieve view. Exit if it doesn't exist. @@ -1311,7 +1308,7 @@ func (f *Field) ClearBit(qcx *Qcx, rowID, colID uint64) (changed bool, err error } // Clear non-time bit. - if v, err := view.clearBit(tx, rowID, colID); err != nil { + if v, err := view.clearBit(qcx, rowID, colID); err != nil { return false, errors.Wrap(err, "clearing on view") } else if v { changed = changed || v @@ -1329,7 +1326,7 @@ func (f *Field) ClearBit(qcx *Qcx, rowID, colID uint64) (changed bool, err error level-- } if level < skipAbove { - cleared, err := view.clearBit(tx, rowID, colID) + cleared, err := view.clearBit(qcx, rowID, colID) changed = changed || cleared if err != nil { return changed, errors.Wrapf(err, "clearing on view %s", view.name) @@ -1428,8 +1425,6 @@ func (f *Field) StringValue(qcx *Qcx, columnID uint64) (value string, exists boo // Value reads a field value for a column. func (f *Field) Value(qcx *Qcx, columnID uint64) (value int64, exists bool, err error) { - tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: f.idx, Shard: columnID / ShardWidth}) - defer finisher(&err) bsig := f.bsiGroup(f.name) if bsig == nil { return 0, false, ErrBSIGroupNotFound @@ -1441,7 +1436,7 @@ func (f *Field) Value(qcx *Qcx, columnID uint64) (value int64, exists bool, err return 0, false, nil } - v, exists, err := view.value(tx, columnID, bsig.BitDepth) + v, exists, err := view.value(qcx, columnID, bsig.BitDepth) if err != nil { return 0, false, err } else if !exists { @@ -1452,8 +1447,6 @@ func (f *Field) Value(qcx *Qcx, columnID uint64) (value int64, exists bool, err // SetValue sets a field value for a column. func (f *Field) SetValue(qcx *Qcx, columnID uint64, value int64) (changed bool, err error) { - tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: f.idx, Shard: columnID / ShardWidth}) - defer finisher(&err) // Fetch bsiGroup & validate min/max. bsig := f.bsiGroup(f.name) if bsig == nil { @@ -1501,13 +1494,11 @@ func (f *Field) SetValue(qcx *Qcx, columnID uint64, value int64) (changed bool, } view.holder.addIndex(view.idx) - return view.setValue(tx, columnID, bsig.BitDepth, baseValue) + return view.setValue(qcx, columnID, bsig.BitDepth, baseValue) } // ClearValue removes a field value for a column. func (f *Field) ClearValue(qcx *Qcx, columnID uint64) (changed bool, err error) { - tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: f.idx, Shard: columnID / ShardWidth}) - defer finisher(&err) bsig := f.bsiGroup(f.name) if bsig == nil { return false, ErrBSIGroupNotFound @@ -1517,12 +1508,12 @@ func (f *Field) ClearValue(qcx *Qcx, columnID uint64) (changed bool, err error) if view == nil { return false, nil } - value, exists, err := view.value(tx, columnID, bsig.BitDepth) + value, exists, err := view.value(qcx, columnID, bsig.BitDepth) if err != nil { return false, err } if exists { - return view.clearValue(tx, columnID, bsig.BitDepth, value) + return view.clearValue(qcx, columnID, bsig.BitDepth, value) } return false, nil } @@ -1545,15 +1536,7 @@ func (f *Field) MaxForShard(qcx *Qcx, shard uint64, filter *Row) (ValCount, erro return ValCount{}, nil } - var localTx Tx - if NilInside(tx) { - localTx = f.holder.txf.NewTx(Txo{Write: !writable, Index: f.idx, Fragment: fragment, Shard: fragment.shard}) - defer localTx.Rollback() - } else { - localTx = tx - } - - max, cnt, err := fragment.max(localTx, filter, bsig.BitDepth) + max, cnt, err := fragment.max(tx, filter, bsig.BitDepth) if err != nil { return ValCount{}, errors.Wrap(err, "calling fragment.max") } @@ -1583,15 +1566,7 @@ func (f *Field) MinForShard(qcx *Qcx, shard uint64, filter *Row) (ValCount, erro return ValCount{}, nil } - var localTx Tx - if NilInside(tx) { - localTx = f.idx.holder.txf.NewTx(Txo{Write: !writable, Index: f.idx, Fragment: fragment, Shard: fragment.shard}) - defer localTx.Rollback() - } else { - localTx = tx - } - - min, cnt, err := fragment.min(localTx, filter, bsig.BitDepth) + min, cnt, err := fragment.min(tx, filter, bsig.BitDepth) if err != nil { return ValCount{}, errors.Wrap(err, "calling fragment.min") } @@ -2426,13 +2401,5 @@ func (f *Field) SortShardRow(tx Tx, shard uint64, filter *Row, sort_desc bool) ( return nil, errors.New("fragment is nil") } - var localTx Tx - if NilInside(tx) { - localTx = f.holder.txf.NewTx(Txo{Write: !writable, Index: f.idx, Fragment: fragment, Shard: fragment.shard}) - defer localTx.Rollback() - } else { - localTx = tx - } - - return fragment.sortBsiData(localTx, filter, bsig.BitDepth, sort_desc) + return fragment.sortBsiData(tx, filter, bsig.BitDepth, sort_desc) } diff --git a/fragment.go b/fragment.go index be650ce61..920e42ca7 100644 --- a/fragment.go +++ b/fragment.go @@ -693,20 +693,9 @@ func (f *fragment) positionsForValue(columnID uint64, bitDepth uint64, value int } // TODO get rid of this and use positionsForValue to generate a single write op, and set that with importPositions. -func (f *fragment) setValueBase(txOrig Tx, columnID uint64, bitDepth uint64, value int64, clear bool) (changed bool, err error) { +func (f *fragment) setValueBase(tx Tx, columnID uint64, bitDepth uint64, value int64, clear bool) (changed bool, err error) { f.mu.Lock() defer f.mu.Unlock() - tx := txOrig - if NilInside(tx) { - tx = f.idx.holder.txf.NewTx(Txo{Write: writable, Index: f.idx, Fragment: f, Shard: f.shard}) - defer func() { - if err == nil { - vprint.PanicOn(tx.Commit()) - } else { - tx.Rollback() - } - }() - } err = func() error { // Convert value to an unsigned representation. diff --git a/txfactory.go b/txfactory.go index 1aee2bec8..00ee963c5 100644 --- a/txfactory.go +++ b/txfactory.go @@ -571,9 +571,6 @@ func (g *TxGroup) AddTx(tx Tx, o Txo) { if g.finished { vprint.PanicOn("in TxGroup.Finish(): TxGroup already finished") } - if NilInside(tx) { - vprint.PanicOn("Cannot add nil Tx to TxGroup") - } g.reads = append(g.reads, tx) diff --git a/util.go b/util.go index 38463a54e..857cc406f 100644 --- a/util.go +++ b/util.go @@ -8,7 +8,6 @@ import ( "os" "os/user" "path/filepath" - "reflect" "strings" "time" @@ -26,19 +25,6 @@ import ( // with an offset of 0. const LeftShifted16MaxContainerKey = uint64(0xffffffffffff0000) // or math.MaxUint64 - (1<<16 - 1), or 18446744073709486080 -// NilInside checks if the provided iface is nil or -// contains a nil pointer, slice, array, map, or channel. -func NilInside(iface interface{}) bool { - if iface == nil { - return true - } - switch reflect.TypeOf(iface).Kind() { - case reflect.Ptr, reflect.Slice, reflect.Array, reflect.Map, reflect.Chan: - return reflect.ValueOf(iface).IsNil() - } - return false -} - ////////////////////////////////// // helper utility functions diff --git a/view.go b/view.go index edc6acd33..5c1b08daf 100644 --- a/view.go +++ b/view.go @@ -525,8 +525,10 @@ func (v *view) mutexCheck(ctx context.Context, qcx *Qcx, details bool, limit int } // setBit sets a bit within the view. -func (v *view) setBit(tx Tx, rowID, columnID uint64) (changed bool, err error) { +func (v *view) setBit(qcx *Qcx, rowID, columnID uint64) (changed bool, err error) { shard := columnID / ShardWidth + tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: v.idx, Shard: shard}) + defer finisher(&err) var frag *fragment frag, err = v.CreateFragmentIfNotExists(shard) if err != nil { @@ -537,8 +539,10 @@ func (v *view) setBit(tx Tx, rowID, columnID uint64) (changed bool, err error) { } // clearBit clears a bit within the view. -func (v *view) clearBit(tx Tx, rowID, columnID uint64) (changed bool, err error) { +func (v *view) clearBit(qcx *Qcx, rowID, columnID uint64) (changed bool, err error) { shard := columnID / ShardWidth + tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: v.idx, Shard: shard}) + defer finisher(&err) frag := v.Fragment(shard) if frag == nil { return false, nil @@ -548,8 +552,10 @@ func (v *view) clearBit(tx Tx, rowID, columnID uint64) (changed bool, err error) } // value uses a column of bits to read a multi-bit value. -func (v *view) value(tx Tx, columnID uint64, bitDepth uint64) (value int64, exists bool, err error) { +func (v *view) value(qcx *Qcx, columnID uint64, bitDepth uint64) (value int64, exists bool, err error) { shard := columnID / ShardWidth + tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: v.idx, Shard: shard}) + defer finisher(&err) frag, err := v.CreateFragmentIfNotExists(shard) if err != nil { return value, exists, err @@ -559,8 +565,10 @@ func (v *view) value(tx Tx, columnID uint64, bitDepth uint64) (value int64, exis } // setValue uses a column of bits to set a multi-bit value. -func (v *view) setValue(tx Tx, columnID uint64, bitDepth uint64, value int64) (changed bool, err error) { +func (v *view) setValue(qcx *Qcx, columnID uint64, bitDepth uint64, value int64) (changed bool, err error) { shard := columnID / ShardWidth + tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: v.idx, Shard: shard}) + defer finisher(&err) frag, err := v.CreateFragmentIfNotExists(shard) if err != nil { return changed, err @@ -570,8 +578,10 @@ func (v *view) setValue(tx Tx, columnID uint64, bitDepth uint64, value int64) (c } // clearValue removes a specific value assigned to columnID -func (v *view) clearValue(tx Tx, columnID uint64, bitDepth uint64, value int64) (changed bool, err error) { +func (v *view) clearValue(qcx *Qcx, columnID uint64, bitDepth uint64, value int64) (changed bool, err error) { shard := columnID / ShardWidth + tx, finisher, err := qcx.GetTx(Txo{Write: true, Index: v.idx, Shard: shard}) + defer finisher(&err) frag := v.Fragment(shard) if frag == nil { return false, nil