From 4cc6f21b89760a70f376f2692eb8d02cd470cc6b Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 16:20:11 +0200 Subject: [PATCH 01/26] Append "_test" in package name --- kv/bbolt/bolt_test.go | 5 ++- kv/bolt/bolt_test.go | 5 ++- kv/flat/badger/badger_test.go | 5 ++- kv/flat/btree/btree_test.go | 7 +-- kv/flat/leveldb/leveldb_test.go | 5 ++- kv/flat/pebble/pebble_test.go | 5 ++- kv/flat/upgrade_test.go | 5 ++- kv/kv_test.go | 71 ++++++++++++++++--------------- legacy/nosql/value_test.go | 23 +++++----- tuple/datastore/datastore_test.go | 6 ++- tuple/tuplepb/tuplepb_test.go | 7 +-- 11 files changed, 78 insertions(+), 66 deletions(-) diff --git a/kv/bbolt/bolt_test.go b/kv/bbolt/bolt_test.go index ecc2f7b..a4ec8a2 100644 --- a/kv/bbolt/bolt_test.go +++ b/kv/bbolt/bolt_test.go @@ -1,16 +1,17 @@ -package bbolt +package bbolt_test import ( "path/filepath" "testing" "github.com/hidal-go/hidalgo/kv" + "github.com/hidal-go/hidalgo/kv/bbolt" "github.com/hidal-go/hidalgo/kv/kvtest" ) func TestBBolt(t *testing.T) { kvtest.RunTestLocal(t, func(path string) (kv.KV, error) { path = filepath.Join(path, "bbolt.db") - return OpenPath(path) + return bbolt.OpenPath(path) }, nil) } diff --git a/kv/bolt/bolt_test.go b/kv/bolt/bolt_test.go index 1178479..1b33919 100644 --- a/kv/bolt/bolt_test.go +++ b/kv/bolt/bolt_test.go @@ -1,16 +1,17 @@ -package bolt +package bolt_test import ( "path/filepath" "testing" "github.com/hidal-go/hidalgo/kv" + "github.com/hidal-go/hidalgo/kv/bolt" "github.com/hidal-go/hidalgo/kv/kvtest" ) func TestBolt(t *testing.T) { kvtest.RunTestLocal(t, func(path string) (kv.KV, error) { path = filepath.Join(path, "bolt.db") - return OpenPath(path) + return bolt.OpenPath(path) }, nil) } diff --git a/kv/flat/badger/badger_test.go b/kv/flat/badger/badger_test.go index e32b1f9..41124f2 100644 --- a/kv/flat/badger/badger_test.go +++ b/kv/flat/badger/badger_test.go @@ -1,12 +1,13 @@ -package badger +package badger_test import ( "testing" "github.com/hidal-go/hidalgo/kv/flat" + "github.com/hidal-go/hidalgo/kv/flat/badger" "github.com/hidal-go/hidalgo/kv/kvtest" ) func TestBadger(t *testing.T) { - kvtest.RunTestLocal(t, flat.UpgradeOpenPath(OpenPath), nil) + kvtest.RunTestLocal(t, flat.UpgradeOpenPath(badger.OpenPath), nil) } diff --git a/kv/flat/btree/btree_test.go b/kv/flat/btree/btree_test.go index 6de5d55..da0a39a 100644 --- a/kv/flat/btree/btree_test.go +++ b/kv/flat/btree/btree_test.go @@ -1,15 +1,16 @@ -package btree +package btree_test import ( "testing" "github.com/hidal-go/hidalgo/kv" "github.com/hidal-go/hidalgo/kv/flat" + "github.com/hidal-go/hidalgo/kv/flat/btree" "github.com/hidal-go/hidalgo/kv/kvtest" ) func TestBtree(t *testing.T) { - kvtest.RunTest(t, func(t testing.TB) kv.KV { - return flat.Upgrade(New()) + kvtest.RunTest(t, func(tb testing.TB) kv.KV { + return flat.Upgrade(btree.New()) }, nil) } diff --git a/kv/flat/leveldb/leveldb_test.go b/kv/flat/leveldb/leveldb_test.go index a48976f..47af8e9 100644 --- a/kv/flat/leveldb/leveldb_test.go +++ b/kv/flat/leveldb/leveldb_test.go @@ -1,12 +1,13 @@ -package leveldb +package leveldb_test import ( "testing" "github.com/hidal-go/hidalgo/kv/flat" + "github.com/hidal-go/hidalgo/kv/flat/leveldb" "github.com/hidal-go/hidalgo/kv/kvtest" ) func TestLeveldb(t *testing.T) { - kvtest.RunTestLocal(t, flat.UpgradeOpenPath(OpenPath), nil) + kvtest.RunTestLocal(t, flat.UpgradeOpenPath(leveldb.OpenPath), nil) } diff --git a/kv/flat/pebble/pebble_test.go b/kv/flat/pebble/pebble_test.go index bcfab69..d317729 100644 --- a/kv/flat/pebble/pebble_test.go +++ b/kv/flat/pebble/pebble_test.go @@ -1,14 +1,15 @@ -package pebble +package pebble_test import ( "testing" "github.com/hidal-go/hidalgo/kv/flat" + "github.com/hidal-go/hidalgo/kv/flat/pebble" "github.com/hidal-go/hidalgo/kv/kvtest" ) func TestPebble(t *testing.T) { - kvtest.RunTestLocal(t, flat.UpgradeOpenPath(OpenPath), &kvtest.Options{ + kvtest.RunTestLocal(t, flat.UpgradeOpenPath(pebble.OpenPath), &kvtest.Options{ NoTx: true, }) } diff --git a/kv/flat/upgrade_test.go b/kv/flat/upgrade_test.go index 4d661ad..e847eee 100644 --- a/kv/flat/upgrade_test.go +++ b/kv/flat/upgrade_test.go @@ -1,4 +1,4 @@ -package flat +package flat_test import ( "testing" @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" "github.com/hidal-go/hidalgo/kv" + "github.com/hidal-go/hidalgo/kv/flat" ) func TestSepEscape(t *testing.T) { @@ -13,6 +14,6 @@ func TestSepEscape(t *testing.T) { []byte(`\/aa/b\b/c/d/\`), []byte(`/aa/b\b/c/d/`), } - k2 := KeyUnescape(KeyEscape(k)) + k2 := flat.KeyUnescape(flat.KeyEscape(k)) require.Equal(t, k, k2) } diff --git a/kv/kv_test.go b/kv/kv_test.go index 1ef4c67..d283965 100644 --- a/kv/kv_test.go +++ b/kv/kv_test.go @@ -1,43 +1,44 @@ -package kv +package kv_test import ( "testing" + "github.com/hidal-go/hidalgo/kv" "github.com/stretchr/testify/require" ) var keyCompareCases = []struct { - k1, k2 Key + k1, k2 kv.Key exp int }{ { - k1: SKey("a"), - k2: SKey("a"), + k1: kv.SKey("a"), + k2: kv.SKey("a"), exp: 0, }, { - k1: SKey("a"), - k2: SKey("b"), + k1: kv.SKey("a"), + k2: kv.SKey("b"), exp: -1, }, { - k1: SKey("a"), - k2: SKey("a", "b"), + k1: kv.SKey("a"), + k2: kv.SKey("a", "b"), exp: -1, }, { - k1: SKey("a", "b"), - k2: SKey("a", "a"), + k1: kv.SKey("a", "b"), + k2: kv.SKey("a", "a"), exp: +1, }, { k1: nil, - k2: SKey("a", "b"), + k2: kv.SKey("a", "b"), exp: -1, }, { - k1: SKey("ab"), - k2: SKey("a", "b"), + k1: kv.SKey("ab"), + k2: kv.SKey("a", "b"), exp: +1, }, } @@ -55,52 +56,52 @@ func TestKeyCompare(t *testing.T) { } var keyHasPrefixCases = []struct { - key, pref Key + key, pref kv.Key exp bool }{ { - key: SKey("a"), - pref: SKey("a"), + key: kv.SKey("a"), + pref: kv.SKey("a"), exp: true, }, { - key: SKey("a"), - pref: SKey("b"), + key: kv.SKey("a"), + pref: kv.SKey("b"), exp: false, }, { - key: SKey("a"), - pref: SKey("a", "b"), + key: kv.SKey("a"), + pref: kv.SKey("a", "b"), exp: false, }, { - key: SKey("a", "b"), - pref: SKey("a"), + key: kv.SKey("a", "b"), + pref: kv.SKey("a"), exp: true, }, { - key: SKey("a", "b"), - pref: SKey("a", "a"), + key: kv.SKey("a", "b"), + pref: kv.SKey("a", "a"), exp: false, }, { key: nil, - pref: SKey("a", "b"), + pref: kv.SKey("a", "b"), exp: false, }, { - key: SKey("a", "b"), + key: kv.SKey("a", "b"), pref: nil, exp: true, }, { - key: SKey("ab"), - pref: SKey("a", "b"), + key: kv.SKey("ab"), + pref: kv.SKey("a", "b"), exp: false, }, { - key: SKey("a", "b"), - pref: SKey("ab"), + key: kv.SKey("a", "b"), + pref: kv.SKey("ab"), exp: false, }, } @@ -115,10 +116,10 @@ func TestKeyHasPrefix(t *testing.T) { } func TestKeyAppend(t *testing.T) { - k := SKey("a", "b", "c") - k = k.Append(SKey("d")) - require.Equal(t, SKey("a", "b", "c", "d"), k) - k = SKey("a", "b", "c") + k := kv.SKey("a", "b", "c") + k = k.Append(kv.SKey("d")) + require.Equal(t, kv.SKey("a", "b", "c", "d"), k) + k = kv.SKey("a", "b", "c") k = k.AppendBytes([]byte("d")) - require.Equal(t, SKey("a", "b", "c", "d"), k) + require.Equal(t, kv.SKey("a", "b", "c", "d"), k) } diff --git a/legacy/nosql/value_test.go b/legacy/nosql/value_test.go index 0878f69..5b6dac8 100644 --- a/legacy/nosql/value_test.go +++ b/legacy/nosql/value_test.go @@ -1,34 +1,35 @@ -package nosql +package nosql_test import ( "testing" + "github.com/hidal-go/hidalgo/legacy/nosql" "github.com/stretchr/testify/require" ) var filterMatch = []struct { - f FieldFilter - d Document + d nosql.Document + f nosql.FieldFilter exp bool }{ { - f: FieldFilter{Path: []string{"value", "str"}, Filter: GT, Value: String("f")}, - d: Document{"value": Document{"str": String("bob")}}, + f: nosql.FieldFilter{Path: []string{"value", "str"}, Filter: nosql.GT, Value: nosql.String("f")}, + d: nosql.Document{"value": nosql.Document{"str": nosql.String("bob")}}, exp: false, }, { - f: FieldFilter{Path: []string{"value", "str"}, Filter: Equal, Value: String("f")}, - d: Document{"value": Document{"str": String("bob")}}, + f: nosql.FieldFilter{Path: []string{"value", "str"}, Filter: nosql.Equal, Value: nosql.String("f")}, + d: nosql.Document{"value": nosql.Document{"str": nosql.String("bob")}}, exp: false, }, { - f: FieldFilter{Path: []string{"value", "str"}, Filter: Equal, Value: String("bob")}, - d: Document{"value": Document{"str": String("bob")}}, + f: nosql.FieldFilter{Path: []string{"value", "str"}, Filter: nosql.Equal, Value: nosql.String("bob")}, + d: nosql.Document{"value": nosql.Document{"str": nosql.String("bob")}}, exp: true, }, { - f: FieldFilter{Path: []string{"value", "str"}, Filter: NotEqual, Value: String("bob")}, - d: Document{"value1": Document{"str": String("bob")}}, + f: nosql.FieldFilter{Path: []string{"value", "str"}, Filter: nosql.NotEqual, Value: nosql.String("bob")}, + d: nosql.Document{"value1": nosql.Document{"str": nosql.String("bob")}}, exp: true, }, } diff --git a/tuple/datastore/datastore_test.go b/tuple/datastore/datastore_test.go index 23509af..dee5707 100644 --- a/tuple/datastore/datastore_test.go +++ b/tuple/datastore/datastore_test.go @@ -1,4 +1,4 @@ -package datastore +package datastore_test import ( "context" @@ -9,6 +9,7 @@ import ( "github.com/ory/dockertest" "github.com/hidal-go/hidalgo/tuple" + ds "github.com/hidal-go/hidalgo/tuple/datastore" "github.com/hidal-go/hidalgo/tuple/tupletest" ) @@ -58,6 +59,7 @@ func TestDatastore(t *testing.T) { t.Cleanup(func() { _ = cli.Close() }) - return OpenClient(cli) + + return ds.OpenClient(cli) }, nil) } diff --git a/tuple/tuplepb/tuplepb_test.go b/tuple/tuplepb/tuplepb_test.go index 5c20536..1bb9aa7 100644 --- a/tuple/tuplepb/tuplepb_test.go +++ b/tuple/tuplepb/tuplepb_test.go @@ -1,4 +1,4 @@ -package tuplepb +package tuplepb_test import ( "testing" @@ -6,6 +6,7 @@ import ( "github.com/stretchr/testify/require" "github.com/hidal-go/hidalgo/tuple" + "github.com/hidal-go/hidalgo/tuple/tuplepb" "github.com/hidal-go/hidalgo/values" ) @@ -23,10 +24,10 @@ func TestTableEncoding(t *testing.T) { }, } - data, err := MarshalTable(tbl) + data, err := tuplepb.MarshalTable(tbl) require.NoError(t, err) - tbl2, err := UnmarshalTable(data) + tbl2, err := tuplepb.UnmarshalTable(data) require.NoError(t, err) require.Equal(t, tbl, tbl2) } From 3432fd146da7291791de172e82f555bfbd192f27 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 16:41:39 +0200 Subject: [PATCH 02/26] Rearrange struct fields to reduce memory footprint --- kv/flat/badger/badger.go | 2 +- kv/flat/btree/keys.go | 16 +++++++------- kv/flat/pebble/pebble.go | 2 +- kv/flat/registry.go | 2 +- kv/kvdebug/kvdebug.go | 4 ++-- kv/kvtest/kvtest.go | 2 +- kv/registry.go | 2 +- legacy/nosql/couch/ouch.go | 16 +++++++------- legacy/nosql/elastic/elastic.go | 33 ++++++++++++++--------------- legacy/nosql/mongo/mongo.go | 10 ++++----- legacy/nosql/nosql.go | 4 ++-- legacy/nosql/nosqltest/nosqltest.go | 10 ++++----- legacy/nosql/nosqltest/registry.go | 2 +- legacy/nosql/registry.go | 4 ++-- tuple/datastore/datastore.go | 11 +++++----- tuple/kv/downgrade.go | 4 ++-- tuple/sql/builder.go | 2 +- tuple/sql/dialect.go | 10 ++++----- tuple/sql/registry.go | 6 +++--- tuple/sql/sql.go | 10 ++++----- tuple/sql/sqltest/registry.go | 2 +- tuple/tupletest/tupletest.go | 2 +- values/values_test.go | 2 +- 23 files changed, 77 insertions(+), 81 deletions(-) diff --git a/kv/flat/badger/badger.go b/kv/flat/badger/badger.go index 7ca9b25..fe9e141 100644 --- a/kv/flat/badger/badger.go +++ b/kv/flat/badger/badger.go @@ -142,11 +142,11 @@ var ( ) type Iterator struct { + err error it *badger.Iterator pref flat.Key first bool valid bool - err error } func (it *Iterator) Reset() { diff --git a/kv/flat/btree/keys.go b/kv/flat/btree/keys.go index d5c4f75..97ab6e8 100644 --- a/kv/flat/btree/keys.go +++ b/kv/flat/btree/keys.go @@ -110,10 +110,10 @@ type ( Cmp func(a, b []byte) int d struct { // data page - c int - d [2*kd + 1]de n *d p *d + d [2*kd + 1]de + c int } de struct { // d element @@ -131,21 +131,21 @@ type ( // other words, io.EOF from an Enumaretor is "sticky" (idempotent). Enumerator struct { err error - hit bool - i int - k []byte q *d t *Tree + k []byte + i int ver int64 + hit bool } // Tree is a B+tree. Tree struct { - c int + r interface{} cmp Cmp first *d last *d - r interface{} + c int ver int64 } @@ -155,8 +155,8 @@ type ( } x struct { // index page - c int x [2*kx + 2]xe + c int } ) diff --git a/kv/flat/pebble/pebble.go b/kv/flat/pebble/pebble.go index 4e08e0a..7143372 100644 --- a/kv/flat/pebble/pebble.go +++ b/kv/flat/pebble/pebble.go @@ -136,10 +136,10 @@ var ( ) type Iterator struct { + err error it *pebble.Iterator pref flat.Key first bool - err error } func (it *Iterator) Reset() { diff --git a/kv/flat/registry.go b/kv/flat/registry.go index da04d93..ef84c01 100644 --- a/kv/flat/registry.go +++ b/kv/flat/registry.go @@ -12,8 +12,8 @@ type OpenPathFunc func(path string) (KV, error) // Registration is an information about the database driver. type Registration struct { - base.Registration OpenPath OpenPathFunc + base.Registration } var registry = make(map[string]Registration) diff --git a/kv/kvdebug/kvdebug.go b/kv/kvdebug/kvdebug.go index 3bbc870..dac8c4d 100644 --- a/kv/kvdebug/kvdebug.go +++ b/kv/kvdebug/kvdebug.go @@ -40,6 +40,8 @@ type Stats struct { } type KV struct { + KV kv.KV + stats Stats running struct { txRO int64 @@ -47,8 +49,6 @@ type KV struct { iter int64 } log bool - - KV kv.KV } func (d *KV) logging() bool { diff --git a/kv/kvtest/kvtest.go b/kv/kvtest/kvtest.go index f57bbc8..2ffa413 100644 --- a/kv/kvtest/kvtest.go +++ b/kv/kvtest/kvtest.go @@ -67,8 +67,8 @@ func RunTestLocal(t *testing.T, open kv.OpenPathFunc, opts *Options) { } var testList = []struct { + test func(tb testing.TB, db kv.KV) name string - test func(t testing.TB, db kv.KV) concurrent bool // requires concurrent safety txOnly bool // requires transactions }{ diff --git a/kv/registry.go b/kv/registry.go index 1eb62ea..cd8c45e 100644 --- a/kv/registry.go +++ b/kv/registry.go @@ -11,8 +11,8 @@ type OpenPathFunc func(path string) (KV, error) // Registration is an information about the database driver. type Registration struct { - base.Registration OpenPath OpenPathFunc + base.Registration } var registry = make(map[string]Registration) diff --git a/legacy/nosql/couch/ouch.go b/legacy/nosql/couch/ouch.go index bda9add..b48f7cb 100644 --- a/legacy/nosql/couch/ouch.go +++ b/legacy/nosql/couch/ouch.go @@ -63,8 +63,8 @@ func Dial(create bool, driver, addr, ns string, opt nosql.Options) (*DB, error) } type collection struct { - primary nosql.Index secondary []nosql.Index + primary nosql.Index } type DB struct { @@ -259,9 +259,9 @@ func (q ouchQuery) putSelector(field string, v interface{}) { type Query struct { db *DB - col string qu ouchQuery pathFilters map[string][]nosql.FieldFilter + col string } func (q *Query) WithFields(filters ...nosql.FieldFilter) nosql.Query { @@ -383,13 +383,13 @@ func (q *Query) Iterate() nosql.DocIterator { } type Iterator struct { - db *DB - col string - qu ouchQuery + prevID interface{} err error + qu ouchQuery rows *kivik.Rows doc map[string]interface{} - prevID interface{} + db *DB + col string closed bool } @@ -555,11 +555,11 @@ func (d *Delete) Do(ctx context.Context) error { type Update struct { db *DB + update nosql.Document + inc map[string]int // increment the named numeric field by the int col string key nosql.Key - update nosql.Document upsert bool - inc map[string]int // increment the named numeric field by the int } func (u *Update) Inc(field string, dn int) nosql.Update { diff --git a/legacy/nosql/elastic/elastic.go b/legacy/nosql/elastic/elastic.go index 8023d80..b6e0007 100644 --- a/legacy/nosql/elastic/elastic.go +++ b/legacy/nosql/elastic/elastic.go @@ -88,19 +88,19 @@ func Dial(addr, index string, opt nosql.Options) (*DB, error) { type collection struct { typ string - compPK bool // compose PK from existing keys; if false, use id instead of target field - primary nosql.Index secondary []nosql.Index + primary nosql.Index + compPK bool // compose PK from existing keys; if false, use id instead of target field } type DB struct { - cli *elastic.Client - ind struct { - one bool // use one index for all types (<= v5) + cli *elastic.Client + colls map[string]collection + ind struct { pref string settings json.RawMessage + one bool // use one index for all types (<= v5) } - colls map[string]collection } func (db *DB) Close() error { @@ -508,14 +508,14 @@ func (q elasticQuery) Source() (interface{}, error) { type indexRef struct { cli *elastic.Client - ind string c *collection + ind string } type Query struct { indexRef - limit int64 qu elasticQuery + limit int64 } func (q *Query) WithFields(filters ...nosql.FieldFilter) nosql.Query { @@ -569,13 +569,12 @@ func (q *Query) Iterate() nosql.DocIterator { } type Iterator struct { + err error + qu *elastic.ScrollService + buf *elastic.SearchResult indexRef - qu *elastic.ScrollService - - buf *elastic.SearchResult - done bool i int - err error + done bool } func (it *Iterator) Next(ctx context.Context) bool { @@ -660,11 +659,11 @@ func (d *Delete) Do(ctx context.Context) error { } type Update struct { - indexRef - key nosql.Key - upsert map[string]interface{} inc map[string]int + + indexRef + key nosql.Key } func (u *Update) Inc(field string, dn int) nosql.Update { @@ -720,11 +719,11 @@ func (db *DB) BatchInsert(col string) nosql.DocWriter { const batchSize = 100 type inserter struct { + err error indexRef buf []elastic.BulkableRequest ikeys []nosql.Key keys []nosql.Key - err error } func (w *inserter) WriteDoc(ctx context.Context, key nosql.Key, d nosql.Document) error { diff --git a/legacy/nosql/mongo/mongo.go b/legacy/nosql/mongo/mongo.go index 8a44237..46a1c51 100644 --- a/legacy/nosql/mongo/mongo.go +++ b/legacy/nosql/mongo/mongo.go @@ -97,9 +97,9 @@ func Dial(addr, dbName string, opt nosql.Options) (*DB, error) { type collection struct { c *mongo.Collection - compPK bool // compose PK from existing keys; if false, use _id instead of target field - primary nosql.Index secondary []nosql.Index + primary nosql.Index + compPK bool // compose PK from existing keys; if false, use _id instead of target field } type DB struct { @@ -439,8 +439,8 @@ func mergeFilters(dst, src primitive.M) { type Query struct { c *collection - limit int query primitive.M + limit int } func (q *Query) WithFields(filters ...nosql.FieldFilter) nosql.Query { @@ -618,9 +618,9 @@ func (d *Delete) Do(ctx context.Context) error { type Update struct { col *collection - key nosql.Key upsert primitive.M update primitive.M + key nosql.Key } func (u *Update) Inc(field string, dn int) nosql.Update { @@ -673,11 +673,11 @@ func (db *DB) BatchInsert(col string) nosql.DocWriter { const batchSize = 100 type inserter struct { + err error col *collection buf []interface{} ikeys []nosql.Key keys []nosql.Key - err error } func (w *inserter) WriteDoc(ctx context.Context, key nosql.Key, d nosql.Document) error { diff --git a/legacy/nosql/nosql.go b/legacy/nosql/nosql.go index f8aef16..7403af1 100644 --- a/legacy/nosql/nosql.go +++ b/legacy/nosql/nosql.go @@ -114,9 +114,9 @@ const ( // FieldFilter represents a single field comparison operation. type FieldFilter struct { + Value Value // value that will be compared with field of the document Path []string // path is a path to specific field in the document Filter FilterOp // comparison operation - Value Value // value that will be compared with field of the document } func (f FieldFilter) Matches(doc Document) bool { @@ -241,9 +241,9 @@ func BatchInsert(db Database, col string) DocWriter { type seqInsert struct { db Database + err error col string keys []Key - err error } func (w *seqInsert) WriteDoc(ctx context.Context, key Key, d Document) error { diff --git a/legacy/nosql/nosqltest/nosqltest.go b/legacy/nosql/nosqltest/nosqltest.go index 87f8361..e4aa104 100644 --- a/legacy/nosql/nosqltest/nosqltest.go +++ b/legacy/nosql/nosqltest/nosqltest.go @@ -15,8 +15,8 @@ import ( ) type Database struct { + Run func(tb testing.TB) nosql.Database Traits nosql.Traits - Run func(t testing.TB) nosql.Database } func TestNoSQL(t *testing.T, gen Database) { @@ -56,9 +56,9 @@ func init() { } type keyType struct { + Gen func() nosql.Key Name string Fields []string - Gen func() nosql.Key } func (kt keyType) SetKey(d nosql.Document, k nosql.Key) { @@ -115,8 +115,8 @@ var keyTypes = []keyType{ } var testsNoSQLKey = []struct { - name string t func(t *testing.T, c tableConf) + name string }{ {name: "ensure", t: testEnsure}, {name: "insert", t: testInsert}, @@ -129,8 +129,8 @@ type tableConf struct { ctx context.Context db nosql.Database col string - tr nosql.Traits kt keyType + tr nosql.Traits } func (c tableConf) ensurePK(t testing.TB, secondary ...nosql.Index) { @@ -327,8 +327,8 @@ func testInsert(t *testing.T, c tableConf) { c.expectAll(t, nil) type insert struct { - Key nosql.Key Doc nosql.Document + Key nosql.Key } k1 := c.kt.Gen() diff --git a/legacy/nosql/nosqltest/registry.go b/legacy/nosql/nosqltest/registry.go index 1208673..137b4ab 100644 --- a/legacy/nosql/nosqltest/registry.go +++ b/legacy/nosql/nosqltest/registry.go @@ -8,8 +8,8 @@ import ( ) type Registration struct { - nosql.Registration Versions []Version + nosql.Registration } type Version struct { diff --git a/legacy/nosql/registry.go b/legacy/nosql/registry.go index 0ddc784..66d190a 100644 --- a/legacy/nosql/registry.go +++ b/legacy/nosql/registry.go @@ -11,9 +11,9 @@ type OpenFunc func(addr, ns string, opt Options) (Database, error) // Registration is an information about the database driver. type Registration struct { - base.Registration New, Open OpenFunc - Traits Traits + base.Registration + Traits Traits } var registry = make(map[string]Registration) diff --git a/tuple/datastore/datastore.go b/tuple/datastore/datastore.go index 30c935d..5c13abf 100644 --- a/tuple/datastore/datastore.go +++ b/tuple/datastore/datastore.go @@ -579,14 +579,13 @@ func (tbl *Table) Scan(opt *tuple.ScanOptions) tuple.Iterator { } type Iterator struct { - tbl *Table + err error q *datastore.Query - keysOnly bool f *tuple.Filter - - it *datastore.Iterator - t tuple.Tuple - err error + it *datastore.Iterator + tbl *Table + t tuple.Tuple + keysOnly bool } func (it *Iterator) Reset() { diff --git a/tuple/kv/downgrade.go b/tuple/kv/downgrade.go index abaa9ab..2170ddb 100644 --- a/tuple/kv/downgrade.go +++ b/tuple/kv/downgrade.go @@ -159,10 +159,10 @@ var ( ) type flatIterator struct { - tx *flatTx - pref flat.Key it tuple.Iterator err error + tx *flatTx + pref flat.Key } func (it *flatIterator) Reset() { diff --git a/tuple/sql/builder.go b/tuple/sql/builder.go index 9945c6a..bc48e2e 100644 --- a/tuple/sql/builder.go +++ b/tuple/sql/builder.go @@ -11,9 +11,9 @@ func (d *Dialect) NewBuilder() *Builder { type Builder struct { d *Dialect - pi int buf *bytes.Buffer args []interface{} + pi int } func (b *Builder) Reset() { diff --git a/tuple/sql/dialect.go b/tuple/sql/dialect.go index 8e60184..8affac3 100644 --- a/tuple/sql/dialect.go +++ b/tuple/sql/dialect.go @@ -10,6 +10,10 @@ import ( type ErrorFunc func(err error) error type Dialect struct { + QuoteIdentifierFunc func(s string) string + Placeholder func(i int) string + ColumnCommentInline func(s string) string + ColumnCommentSet func(b *Builder, tbl, col, s string) Errors ErrorFunc BytesType string StringType string @@ -18,8 +22,6 @@ type Dialect struct { TimeType string StringTypeCollation string AutoType string - QuoteIdentifierFunc func(s string) string - Placeholder func(i int) string // DefaultSchema will be used to query table metadata. // If not set, defaults to the database name. DefaultSchema string @@ -37,9 +39,7 @@ type Dialect struct { OnConflict bool // Returning indicates that INSERT queries needs an RETURNING keyword to return last // inserted id. - Returning bool - ColumnCommentInline func(s string) string - ColumnCommentSet func(b *Builder, tbl, col, s string) + Returning bool } func (d *Dialect) SetDefaults() { diff --git a/tuple/sql/registry.go b/tuple/sql/registry.go index ab785f9..7c8414d 100644 --- a/tuple/sql/registry.go +++ b/tuple/sql/registry.go @@ -11,10 +11,10 @@ type DSNFunc func(addr, ns string) (string, error) // Registration is an information about the database driver. type Registration struct { - base.Registration - Driver string - DSN DSNFunc Dialect Dialect + DSN DSNFunc + Driver string + base.Registration } var registry = make(map[string]Registration) diff --git a/tuple/sql/sql.go b/tuple/sql/sql.go index d2239b3..9b4ee85 100644 --- a/tuple/sql/sql.go +++ b/tuple/sql/sql.go @@ -816,15 +816,13 @@ func (tbl *sqlTable) Scan(opt *tuple.ScanOptions) tuple.Iterator { type rowsFunc func(ctx context.Context) (*sql.Rows, error) type sqlIterator struct { - tbl *sqlTable - + err error rows *sql.Rows open rowsFunc - keysOnly bool f *tuple.Filter - - t *tuple.Tuple - err error + t *tuple.Tuple + tbl *sqlTable + keysOnly bool } func (it *sqlIterator) Reset() { diff --git a/tuple/sql/sqltest/registry.go b/tuple/sql/sqltest/registry.go index e76df78..e975940 100644 --- a/tuple/sql/sqltest/registry.go +++ b/tuple/sql/sqltest/registry.go @@ -13,8 +13,8 @@ type Registration struct { } type Version struct { - Name string Factory Database + Name string } var registry = make(map[string][]Version) diff --git a/tuple/tupletest/tupletest.go b/tuple/tupletest/tupletest.go index 729f479..da04408 100644 --- a/tuple/tupletest/tupletest.go +++ b/tuple/tupletest/tupletest.go @@ -58,8 +58,8 @@ func RunTest(t *testing.T, fnc Func, opts *Options) { } var testList = []struct { - name string test func(t *testing.T, db tuple.Store) + name string }{ {name: "basic", test: basic}, {name: "typed", test: typed}, diff --git a/values/values_test.go b/values/values_test.go index 12daa0a..fba2230 100644 --- a/values/values_test.go +++ b/values/values_test.go @@ -100,8 +100,8 @@ func TestMarshalBinary(t *testing.T) { } var casesCompare = []struct { - name string a, b Sortable + name string exp int }{ {name: "nil == nil", a: nil, b: nil, exp: 0}, From d057ebef81ff8284102829c2a3f5808d9ae63a1c Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:17:36 +0200 Subject: [PATCH 03/26] Rename "t" to "tb" for testing.TB --- kv/kvtest/helpers.go | 48 ++++++++-------- kv/kvtest/kvtest.go | 72 ++++++++++++------------ legacy/nosql/couch/test/couchtest.go | 14 ++--- legacy/nosql/couch/test/pouchtest.go | 10 ++-- legacy/nosql/elastic/test/elastictest.go | 14 ++--- legacy/nosql/mongo/test/mongotest.go | 14 ++--- legacy/nosql/nosqltest/nosqltest.go | 29 +++++----- tuple/datastore/datastore_test.go | 19 ++++--- tuple/sql/mysql/test/mysqltest.go | 10 ++-- tuple/sql/postgres/test/postgrestest.go | 10 ++-- tuple/sql/sqltest/sqltest.go | 16 +++--- tuple/tupletest/helpers.go | 4 +- tuple/tupletest/tupletest.go | 66 +++++++++++----------- 13 files changed, 164 insertions(+), 162 deletions(-) diff --git a/kv/kvtest/helpers.go b/kv/kvtest/helpers.go index af38ef3..15bfaae 100644 --- a/kv/kvtest/helpers.go +++ b/kv/kvtest/helpers.go @@ -9,64 +9,64 @@ import ( "github.com/hidal-go/hidalgo/kv" ) -func NewTest(t testing.TB, db kv.KV) *Test { - return &Test{t: t, db: db} +func NewTest(tb testing.TB, db kv.KV) *Test { + return &Test{tb: tb, db: db} } type Test struct { - t testing.TB + tb testing.TB db kv.KV } func (t Test) Get(key kv.Key) (kv.Value, error) { tx, err := t.db.Tx(false) - require.NoError(t.t, err) + require.NoError(t.tb, err) defer tx.Close() return tx.Get(context.TODO(), key) } func (t Test) NotExists(k kv.Key) { v, err := t.Get(k) - require.Equal(t.t, kv.ErrNotFound, err) - require.Equal(t.t, kv.Value(nil), v) + require.Equal(t.tb, kv.ErrNotFound, err) + require.Equal(t.tb, kv.Value(nil), v) } func (t Test) Expect(k kv.Key, exp kv.Value) { v, err := t.Get(k) - require.NoError(t.t, err) - require.Equal(t.t, exp, v) + require.NoError(t.tb, err) + require.Equal(t.tb, exp, v) } func (t Test) Put(key kv.Key, val kv.Value) { tx, err := t.db.Tx(true) - require.NoError(t.t, err) + require.NoError(t.tb, err) defer tx.Close() err = tx.Put(key, val) - require.NoError(t.t, err) + require.NoError(t.tb, err) ctx := context.TODO() got, err := tx.Get(ctx, key) - require.NoError(t.t, err) - require.Equal(t.t, val, got) + require.NoError(t.tb, err) + require.Equal(t.tb, val, got) err = tx.Commit(ctx) - require.NoError(t.t, err) + require.NoError(t.tb, err) } func (t Test) Del(key kv.Key) { tx, err := t.db.Tx(true) - require.NoError(t.t, err) + require.NoError(t.tb, err) defer tx.Close() err = tx.Del(key) - require.NoError(t.t, err) + require.NoError(t.tb, err) ctx := context.TODO() got, err := tx.Get(ctx, key) - require.Equal(t.t, kv.ErrNotFound, err) - require.Equal(t.t, kv.Value(nil), got) + require.Equal(t.tb, kv.ErrNotFound, err) + require.Equal(t.tb, kv.Value(nil), got) err = tx.Commit(ctx) - require.NoError(t.t, err) + require.NoError(t.tb, err) } func (t Test) ExpectIt(it kv.Iterator, exp []kv.Pair) { @@ -81,30 +81,30 @@ func (t Test) ExpectIt(it kv.Iterator, exp []kv.Pair) { Val: it.Val().Clone(), }) } - require.NoError(t.t, it.Err()) - require.Equal(t.t, exp, got) + require.NoError(t.tb, it.Err()) + require.Equal(t.tb, exp, got) } func (t Test) Scan(exp []kv.Pair, opts ...kv.IteratorOption) { tx, err := t.db.Tx(false) - require.NoError(t.t, err) + require.NoError(t.tb, err) defer tx.Close() it := tx.Scan(opts...) defer it.Close() - require.NoError(t.t, it.Err()) + require.NoError(t.tb, it.Err()) t.ExpectIt(it, exp) } func (t Test) ScanReset(exp []kv.Pair, opts ...kv.IteratorOption) { tx, err := t.db.Tx(false) - require.NoError(t.t, err) + require.NoError(t.tb, err) defer tx.Close() it := tx.Scan(opts...) defer it.Close() - require.NoError(t.t, it.Err()) + require.NoError(t.tb, it.Err()) t.ExpectIt(it, exp) it.Reset() diff --git a/kv/kvtest/kvtest.go b/kv/kvtest/kvtest.go index 2ffa413..9eefe33 100644 --- a/kv/kvtest/kvtest.go +++ b/kv/kvtest/kvtest.go @@ -16,7 +16,7 @@ import ( // Func is a constructor for database implementations. // It returns an empty database and a function to destroy it. -type Func func(t testing.TB) kv.KV +type Func func(tb testing.TB) kv.KV type Options struct { NoLocks bool // not safe for concurrent writes @@ -47,18 +47,18 @@ func RunTestLocal(t *testing.T, open kv.OpenPathFunc, opts *Options) { if opts == nil { opts = &Options{} } - RunTest(t, func(t testing.TB) kv.KV { + RunTest(t, func(tb testing.TB) kv.KV { dir, err := ioutil.TempDir("", "dal-kv-") - require.NoError(t, err) - t.Cleanup(func() { + require.NoError(tb, err) + tb.Cleanup(func() { _ = os.RemoveAll(dir) }) db, err := open(dir) if err != nil { - require.NoError(t, err) + require.NoError(tb, err) } - t.Cleanup(func() { + tb.Cleanup(func() { db.Close() db.Close() // test double close }) @@ -78,8 +78,8 @@ var testList = []struct { {name: "increment", test: increment, txOnly: true, concurrent: true}, } -func basic(t testing.TB, db kv.KV) { - td := NewTest(t, db) +func basic(tb testing.TB, db kv.KV) { + td := NewTest(tb, db) keys := []kv.Key{ {[]byte("a")}, @@ -117,8 +117,8 @@ func basic(t testing.TB, db kv.KV) { } } -func readonly(t testing.TB, db kv.KV) { - td := NewTest(t, db) +func readonly(tb testing.TB, db kv.KV) { + td := NewTest(tb, db) key := kv.Key{[]byte("a")} val := []byte("v") @@ -127,26 +127,26 @@ func readonly(t testing.TB, db kv.KV) { nokey := kv.Key{[]byte("b")} tx, err := db.Tx(false) - require.NoError(t, err) + require.NoError(tb, err) defer tx.Close() // writing anything on read-only tx must fail err = tx.Put(key, val) - require.Equal(t, kv.ErrReadOnly, err) + require.Equal(tb, kv.ErrReadOnly, err) err = tx.Put(nokey, val) - require.Equal(t, kv.ErrReadOnly, err) + require.Equal(tb, kv.ErrReadOnly, err) // deleting records on read-only tx must fail err = tx.Del(key) - require.Equal(t, kv.ErrReadOnly, err) + require.Equal(tb, kv.ErrReadOnly, err) // deleting non-existed record on read-only tx must still fail err = tx.Del(nokey) - require.Equal(t, kv.ErrReadOnly, err) + require.Equal(tb, kv.ErrReadOnly, err) } -func seek(t testing.TB, db kv.KV) { - td := NewTest(t, db) +func seek(tb testing.TB, db kv.KV) { + td := NewTest(tb, db) keys := []kv.Key{ {[]byte("a")}, @@ -165,7 +165,7 @@ func seek(t testing.TB, db kv.KV) { } tx, err := db.Tx(false) - require.NoError(t, err) + require.NoError(tb, err) defer tx.Close() ctx := context.TODO() @@ -181,9 +181,9 @@ func seek(t testing.TB, db kv.KV) { // seek to each key, current value must match corresponding element, and iterating further must return everything else for i, p := range all { ok := kv.Seek(ctx, it, p.Key) - require.True(t, ok) - require.Equal(t, p.Key, it.Key()) - require.Equal(t, p.Val, it.Val()) + require.True(tb, ok) + require.Equal(tb, p.Key, it.Key()) + require.Equal(tb, p.Val, it.Val()) td.ExpectIt(it, all[i+1:]) } @@ -191,14 +191,14 @@ func seek(t testing.TB, db kv.KV) { for _, off := range []int{1, 2} { for i := range all[:len(all)-off] { ok := kv.Seek(ctx, it, all[i].Key) - require.True(t, ok) - require.Equal(t, all[i].Key, it.Key()) - require.Equal(t, all[i].Val, it.Val()) + require.True(tb, ok) + require.Equal(tb, all[i].Key, it.Key()) + require.Equal(tb, all[i].Val, it.Val()) ok = kv.Seek(ctx, it, all[i+off].Key) - require.True(t, ok) - require.Equal(t, all[i+off].Key, it.Key()) - require.Equal(t, all[i+off].Val, it.Val()) + require.True(tb, ok) + require.Equal(tb, all[i+off].Key, it.Key()) + require.Equal(tb, all[i+off].Val, it.Val()) td.ExpectIt(it, all[i+off+1:]) } @@ -211,14 +211,14 @@ func seek(t testing.TB, db kv.KV) { continue } ok := kv.Seek(ctx, it, all[i].Key) - require.True(t, ok) - require.Equal(t, all[i].Key, it.Key()) - require.Equal(t, all[i].Val, it.Val()) + require.True(tb, ok) + require.Equal(tb, all[i].Key, it.Key()) + require.Equal(tb, all[i].Val, it.Val()) ok = kv.Seek(ctx, it, all[i-off].Key) - require.True(t, ok) - require.Equal(t, all[i-off].Key, it.Key()) - require.Equal(t, all[i-off].Val, it.Val()) + require.True(tb, ok) + require.Equal(tb, all[i-off].Key, it.Key()) + require.Equal(tb, all[i-off].Val, it.Val()) td.ExpectIt(it, all[i-off+1:]) } @@ -228,8 +228,8 @@ func seek(t testing.TB, db kv.KV) { td.ExpectIt(it, all) } -func increment(t testing.TB, db kv.KV) { - td := NewTest(t, db) +func increment(tb testing.TB, db kv.KV) { + td := NewTest(tb, db) key := kv.Key{[]byte("a")} td.Put(key, []byte("0")) @@ -266,7 +266,7 @@ func increment(t testing.TB, db kv.KV) { wg.Wait() select { case err := <-errc: - require.NoError(t, err) + require.NoError(tb, err) default: } td.Expect(key, []byte("10")) diff --git a/legacy/nosql/couch/test/couchtest.go b/legacy/nosql/couch/test/couchtest.go index d07d942..cbe8435 100644 --- a/legacy/nosql/couch/test/couchtest.go +++ b/legacy/nosql/couch/test/couchtest.go @@ -24,10 +24,10 @@ func init() { func CouchVersion(vers string) nosqltest.Database { return nosqltest.Database{ Traits: couch.Traits(), - Run: func(t testing.TB) nosql.Database { + Run: func(tb testing.TB) nosql.Database { pool, err := dockertest.NewPool("") if err != nil { - t.Fatal(err) + tb.Fatal(err) } cont, err := pool.Run("couchdb", vers, []string{ @@ -35,9 +35,9 @@ func CouchVersion(vers string) nosqltest.Database { "COUCHDB_PASSWORD=test", }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = cont.Close() }) @@ -54,14 +54,14 @@ func CouchVersion(vers string) nosqltest.Database { return err }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } qs, err := couch.Dial(true, couch.DriverCouch, addr, "test", nil) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = qs.Close() }) return qs diff --git a/legacy/nosql/couch/test/pouchtest.go b/legacy/nosql/couch/test/pouchtest.go index 78615fe..468c0a4 100644 --- a/legacy/nosql/couch/test/pouchtest.go +++ b/legacy/nosql/couch/test/pouchtest.go @@ -24,14 +24,14 @@ func init() { func Pouch() nosqltest.Database { return nosqltest.Database{ Traits: couch.Traits(), - Run: func(t testing.TB) nosql.Database { + Run: func(tb testing.TB) nosql.Database { dir, err := ioutil.TempDir("", "pouch-") if err != nil { - t.Fatal("failed to make temp dir:", err) + tb.Fatal("failed to make temp dir:", err) } t.Cleanup(func() { if err := os.RemoveAll(dir); err != nil { // remove the test data - t.Fatal(err) + tb.Fatal(err) } }) @@ -40,9 +40,9 @@ func Pouch() nosqltest.Database { qs, err := couch.Dial(false, couch.DriverPouch, dir+"/"+name, name, nil) if err != nil { os.RemoveAll(dir) - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { qs.Close() ctx := context.TODO() if c, err := kivik.New(ctx, couch.DriverPouch, dir); err == nil { diff --git a/legacy/nosql/elastic/test/elastictest.go b/legacy/nosql/elastic/test/elastictest.go index 232af06..dfedeeb 100644 --- a/legacy/nosql/elastic/test/elastictest.go +++ b/legacy/nosql/elastic/test/elastictest.go @@ -29,19 +29,19 @@ func init() { func ElasticVersion(vers string) nosqltest.Database { return nosqltest.Database{ Traits: elastic.Traits(), - Run: func(t testing.TB) nosql.Database { + Run: func(tb testing.TB) nosql.Database { name := "docker.elastic.co/elasticsearch/elasticsearch" pool, err := dockertest.NewPool("") if err != nil { - t.Fatal(err) + tb.Fatal(err) } cont, err := pool.Run(name, vers, nil) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = cont.Close() }) @@ -61,14 +61,14 @@ func ElasticVersion(vers string) nosqltest.Database { return err }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } db, err := elastic.Dial(addr, "test", nil) if err != nil { - t.Fatal(addr, err) + tb.Fatal(addr, err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = db.Close() }) return db diff --git a/legacy/nosql/mongo/test/mongotest.go b/legacy/nosql/mongo/test/mongotest.go index abb28da..ef2a217 100644 --- a/legacy/nosql/mongo/test/mongotest.go +++ b/legacy/nosql/mongo/test/mongotest.go @@ -25,17 +25,17 @@ func init() { func MongoVersion(vers string) nosqltest.Database { return nosqltest.Database{ Traits: mongo.Traits(), - Run: func(t testing.TB) nosql.Database { + Run: func(tb testing.TB) nosql.Database { pool, err := dockertest.NewPool("") if err != nil { - t.Fatal(err) + tb.Fatal(err) } cont, err := pool.Run("mongo", vers, nil) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = cont.Close() }) @@ -55,13 +55,13 @@ func MongoVersion(vers string) nosqltest.Database { return nil }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } qs, err := mongo.Dial(addr, "test", nil) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = qs.Close() }) return qs diff --git a/legacy/nosql/nosqltest/nosqltest.go b/legacy/nosql/nosqltest/nosqltest.go index e4aa104..ed85258 100644 --- a/legacy/nosql/nosqltest/nosqltest.go +++ b/legacy/nosql/nosqltest/nosqltest.go @@ -133,12 +133,12 @@ type tableConf struct { tr nosql.Traits } -func (c tableConf) ensurePK(t testing.TB, secondary ...nosql.Index) { +func (c tableConf) ensurePK(tb testing.TB, secondary ...nosql.Index) { err := c.db.EnsureIndex(c.ctx, c.col, nosql.Index{ Fields: c.kt.Fields, Type: nosql.StringExact, }, secondary) - require.NoError(t, err) + require.NoError(tb, err) } func (c tableConf) FindByKey(key nosql.Key) (nosql.Document, error) { @@ -154,11 +154,11 @@ func (c tableConf) fixDoc(k nosql.Key, d nosql.Document) { fixDoc(&c.tr, d) } -func (c tableConf) expectAll(t testing.TB, docs []nosql.Document) { - iterateExpect(t, c.kt, c.db.Query(c.col), docs) +func (c tableConf) expectAll(tb testing.TB, docs []nosql.Document) { + iterateExpect(tb, c.kt, c.db.Query(c.col), docs) } -func (c tableConf) insertDocs(t testing.TB, n int, fnc func(i int) nosql.Document) ([]nosql.Key, []nosql.Document) { +func (c tableConf) insertDocs(tb testing.TB, n int, fnc func(i int) nosql.Document) ([]nosql.Key, []nosql.Document) { var docs []nosql.Document w := nosql.BatchInsert(c.db, c.col) defer w.Close() @@ -169,18 +169,18 @@ func (c tableConf) insertDocs(t testing.TB, n int, fnc func(i int) nosql.Documen key = c.kt.Gen() } err := w.WriteDoc(c.ctx, key, doc) - require.NoError(t, err) + require.NoError(tb, err) docs = append(docs, doc) } err := w.Flush(c.ctx) - require.NoError(t, err) + require.NoError(tb, err) keys := w.Keys() for i := range docs { c.fixDoc(keys[i], docs[i]) } - c.expectAll(t, docs) + c.expectAll(tb, docs) return keys, docs } @@ -273,7 +273,7 @@ func (s docsAndKeys) Swap(i, j int) { s.Keys[i], s.Keys[j] = s.Keys[j], s.Keys[i] } -func iterateExpect(t testing.TB, kt keyType, qu nosql.Query, exp []nosql.Document) { +func iterateExpect(tb testing.TB, kt keyType, qu nosql.Query, exp []nosql.Document) { ctx := context.TODO() it := qu.Iterate() @@ -286,7 +286,8 @@ func iterateExpect(t testing.TB, kt keyType, qu nosql.Query, exp []nosql.Documen keys = append(keys, it.Key()) got = append(got, it.Doc()) } - require.NoError(t, it.Err()) + + require.NoError(tb, it.Err()) sorter := byFields(kt.Fields) exp = append([]nosql.Document{}, exp...) @@ -302,12 +303,12 @@ func iterateExpect(t testing.TB, kt keyType, qu nosql.Query, exp []nosql.Documen LessFunc: sorter.Less, Docs: got, Keys: keys, }) - require.Equal(t, exp, got) - require.Equal(t, expKeys, keys) + require.Equal(tb, exp, got) + require.Equal(tb, expKeys, keys) n, err := qu.Count(ctx) - require.NoError(t, err) - require.Equal(t, int64(len(exp)), int64(n)) + require.NoError(tb, err) + require.Equal(tb, int64(len(exp)), int64(n)) } func testEnsure(t *testing.T, c tableConf) { diff --git a/tuple/datastore/datastore_test.go b/tuple/datastore/datastore_test.go index dee5707..dc87f51 100644 --- a/tuple/datastore/datastore_test.go +++ b/tuple/datastore/datastore_test.go @@ -14,10 +14,10 @@ import ( ) func TestDatastore(t *testing.T) { - tupletest.RunTest(t, func(t testing.TB) tuple.Store { + tupletest.RunTest(t, func(tb testing.TB) tuple.Store { pool, err := dockertest.NewPool("") if err != nil { - t.Fatal(err) + tb.Fatal(err) } const ( @@ -36,27 +36,28 @@ func TestDatastore(t *testing.T) { }, }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = cont.Close() }) ctx := context.Background() host := cont.GetHostPort("8080/tcp") if host == "" { - t.Fatal("empty host") + tb.Fatal("empty host") } + if err = os.Setenv("DATASTORE_EMULATOR_HOST", host); err != nil { - t.Fatal(err) + tb.Fatal(err) } else if host := os.Getenv("DATASTORE_EMULATOR_HOST"); host == "" { - t.Fatal("set env failed") + tb.Fatal("set env failed") } cli, err := datastore.NewClient(ctx, proj) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = cli.Close() }) diff --git a/tuple/sql/mysql/test/mysqltest.go b/tuple/sql/mysql/test/mysqltest.go index a67a426..2724f08 100644 --- a/tuple/sql/mysql/test/mysqltest.go +++ b/tuple/sql/mysql/test/mysqltest.go @@ -28,19 +28,19 @@ func MySQLVersion(vers string) sqltest.Database { const image = "mysql" return sqltest.Database{ Recreate: false, - Run: func(t testing.TB) string { + Run: func(tb testing.TB) string { pool, err := dockertest.NewPool("") if err != nil { - t.Fatal(err) + tb.Fatal(err) } cont, err := pool.Run(image, vers, []string{ "MYSQL_ROOT_PASSWORD=root", }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = cont.Close() }) @@ -56,7 +56,7 @@ func MySQLVersion(vers string) sqltest.Database { return cli.Ping() }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } return addr }, diff --git a/tuple/sql/postgres/test/postgrestest.go b/tuple/sql/postgres/test/postgrestest.go index 7c41adc..724377a 100644 --- a/tuple/sql/postgres/test/postgrestest.go +++ b/tuple/sql/postgres/test/postgrestest.go @@ -28,19 +28,19 @@ func PostgresVersion(vers string) sqltest.Database { const image = "postgres" return sqltest.Database{ Recreate: false, - Run: func(t testing.TB) string { + Run: func(tb testing.TB) string { pool, err := dockertest.NewPool("") if err != nil { - t.Fatal(err) + tb.Fatal(err) } cont, err := pool.Run(image, vers, []string{ "POSTGRES_PASSWORD=postgres", }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = cont.Close() }) @@ -56,7 +56,7 @@ func PostgresVersion(vers string) sqltest.Database { return cli.Ping() }) if err != nil { - t.Fatal(err) + tb.Fatal(err) } return addr }, diff --git a/tuple/sql/sqltest/sqltest.go b/tuple/sql/sqltest/sqltest.go index e8761df..1d062d5 100644 --- a/tuple/sql/sqltest/sqltest.go +++ b/tuple/sql/sqltest/sqltest.go @@ -14,8 +14,8 @@ import ( ) type Database struct { + Run func(tb testing.TB) string Recreate bool - Run func(t testing.TB) string } func TestSQL(t *testing.T, name string, gen Database) { @@ -24,26 +24,26 @@ func TestSQL(t *testing.T, name string, gen Database) { if !recreate { addr = gen.Run(t) } - tupletest.RunTest(t, func(t testing.TB) tuple.Store { + tupletest.RunTest(t, func(tb testing.TB) tuple.Store { db := fmt.Sprintf("db_%x", rand.Int()) addr := addr if recreate { - addr = gen.Run(t) + addr = gen.Run(tb) } conn, err := sqltuple.OpenSQL(name, addr, "") if err != nil { - require.NoError(t, err) + require.NoError(tb, err) } _, err = conn.Exec(`CREATE DATABASE ` + db) conn.Close() if err != nil { - require.NoError(t, err) + require.NoError(tb, err) } conn, err = sqltuple.OpenSQL(name, addr, db) if err != nil { - require.NoError(t, err) + require.NoError(tb, err) } - t.Cleanup(func() { + tb.Cleanup(func() { conn.Close() if !recreate { conn, err := sqltuple.OpenSQL(name, addr, "") @@ -52,7 +52,7 @@ func TestSQL(t *testing.T, name string, gen Database) { conn.Close() } if err != nil { - t.Errorf("cannot remove test database: %v", err) + tb.Errorf("cannot remove test database: %v", err) } } }) diff --git a/tuple/tupletest/helpers.go b/tuple/tupletest/helpers.go index fd1728b..ca7cceb 100644 --- a/tuple/tupletest/helpers.go +++ b/tuple/tupletest/helpers.go @@ -6,8 +6,8 @@ import ( "github.com/hidal-go/hidalgo/tuple" ) -func NewTest(t testing.TB, db tuple.Store) *Test { - return &Test{t: t, db: db} +func NewTest(tb testing.TB, db tuple.Store) *Test { + return &Test{t: tb, db: db} } type Test struct { diff --git a/tuple/tupletest/tupletest.go b/tuple/tupletest/tupletest.go index da04408..35fbabb 100644 --- a/tuple/tupletest/tupletest.go +++ b/tuple/tupletest/tupletest.go @@ -20,7 +20,7 @@ import ( // Func is a constructor for database implementations. // It returns an empty database and a function to destroy it. -type Func func(t testing.TB) tuple.Store +type Func func(tb testing.TB) tuple.Store type Options struct { NoLocks bool // not safe for concurrent writes @@ -38,15 +38,15 @@ func RunTest(t *testing.T, fnc Func, opts *Options) { }) } t.Run("kv", func(t *testing.T) { - kvtest.RunTest(t, func(t testing.TB) hkv.KV { - db := fnc(t) + kvtest.RunTest(t, func(tb testing.TB) hkv.KV { + db := fnc(tb) ctx := context.TODO() kdb, err := tuplekv.NewKV(ctx, db, "kv") if err != nil { - require.NoError(t, err) + require.NoError(tb, err) } - t.Cleanup(func() { + tb.Cleanup(func() { _ = kdb.Close() }) return flat.Upgrade(kdb) @@ -314,7 +314,7 @@ func tables(t *testing.T, db tuple.Store) { }) } -func tablesSimple(t testing.TB, db tuple.Store) { +func tablesSimple(tb testing.TB, db tuple.Store) { ctx := context.Background() const name = "test1" @@ -330,7 +330,7 @@ func tablesSimple(t testing.TB, db tuple.Store) { newTx := func(rw bool) tuple.Tx { tx, err := db.Tx(rw) - require.NoError(t, err) + require.NoError(tb, err) return tx } @@ -338,70 +338,70 @@ func tablesSimple(t testing.TB, db tuple.Store) { notExists := func() { tbl, err := tx.Table(ctx, name) - require.Equal(t, tuple.ErrTableNotFound, err) - require.Nil(t, tbl) + require.Equal(tb, tuple.ErrTableNotFound, err) + require.Nil(tb, tbl) } // access table when it not exists notExists() list, err := tx.ListTables(ctx) - require.NoError(t, err) - require.Empty(t, list) + require.NoError(tb, err) + require.Empty(tb, list) // create table on read-only transaction _, err = tx.CreateTable(ctx, schema) - require.Equal(t, tuple.ErrReadOnly, err) + require.Equal(tb, tuple.ErrReadOnly, err) notExists() err = tx.Close() - require.NoError(t, err) + require.NoError(tb, err) // reopen read-write transaction tx = newTx(true) // table should not exist after failed creation tbl, err := tx.Table(ctx, name) - require.Equal(t, tuple.ErrTableNotFound, err) - require.Nil(t, tbl) + require.Equal(tb, tuple.ErrTableNotFound, err) + require.Nil(tb, tbl) tbl, err = tx.CreateTable(ctx, schema) - require.NoError(t, err) - require.NotNil(t, tbl) + require.NoError(tb, err) + require.NotNil(tb, tbl) // TODO: check create + rollback err = tx.Commit(ctx) - require.NoError(t, err) + require.NoError(tb, err) tx = newTx(true) tbl, err = tx.Table(ctx, name) - require.NoError(t, err) - assert.Equal(t, schema, tbl.Header()) + require.NoError(tb, err) + assert.Equal(tb, schema, tbl.Header()) err = tbl.Drop(ctx) - require.NoError(t, err) + require.NoError(tb, err) tbl, err = tx.Table(ctx, name) - require.Equal(t, tuple.ErrTableNotFound, err) - require.Nil(t, tbl) + require.Equal(tb, tuple.ErrTableNotFound, err) + require.Nil(tb, tbl) err = tx.Commit(ctx) - require.NoError(t, err) + require.NoError(tb, err) tx = newTx(false) notExists() err = tx.Close() - require.NoError(t, err) + require.NoError(tb, err) // TODO: test multiple tables // TODO: test different headers (only keys, only values) } -func tablesAuto(t testing.TB, db tuple.Store) { +func tablesAuto(tb testing.TB, db tuple.Store) { ctx := context.Background() const name = "test2" @@ -417,27 +417,27 @@ func tablesAuto(t testing.TB, db tuple.Store) { newTx := func(rw bool) tuple.Tx { tx, err := db.Tx(rw) - require.NoError(t, err) + require.NoError(tb, err) return tx } tx := newTx(true) tbl, err := tx.CreateTable(ctx, schema) - require.NoError(t, err) - require.NotNil(t, tbl) + require.NoError(tb, err) + require.NotNil(tb, tbl) err = tx.Commit(ctx) - require.NoError(t, err) + require.NoError(tb, err) tx = newTx(false) tbl, err = tx.Table(ctx, name) - require.NoError(t, err) - assert.Equal(t, schema, tbl.Header()) + require.NoError(tb, err) + assert.Equal(tb, schema, tbl.Header()) err = tx.Close() - require.NoError(t, err) + require.NoError(tb, err) } func auto(t *testing.T, db tuple.Store) { From 013c1904d7733c77d65c321380fd1d2c2b12f2b6 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 21:14:49 +0200 Subject: [PATCH 04/26] Rename "t" to "b" for testing.B --- legacy/nosql/nosqltest/nosqltest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/legacy/nosql/nosqltest/nosqltest.go b/legacy/nosql/nosqltest/nosqltest.go index ed85258..466589e 100644 --- a/legacy/nosql/nosqltest/nosqltest.go +++ b/legacy/nosql/nosqltest/nosqltest.go @@ -47,7 +47,7 @@ func TestNoSQL(t *testing.T, gen Database) { } } -func BenchmarkNoSQL(t *testing.B, gen Database) { +func BenchmarkNoSQL(b *testing.B, gen Database) { // TODO } From c56850fbfc2bc5790c98139d958fec62e94d543d Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:19:21 +0200 Subject: [PATCH 05/26] Rename types treePool and enumPool --- kv/flat/btree/keys.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/kv/flat/btree/keys.go b/kv/flat/btree/keys.go index 97ab6e8..5ce18b0 100644 --- a/kv/flat/btree/keys.go +++ b/kv/flat/btree/keys.go @@ -79,22 +79,22 @@ func init() { var ( btDPool = sync.Pool{New: func() interface{} { return &d{} }} - btEPool = btEpool{sync.Pool{New: func() interface{} { return &Enumerator{} }}} - btTPool = btTpool{sync.Pool{New: func() interface{} { return &Tree{} }}} + btEPool = enumPool{sync.Pool{New: func() interface{} { return &Enumerator{} }}} + btTPool = treePool{sync.Pool{New: func() interface{} { return &Tree{} }}} btXPool = sync.Pool{New: func() interface{} { return &x{} }} ) -type btTpool struct{ sync.Pool } +type treePool struct{ sync.Pool } -func (p *btTpool) get(cmp Cmp) *Tree { +func (p *treePool) get(cmp Cmp) *Tree { x := p.Get().(*Tree) x.cmp = cmp return x } -type btEpool struct{ sync.Pool } +type enumPool struct{ sync.Pool } -func (p *btEpool) get(err error, hit bool, i int, k []byte, q *d, t *Tree, ver int64) *Enumerator { +func (p *enumPool) get(err error, hit bool, i int, k []byte, q *d, t *Tree, ver int64) *Enumerator { x := p.Get().(*Enumerator) x.err, x.hit, x.i, x.k, x.q, x.t, x.ver = err, hit, i, k, q, t, ver return x From b770175775ed74f389872e0764abd6127d5ebcd9 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:21:18 +0200 Subject: [PATCH 06/26] Use local "ok" variable --- kv/flat/btree/keys.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/kv/flat/btree/keys.go b/kv/flat/btree/keys.go index 5ce18b0..af9202d 100644 --- a/kv/flat/btree/keys.go +++ b/kv/flat/btree/keys.go @@ -332,7 +332,7 @@ func (t *Tree) catX(p, q, r *x, pi int) { // Delete removes the k's KV pair, if it exists, in which case Delete returns // true. -func (t *Tree) Delete(k []byte) (ok bool) { +func (t *Tree) Delete(k []byte) bool { pi := -1 var p *x q := t.r @@ -341,8 +341,7 @@ func (t *Tree) Delete(k []byte) (ok bool) { } for { - var i int - i, ok = t.find(q, k) + i, ok := t.find(q, k) if ok { switch x := q.(type) { case *x: @@ -352,7 +351,6 @@ func (t *Tree) Delete(k []byte) (ok bool) { pi = i + 1 p = x q = x.x[pi].ch - ok = false continue case *d: t.extract(x, i) From 4322d640f9c4e830aa5c714b274665f2f13895ba Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:22:12 +0200 Subject: [PATCH 07/26] Simplify Next() and Prev() --- kv/flat/btree/keys.go | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/kv/flat/btree/keys.go b/kv/flat/btree/keys.go index af9202d..6e65292 100644 --- a/kv/flat/btree/keys.go +++ b/kv/flat/btree/keys.go @@ -862,37 +862,37 @@ func (e *Enumerator) Close() { // next item in the key collation order. If there is no item to return, err == // io.EOF is returned. func (e *Enumerator) Next() (k, v []byte, err error) { - if err = e.err; err != nil { - return + if e.err != nil { + return nil, nil, e.err } if e.ver != e.t.ver { f, hit := e.t.Seek(e.k) if !e.hit && hit { if err = f.next(); err != nil { - return + return nil, nil, err } } *e = *f f.Close() } + if e.q == nil { - e.err, err = io.EOF, io.EOF - return + e.err = io.EOF + return nil, nil, io.EOF } if e.i >= e.q.c { if err = e.next(); err != nil { - return + return nil, nil, err } } i := e.q.d[e.i] - k, v = i.k, i.v - e.k, e.hit = k, false - e.next() - return + e.k, e.hit = i.k, false + _ = e.next() + return i.k, i.v, err } func (e *Enumerator) next() error { @@ -909,6 +909,7 @@ func (e *Enumerator) next() error { e.err = io.EOF } } + return e.err } @@ -916,37 +917,37 @@ func (e *Enumerator) next() error { // previous item in the key collation order. If there is no item to return, err // == io.EOF is returned. func (e *Enumerator) Prev() (k, v []byte, err error) { - if err = e.err; err != nil { - return + if e.err != nil { + return nil, nil, e.err } if e.ver != e.t.ver { f, hit := e.t.Seek(e.k) if !e.hit && hit { if err = f.prev(); err != nil { - return + return nil, nil, err } } *e = *f f.Close() } + if e.q == nil { - e.err, err = io.EOF, io.EOF - return + e.err = io.EOF + return nil, nil, io.EOF } if e.i >= e.q.c { if err = e.next(); err != nil { - return + return nil, nil, err } } i := e.q.d[e.i] - k, v = i.k, i.v - e.k, e.hit = k, false - e.prev() - return + e.k, e.hit = i.k, false + _ = e.prev() + return i.k, i.v, err } func (e *Enumerator) prev() error { @@ -966,5 +967,6 @@ func (e *Enumerator) prev() error { e.i = e.q.c - 1 } + return e.err } From 41c99882be55688012e5020eb0a2050231bae3e3 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:30:02 +0200 Subject: [PATCH 08/26] Simplify if blocks --- kv/options/prefix_flat.go | 11 ++++------- kv/options/prefix_kv.go | 8 +++----- legacy/nosql/mongo/mongo.go | 3 +-- 3 files changed, 8 insertions(+), 14 deletions(-) diff --git a/kv/options/prefix_flat.go b/kv/options/prefix_flat.go index 71dd31b..5d7ed21 100644 --- a/kv/options/prefix_flat.go +++ b/kv/options/prefix_flat.go @@ -73,14 +73,11 @@ func (it *prefixIteratorFlat) Next(ctx context.Context) bool { it.done = true return false } - } else { - if !it.base.Next(ctx) { - it.done = true - return false - } + } else if !it.base.Next(ctx) { + it.done = true + return false } - key := it.base.Key() - if bytes.HasPrefix(key, it.pref) { + if key := it.base.Key(); bytes.HasPrefix(key, it.pref) { return true } // keys are sorted, and we reached the end of the prefix diff --git a/kv/options/prefix_kv.go b/kv/options/prefix_kv.go index 4334690..0d9fcc7 100644 --- a/kv/options/prefix_kv.go +++ b/kv/options/prefix_kv.go @@ -72,11 +72,9 @@ func (it *prefixIteratorKV) Next(ctx context.Context) bool { it.done = true return false } - } else { - if !it.base.Next(ctx) { - it.done = true - return false - } + } else if !it.base.Next(ctx) { + it.done = true + return false } key := it.base.Key() if key.HasPrefix(it.pref) { diff --git a/legacy/nosql/mongo/mongo.go b/legacy/nosql/mongo/mongo.go index 46a1c51..15b89be 100644 --- a/legacy/nosql/mongo/mongo.go +++ b/legacy/nosql/mongo/mongo.go @@ -533,9 +533,8 @@ func (it *Iterator) Next(ctx context.Context) bool { if !it.it.Next(ctx) { return false } - err := it.it.Decode(&elem) - if err == nil { + if err := it.it.Decode(&elem); err == nil { it.res = elem } From c1b17251dd49c383fbf59ee0932cca409b74945b Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:30:29 +0200 Subject: [PATCH 09/26] Simplify FilterOp.String() --- legacy/nosql/nosql.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/legacy/nosql/nosql.go b/legacy/nosql/nosql.go index 7403af1..7163d07 100644 --- a/legacy/nosql/nosql.go +++ b/legacy/nosql/nosql.go @@ -78,24 +78,24 @@ type Database interface { type FilterOp int func (op FilterOp) String() string { - name := "" switch op { case Equal: - name = "Equal" + return "Equal" case NotEqual: - name = "NotEqual" + return "NotEqual" case GT: - name = "GT" + return "GT" case GTE: - name = "GTE" + return "GTE" case LT: - name = "LT" + return "LT" case LTE: - name = "LTE" + return "LTE" + case Regexp: + return "Regexp" default: return fmt.Sprintf("FilterOp(%d)", int(op)) } - return name } func (op FilterOp) GoString() string { From e992b9cb39f2c1b6d17d6d3a9218c0d0187ad7fb Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:33:18 +0200 Subject: [PATCH 10/26] Do not assign when not neccessary --- legacy/nosql/couch/ouch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/legacy/nosql/couch/ouch.go b/legacy/nosql/couch/ouch.go index b48f7cb..19f27c7 100644 --- a/legacy/nosql/couch/ouch.go +++ b/legacy/nosql/couch/ouch.go @@ -277,7 +277,7 @@ func (q *Query) buildFilters() { term := map[string]interface{}{} for _, filter := range filterList { testValue := toOuchValue(filter.Value) - test := "" + var test string switch filter.Filter { case nosql.Equal: test = "$eq" From 62d66f415b84b350c86632fd92a23caf85eb8c4c Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:34:31 +0200 Subject: [PATCH 11/26] Simplify incrementing --- legacy/nosql/elastic/elastic.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/legacy/nosql/elastic/elastic.go b/legacy/nosql/elastic/elastic.go index b6e0007..9be66ef 100644 --- a/legacy/nosql/elastic/elastic.go +++ b/legacy/nosql/elastic/elastic.go @@ -408,7 +408,7 @@ func convRegexp(o interface{}) interface{} { if strings.HasSuffix(s, "$") { s = s[:len(s)-1] } else { - s = s + ".*" + s += ".*" } return s } @@ -670,7 +670,7 @@ func (u *Update) Inc(field string, dn int) nosql.Update { if u.inc == nil { u.inc = make(map[string]int) } - u.inc[field] = u.inc[field] + dn + u.inc[field] += dn return u } From a0c708e4ce57086141f7d726dae980969f6c0567 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:37:01 +0200 Subject: [PATCH 12/26] Return error from db.sess.Disconnect() --- legacy/nosql/mongo/mongo.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/legacy/nosql/mongo/mongo.go b/legacy/nosql/mongo/mongo.go index 15b89be..d4a0064 100644 --- a/legacy/nosql/mongo/mongo.go +++ b/legacy/nosql/mongo/mongo.go @@ -109,8 +109,7 @@ type DB struct { } func (db *DB) Close() error { - db.sess.Disconnect(context.TODO()) - return nil + return db.sess.Disconnect(context.TODO()) } func (db *DB) EnsureIndex(ctx context.Context, col string, primary nosql.Index, secondary []nosql.Index) error { From d7b7b25933319affe3df60dfece480d9fee9dd62 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:38:47 +0200 Subject: [PATCH 13/26] Uppercase "id" in objidString -> objIDString --- legacy/nosql/mongo/mongo.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/legacy/nosql/mongo/mongo.go b/legacy/nosql/mongo/mongo.go index d4a0064..3087ba9 100644 --- a/legacy/nosql/mongo/mongo.go +++ b/legacy/nosql/mongo/mongo.go @@ -204,7 +204,7 @@ func fromBsonValue(v interface{}) nosql.Value { } return arr case primitive.ObjectID: - return nosql.String(objidString(v)) + return nosql.String(objIDString(v)) case primitive.M: return fromBsonDoc(v) case primitive.A: @@ -313,7 +313,7 @@ func getOrGenID(key nosql.Key) (nosql.Key, string) { var mid string if key == nil { // TODO: maybe allow to pass custom key types as nosql.Key - oid := objidString(primitive.NewObjectID()) + oid := objIDString(primitive.NewObjectID()) mid = oid key = nosql.Key{oid} } else { @@ -333,7 +333,7 @@ func (c *collection) convIns(key nosql.Key, d nosql.Document) (nosql.Key, primit return key, m } -func objidString(id primitive.ObjectID) string { +func objIDString(id primitive.ObjectID) string { return base64.StdEncoding.EncodeToString(id[:]) } From 8d13172a53498665a06acd064d6186804fc4f02e Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:40:30 +0200 Subject: [PATCH 14/26] Use "_" for unused func. parameters --- kv/flat/helpers.go | 2 +- legacy/nosql/couch/ouch.go | 6 +++--- tuple/kv/upgrade_test.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/kv/flat/helpers.go b/kv/flat/helpers.go index 0fdce20..ff9215f 100644 --- a/kv/flat/helpers.go +++ b/kv/flat/helpers.go @@ -28,7 +28,7 @@ func Update(ctx context.Context, kv KV, update func(tx Tx) error) error { } // View is a helper to open a read-only transaction to read the database. -func View(ctx context.Context, kv KV, view func(tx Tx) error) error { +func View(_ context.Context, kv KV, view func(tx Tx) error) error { tx, err := kv.Tx(false) if err != nil { return err diff --git a/legacy/nosql/couch/ouch.go b/legacy/nosql/couch/ouch.go index 19f27c7..4f57567 100644 --- a/legacy/nosql/couch/ouch.go +++ b/legacy/nosql/couch/ouch.go @@ -22,7 +22,7 @@ func Traits() nosql.Traits { } } -func DialDriver(ctx context.Context, driver, addr, dbName string) (*kivik.Client, string, error) { +func DialDriver(_ context.Context, driver, addr, dbName string) (*kivik.Client, string, error) { addrParsed, err := url.Parse(addr) if err != nil { return nil, "", err @@ -40,7 +40,7 @@ func DialDriver(ctx context.Context, driver, addr, dbName string) (*kivik.Client return cli, dbName, err } -func Dial(create bool, driver, addr, ns string, opt nosql.Options) (*DB, error) { +func Dial(create bool, driver, addr, ns string, _ nosql.Options) (*DB, error) { ctx := context.TODO() // TODO - replace with parameter value client, dbName, err := DialDriver(ctx, driver, addr, ns) @@ -580,7 +580,7 @@ func (u *Update) Upsert(d nosql.Document) nosql.Update { func (u *Update) Do(ctx context.Context) error { orig, id, rev, err := u.db.findByKey(ctx, u.col, u.key) - if err == nosql.ErrNotFound { + if errors.Is(err, nosql.ErrNotFound) { if !u.upsert { return err } diff --git a/tuple/kv/upgrade_test.go b/tuple/kv/upgrade_test.go index b7be352..0a06428 100644 --- a/tuple/kv/upgrade_test.go +++ b/tuple/kv/upgrade_test.go @@ -11,7 +11,7 @@ import ( ) func TestKV2Tuple(t *testing.T) { - tupletest.RunTest(t, func(t testing.TB) tuple.Store { + tupletest.RunTest(t, func(_ testing.TB) tuple.Store { kdb := btree.New() db := tuplekv.New(flat.Upgrade(kdb)) return db From 084fc1818dc47cdfb6101c741e558c8a84ed3ded Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 21:39:56 +0200 Subject: [PATCH 15/26] Use "_" for intentionnally unchecked returned errors --- tuple/datastore/datastore.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tuple/datastore/datastore.go b/tuple/datastore/datastore.go index 5c13abf..7b203ed 100644 --- a/tuple/datastore/datastore.go +++ b/tuple/datastore/datastore.go @@ -483,12 +483,12 @@ func (tbl *Table) InsertTuple(ctx context.Context, t tuple.Tuple) (tuple.Key, er } k := tbl.key(t.Key, true) if err := tx.Get(k, &payload{h: &tbl.h}); err == nil { - tx.Rollback() + _ = tx.Rollback() return nil, tuple.ErrExists } pk, err := tx.Put(k, &payload{h: &tbl.h, t: t}) if err != nil { - tx.Rollback() + _ = tx.Rollback() return nil, err } c, err := tx.Commit() From 68cb595c01ca7e3c98de2ccd415171cf951cd4bb Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:51:45 +0200 Subject: [PATCH 16/26] Use errors.Is(err,X) --- kv/bbolt/bolt.go | 7 ++++--- kv/bolt/bolt.go | 5 +++-- kv/flat/badger/badger.go | 9 +++++---- kv/flat/btree/btree.go | 3 ++- kv/flat/helpers.go | 7 +++++-- kv/flat/kv.go | 3 ++- kv/flat/leveldb/leveldb.go | 3 ++- kv/flat/pebble/pebble.go | 3 ++- kv/helpers.go | 7 +++++-- kv/kv.go | 2 +- kv/kvdebug/kvdebug.go | 3 ++- legacy/nosql/elastic/elastic.go | 3 ++- legacy/nosql/mongo/mongo.go | 3 ++- tuple/datastore/datastore.go | 10 +++++----- tuple/kv/downgrade.go | 5 +++-- tuple/kv/upgrade.go | 22 ++++++++++++---------- tuple/sql/sql.go | 6 +++--- 17 files changed, 60 insertions(+), 41 deletions(-) diff --git a/kv/bbolt/bolt.go b/kv/bbolt/bolt.go index 4487341..815f472 100644 --- a/kv/bbolt/bolt.go +++ b/kv/bbolt/bolt.go @@ -17,6 +17,7 @@ package bbolt import ( "bytes" "context" + "errors" "time" bolt "go.etcd.io/bbolt" @@ -165,7 +166,7 @@ func (tx *Tx) Put(k kv.Key, v kv.Value) error { return nil // bucket creation, no need to put value } err = b.Put(k[0], v) - if err == bolt.ErrTxNotWritable { + if errors.Is(err, bolt.ErrTxNotWritable) { err = kv.ErrReadOnly } return err @@ -177,8 +178,8 @@ func (tx *Tx) Del(k kv.Key) error { return nil } err := b.Delete(k[0]) - if err == bolt.ErrTxNotWritable { - err = kv.ErrReadOnly + if errors.Is(err, bolt.ErrTxNotWritable) { + return kv.ErrReadOnly } return err } diff --git a/kv/bolt/bolt.go b/kv/bolt/bolt.go index 1d29462..e544b00 100644 --- a/kv/bolt/bolt.go +++ b/kv/bolt/bolt.go @@ -17,6 +17,7 @@ package bolt import ( "bytes" "context" + "errors" "time" "github.com/boltdb/bolt" @@ -165,7 +166,7 @@ func (tx *Tx) Put(k kv.Key, v kv.Value) error { return nil // bucket creation, no need to put value } err = b.Put(k[0], v) - if err == bolt.ErrTxNotWritable { + if errors.Is(err, bolt.ErrTxNotWritable) { err = kv.ErrReadOnly } return err @@ -177,7 +178,7 @@ func (tx *Tx) Del(k kv.Key) error { return nil } err := b.Delete(k[0]) - if err == bolt.ErrTxNotWritable { + if errors.Is(err, bolt.ErrTxNotWritable) { err = kv.ErrReadOnly } return err diff --git a/kv/flat/badger/badger.go b/kv/flat/badger/badger.go index fe9e141..18d6f2d 100644 --- a/kv/flat/badger/badger.go +++ b/kv/flat/badger/badger.go @@ -2,6 +2,7 @@ package badger import ( "context" + "errors" "github.com/dgraph-io/badger/v2" @@ -85,7 +86,7 @@ type Tx struct { func (tx *Tx) Commit(ctx context.Context) error { err := tx.tx.Commit() - if err == badger.ErrConflict { + if errors.Is(err, badger.ErrConflict) { err = flat.ErrConflict } return err @@ -101,7 +102,7 @@ func (tx *Tx) Get(ctx context.Context, key flat.Key) (flat.Value, error) { return nil, flat.ErrNotFound } item, err := tx.tx.Get(key) - if err == badger.ErrKeyNotFound { + if errors.Is(err, badger.ErrKeyNotFound) { return nil, flat.ErrNotFound } else if err != nil { return nil, err @@ -115,7 +116,7 @@ func (tx *Tx) GetBatch(ctx context.Context, keys []flat.Key) ([]flat.Value, erro func (tx *Tx) Put(k flat.Key, v flat.Value) error { err := tx.tx.Set(k, v) - if err == badger.ErrConflict { + if errors.Is(err, badger.ErrConflict) { err = flat.ErrConflict } return err @@ -123,7 +124,7 @@ func (tx *Tx) Put(k flat.Key, v flat.Value) error { func (tx *Tx) Del(k flat.Key) error { err := tx.tx.Delete(k) - if err == badger.ErrConflict { + if errors.Is(err, badger.ErrConflict) { err = flat.ErrConflict } return err diff --git a/kv/flat/btree/btree.go b/kv/flat/btree/btree.go index 311bb0e..3cc5e76 100644 --- a/kv/flat/btree/btree.go +++ b/kv/flat/btree/btree.go @@ -17,6 +17,7 @@ package btree import ( "bytes" "context" + "errors" "io" "github.com/hidal-go/hidalgo/base" @@ -152,7 +153,7 @@ func (it *Iterator) WithPrefix(pref flat.Key) flat.Iterator { func (it *Iterator) next() bool { k, v, err := it.e.Next() - if err == io.EOF { + if errors.Is(err, io.EOF) { return false } else if !bytes.HasPrefix(k, it.pref) { return false diff --git a/kv/flat/helpers.go b/kv/flat/helpers.go index ff9215f..7a01249 100644 --- a/kv/flat/helpers.go +++ b/kv/flat/helpers.go @@ -1,6 +1,9 @@ package flat -import "context" +import ( + "context" + "errors" +) // Update is a helper to open a read-write transaction and update the database. // The update function may be called multiple times in case of conflicts with other writes. @@ -18,7 +21,7 @@ func Update(ctx context.Context, kv KV, update func(tx Tx) error) error { } return tx.Commit(ctx) }() - if err == ErrConflict { + if errors.Is(err, ErrConflict) { continue } else if err != nil { return err diff --git a/kv/flat/kv.go b/kv/flat/kv.go index b8500bb..19cf38e 100644 --- a/kv/flat/kv.go +++ b/kv/flat/kv.go @@ -3,6 +3,7 @@ package flat import ( "context" + "errors" "fmt" "github.com/hidal-go/hidalgo/base" @@ -82,7 +83,7 @@ func GetBatch(ctx context.Context, tx Getter, keys []Key) ([]Value, error) { var err error for i, k := range keys { vals[i], err = tx.Get(ctx, k) - if err == ErrNotFound { + if errors.Is(err, ErrNotFound) { vals[i] = nil } else if err != nil { return nil, err diff --git a/kv/flat/leveldb/leveldb.go b/kv/flat/leveldb/leveldb.go index 97a85d2..7fd4852 100644 --- a/kv/flat/leveldb/leveldb.go +++ b/kv/flat/leveldb/leveldb.go @@ -16,6 +16,7 @@ package leveldb import ( "context" + "errors" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/iterator" @@ -140,7 +141,7 @@ func (tx *Tx) Get(ctx context.Context, key flat.Key) (flat.Value, error) { } else { val, err = tx.sn.Get(key, tx.db.ro) } - if err == leveldb.ErrNotFound { + if errors.Is(err, leveldb.ErrNotFound) { return nil, flat.ErrNotFound } else if err != nil { return nil, err diff --git a/kv/flat/pebble/pebble.go b/kv/flat/pebble/pebble.go index 7143372..44c52bf 100644 --- a/kv/flat/pebble/pebble.go +++ b/kv/flat/pebble/pebble.go @@ -3,6 +3,7 @@ package pebble import ( "bytes" "context" + "errors" "github.com/cockroachdb/pebble" @@ -93,7 +94,7 @@ func (tx *Tx) Get(ctx context.Context, key flat.Key) (flat.Value, error) { return nil, flat.ErrNotFound } val, closer, err := tx.tx.Get(key) - if err == pebble.ErrNotFound { + if errors.Is(err, pebble.ErrNotFound) { return nil, flat.ErrNotFound } else if err != nil { return nil, err diff --git a/kv/helpers.go b/kv/helpers.go index 071d0d0..5d99edd 100644 --- a/kv/helpers.go +++ b/kv/helpers.go @@ -1,6 +1,9 @@ package kv -import "context" +import ( + "context" + "errors" +) // Update is a helper to open a read-write transaction and update the database. // The update function may be called multiple times in case of conflicts with other writes. @@ -18,7 +21,7 @@ func Update(ctx context.Context, kv KV, update func(tx Tx) error) error { } return tx.Commit(ctx) }() - if err == ErrConflict { + if errors.Is(err, ErrConflict) { continue } else if err != nil { return err diff --git a/kv/kv.go b/kv/kv.go index e68d4e8..0bddebe 100644 --- a/kv/kv.go +++ b/kv/kv.go @@ -170,7 +170,7 @@ func GetBatch(ctx context.Context, tx Getter, keys []Key) ([]Value, error) { var err error for i, k := range keys { vals[i], err = tx.Get(ctx, k) - if err == ErrNotFound { + if errors.Is(err, ErrNotFound) { vals[i] = nil } else if err != nil { return nil, err diff --git a/kv/kvdebug/kvdebug.go b/kv/kvdebug/kvdebug.go index dac8c4d..21359e8 100644 --- a/kv/kvdebug/kvdebug.go +++ b/kv/kvdebug/kvdebug.go @@ -2,6 +2,7 @@ package kvdebug import ( "context" + "errors" "fmt" "log" "sync/atomic" @@ -148,7 +149,7 @@ func (tx *kvTX) Get(ctx context.Context, k kv.Key) (kv.Value, error) { v, err := tx.tx.Get(ctx, k) d := tx.kv atomic.AddInt64(&d.stats.Get.N, 1) - if err == kv.ErrNotFound { + if errors.Is(err, kv.ErrNotFound) { atomic.AddInt64(&d.stats.Get.Miss, 1) } else if err != nil { atomic.AddInt64(&d.stats.Errs, 1) diff --git a/legacy/nosql/elastic/elastic.go b/legacy/nosql/elastic/elastic.go index 9be66ef..411afa9 100644 --- a/legacy/nosql/elastic/elastic.go +++ b/legacy/nosql/elastic/elastic.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "errors" "fmt" "io" "strconv" @@ -589,7 +590,7 @@ func (it *Iterator) Next(ctx context.Context) bool { } else { it.i++ } - if it.err == io.EOF { + if errors.Is(it.err, io.EOF) { it.err = nil it.done = true } diff --git a/legacy/nosql/mongo/mongo.go b/legacy/nosql/mongo/mongo.go index 3087ba9..dbe9481 100644 --- a/legacy/nosql/mongo/mongo.go +++ b/legacy/nosql/mongo/mongo.go @@ -3,6 +3,7 @@ package mongo import ( "context" "encoding/base64" + "errors" "fmt" "net/url" "strings" @@ -362,7 +363,7 @@ func (db *DB) FindByKey(ctx context.Context, col string, key nosql.Key) (nosql.D res := c.c.FindOne(ctx, primitive.M{"_id": compKey(key)}) var m primitive.M - if err := res.Decode(&m); err == mongo.ErrNoDocuments { + if err := res.Decode(&m); errors.Is(err, mongo.ErrNoDocuments) { return nil, nosql.ErrNotFound } else if err != nil { return nil, err diff --git a/tuple/datastore/datastore.go b/tuple/datastore/datastore.go index 7b203ed..0f5b187 100644 --- a/tuple/datastore/datastore.go +++ b/tuple/datastore/datastore.go @@ -3,6 +3,7 @@ package datastore import ( "context" "encoding/hex" + "errors" "fmt" "time" @@ -62,7 +63,7 @@ func (t *tableInfo) Open(tx tuple.Tx) (tuple.Table, error) { func (s *TupleStore) Table(ctx context.Context, name string) (tuple.TableInfo, error) { var t tableObject err := s.c.Get(ctx, s.tableKey(name), &t) - if err == datastore.ErrNoSuchEntity { + if errors.Is(err, datastore.ErrNoSuchEntity) { return nil, tuple.ErrTableNotFound } else if err != nil { return nil, err @@ -166,8 +167,7 @@ func (tx *Tx) CreateTable(ctx context.Context, table tuple.Header) (tuple.Table, err := tx.Get(k, &t) if err == nil { return tuple.ErrTableExists - } else if err != nil && err != datastore.ErrNoSuchEntity { - return err + } else if !errors.Is(err, datastore.ErrNoSuchEntity) { } t = tableObject{Data: data} _, err = tx.Put(k, &t) @@ -438,7 +438,7 @@ func (tbl *Table) GetTuple(ctx context.Context, key tuple.Key) (tuple.Data, erro p := &payload{h: &tbl.h} p.t.Key = key err := tbl.cli().Get(ctx, tbl.key(key, false), p) - if err == datastore.ErrNoSuchEntity { + if errors.Is(err, datastore.ErrNoSuchEntity) { return nil, tuple.ErrNotFound } else if err != nil { return nil, err @@ -633,7 +633,7 @@ func (it *Iterator) Next(ctx context.Context) bool { } func (it *Iterator) Err() error { - if it.err == iterator.Done { + if errors.Is(it.err, iterator.Done) { return nil } return it.err diff --git a/tuple/kv/downgrade.go b/tuple/kv/downgrade.go index 2170ddb..e33d6bc 100644 --- a/tuple/kv/downgrade.go +++ b/tuple/kv/downgrade.go @@ -2,6 +2,7 @@ package tuplekv import ( "context" + "errors" "fmt" "github.com/hidal-go/hidalgo/filter" @@ -17,7 +18,7 @@ func NewKV(ctx context.Context, db tuple.Store, table string) (flat.KV, error) { } defer tx.Close() _, err = tx.Table(ctx, table) - if err == tuple.ErrTableNotFound { + if errors.Is(err, tuple.ErrTableNotFound) { _, err = tx.CreateTable(ctx, tuple.Header{ Name: table, Key: []tuple.KeyField{ @@ -97,7 +98,7 @@ func flatData(b flat.Value) tuple.Data { func (tx *flatTx) Get(ctx context.Context, key flat.Key) (flat.Value, error) { row, err := tx.tbl.GetTuple(ctx, flatKey(key)) - if err == tuple.ErrNotFound { + if errors.Is(err, tuple.ErrNotFound) { return nil, flat.ErrNotFound } else if err != nil { return nil, err diff --git a/tuple/kv/upgrade.go b/tuple/kv/upgrade.go index 6c6891d..ad3b713 100644 --- a/tuple/kv/upgrade.go +++ b/tuple/kv/upgrade.go @@ -3,6 +3,7 @@ package tuplekv import ( "context" "encoding/binary" + "errors" "fmt" "io" @@ -19,13 +20,14 @@ func New(kv kv.KV) tuple.Store { } func tupleErr(err error) error { - switch err { - case kv.ErrNotFound: + switch { + case errors.Is(err, kv.ErrNotFound): return tuple.ErrNotFound - case kv.ErrReadOnly: + case errors.Is(err, kv.ErrReadOnly): return tuple.ErrReadOnly + default: + return err } - return err } type tupleStore struct { @@ -71,7 +73,7 @@ func (db *tupleStore) tableAuto(name string) kv.Key { func (db *tupleStore) tableWith(ctx context.Context, tx kv.Tx, name string) (*tupleTableInfo, error) { // TODO: cache data, err := tx.Get(ctx, db.tableSchema(name)) - if err == kv.ErrNotFound { + if errors.Is(err, kv.ErrNotFound) { return nil, tuple.ErrTableNotFound } else if err != nil { return nil, tupleErr(err) @@ -195,7 +197,7 @@ func (tx *tupleTx) CreateTable(ctx context.Context, table tuple.Header) (tuple.T _, err := tx.tx.Get(ctx, key) if err == nil { return nil, tuple.ErrExists - } else if err != nil && err != kv.ErrNotFound { + } else if !errors.Is(err, kv.ErrNotFound) { return nil, tupleErr(err) } data, err := tuplepb.MarshalTable(&table) @@ -349,7 +351,7 @@ func (tbl *tupleTable) GetTuple(ctx context.Context, key tuple.Key) (tuple.Data, return nil, err } data, err := tbl.tx.tx.Get(ctx, tbl.row(key)) - if err == kv.ErrNotFound { + if errors.Is(err, kv.ErrNotFound) { return nil, tuple.ErrNotFound } else if err != nil { return nil, tupleErr(err) @@ -386,7 +388,7 @@ func (tbl *tupleTable) GetTupleBatch(ctx context.Context, key []tuple.Key) ([]tu func (tbl *tupleTable) nextAuto(ctx context.Context) (tuple.Key, error) { key := tbl.auto() v, err := tbl.tx.tx.Get(ctx, key) - if err == kv.ErrNotFound { + if errors.Is(err, kv.ErrNotFound) { // first - set to 0 v = kv.Value{0} } else if err != nil { @@ -424,7 +426,7 @@ func (tbl *tupleTable) InsertTuple(ctx context.Context, t tuple.Tuple) (tuple.Ke _, err := tbl.tx.tx.Get(ctx, key) if err == nil { return nil, tuple.ErrExists - } else if err != nil && err != kv.ErrNotFound { + } else if !errors.Is(err, kv.ErrNotFound) { return nil, tupleErr(err) } val, err := tbl.encodeTuple(t.Data) @@ -450,7 +452,7 @@ func (tbl *tupleTable) UpdateTuple(ctx context.Context, t tuple.Tuple, opt *tupl } if !opt.Upsert { _, err := tbl.tx.tx.Get(ctx, key) - if err == kv.ErrNotFound { + if errors.Is(err, kv.ErrNotFound) { return tuple.ErrNotFound } else if err != nil { return tupleErr(err) diff --git a/tuple/sql/sql.go b/tuple/sql/sql.go index 9b4ee85..839664f 100644 --- a/tuple/sql/sql.go +++ b/tuple/sql/sql.go @@ -3,6 +3,7 @@ package sqltuple import ( "context" "database/sql" + "errors" "fmt" "log" "strconv" @@ -64,8 +65,7 @@ func (s *sqlStore) convError(err error) error { if s.dia.Errors != nil { err = s.dia.Errors(err) } - switch err { - case ErrTableNotFound: + if errors.Is(err, ErrTableNotFound) { return tuple.ErrTableNotFound } return err @@ -567,7 +567,7 @@ func (tbl *sqlTable) GetTuple(ctx context.Context, key tuple.Key) (tuple.Data, e b.Write(" LIMIT 1") row := tbl.tx.db.queryRow(ctx, tbl.tx.tx, b.String(), b.Args()...) data, err := tbl.scanPayload(row) - if err == sql.ErrNoRows { + if errors.Is(err, sql.ErrNoRows) { return nil, tuple.ErrNotFound } else if err != nil { return nil, err From dbde17256def4c1046faf3c51c15de2ebdb8093b Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:52:11 +0200 Subject: [PATCH 17/26] Use fmt.Errorf("%w",err) --- legacy/nosql/couch/ouch.go | 2 +- legacy/nosql/elastic/elastic.go | 2 +- tuple/kv/upgrade.go | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/legacy/nosql/couch/ouch.go b/legacy/nosql/couch/ouch.go index 4f57567..9a56c1e 100644 --- a/legacy/nosql/couch/ouch.go +++ b/legacy/nosql/couch/ouch.go @@ -45,7 +45,7 @@ func Dial(create bool, driver, addr, ns string, _ nosql.Options) (*DB, error) { client, dbName, err := DialDriver(ctx, driver, addr, ns) if err != nil { - return nil, fmt.Errorf("cannot open driver: %v", err) + return nil, fmt.Errorf("cannot open driver: %w", err) } var db *kivik.DB diff --git a/legacy/nosql/elastic/elastic.go b/legacy/nosql/elastic/elastic.go index 411afa9..9fa5a8c 100644 --- a/legacy/nosql/elastic/elastic.go +++ b/legacy/nosql/elastic/elastic.go @@ -63,7 +63,7 @@ func Dial(addr, index string, opt nosql.Options) (*DB, error) { } major, err := strconv.Atoi(strings.SplitN(vers, ".", 2)[0]) if err != nil { - return nil, fmt.Errorf("cannot parse version: %v", err) + return nil, fmt.Errorf("cannot parse version: %w", err) } ind := opt.GetString("index", index) if err != nil { diff --git a/tuple/kv/upgrade.go b/tuple/kv/upgrade.go index ad3b713..f891527 100644 --- a/tuple/kv/upgrade.go +++ b/tuple/kv/upgrade.go @@ -293,7 +293,7 @@ func (tbl *tupleTable) decodeKey(key kv.Key) (tuple.Key, error) { v := f.Type.NewSortable() err := v.UnmarshalSortable(key[i]) if err != nil { - return nil, fmt.Errorf("cannot decode tuple key: %v", err) + return nil, fmt.Errorf("cannot decode tuple key: %w", err) } row[i] = v.Sortable() } @@ -328,7 +328,7 @@ func (tbl *tupleTable) decodeTuple(data kv.Value) (tuple.Data, error) { sz, n := binary.Uvarint(data) data = data[n:] if n == 0 { - return nil, fmt.Errorf("cannot decode tuple data: %v", io.ErrUnexpectedEOF) + return nil, fmt.Errorf("cannot decode tuple data: %w", io.ErrUnexpectedEOF) } else if sz > uint64(len(data)) { return nil, fmt.Errorf("invalid tuple field size: %d vs %d", sz, len(data)) } @@ -339,7 +339,7 @@ func (tbl *tupleTable) decodeTuple(data kv.Value) (tuple.Data, error) { v := f.Type.New() err := v.UnmarshalBinary(head) if err != nil { - return nil, fmt.Errorf("cannot decode tuple field: %v", err) + return nil, fmt.Errorf("cannot decode tuple field: %w", err) } row[i] = v.Value() } From 4882c639285d2874609a1f0b8ee38a87f71436e5 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:54:28 +0200 Subject: [PATCH 18/26] Replace incorrect err check by TODO FIXME comment --- legacy/nosql/elastic/elastic.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/legacy/nosql/elastic/elastic.go b/legacy/nosql/elastic/elastic.go index 9fa5a8c..9ebf962 100644 --- a/legacy/nosql/elastic/elastic.go +++ b/legacy/nosql/elastic/elastic.go @@ -66,9 +66,7 @@ func Dial(addr, index string, opt nosql.Options) (*DB, error) { return nil, fmt.Errorf("cannot parse version: %w", err) } ind := opt.GetString("index", index) - if err != nil { - return nil, err - } + // TODO FIXME: Check returned value from opt.GetString(). settings := `{ "number_of_shards":1, "number_of_replicas":0 From f4d1c5aa3f5ea93f602f0c1c91a32fc1dfb03b08 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 17:40:50 +0200 Subject: [PATCH 19/26] Preallocate slides --- kv/kvtest/kvtest.go | 8 ++++---- legacy/nosql/elastic/test/elastictest.go | 2 +- legacy/nosql/nosqltest/nosqltest.go | 11 +++++++---- legacy/nosql/nosqltest/registry.go | 2 +- tuple/sql/mysql/test/mysqltest.go | 2 +- tuple/sql/postgres/test/postgrestest.go | 2 +- tuple/sql/sqltest/registry.go | 2 +- tuple/tupletest/tupletest.go | 1 + 8 files changed, 17 insertions(+), 13 deletions(-) diff --git a/kv/kvtest/kvtest.go b/kv/kvtest/kvtest.go index 9eefe33..55d9606 100644 --- a/kv/kvtest/kvtest.go +++ b/kv/kvtest/kvtest.go @@ -95,12 +95,12 @@ func basic(tb testing.TB, db kv.KV) { td.NotExists(k) } - var all []kv.Pair + all := make([]kv.Pair, len(keys)) for i, k := range keys { v := kv.Value(strconv.Itoa(i)) td.Put(k, v) td.Expect(k, v) - all = append(all, kv.Pair{Key: k, Val: v}) + all[i] = kv.Pair{Key: k, Val: v} } td.ScanReset(all) @@ -157,11 +157,11 @@ func seek(tb testing.TB, db kv.KV) { {[]byte("c")}, } - var all []kv.Pair + all := make([]kv.Pair, len(keys)) for i, k := range keys { v := kv.Value(strconv.Itoa(i)) td.Put(k, v) - all = append(all, kv.Pair{Key: k, Val: v}) + all[i] = kv.Pair{Key: k, Val: v} } tx, err := db.Tx(false) diff --git a/legacy/nosql/elastic/test/elastictest.go b/legacy/nosql/elastic/test/elastictest.go index dfedeeb..4558530 100644 --- a/legacy/nosql/elastic/test/elastictest.go +++ b/legacy/nosql/elastic/test/elastictest.go @@ -17,7 +17,7 @@ var versions = []string{ } func init() { - var vers []nosqltest.Version + vers := make([]nosqltest.Version, 0, len(versions)) for _, v := range versions { vers = append(vers, nosqltest.Version{ Name: v, Factory: ElasticVersion(v), diff --git a/legacy/nosql/nosqltest/nosqltest.go b/legacy/nosql/nosqltest/nosqltest.go index 466589e..360c4d5 100644 --- a/legacy/nosql/nosqltest/nosqltest.go +++ b/legacy/nosql/nosqltest/nosqltest.go @@ -295,8 +295,11 @@ func iterateExpect(tb testing.TB, kt keyType, qu nosql.Query, exp []nosql.Docume return sorter.Less(exp[i], exp[j]) }) var expKeys []nosql.Key - for _, d := range exp { - expKeys = append(expKeys, sorter.Key(d)) + if len(exp) > 0 { + expKeys = make([]nosql.Key, 0, len(exp)) + for _, d := range exp { + expKeys = append(expKeys, sorter.Key(d)) + } } sort.Sort(docsAndKeys{ @@ -366,7 +369,7 @@ func testInsert(t *testing.T, c tableConf) { } } - var docs []nosql.Document + docs := make([]nosql.Document, 0, len(ins)) for _, in := range ins { doc, err := c.FindByKey(in.Key) require.NoError(t, err, "find %#v", in.Key) @@ -417,7 +420,7 @@ func testUpdate(t *testing.T, c tableConf) { "n": nosql.Int(2), }, } - var keys []nosql.Key + keys := make([]nosql.Key, 0, len(docs)) for range docs { keys = append(keys, c.kt.Gen()) } diff --git a/legacy/nosql/nosqltest/registry.go b/legacy/nosql/nosqltest/registry.go index 137b4ab..9076faf 100644 --- a/legacy/nosql/nosqltest/registry.go +++ b/legacy/nosql/nosqltest/registry.go @@ -63,7 +63,7 @@ func ByName(name string) *Registration { } func allNames() []string { - var names []string + names := make([]string, 0, len(registry)) for name := range registry { names = append(names, name) } diff --git a/tuple/sql/mysql/test/mysqltest.go b/tuple/sql/mysql/test/mysqltest.go index 2724f08..4e75522 100644 --- a/tuple/sql/mysql/test/mysqltest.go +++ b/tuple/sql/mysql/test/mysqltest.go @@ -15,7 +15,7 @@ var versions = []string{ } func init() { - var vers []sqltest.Version + vers := make([]sqltest.Version, 0, len(versions)) for _, v := range versions { vers = append(vers, sqltest.Version{ Name: v, Factory: MySQLVersion(v), diff --git a/tuple/sql/postgres/test/postgrestest.go b/tuple/sql/postgres/test/postgrestest.go index 724377a..1579cc4 100644 --- a/tuple/sql/postgres/test/postgrestest.go +++ b/tuple/sql/postgres/test/postgrestest.go @@ -15,7 +15,7 @@ var versions = []string{ } func init() { - var vers []sqltest.Version + vers := make([]sqltest.Version, 0, len(versions)) for _, v := range versions { vers = append(vers, sqltest.Version{ Name: v, Factory: PostgresVersion(v), diff --git a/tuple/sql/sqltest/registry.go b/tuple/sql/sqltest/registry.go index e975940..c6d8863 100644 --- a/tuple/sql/sqltest/registry.go +++ b/tuple/sql/sqltest/registry.go @@ -63,7 +63,7 @@ func ByName(name string) *Registration { } func allNames() []string { - var names []string + names := make([]string, 0, len(registry)) for name := range registry { names = append(names, name) } diff --git a/tuple/tupletest/tupletest.go b/tuple/tupletest/tupletest.go index 35fbabb..8240314 100644 --- a/tuple/tupletest/tupletest.go +++ b/tuple/tupletest/tupletest.go @@ -125,6 +125,7 @@ func typed(t *testing.T, db tuple.Store) { // FIXME: test nanoseconds on backends that support it values.AsTime(time.Unix(123, 456789000)), } + var payloads []values.Value for _, tp := range sortable { payloads = append(payloads, tp) From aedff3c2735f33511818770702553e046d7afd2c Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 21:13:45 +0200 Subject: [PATCH 20/26] Simplify DB.EnsureIndex() --- legacy/nosql/elastic/elastic.go | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/legacy/nosql/elastic/elastic.go b/legacy/nosql/elastic/elastic.go index 9ebf962..433f3ca 100644 --- a/legacy/nosql/elastic/elastic.go +++ b/legacy/nosql/elastic/elastic.go @@ -163,13 +163,8 @@ func (db *DB) EnsureIndex(ctx context.Context, typ string, primary nosql.Index, if _, ok := props[f]; ok { continue } - var typ indType - switch ind.Type { - case nosql.StringExact: - typ = indKeyword - } - if typ != "" { - props[f] = property{Type: typ} + if ind.Type == nosql.StringExact { + props[f] = property{Type: indKeyword} } } } From e721d54580c7daf637cbb937954bc765ec9ceb18 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 19:56:31 +0200 Subject: [PATCH 21/26] Unshadow "err" variables by renaming it to "er" --- legacy/nosql/elastic/test/elastictest.go | 10 +++++----- legacy/nosql/mongo/test/mongotest.go | 13 ++++++------- legacy/nosql/nosqltest/nosqltest.go | 8 ++++---- tuple/datastore/datastore.go | 14 +++++++++----- 4 files changed, 24 insertions(+), 21 deletions(-) diff --git a/legacy/nosql/elastic/test/elastictest.go b/legacy/nosql/elastic/test/elastictest.go index 4558530..bf26fcb 100644 --- a/legacy/nosql/elastic/test/elastictest.go +++ b/legacy/nosql/elastic/test/elastictest.go @@ -53,12 +53,12 @@ func ElasticVersion(vers string) nosqltest.Database { ctx := context.Background() err = pool.Retry(func() error { - cli, err := edriver.NewClient(edriver.SetURL(addr)) - if err != nil { - return err + cli, er := edriver.NewClient(edriver.SetURL(addr)) + if er != nil { + return er } - _, _, err = cli.Ping(addr).Do(ctx) - return err + _, _, er = cli.Ping(addr).Do(ctx) + return er }) if err != nil { tb.Fatal(err) diff --git a/legacy/nosql/mongo/test/mongotest.go b/legacy/nosql/mongo/test/mongotest.go index ef2a217..05ccbad 100644 --- a/legacy/nosql/mongo/test/mongotest.go +++ b/legacy/nosql/mongo/test/mongotest.go @@ -41,16 +41,15 @@ func MongoVersion(vers string) nosqltest.Database { addr := fmt.Sprintf("mongodb://%s", cont.GetHostPort("27017/tcp")) err = pool.Retry(func() error { - sess, err := gomongo.NewClient(options.Client().ApplyURI(addr)) - if err != nil { - return err + sess, er := gomongo.NewClient(options.Client().ApplyURI(addr)) + if er != nil { + return er } defer sess.Disconnect(context.TODO()) - err = sess.Connect(context.TODO()) - - if err != nil { - return err + er = sess.Connect(context.TODO()) + if er != nil { + return er } return nil }) diff --git a/legacy/nosql/nosqltest/nosqltest.go b/legacy/nosql/nosqltest/nosqltest.go index 360c4d5..235a758 100644 --- a/legacy/nosql/nosqltest/nosqltest.go +++ b/legacy/nosql/nosqltest/nosqltest.go @@ -359,8 +359,8 @@ func testInsert(t *testing.T, c tableConf) { } for i := range ins { in := &ins[i] - k, err := c.Insert(in.Key, in.Doc) - require.NoError(t, err) + k, er := c.Insert(in.Key, in.Doc) + require.NoError(t, er) if in.Key == nil { require.NotNil(t, k) in.Key = k @@ -371,8 +371,8 @@ func testInsert(t *testing.T, c tableConf) { docs := make([]nosql.Document, 0, len(ins)) for _, in := range ins { - doc, err := c.FindByKey(in.Key) - require.NoError(t, err, "find %#v", in.Key) + doc, er := c.FindByKey(in.Key) + require.NoError(t, er, "find %#v", in.Key) c.fixDoc(in.Key, in.Doc) require.Equal(t, in.Doc, doc, "got: %#v", doc) docs = append(docs, in.Doc) diff --git a/tuple/datastore/datastore.go b/tuple/datastore/datastore.go index 0f5b187..f2aa621 100644 --- a/tuple/datastore/datastore.go +++ b/tuple/datastore/datastore.go @@ -164,10 +164,11 @@ func (tx *Tx) CreateTable(ctx context.Context, table tuple.Header) (tuple.Table, k := tx.s.tableKey(table.Name) _, err = tx.s.c.RunInTransaction(ctx, func(tx *datastore.Transaction) error { var t tableObject - err := tx.Get(k, &t) - if err == nil { + er := tx.Get(k, &tableObject{}) + if er == nil { return tuple.ErrTableExists - } else if !errors.Is(err, datastore.ErrNoSuchEntity) { + } else if !errors.Is(er, datastore.ErrNoSuchEntity) { + return er } t = tableObject{Data: data} _, err = tx.Put(k, &t) @@ -482,7 +483,8 @@ func (tbl *Table) InsertTuple(ctx context.Context, t tuple.Tuple) (tuple.Key, er return nil, err } k := tbl.key(t.Key, true) - if err := tx.Get(k, &payload{h: &tbl.h}); err == nil { + + if er := tx.Get(k, &payload{h: &tbl.h}); er == nil { _ = tx.Rollback() return nil, tuple.ErrExists } @@ -522,7 +524,9 @@ func (tbl *Table) UpdateTuple(ctx context.Context, t tuple.Tuple, opt *tuple.Upd return err } k := tbl.key(t.Key, false) - if err := tx.Get(k, &payload{h: &tbl.h}); err == datastore.ErrNoSuchEntity { + + err = tx.Get(k, &payload{h: &tbl.h}) + if errors.Is(err, datastore.ErrNoSuchEntity) { return tuple.ErrNotFound } _, err = tx.Put(k, &payload{h: &tbl.h, t: t}) From 67b8fb57beec003ed00b70088863f25a60d182f7 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 21:21:42 +0200 Subject: [PATCH 22/26] Nest "err" varible within if condition --- tuple/datastore/datastore.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tuple/datastore/datastore.go b/tuple/datastore/datastore.go index f2aa621..60a6ea4 100644 --- a/tuple/datastore/datastore.go +++ b/tuple/datastore/datastore.go @@ -78,8 +78,8 @@ func (s *TupleStore) Table(ctx context.Context, name string) (tuple.TableInfo, e func (s *TupleStore) ListTables(ctx context.Context) ([]tuple.TableInfo, error) { q := datastore.NewQuery(kindTable).Ancestor(s.metaRoot()) var tables []tableObject - _, err := s.c.GetAll(ctx, q, &tables) - if err != nil { + + if _, err := s.c.GetAll(ctx, q, &tables); err != nil { return nil, err } out := make([]tuple.TableInfo, 0, len(tables)) From ca24be5f83d824842027a393faef44a8a5d96094 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 21:37:51 +0200 Subject: [PATCH 23/26] Simplify code of datastore --- tuple/datastore/datastore.go | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/tuple/datastore/datastore.go b/tuple/datastore/datastore.go index 60a6ea4..14963e8 100644 --- a/tuple/datastore/datastore.go +++ b/tuple/datastore/datastore.go @@ -163,15 +163,13 @@ func (tx *Tx) CreateTable(ctx context.Context, table tuple.Header) (tuple.Table, } k := tx.s.tableKey(table.Name) _, err = tx.s.c.RunInTransaction(ctx, func(tx *datastore.Transaction) error { - var t tableObject er := tx.Get(k, &tableObject{}) if er == nil { return tuple.ErrTableExists } else if !errors.Is(er, datastore.ErrNoSuchEntity) { return er } - t = tableObject{Data: data} - _, err = tx.Put(k, &t) + _, err = tx.Put(k, &tableObject{Data: data}) return err }) if err != nil { @@ -226,9 +224,8 @@ func (tbl *Table) Clear(ctx context.Context) error { } } -func (tbl *Table) key(key tuple.Key, auto bool) *datastore.Key { +func (tbl *Table) key(key tuple.Key, auto bool) (k *datastore.Key) { kind := tbl.h.Name - var k *datastore.Key for i, c := range tbl.h.Key { v := key[i] switch c.Type.(type) { @@ -254,19 +251,19 @@ func (tbl *Table) key(key tuple.Key, auto bool) *datastore.Key { } func (tbl *Table) parseKey(key *datastore.Key) (tuple.Key, error) { - k := make(tuple.Key, len(tbl.h.Key)) - for i := len(k) - 1; i >= 0; i-- { + keys := make(tuple.Key, len(tbl.h.Key)) + for i := len(keys) - 1; i >= 0; i-- { if key == nil { return nil, fmt.Errorf("short key") } c := tbl.h.Key[i] switch c.Type.(type) { case values.StringType: - k[i] = values.String(key.Name) + keys[i] = values.String(key.Name) case values.IntType: - k[i] = values.Int(key.ID) + keys[i] = values.Int(key.ID) case values.UIntType: - k[i] = values.UInt(key.ID) + keys[i] = values.UInt(key.ID) default: d, err := hex.DecodeString(key.Name) if err != nil { @@ -277,11 +274,11 @@ func (tbl *Table) parseKey(key *datastore.Key) (tuple.Key, error) { if err != nil { return nil, err } - k[i] = v.Sortable() + keys[i] = v.Sortable() } key = key.Parent } - return k, nil + return keys, nil } var _ datastore.PropertyLoadSaver = (*payload)(nil) @@ -397,8 +394,8 @@ func (p *payload) Save() ([]datastore.Property, error) { }) } for i, c := range p.h.Data { - v := p.t.Data[i] var val interface{} + v := p.t.Data[i] if v != nil { switch c.Type.(type) { case values.BytesType: From 3f6c5fcf52ab30c4fcc767ca4627f3e13cf6d251 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 21:41:43 +0200 Subject: [PATCH 24/26] Replace strings.Replace() by ReplaceAll() when possible --- tuple/sql/dialect.go | 4 ++-- tuple/sql/mysql/mysql.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tuple/sql/dialect.go b/tuple/sql/dialect.go index 8affac3..4f82fc6 100644 --- a/tuple/sql/dialect.go +++ b/tuple/sql/dialect.go @@ -69,12 +69,12 @@ func (d *Dialect) QuoteIdentifier(s string) string { if q := d.QuoteIdentifierFunc; q != nil { return q(s) } - return "`" + strings.Replace(s, "`", "", -1) + "`" + return "`" + strings.ReplaceAll(s, "`", "") + "`" } func (d *Dialect) QuoteString(s string) string { // only used when setting comments, so it's pretty naive - return "'" + strings.Replace(s, "'", "''", -1) + "'" + return "'" + strings.ReplaceAll(s, "'", "''") + "'" } func (d *Dialect) sqlType(t values.Type, key bool) string { diff --git a/tuple/sql/mysql/mysql.go b/tuple/sql/mysql/mysql.go index eaf7136..3d543ba 100644 --- a/tuple/sql/mysql/mysql.go +++ b/tuple/sql/mysql/mysql.go @@ -37,7 +37,7 @@ func init() { ListColumns: `SELECT column_name, column_type, is_nullable, column_key, column_comment FROM information_schema.columns WHERE table_schema = ? AND table_name = ?`, QuoteIdentifierFunc: func(s string) string { - return "`" + strings.Replace(s, "`", "", -1) + "`" + return "`" + strings.ReplaceAll(s, "`", "") + "`" }, ColumnCommentInline: func(s string) string { return "COMMENT " + s From 12e528363050fde7d850e88ae2df451fc845870b Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 26 Apr 2022 21:42:53 +0200 Subject: [PATCH 25/26] Reuse the wider scope variable "conn" --- tuple/sql/sqltest/sqltest.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tuple/sql/sqltest/sqltest.go b/tuple/sql/sqltest/sqltest.go index 1d062d5..b439c20 100644 --- a/tuple/sql/sqltest/sqltest.go +++ b/tuple/sql/sqltest/sqltest.go @@ -46,7 +46,7 @@ func TestSQL(t *testing.T, name string, gen Database) { tb.Cleanup(func() { conn.Close() if !recreate { - conn, err := sqltuple.OpenSQL(name, addr, "") + conn, err = sqltuple.OpenSQL(name, addr, "") if err == nil { _, err = conn.Exec(`DROP DATABASE ` + db) conn.Close() From 9afa72c8bb9019ce6ff2949d7d8bf03c26d2c253 Mon Sep 17 00:00:00 2001 From: Oliver <0uep@hessling.fr> Date: Tue, 22 Mar 2022 18:50:36 +0100 Subject: [PATCH 26/26] Avoid returning an interface when possible --- filter/filters.go | 2 +- kv/options/prefix_flat.go | 2 +- kv/options/prefix_kv.go | 2 +- tuple/sql/sql.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/filter/filters.go b/filter/filters.go index 4165dfb..71726fc 100644 --- a/filter/filters.go +++ b/filter/filters.go @@ -35,7 +35,7 @@ func (Any) ValuesRange() *Range { } // EQ is a shorthand for Equal. -func EQ(v values.Value) SortableFilter { +func EQ(v values.Value) Equal { return Equal{Value: v} } diff --git a/kv/options/prefix_flat.go b/kv/options/prefix_flat.go index 5d7ed21..2ff4f00 100644 --- a/kv/options/prefix_flat.go +++ b/kv/options/prefix_flat.go @@ -10,7 +10,7 @@ import ( // WithPrefixFlat returns IteratorOption that limits scanned key to a given binary prefix. // Store implementations can optimize this by implementing flat.PrefixIterator. -func WithPrefixFlat(pref flat.Key) IteratorOption { +func WithPrefixFlat(pref flat.Key) PrefixFlat { return PrefixFlat{Pref: pref} } diff --git a/kv/options/prefix_kv.go b/kv/options/prefix_kv.go index 0d9fcc7..ceddb94 100644 --- a/kv/options/prefix_kv.go +++ b/kv/options/prefix_kv.go @@ -9,7 +9,7 @@ import ( // WithPrefixKV returns IteratorOption that limits scanned key to a given binary prefix. // Store implementations can optimize this by implementing kv.PrefixIterator. -func WithPrefixKV(pref kv.Key) IteratorOption { +func WithPrefixKV(pref kv.Key) PrefixKV { return PrefixKV{Pref: pref} } diff --git a/tuple/sql/sql.go b/tuple/sql/sql.go index 839664f..b673761 100644 --- a/tuple/sql/sql.go +++ b/tuple/sql/sql.go @@ -765,7 +765,7 @@ func (tbl *sqlTable) DeleteTuples(ctx context.Context, f *tuple.Filter) error { return it.Err() } -func (tbl *sqlTable) scan(open rowsFunc, keysOnly bool, f *tuple.Filter) tuple.Iterator { +func (tbl *sqlTable) scan(open rowsFunc, keysOnly bool, f *tuple.Filter) *sqlIterator { return &sqlIterator{tbl: tbl, open: open, f: f, keysOnly: keysOnly} }