From f27f7b10558eab128a9af769e554933b0eaf6a5d Mon Sep 17 00:00:00 2001 From: luk3skyw4lker Date: Mon, 1 Jul 2024 08:58:54 -0300 Subject: [PATCH] fix: time.Time.IsZero returning false negative --- clickhouse/clickhouse.go | 86 ++++++++++++++++++----------------- clickhouse/clickhouse_test.go | 8 ++-- clickhouse/config.go | 5 +- clickhouse/go.sum | 2 + 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/clickhouse/clickhouse.go b/clickhouse/clickhouse.go index 4bd60eb2..5f982f3f 100644 --- a/clickhouse/clickhouse.go +++ b/clickhouse/clickhouse.go @@ -2,6 +2,8 @@ package clickhouse import ( "context" + "database/sql" + "errors" "fmt" "time" @@ -26,17 +28,15 @@ func New(configuration Config) (*Storage, error) { return nil, err } - ctx := driver.Context(context.Background(), driver.WithParameters(driver.Parameters{ - "table": configuration.Table, - })) + ctx := context.Background() + + queryWithEngine := fmt.Sprintf(createTableString, engine) + if err := conn.Exec(ctx, queryWithEngine, driver.Named("table", configuration.Table)); err != nil { + return &Storage{}, err + } if configuration.Clean { - queryWithEngine := fmt.Sprintf(createTableString, engine) - if err := conn.Exec(ctx, queryWithEngine); err != nil { - return &Storage{}, err - } - - if err := conn.Exec(ctx, resetDataString); err != nil { + if err := conn.Exec(ctx, resetDataString, driver.Named("table", configuration.Table)); err != nil { return &Storage{}, err } } @@ -63,59 +63,63 @@ func (s *Storage) Set(key string, value []byte, expiration time.Duration) error exp = time.Now().Add(expiration).UTC() } - ctx := driver.Context(s.context, driver.WithParameters(driver.Parameters{ - "key": key, - "value": string(value), - "expiration": exp.Format("2006-01-02 15:04:05"), - "table": s.table, - })) - - err := s. + return s. session. Exec( - ctx, + s.context, insertDataString, + driver.Named("table", s.table), + driver.Named("key", key), + driver.Named("value", string(value)), + driver.Named("expiration", exp.Format("2006-01-02 15:04:05")), ) - - return err } func (s *Storage) Get(key string) ([]byte, error) { - var resultSlice []schema + if len(key) == 0 { + return []byte{}, nil + } + + var result schema + + row := s.session.QueryRow( + s.context, + selectDataString, + driver.Named("table", s.table), + driver.Named("key", key), + ) + if row.Err() != nil { + return []byte{}, row.Err() + } + + if err := row.ScanStruct(&result); err != nil { + if errors.Is(err, sql.ErrNoRows) { + return []byte{}, nil + } - ctx := driver.Context(s.context, driver.WithParameters(driver.Parameters{ - "key": key, - "table": s.table, - })) - err := s.session.Select(ctx, &resultSlice, selectDataString) - if err != nil { return []byte{}, err } - if len(resultSlice) == 0 { + // The result.Expiration.IsZero() was returning a false value even when the time was + // set to be the zero value of the time.Time struct (Jan 1st 1970, 00:00:00 UTC) + // so we had to change the comparision + if !time.Unix(0, 0).Equal(result.Expiration) && result.Expiration.Before(time.Now().UTC()) { return []byte{}, nil } - result := resultSlice[0] - - if !result.Expiration.IsZero() && result.Expiration.Before(time.Now().UTC()) { - return []byte{}, nil - } - - return []byte(result.Value), err + return []byte(result.Value), nil } func (s *Storage) Delete(key string) error { - ctx := driver.Context(s.context, driver.WithParameters(driver.Parameters{ - "table": s.table, - "key": key, - })) + if len(key) == 0 { + return nil + } - return s.session.Exec(ctx, deleteDataString) + return s.session.Exec(s.context, deleteDataString, driver.Named("table", s.table), driver.Named("key", key)) } func (s *Storage) Reset() error { - return s.session.Exec(s.context, resetDataString) + return s.session.Exec(s.context, resetDataString, driver.Named("table", s.table)) } func (s *Storage) Close() error { diff --git a/clickhouse/clickhouse_test.go b/clickhouse/clickhouse_test.go index f56ddd5e..f1278ce2 100644 --- a/clickhouse/clickhouse_test.go +++ b/clickhouse/clickhouse_test.go @@ -47,7 +47,7 @@ func Test_Set(t *testing.T) { require.NoError(t, err) } -func Test_SetWithExp(t *testing.T) { +func Test_Set_With_Exp(t *testing.T) { client, err := getTestConnection(t, Config{ Host: "127.0.0.1", Port: 9001, @@ -83,7 +83,7 @@ func Test_Get(t *testing.T) { assert.Equal(t, "somevalue", string(value)) } -func Test_GetWithExp(t *testing.T) { +func Test_Get_With_Exp(t *testing.T) { client, err := getTestConnection(t, Config{ Host: "127.0.0.1", Port: 9001, @@ -94,7 +94,7 @@ func Test_GetWithExp(t *testing.T) { require.NoError(t, err) defer client.Close() - err = client.Set("getsomekeywithexp", []byte("somevalue"), time.Second*5) + err = client.Set("getsomekeywithexp", []byte("somevalue"), time.Second*2) require.NoError(t, err) value, err := client.Get("getsomekeywithexp") @@ -103,7 +103,7 @@ func Test_GetWithExp(t *testing.T) { assert.NotNil(t, value) assert.Equal(t, "somevalue", string(value)) - time.Sleep(time.Second * 10) + time.Sleep(time.Second * 5) value, err = client.Get("getsomekeywithexp") diff --git a/clickhouse/config.go b/clickhouse/config.go index 9b3aa28a..d9b820ce 100644 --- a/clickhouse/config.go +++ b/clickhouse/config.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "errors" "fmt" + "log" "time" driver "github.com/ClickHouse/clickhouse-go/v2" @@ -64,7 +65,7 @@ func defaultConfig(configuration Config) (driver.Options, ClickhouseEngine, erro } if configuration.Engine == "" { - configuration.Engine = "Memory" + configuration.Engine = Memory } config := driver.Options{ @@ -84,7 +85,7 @@ func defaultConfig(configuration Config) (driver.Options, ClickhouseEngine, erro } if configuration.Debug && config.Debugf == nil { - config.Debugf = func(format string, v ...any) { fmt.Printf(format, v...) } + config.Debugf = log.Printf } return config, configuration.Engine, nil diff --git a/clickhouse/go.sum b/clickhouse/go.sum index 41017a58..9a025ec3 100644 --- a/clickhouse/go.sum +++ b/clickhouse/go.sum @@ -1,5 +1,7 @@ github.com/ClickHouse/ch-go v0.61.5 h1:zwR8QbYI0tsMiEcze/uIMK+Tz1D3XZXLdNrlaOpeEI4= github.com/ClickHouse/ch-go v0.61.5/go.mod h1:s1LJW/F/LcFs5HJnuogFMta50kKDO0lf9zzfrbl0RQg= +github.com/ClickHouse/clickhouse-go/v2 v2.24.0 h1:L/n/pVVpk95KtkHOiKuSnO7cu2ckeW4gICbbOh5qs74= +github.com/ClickHouse/clickhouse-go/v2 v2.24.0/go.mod h1:iDTViXk2Fgvf1jn2dbJd1ys+fBkdD1UMRnXlwmhijhQ= github.com/ClickHouse/clickhouse-go/v2 v2.26.0 h1:j4/y6NYaCcFkJwN/TU700ebW+nmsIy34RmUAAcZKy9w= github.com/ClickHouse/clickhouse-go/v2 v2.26.0/go.mod h1:iDTViXk2Fgvf1jn2dbJd1ys+fBkdD1UMRnXlwmhijhQ= github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=