Skip to content

Commit

Permalink
refactor: Make db stuff internal/private (#291)
Browse files Browse the repository at this point in the history
* Remove Listen function from db package

* Make Collection struct private

* Remove unused structs

* Make db.DB private
  • Loading branch information
AndrewSisley authored Mar 17, 2022
1 parent dcc1978 commit 8951302
Show file tree
Hide file tree
Showing 21 changed files with 180 additions and 180 deletions.
42 changes: 25 additions & 17 deletions bench/bench_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (

"github.com/sourcenetwork/defradb/bench/fixtures"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/db"
defradb "github.com/sourcenetwork/defradb/db"
testutils "github.com/sourcenetwork/defradb/db/tests"
"github.com/sourcenetwork/defradb/document"
"github.com/sourcenetwork/defradb/document/key"
Expand Down Expand Up @@ -66,20 +64,12 @@ func hashToInt64(s string) int64 {
return int64(h.Sum64())
}

func SetupCollections(b *testing.B, ctx context.Context, db *defradb.DB, fixture fixtures.Generator) ([]client.Collection, error) {
func SetupCollections(b *testing.B, ctx context.Context, db client.DB, fixture fixtures.Generator) ([]client.Collection, error) {
numTypes := len(fixture.Types())
collections := make([]client.Collection, numTypes)
var schema string

// loop to get the schemas
for i := 0; i < numTypes; i++ {
gql, err := fixtures.ExtractGQLFromType(fixture.Types()[i])
if err != nil {
return nil, fmt.Errorf("failed generating GQL: %w", err)
}

schema += gql
schema += "\n\n"
schema, err := ConstructSchema(fixture)
if err != nil {
return nil, err
}

// b.Logf("Loading schema: \n%s", schema)
Expand All @@ -101,7 +91,25 @@ func SetupCollections(b *testing.B, ctx context.Context, db *defradb.DB, fixture
return collections, nil
}

func SetupDBAndCollections(b *testing.B, ctx context.Context, fixture fixtures.Generator) (*defradb.DB, []client.Collection, error) {
func ConstructSchema(fixture fixtures.Generator) (string, error) {
numTypes := len(fixture.Types())
var schema string

// loop to get the schemas
for i := 0; i < numTypes; i++ {
gql, err := fixtures.ExtractGQLFromType(fixture.Types()[i])
if err != nil {
return "", fmt.Errorf("failed generating GQL: %w", err)
}

schema += gql
schema += "\n\n"
}

return schema, nil
}

func SetupDBAndCollections(b *testing.B, ctx context.Context, fixture fixtures.Generator) (client.DB, []client.Collection, error) {
db, err := NewTestDB(ctx, b)
if err != nil {
return nil, nil, err
Expand Down Expand Up @@ -202,10 +210,10 @@ func BackfillBenchmarkDB(b *testing.B, ctx context.Context, cols []client.Collec

type dbInfo interface {
Rootstore() ds.Batching
DB() *db.DB
DB() client.DB
}

func NewTestDB(ctx context.Context, t testing.TB) (*db.DB, error) {
func NewTestDB(ctx context.Context, t testing.TB) (client.DB, error) {
//nolint
dbi, err := newBenchStoreInfo(ctx, t)
return dbi.DB(), err
Expand Down
34 changes: 28 additions & 6 deletions bench/query/planner/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,19 @@ import (

benchutils "github.com/sourcenetwork/defradb/bench"
"github.com/sourcenetwork/defradb/bench/fixtures"
"github.com/sourcenetwork/defradb/query/graphql/planner"
"github.com/sourcenetwork/defradb/query/graphql/schema"
)

func runQueryParserBench(b *testing.B, ctx context.Context, fixture fixtures.Generator, query string) error {
db, _, err := benchutils.SetupDBAndCollections(b, ctx, fixture)
exec, err := buildExecutor(ctx, fixture)
if err != nil {
return err
}
defer db.Close(ctx)

b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := db.Executor().ParseQueryString(query)
_, err := exec.ParseQueryString(query)
if err != nil {
return fmt.Errorf("Failed to parse query string: %w", err)
}
Expand All @@ -45,9 +46,9 @@ func runMakePlanBench(b *testing.B, ctx context.Context, fixture fixtures.Genera
}
defer db.Close(ctx)

exec := db.Executor()
if exec == nil {
return fmt.Errorf("Executor can't be nil")
exec, err := buildExecutor(ctx, fixture)
if err != nil {
return err
}

q, err := exec.ParseQueryString(query)
Expand All @@ -69,3 +70,24 @@ func runMakePlanBench(b *testing.B, ctx context.Context, fixture fixtures.Genera
b.StopTimer()
return nil
}

func buildExecutor(ctx context.Context, fixture fixtures.Generator) (*planner.QueryExecutor, error) {
sm, err := schema.NewSchemaManager()
if err != nil {
return nil, err
}
schema, err := benchutils.ConstructSchema(fixture)
if err != nil {
return nil, err
}
types, _, err := sm.Generator.FromSDL(ctx, schema)
if err != nil {
return nil, err
}
_, err = sm.Generator.CreateDescriptions(types)
if err != nil {
return nil, err
}

return planner.NewQueryExecutor(sm)
}
4 changes: 2 additions & 2 deletions bench/query/simple/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

benchutils "github.com/sourcenetwork/defradb/bench"
"github.com/sourcenetwork/defradb/bench/fixtures"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/document/key"
)

Expand All @@ -45,7 +45,7 @@ func runQueryBenchGet(b *testing.B, ctx context.Context, fixture fixtures.Genera
func runQueryBenchGetSync(
b *testing.B,
ctx context.Context,
db *db.DB,
db client.DB,
docCount int,
dockeys [][]key.DocKey,
query string,
Expand Down
4 changes: 2 additions & 2 deletions bench/storage/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"github.com/ipfs/go-datastore/query"

benchutils "github.com/sourcenetwork/defradb/bench"
"github.com/sourcenetwork/defradb/db"
"github.com/sourcenetwork/defradb/client"
)

func runStorageBenchGet(b *testing.B, ctx context.Context, valueSize, objCount, opCount int, doSync bool) error {
Expand Down Expand Up @@ -254,7 +254,7 @@ func backfillBenchmarkStorageDB(ctx context.Context, db ds.Batching, objCount in
return keys, batch.Commit(ctx)
}

func backfillBenchmarkTxn(ctx context.Context, db *db.DB, objCount int, valueSize int) ([]string, error) {
func backfillBenchmarkTxn(ctx context.Context, db client.DB, objCount int, valueSize int) ([]string, error) {
txn, err := db.NewTxn(ctx, false)
if err != nil {
return nil, err
Expand Down
6 changes: 5 additions & 1 deletion cli/defradb/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package cmd
import (
"context"
"errors"
"fmt"
gonet "net"
"os"
"os/signal"
Expand All @@ -31,6 +32,7 @@ import (

badger "github.com/dgraph-io/badger/v3"
ds "github.com/ipfs/go-datastore"
api "github.com/sourcenetwork/defradb/api/http"
"github.com/sourcenetwork/defradb/logging"
"github.com/spf13/cobra"
"github.com/textileio/go-threads/broadcast"
Expand Down Expand Up @@ -155,14 +157,16 @@ var startCmd = &cobra.Command{

// run the server listener in a separate goroutine
go func() {
if err := db.Listen(ctx, config.Database.Address); err != nil {
s := api.NewServer(db)
if err := s.Listen(config.Database.Address); err != nil {
log.ErrorE(ctx, "Failed to start API listener", err)
if n != nil {
n.Close() //nolint
}
db.Close(ctx)
os.Exit(1)
}
log.Info(ctx, fmt.Sprintf("Running HTTP API at http://%s. Try it out at > curl http://%s/graphql", config.Database.Address, config.Database.Address))
}()

// wait for shutdown signal
Expand Down
2 changes: 2 additions & 0 deletions client/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ type DB interface {

NewTxn(context.Context, bool) (datastore.Txn, error)
ExecQuery(context.Context, string) *QueryResult
ExecTransactionalQuery(ctx context.Context, query string, txn datastore.Txn) *QueryResult
Close(context.Context)

PrintDump(ctx context.Context)
}
Expand Down
35 changes: 0 additions & 35 deletions db/api.go

This file was deleted.

Loading

0 comments on commit 8951302

Please sign in to comment.