Skip to content

Commit

Permalink
refactor: Purge all println and ban it (#253)
Browse files Browse the repository at this point in the history
* Use logger in bench package

* Log command result instead of println

* Remove printlns from db_test

Here they are actually hidding what is actually untested

* Write to log in db.printstore

Instead of println

* Use logger in db.tests

* Delete println

* Ban fmt.PrintX
  • Loading branch information
AndrewSisley authored Mar 1, 2022
1 parent 42a3b8b commit 38c4b2c
Show file tree
Hide file tree
Showing 16 changed files with 25 additions and 141 deletions.
2 changes: 1 addition & 1 deletion .golangci.sourceinc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,6 @@ linters-settings:
forbidigo:
# Forbid the following identifiers (identifiers are written using regexp):
forbid:
- ^print.*$
- 'fmt\.Print.*'
# Exclude godoc examples from forbidigo checks. Default is true.
exclude_godoc_examples: false
Expand Down Expand Up @@ -790,6 +789,7 @@ linters:
- unused
- varcheck
- errcheck
- forbidigo
# - wrapcheck
# - errorlint
# - gci
Expand Down
1 change: 0 additions & 1 deletion api/http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,6 @@ func (s *Server) getBlock(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
return
}
// fmt.Println(string(buf))

reg := corecrdt.LWWRegister{}
delta, err := reg.DeltaDecode(nd)
Expand Down
7 changes: 3 additions & 4 deletions bench/bench_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
testutils "github.com/sourcenetwork/defradb/db/tests"
"github.com/sourcenetwork/defradb/document"
"github.com/sourcenetwork/defradb/document/key"
"github.com/sourcenetwork/defradb/logging"
)

