Skip to content

Commit

Permalink
Add stringutil package
Browse files Browse the repository at this point in the history
  • Loading branch information
asdine committed Mar 16, 2021
1 parent aa68a5c commit ae5a892
Show file tree
Hide file tree
Showing 27 changed files with 213 additions and 87 deletions.
4 changes: 2 additions & 2 deletions cmd/genji/commands/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"context"
"errors"
"fmt"
"io"
"os"

Expand All @@ -14,6 +13,7 @@ import (
"github.com/genjidb/genji/engine"
"github.com/genjidb/genji/engine/badgerengine"
"github.com/genjidb/genji/engine/boltengine"
"github.com/genjidb/genji/stringutil"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -75,7 +75,7 @@ func executeRestore(ctx context.Context, r io.Reader, e, dbPath string) error {
case "badger":
ng, err = badgerengine.NewEngine(badger.DefaultOptions(dbPath).WithLogger(nil))
default:
return fmt.Errorf(`engine should be "bolt" or "badger, got %q`, e)
return stringutil.Errorf(`engine should be "bolt" or "badger, got %q`, e)
}
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions cmd/genji/dbutil/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package dbutil
import (
"context"
"errors"
"fmt"
"strings"
"time"

Expand All @@ -13,6 +12,7 @@ import (
"github.com/genjidb/genji/engine/badgerengine"
"github.com/genjidb/genji/engine/boltengine"
"github.com/genjidb/genji/engine/memoryengine"
"github.com/genjidb/genji/stringutil"
"go.etcd.io/bbolt"
)

Expand All @@ -39,7 +39,7 @@ func OpenDB(ctx context.Context, dbPath, engineName string) (*genji.DB, error) {
return nil, errors.New("database is locked")
}
default:
return nil, fmt.Errorf(`engine should be "bolt" or "badger", got %q`, engineName)
return nil, stringutil.Errorf(`engine should be "bolt" or "badger", got %q`, engineName)
}
if err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions cmd/genji/dbutil/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

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

// InsertJSON reads json documents from r and inserts them into the selected table.
Expand Down Expand Up @@ -76,11 +77,11 @@ func InsertJSON(db *genji.DB, table string, r io.Reader) error {
}
d, ok := t.(json.Delim)
if ok && d.String() != "]" {
return fmt.Errorf("found %q, but expected ']'", c)
return stringutil.Errorf("found %q, but expected ']'", c)
}

default:
return fmt.Errorf("found %q, but expected '{' or '['", c)
return stringutil.Errorf("found %q, but expected '{' or '['", c)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion cmd/genji/shell/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/genjidb/genji/database"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/engine"
"github.com/genjidb/genji/stringutil"
)

