perf: query data AS OF SYSTEM TIME (#5231)

Queries the data in the storage layser at the timestamp when the call hit the API layer
This commit is contained in:
Silvan
2023-02-27 22:36:43 +01:00
committed by GitHub
parent 80003939ad
commit e38abdcdf3
170 changed files with 3101 additions and 3169 deletions

View File

@@ -2,6 +2,7 @@ package cockroach
import (
"database/sql"
"fmt"
"strconv"
"strings"
"time"
@@ -89,6 +90,15 @@ func (c *Config) Type() string {
return "cockroach"
}
func (c *Config) Timetravel(d time.Duration) string {
// verify that it is at least 1 micro second
if d < time.Microsecond {
d = time.Microsecond
}
return fmt.Sprintf(" AS OF SYSTEM TIME '-%d µs' ", d.Microseconds())
}
type User struct {
Username string
Password string

View File

@@ -0,0 +1,61 @@
package cockroach
import (
"testing"
"time"
)
func TestConfig_Timetravel(t *testing.T) {
type args struct {
d time.Duration
}
tests := []struct {
name string
args args
want string
}{
{
name: "no duration",
args: args{
d: 0,
},
want: " AS OF SYSTEM TIME '-1 µs' ",
},
{
name: "less than microsecond",
args: args{
d: 100 * time.Nanosecond,
},
want: " AS OF SYSTEM TIME '-1 µs' ",
},
{
name: "10 microseconds",
args: args{
d: 10 * time.Microsecond,
},
want: " AS OF SYSTEM TIME '-10 µs' ",
},
{
name: "10 milliseconds",
args: args{
d: 10 * time.Millisecond,
},
want: " AS OF SYSTEM TIME '-10000 µs' ",
},
{
name: "1 second",
args: args{
d: 1 * time.Second,
},
want: " AS OF SYSTEM TIME '-1000000 µs' ",
},
}
for _, tt := range tests {
c := &Config{}
t.Run(tt.name, func(t *testing.T) {
if got := c.Timetravel(tt.args.d); got != tt.want {
t.Errorf("Config.Timetravel() = %q, want %q", got, tt.want)
}
})
}
}

View File

@@ -19,7 +19,12 @@ func (c *Config) SetConnector(connector dialect.Connector) {
c.connector = connector
}
func Connect(config Config, useAdmin bool) (*sql.DB, error) {
type DB struct {
*sql.DB
dialect.Database
}
func Connect(config Config, useAdmin bool) (*DB, error) {
client, err := config.connector.Connect(useAdmin)
if err != nil {
return nil, err
@@ -29,7 +34,10 @@ func Connect(config Config, useAdmin bool) (*sql.DB, error) {
return nil, errors.ThrowPreconditionFailed(err, "DATAB-0pIWD", "Errors.Database.Connection.Failed")
}
return client, nil
return &DB{
DB: client,
Database: config.connector,
}, nil
}
func DecodeHook(from, to reflect.Value) (interface{}, error) {
@@ -61,7 +69,7 @@ func DecodeHook(from, to reflect.Value) (interface{}, error) {
return Config{connector: connector}, nil
}
func (c Config) Database() string {
func (c Config) DatabaseName() string {
return c.connector.DatabaseName()
}

View File

@@ -3,6 +3,7 @@ package dialect
import (
"database/sql"
"sync"
"time"
)
type Config struct {
@@ -29,10 +30,15 @@ type Matcher interface {
type Connector interface {
Connect(useAdmin bool) (*sql.DB, error)
Password() string
Database
}
type Database interface {
DatabaseName() string
Username() string
Password() string
Type() string
Timetravel(time.Duration) string
}
func Register(matcher Matcher, config Connector, isDefault bool) {

View File

@@ -89,6 +89,10 @@ func (c *Config) Type() string {
return "postgres"
}
func (c *Config) Timetravel(time.Duration) string {
return ""
}
type User struct {
Username string
Password string