const (
Expand All @@ -39,6 +40,7 @@ const (

var (
storage string = "memory"
log = logging.MustNewLogger("defra.bench")
)

func init() {
Expand Down Expand Up @@ -146,8 +148,6 @@ func BackfillBenchmarkDB(b *testing.B, ctx context.Context, cols []client.Collec
return
}

// fmt.Println(docs)

// create the documents
keys := make([]key.DocKey, numTypes)
for j := 0; j < numTypes; j++ {
Expand All @@ -164,7 +164,7 @@ func BackfillBenchmarkDB(b *testing.B, ctx context.Context, cols []client.Collec
// but its fine :).
for {
if err := cols[j].Create(ctx, doc); err != nil && err.Error() == badger.ErrConflict.Error() {
fmt.Printf("failed to commit TX for doc %s, retrying...\n", doc.Key())
log.Info(ctx, "Failed to commit TX for doc %s, retrying...\n", logging.NewKV("DocKey", doc.Key()))
continue
} else if err != nil {
errCh <- fmt.Errorf("Failed to create document: %w", err)
Expand All @@ -181,7 +181,6 @@ func BackfillBenchmarkDB(b *testing.B, ctx context.Context, cols []client.Collec
}

batchWg.Wait()
// fmt.Printf(".")
}

// wait for our group and signal by closing waitCh
Expand Down
3 changes: 0 additions & 3 deletions bench/collection/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func runCollectionBenchGet(b *testing.B, ctx context.Context, fixture fixtures.G
return err
}

// fmt.Println("Finished backfill...")

// run benchmark
if doSync {
return runCollectionBenchGetSync(b, ctx, collections, fixture, docCount, opCount, dockeys)
Expand Down Expand Up @@ -217,7 +215,6 @@ func runCollectionBenchCreateAsync(b *testing.B,
}

batchWg.Wait()
// fmt.Printf(".")
}

// finish or err
Expand Down
9 changes: 5 additions & 4 deletions bench/query/simple/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import (
"github.com/sourcenetwork/defradb/document/key"
)

var (
//log = logging.MustNewLogger("defra.bench")
)

func runQueryBenchGet(b *testing.B, ctx context.Context, fixture fixtures.Generator, docCount int, query string, doSync bool) error {
db, collections, err := benchutils.SetupDBAndCollections(b, ctx, fixture)
if err != nil {
Expand All @@ -46,8 +50,6 @@ func runQueryBenchGetSync(
dockeys [][]key.DocKey,
query string,
) error {
// fmt.Printf("Query:\n%s\n", query)

// run any preprocessing on the query before execution (mostly just dockey insertion if needed)
query = formatQuery(b, query, dockeys)

Expand All @@ -63,8 +65,7 @@ func runQueryBenchGetSync(
// if l != opCount {
// return fmt.Errorf("Invalid response, returned data doesn't match length, expected %v actual %v", docCount, l)
// }
// fmt.Println(res)
// fmt.Println("--------------------")
// log.Info(ctx, "", logging.NewKV("Response", res))
}
b.StopTimer()

Expand Down
2 changes: 1 addition & 1 deletion cli/defradb/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ the additional documentation found at: https://hackmd.io/@source/BksQY6Qfw.
return
}

fmt.Printf("Response: %s", string(buf))
log.Info(ctx, "", logging.NewKV("Response", string(buf)))
},
}

Expand Down
2 changes: 1 addition & 1 deletion cli/defradb/cmd/schema_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ var addCmd = &cobra.Command{

result, err := ioutil.ReadAll(res.Body)
cobra.CheckErr(err)
fmt.Println(string(result))
log.Info(ctx, "", logging.NewKV("Result", string(result)))
},
}

Expand Down
1 change: 0 additions & 1 deletion db/collection_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,6 @@ func (c *Collection) updateWithKeys(ctx context.Context, txn core.Txn, keys []ke
for i, key := range keys {
doc, err := c.Get(ctx, key)
if err != nil {
fmt.Println("error getting key to update:", key)
return nil, err
}
v, err := doc.ToMap()
Expand Down
3 changes: 1 addition & 2 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ package db
import (
"context"
"errors"
"fmt"
"sync"

"github.com/sourcenetwork/defradb/client"
Expand Down Expand Up @@ -230,6 +229,6 @@ func printStore(ctx context.Context, store core.DSReaderWriter) {
}()

for r := range results.Next() {
fmt.Println(r.Key, ": ", r.Value)
log.Info(ctx, "", logging.NewKV(r.Key, r.Value))
}
}
36 changes: 4 additions & 32 deletions db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ package db

import (
"context"
"fmt"
"testing"

"github.com/sourcenetwork/defradb/db/base"
Expand Down Expand Up @@ -159,9 +158,6 @@ func TestDBUpdateDocument(t *testing.T) {
assert.Equal(t, "Pete", name)
assert.Equal(t, int64(21), age)
assert.Nil(t, weight)

// fmt.Println("\n--")
// db.printDebugDB()
}

func TestDBUpdateNonExistingDocument(t *testing.T) {
Expand Down Expand Up @@ -247,23 +243,15 @@ func TestDBGetDocument(t *testing.T) {
assert.NoError(t, err)

err = col.Save(ctx, doc)
fmt.Println(doc.Get("Name"))
assert.NoError(t, err)

fmt.Printf("-------\n")
db.printDebugDB(ctx)
fmt.Printf("-------\n")

key, err := key.NewFromString("bae-09cd7539-9b86-5661-90f6-14fbf6c1a14d")
assert.NoError(t, err)
doc, err = col.Get(ctx, key)
fmt.Println(doc)
assert.NoError(t, err)

// value check
name, err := doc.Get("Name")
fmt.Println("-----------------------------------------------")
fmt.Println(name)
assert.NoError(t, err)
age, err := doc.Get("Age")
assert.NoError(t, err)
Expand Down Expand Up @@ -352,10 +340,6 @@ func TestDocumentMerkleDAG(t *testing.T) {
cids, _, err := heads.List(ctx)
assert.NoError(t, err)

fmt.Printf("-------\n")
db.printDebugDB(ctx)
fmt.Printf("-------\n")

reg := corecrdt.LWWRegister{}
for _, c := range cids {
b, errGet := db.DAGstore().Get(ctx, c)
Expand All @@ -364,15 +348,11 @@ func TestDocumentMerkleDAG(t *testing.T) {
nd, errDecode := dag.DecodeProtobuf(b.RawData())
assert.NoError(t, errDecode)

buf, errMarshal := nd.MarshalJSON()
_, errMarshal := nd.MarshalJSON()
assert.NoError(t, errMarshal)

fmt.Println(string(buf))
delta, errDeltaDecode := reg.DeltaDecode(nd)
_, errDeltaDecode := reg.DeltaDecode(nd)
assert.NoError(t, errDeltaDecode)

lwwdelta := delta.(*corecrdt.LWWRegDelta)
fmt.Printf("%+v - %v\n", lwwdelta, string(lwwdelta.Data))
}

testJSONObj = []byte(`{
Expand All @@ -391,26 +371,18 @@ func TestDocumentMerkleDAG(t *testing.T) {
cids, _, err = heads.List(ctx)
assert.NoError(t, err)

fmt.Printf("-------\n")
db.printDebugDB(ctx)
fmt.Printf("-------\n")

for _, c := range cids {
b, err := db.DAGstore().Get(ctx, c)
assert.NoError(t, err)

nd, err := dag.DecodeProtobuf(b.RawData())
assert.NoError(t, err)

buf, err := nd.MarshalJSON()
_, err = nd.MarshalJSON()
assert.NoError(t, err)

fmt.Println(string(buf))
delta, err := reg.DeltaDecode(nd)
_, err = reg.DeltaDecode(nd)
assert.NoError(t, err)

lwwdelta := delta.(*corecrdt.LWWRegDelta)
fmt.Printf("%+v - %v\n", lwwdelta, string(lwwdelta.Data))
}
}

Expand Down
1 change: 0 additions & 1 deletion db/fetcher/dag.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,6 @@ func (hh *heads) List() ([]cid.Cid, uint64, error) {
if r.Error != nil {
return nil, 0, fmt.Errorf("Failed to get next query result : %w", err)
}
// fmt.Println(r.Key, hh.namespace.String())
headKey := ds.NewKey(strings.TrimPrefix(r.Key, hh.namespace.String()))
headCid, err := dshelp.DsKeyToCid(headKey)
if err != nil {
Expand Down
5 changes: 0 additions & 5 deletions db/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,6 @@ func (df *DocumentFetcher) processKV(kv *core.KeyValue) error {
df.indexKey = ik.Bytes()
df.doc.Reset()
df.doc.Key = []byte(ik.BaseNamespace())
// fmt.Println(df.doc.Key)
// keyFD := df.schemaFields[0] // _key
// df.doc.Properties[keyFD] = &document.EncProperty{
// Raw: df.doc.Key[:],
// }
}

// extract the FieldID and update the encoded doc properties map
Expand Down
Loading

0 comments on commit 38c4b2c

Please sign in to comment.