Skip to content

Commit

Permalink
Use stringutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Mar 16, 2021
1 parent ae5a892 commit 86edffb
Show file tree
Hide file tree
Showing 28 changed files with 141 additions and 142 deletions.
7 changes: 3 additions & 4 deletions database/transaction.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package database

import (
"fmt"

"github.com/genjidb/genji/engine"
"github.com/genjidb/genji/stringutil"
)

var (
Expand Down Expand Up @@ -168,7 +167,7 @@ func (tx *Transaction) ReIndexAll() error {
func (tx *Transaction) getTableStore() *tableStore {
st, err := tx.tx.GetStore([]byte(tableInfoStoreName))
if err != nil {
panic(fmt.Sprintf("database incorrectly setup: missing %q table: %v", tableInfoStoreName, err))
panic(stringutil.Sprintf("database incorrectly setup: missing %q table: %v", tableInfoStoreName, err))
}

return &tableStore{
Expand All @@ -180,7 +179,7 @@ func (tx *Transaction) getTableStore() *tableStore {
func (tx *Transaction) getIndexStore() *indexStore {
st, err := tx.tx.GetStore([]byte(indexStoreName))
if err != nil {
panic(fmt.Sprintf("database incorrectly setup: missing %q table: %v", indexStoreName, err))
panic(stringutil.Sprintf("database incorrectly setup: missing %q table: %v", indexStoreName, err))
}

return &indexStore{
Expand Down
14 changes: 7 additions & 7 deletions document/encoding/encodingtest/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package encodingtest
import (
"bytes"
"encoding/json"
"fmt"
"testing"

"github.com/genjidb/genji/document"
"github.com/genjidb/genji/document/encoding"
"github.com/genjidb/genji/stringutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -36,7 +36,7 @@ func benchmarkEncodeDocument(b *testing.B, codecBuilder func() encoding.Codec) {
var fb document.FieldBuffer

for i := int64(0); i < 100; i++ {
fb.Add(fmt.Sprintf("name-%d", i), document.NewIntegerValue(i))
fb.Add(stringutil.Sprintf("name-%d", i), document.NewIntegerValue(i))
}

codec := codecBuilder()
Expand All @@ -54,7 +54,7 @@ func benchmarkDocumentGetByField(b *testing.B, codecBuilder func() encoding.Code
var fb document.FieldBuffer

for i := int64(0); i < 100; i++ {
fb.Add(fmt.Sprintf("name-%d", i), document.NewIntegerValue(i))
fb.Add(stringutil.Sprintf("name-%d", i), document.NewIntegerValue(i))
}

codec := codecBuilder()
Expand All @@ -72,7 +72,7 @@ func benchmarkDocumentIterate(b *testing.B, codecBuilder func() encoding.Codec)
var fb document.FieldBuffer

for i := int64(0); i < 100; i++ {
fb.Add(fmt.Sprintf("name-%d", i), document.NewIntegerValue(i))
fb.Add(stringutil.Sprintf("name-%d", i), document.NewIntegerValue(i))
}

codec := codecBuilder()
Expand All @@ -94,7 +94,7 @@ func benchmarkDecodeDocument(b *testing.B, codecBuilder func() encoding.Codec) {
var fb document.FieldBuffer

for i := int64(0); i < 100; i++ {
fb.Add(fmt.Sprintf("name-%d", i), document.NewIntegerValue(i))
fb.Add(stringutil.Sprintf("name-%d", i), document.NewIntegerValue(i))
}

codec := codecBuilder()
Expand All @@ -115,7 +115,7 @@ func benchmarkEncodeDocumentJSON(b *testing.B, codecBuilder func() encoding.Code
m := make(map[string]int64)

for i := int64(0); i < 100; i++ {
m[fmt.Sprintf("name-%d", i)] = i
m[stringutil.Sprintf("name-%d", i)] = i
}

b.ResetTimer()
Expand All @@ -128,7 +128,7 @@ func benchmarkDecodeDocumentJSON(b *testing.B, codecBuilder func() encoding.Code
m := make(map[string]int64)

for i := int64(0); i < 100; i++ {
m[fmt.Sprintf("name-%d", i)] = i
m[stringutil.Sprintf("name-%d", i)] = i
}

d, err := json.Marshal(m)
Expand Down
4 changes: 2 additions & 2 deletions document/encoding/msgpack/codec.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package msgpack

import (
"fmt"
"io"

"github.com/genjidb/genji/document"
"github.com/genjidb/genji/document/encoding"
"github.com/genjidb/genji/stringutil"
"github.com/vmihailenco/msgpack/v5"
"github.com/vmihailenco/msgpack/v5/msgpcode"
)
Expand Down Expand Up @@ -246,7 +246,7 @@ func (d *Decoder) DecodeValue() (v document.Value, err error) {
return
}

panic(fmt.Sprintf("unsupported type %v", c))
panic(stringutil.Sprintf("unsupported type %v", c))
}

// DecodeDocument decodes one document from the reader.
Expand Down
3 changes: 1 addition & 2 deletions document/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package document

import (
"errors"
"fmt"
"reflect"
"strings"
"time"
Expand All @@ -31,7 +30,7 @@ func Scan(d Document, targets ...interface{}) error {

ref := reflect.ValueOf(target)
if !ref.IsValid() {
return &ErrUnsupportedType{target, fmt.Sprintf("Parameter %d is not valid", i)}
return &ErrUnsupportedType{target, stringutil.Sprintf("Parameter %d is not valid", i)}
}

return scanValue(v, ref)
Expand Down
10 changes: 5 additions & 5 deletions document/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"math"
"strconv"

"github.com/buger/jsonparser"
"github.com/genjidb/genji/binarysort"
"github.com/genjidb/genji/stringutil"
)

var (
Expand All @@ -29,7 +29,7 @@ type ErrUnsupportedType struct {
}

func (e *ErrUnsupportedType) Error() string {
return fmt.Sprintf("unsupported type %T. %s", e.Value, e.Msg)
return stringutil.Sprintf("unsupported type %T. %s", e.Value, e.Msg)
}

// ValueType represents a value type supported by the database.
Expand Down Expand Up @@ -295,7 +295,7 @@ func (v Value) String() string {
case TextValue:
return strconv.Quote(v.V.(string))
case BlobValue:
return fmt.Sprintf("%v", v.V)
return stringutil.Sprintf("%v", v.V)
}

d, _ := v.MarshalJSON()
Expand Down Expand Up @@ -547,7 +547,7 @@ func calculateIntegers(a, b Value, operator byte) (res Value, err error) {
case '^':
return NewIntegerValue(xa ^ xb), nil
default:
panic(fmt.Sprintf("unknown operator %c", operator))
panic(stringutil.Sprintf("unknown operator %c", operator))
}
}

Expand Down Expand Up @@ -597,7 +597,7 @@ func calculateFloats(a, b Value, operator byte) (res Value, err error) {
ia, ib := int64(xa), int64(xb)
return NewIntegerValue(ia ^ ib), nil
default:
panic(fmt.Sprintf("unknown operator %c", operator))
panic(stringutil.Sprintf("unknown operator %c", operator))
}
}

Expand Down
10 changes: 5 additions & 5 deletions engine/enginetest/benchmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package enginetest

import (
"bytes"
"fmt"
"testing"

"github.com/genjidb/genji/engine"
"github.com/genjidb/genji/stringutil"
"github.com/stretchr/testify/require"
)

Expand All @@ -14,14 +14,14 @@ func BenchmarkStorePut(b *testing.B, builder Builder) {
v := bytes.Repeat([]byte("v"), 512)

for size := 1; size <= 10000; size *= 10 {
b.Run(fmt.Sprintf("%.05d", size), func(b *testing.B) {
b.Run(stringutil.Sprintf("%.05d", size), func(b *testing.B) {
for i := 0; i < b.N; i++ {
st, cleanup := storeBuilder(b, builder)
defer cleanup()

b.ResetTimer()
for j := 0; j < size; j++ {
k := []byte(fmt.Sprintf("k%d", j))
k := []byte(stringutil.Sprintf("k%d", j))
st.Put(k, v)
}
b.StopTimer()
Expand All @@ -33,14 +33,14 @@ func BenchmarkStorePut(b *testing.B, builder Builder) {
// BenchmarkStoreScan benchmarks the AscendGreaterOrEqual method with 1, 10, 1000 and 10000 successive insertions.
func BenchmarkStoreScan(b *testing.B, builder Builder) {
for size := 1; size <= 10000; size *= 10 {
b.Run(fmt.Sprintf("%.05d", size), func(b *testing.B) {
b.Run(stringutil.Sprintf("%.05d", size), func(b *testing.B) {
st, cleanup := storeBuilder(b, builder)
defer cleanup()

v := bytes.Repeat([]byte("v"), 512)

for i := 0; i < size; i++ {
k := []byte(fmt.Sprintf("k%d", i))
k := []byte(stringutil.Sprintf("k%d", i))
err := st.Put(k, v)
require.NoError(b, err)
}
Expand Down
19 changes: 9 additions & 10 deletions expr/arithmeric.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package expr

import (
"fmt"

"github.com/genjidb/genji/document"
"github.com/genjidb/genji/sql/scanner"
"github.com/genjidb/genji/stringutil"
)

// IsArithmeticOperator returns true if e is one of
Expand Down Expand Up @@ -38,7 +37,7 @@ func (op addOp) Eval(env *Environment) (document.Value, error) {
}

func (op addOp) String() string {
return fmt.Sprintf("%v + %v", op.a, op.b)
return stringutil.Sprintf("%v + %v", op.a, op.b)
}

type subOp struct {
Expand All @@ -60,7 +59,7 @@ func (op subOp) Eval(env *Environment) (document.Value, error) {
}

func (op subOp) String() string {
return fmt.Sprintf("%v - %v", op.a, op.b)
return stringutil.Sprintf("%v - %v", op.a, op.b)
}

type mulOp struct {
Expand All @@ -82,7 +81,7 @@ func (op mulOp) Eval(env *Environment) (document.Value, error) {
}

func (op mulOp) String() string {
return fmt.Sprintf("%v * %v", op.a, op.b)
return stringutil.Sprintf("%v * %v", op.a, op.b)
}

type divOp struct {
Expand All @@ -104,7 +103,7 @@ func (op divOp) Eval(env *Environment) (document.Value, error) {
}

func (op divOp) String() string {
return fmt.Sprintf("%v / %v", op.a, op.b)
return stringutil.Sprintf("%v / %v", op.a, op.b)
}

type modOp struct {
Expand All @@ -126,7 +125,7 @@ func (op modOp) Eval(env *Environment) (document.Value, error) {
}

func (op modOp) String() string {
return fmt.Sprintf("%v %% %v", op.a, op.b)
return stringutil.Sprintf("%v %% %v", op.a, op.b)
}

type bitwiseAndOp struct {
Expand All @@ -148,7 +147,7 @@ func (op bitwiseAndOp) Eval(env *Environment) (document.Value, error) {
}

func (op bitwiseAndOp) String() string {
return fmt.Sprintf("%v & %v", op.a, op.b)
return stringutil.Sprintf("%v & %v", op.a, op.b)
}

type bitwiseOrOp struct {
Expand All @@ -170,7 +169,7 @@ func (op bitwiseOrOp) Eval(env *Environment) (document.Value, error) {
}

func (op bitwiseOrOp) String() string {
return fmt.Sprintf("%v | %v", op.a, op.b)
return stringutil.Sprintf("%v | %v", op.a, op.b)
}

type bitwiseXorOp struct {
Expand All @@ -192,5 +191,5 @@ func (op bitwiseXorOp) Eval(env *Environment) (document.Value, error) {
}

func (op bitwiseXorOp) String() string {
return fmt.Sprintf("%v ^ %v", op.a, op.b)
return stringutil.Sprintf("%v ^ %v", op.a, op.b)
}
Loading

0 comments on commit 86edffb

Please sign in to comment.