var commands = []struct {
Expand Down Expand Up @@ -98,7 +99,7 @@ func runIndexesCmd(db *genji.DB, tableName string, w io.Writer) error {
_, err := tx.QueryDocument("SELECT 1 FROM __genji_tables WHERE table_name = ? LIMIT 1", tableName)
if err != nil {
if err == database.ErrDocumentNotFound {
return fmt.Errorf("%w: %q", database.ErrTableNotFound, tableName)
return stringutil.Errorf("%w: %q", database.ErrTableNotFound, tableName)
}
return err
}
Expand Down
17 changes: 9 additions & 8 deletions cmd/genji/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/genjidb/genji/cmd/genji/dbutil"
"github.com/genjidb/genji/document"
"github.com/genjidb/genji/sql/parser"
"github.com/genjidb/genji/stringutil"
"go.uber.org/multierr"
"golang.org/x/sync/errgroup"
)
Expand Down Expand Up @@ -80,7 +81,7 @@ func (o *Options) validate() error {
switch o.Engine {
case "bolt", "badger", "memory":
default:
return fmt.Errorf("unsupported engine %q", o.Engine)
return stringutil.Errorf("unsupported engine %q", o.Engine)
}

return nil
Expand Down Expand Up @@ -445,19 +446,19 @@ func (sh *Shell) runCommand(ctx context.Context, in string) error {
return runHelpCmd()
case ".tables":
if len(cmd) > 1 {
return fmt.Errorf("usage: .tables")
return stringutil.Errorf("usage: .tables")
}

return runTablesCmd(sh.db, os.Stdout)
case ".exit", "exit":
if len(cmd) > 1 {
return fmt.Errorf("usage: .exit")
return stringutil.Errorf("usage: .exit")
}

return errExitCommand
case ".indexes":
if len(cmd) > 2 {
return fmt.Errorf("usage: .indexes [tablename]")
return stringutil.Errorf("usage: .indexes [tablename]")
}

var tableName string
Expand All @@ -477,7 +478,7 @@ func (sh *Shell) runCommand(ctx context.Context, in string) error {
engine = "bolt"
path = cmd[1]
} else {
return fmt.Errorf("Can't save without output path")
return stringutil.Errorf("Can't save without output path")
}

return runSaveCmd(ctx, sh.db, engine, path)
Expand Down Expand Up @@ -506,11 +507,11 @@ func (sh *Shell) runPipedInput(ctx context.Context) (ran bool, err error) {
}
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return true, fmt.Errorf("Unable to read piped input: %w", err)
return true, stringutil.Errorf("Unable to read piped input: %w", err)
}
err = sh.runQuery(ctx, string(data))
if err != nil {
return true, fmt.Errorf("Unable to execute provided sql statements: %w", err)
return true, stringutil.Errorf("Unable to execute provided sql statements: %w", err)
}

return true, nil
Expand Down Expand Up @@ -622,7 +623,7 @@ func displaySuggestions(in string) error {
}

if len(suggestions) == 0 {
return fmt.Errorf("Unknown command %q. Enter \".help\" for help.", in)
return stringutil.Errorf("Unknown command %q. Enter \".help\" for help.", in)
}

fmt.Printf("\"%s\" is not a command. Did you mean: ", in)
Expand Down
8 changes: 4 additions & 4 deletions database/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package database

import (
"errors"
"fmt"
"strings"
"sync"

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

// Catalog holds all table and index informations.
Expand Down Expand Up @@ -107,7 +107,7 @@ func (c *Catalog) GetTable(tx *Transaction, tableName string) (*Table, error) {
// If it already exists, returns ErrTableAlreadyExists.
func (c *Catalog) CreateTable(tx *Transaction, tableName string, info *TableInfo) error {
if strings.HasPrefix(tableName, internalPrefix) {
return fmt.Errorf("table name must not start with %s", internalPrefix)
return stringutil.Errorf("table name must not start with %s", internalPrefix)
}

if info == nil {
Expand Down Expand Up @@ -135,7 +135,7 @@ func (c *Catalog) CreateTable(tx *Transaction, tableName string, info *TableInfo

err = tx.tx.CreateStore(info.storeName)
if err != nil {
return fmt.Errorf("failed to create table %q: %w", tableName, err)
return stringutil.Errorf("failed to create table %q: %w", tableName, err)
}

return nil
Expand Down Expand Up @@ -319,7 +319,7 @@ func (c *Catalog) buildIndex(tx *Transaction, idx *Index, table *Table) error {

err = idx.Set(v, d.(document.Keyer).RawKey())
if err != nil {
return fmt.Errorf("error while building the index: %w", err)
return stringutil.Errorf("error while building the index: %w", err)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion database/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func TestCatalogReIndex(t *testing.T) {
require.Equal(t, 10, i)
require.NoError(t, err)

idx, err = tx.GetIndex("b")
_, err = tx.GetIndex("b")
require.NoError(t, err)

return errDontCommit
Expand Down
16 changes: 8 additions & 8 deletions database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package database
import (
"bytes"
"encoding/binary"
"fmt"
"strings"

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

const storePrefix = 't'
Expand Down Expand Up @@ -261,12 +261,12 @@ func (f *FieldConstraints) Add(newFc *FieldConstraint) error {

// if constraints are different
if !ok {
return fmt.Errorf("conflicting constraints: %q and %q", c.String(), newFc.String())
return stringutil.Errorf("conflicting constraints: %q and %q", c.String(), newFc.String())
}

// if both non inferred, they are duplicate
if !newFc.IsInferred && !c.IsInferred {
return fmt.Errorf("conflicting constraints: %q and %q", c.String(), newFc.String())
return stringutil.Errorf("conflicting constraints: %q and %q", c.String(), newFc.String())
}

// if both inferred, merge the InferredBy member
Expand All @@ -288,7 +288,7 @@ func (f *FieldConstraints) Add(newFc *FieldConstraint) error {

// ensure we don't have duplicate primary keys
if c.IsPrimaryKey && newFc.IsPrimaryKey {
return fmt.Errorf(
return stringutil.Errorf(
"multiple primary keys are not allowed (%q is primary key)",
c.Path.String(),
)
Expand Down Expand Up @@ -329,7 +329,7 @@ func (f FieldConstraints) ValidateDocument(d document.Document) (*document.Field
// to the right type above.
// check if it is required but null.
if v.Type == document.NullValue && fc.IsNotNull {
return nil, fmt.Errorf("field %q is required and must be not null", fc.Path)
return nil, stringutil.Errorf("field %q is required and must be not null", fc.Path)
}
continue
}
Expand All @@ -348,7 +348,7 @@ func (f FieldConstraints) ValidateDocument(d document.Document) (*document.Field
// if there is no default value
// check if field is required
} else if fc.IsNotNull {
return nil, fmt.Errorf("field %q is required and must be not null", fc.Path)
return nil, stringutil.Errorf("field %q is required and must be not null", fc.Path)
}
}

Expand Down Expand Up @@ -580,7 +580,7 @@ func (t *tableStore) Delete(tx *Transaction, tableName string) error {
err := t.st.Delete([]byte(tableName))
if err != nil {
if err == engine.ErrKeyNotFound {
return fmt.Errorf("%w: %q", ErrTableNotFound, tableName)
return stringutil.Errorf("%w: %q", ErrTableNotFound, tableName)
}

return err
Expand All @@ -603,7 +603,7 @@ func (t *tableStore) Replace(tx *Transaction, tableName string, info *TableInfo)
_, err = t.st.Get(tbName)
if err != nil {
if err == engine.ErrKeyNotFound {
return fmt.Errorf("%w: %q", ErrTableNotFound, tableName)
return stringutil.Errorf("%w: %q", ErrTableNotFound, tableName)
}

return err
Expand Down
5 changes: 0 additions & 5 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ import (
type Database struct {
ng engine.Engine

// This stores the last transaction id created.
// It starts at 0 at database startup and is
// incremented atomically every time Begin is called.
lastTransactionID int64

// If this is non-nil, the user is running an explicit transaction
// using the BEGIN statement.
// Only one attached transaction can be run at a time and any calls to DB.Begin()
Expand Down
4 changes: 2 additions & 2 deletions database/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"

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

const (
Expand Down Expand Up @@ -51,7 +51,7 @@ func (idx *Index) Set(v document.Value, k []byte) error {
}

if idx.Info.Type != 0 && idx.Info.Type != v.Type {
return fmt.Errorf("cannot index value of type %s in %s index", v.Type, idx.Info.Type)
return stringutil.Errorf("cannot index value of type %s in %s index", v.Type, idx.Info.Type)
}

st, err := getOrCreateStore(idx.tx, idx.storeName)
Expand Down
10 changes: 5 additions & 5 deletions database/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"bytes"
"encoding/binary"
"errors"
"fmt"

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

// A Table represents a collection of documents.
Expand Down Expand Up @@ -81,7 +81,7 @@ func (t *Table) Insert(d document.Document) (document.Document, error) {
defer enc.Close()
err = enc.EncodeDocument(fb)
if err != nil {
return nil, fmt.Errorf("failed to encode document: %w", err)
return nil, stringutil.Errorf("failed to encode document: %w", err)
}

err = t.Store.Put(key, buf.Bytes())
Expand Down Expand Up @@ -196,7 +196,7 @@ func (t *Table) replace(indexes []*Index, key []byte, d document.Document) error
defer enc.Close()
err = enc.EncodeDocument(d)
if err != nil {
return fmt.Errorf("failed to encode document: %w", err)
return stringutil.Errorf("failed to encode document: %w", err)
}

// replace old document with new document
Expand Down Expand Up @@ -440,7 +440,7 @@ func (t *Table) GetDocument(key []byte) (document.Document, error) {
if err == engine.ErrKeyNotFound {
return nil, ErrDocumentNotFound
}
return nil, fmt.Errorf("failed to fetch document %q: %w", key, err)
return nil, stringutil.Errorf("failed to fetch document %q: %w", key, err)
}

info := t.Info()
Expand All @@ -463,7 +463,7 @@ func (t *Table) generateKey(info *TableInfo, fb *document.FieldBuffer) ([]byte,

v, err := pk.Path.GetValueFromDocument(fb)
if err == document.ErrFieldNotFound {
return nil, fmt.Errorf("missing primary key at path %q", pk.Path)
return nil, stringutil.Errorf("missing primary key at path %q", pk.Path)
}
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion database/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func TestTableInsert(t *testing.T) {
require.NoError(t, err)

// insert again
d, err = tb.Insert(doc)
_, err = tb.Insert(doc)
require.Equal(t, database.ErrDuplicateDocument, err)
})

Expand Down
Loading

0 comments on commit ae5a892

Please sign in to comment.