From 0e451b9adf42a95e601db01d23c8ecbabdf6f5ce Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Wed, 16 Oct 2024 02:51:01 +0530 Subject: [PATCH 01/16] Rename error variable in go tests to err #8829 renamed the variables from e to err as mentioned in the https://go.dev/doc/effective_go --- bindings/go/src/_stacktester/directory.go | 76 +-- bindings/go/src/_stacktester/stacktester.go | 188 ++++---- bindings/go/src/fdb/database.go | 52 +- bindings/go/src/fdb/directory/allocator.go | 42 +- .../go/src/fdb/directory/directoryLayer.go | 202 ++++---- .../go/src/fdb/directory/directorySubspace.go | 2 +- bindings/go/src/fdb/doc.go | 130 ++--- bindings/go/src/fdb/fdb.go | 34 +- bindings/go/src/fdb/fdb_test.go | 134 ++--- bindings/go/src/fdb/range.go | 22 +- bindings/go/src/fdb/snapshot.go | 6 +- bindings/go/src/fdb/transaction.go | 16 +- bindings/go/src/fdb/tuple/tuple.go | 47 +- contrib/mockkms/get_encryption_keys.go | 456 +++++++++--------- contrib/mockkms/utils.go | 30 +- packaging/docker/samples/golang/app/main.go | 11 +- recipes/go-recipes/blob.go | 4 +- recipes/go-recipes/doc.go | 4 +- recipes/go-recipes/indirect.go | 4 +- recipes/go-recipes/multi.go | 8 +- recipes/go-recipes/priority.go | 20 +- recipes/go-recipes/queue.go | 22 +- recipes/go-recipes/table.go | 28 +- 23 files changed, 768 insertions(+), 770 deletions(-) diff --git a/bindings/go/src/_stacktester/directory.go b/bindings/go/src/_stacktester/directory.go index 28e18300f73..291e7a000d6 100644 --- a/bindings/go/src/_stacktester/directory.go +++ b/bindings/go/src/_stacktester/directory.go @@ -115,7 +115,7 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, } }() - var e error + var err error switch { case op == "CREATE_SUBSPACE": @@ -142,9 +142,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, if l != nil { layer = l.([]byte) } - d, e := de.cwd().CreateOrOpen(t, tupleToPath(tuples[0]), layer) - if e != nil { - panic(e) + d, err := de.cwd().CreateOrOpen(t, tupleToPath(tuples[0]), layer) + if err != nil { + panic(err) } de.store(d) case op == "CREATE": @@ -157,13 +157,13 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, p := sm.waitAndPop().item var d directory.Directory if p == nil { - d, e = de.cwd().Create(t, tupleToPath(tuples[0]), layer) + d, err = de.cwd().Create(t, tupleToPath(tuples[0]), layer) } else { // p.([]byte) itself may be nil, but CreatePrefix handles that appropriately - d, e = de.cwd().CreatePrefix(t, tupleToPath(tuples[0]), layer, p.([]byte)) + d, err = de.cwd().CreatePrefix(t, tupleToPath(tuples[0]), layer, p.([]byte)) } - if e != nil { - panic(e) + if err != nil { + panic(err) } de.store(d) case op == "OPEN": @@ -173,9 +173,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, if l != nil { layer = l.([]byte) } - d, e := de.cwd().Open(rt, tupleToPath(tuples[0]), layer) - if e != nil { - panic(e) + d, err := de.cwd().Open(rt, tupleToPath(tuples[0]), layer) + if err != nil { + panic(err) } de.store(d) case op == "CHANGE": @@ -188,16 +188,16 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, de.errorIndex = sm.waitAndPop().item.(int64) case op == "MOVE": tuples := sm.popTuples(2) - d, e := de.cwd().Move(t, tupleToPath(tuples[0]), tupleToPath(tuples[1])) - if e != nil { - panic(e) + d, err := de.cwd().Move(t, tupleToPath(tuples[0]), tupleToPath(tuples[1])) + if err != nil { + panic(err) } de.store(d) case op == "MOVE_TO": tuples := sm.popTuples(1) - d, e := de.cwd().MoveTo(t, tupleToPath(tuples[0])) - if e != nil { - panic(e) + d, err := de.cwd().MoveTo(t, tupleToPath(tuples[0])) + if err != nil { + panic(err) } de.store(d) case strings.HasPrefix(op, "REMOVE"): @@ -208,10 +208,10 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, // doesn't end up committing the version key. (Other languages have // separate remove() and remove_if_exists() so don't have this tricky // issue). - _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { - ok, e := de.cwd().Remove(tr, path) - if e != nil { - panic(e) + _, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + ok, err := de.cwd().Remove(tr, path) + if err != nil { + panic(err) } switch op[6:] { case "": @@ -222,13 +222,13 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, } return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "LIST": - subs, e := de.cwd().List(rt, sm.maybePath()) - if e != nil { - panic(e) + subs, err := de.cwd().List(rt, sm.maybePath()) + if err != nil { + panic(err) } t := make(tuple.Tuple, len(subs)) for i, s := range subs { @@ -236,9 +236,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, } sm.store(idx, t.Pack()) case op == "EXISTS": - b, e := de.cwd().Exists(rt, sm.maybePath()) - if e != nil { - panic(e) + b, err := de.cwd().Exists(rt, sm.maybePath()) + if err != nil { + panic(err) } if b { sm.store(idx, int64(1)) @@ -249,9 +249,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, tuples := sm.popTuples(1) sm.store(idx, de.css().Pack(tuples[0])) case op == "UNPACK_KEY": - t, e := de.css().Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + t, err := de.css().Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } for _, el := range t { sm.store(idx, el) @@ -288,9 +288,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, v2 := tuple.Tuple{de.cwd().GetLayer()}.Pack() k3 := ss.Pack(tuple.Tuple{"exists"}) var v3 []byte - exists, e := de.cwd().Exists(rt, nil) - if e != nil { - panic(e) + exists, err := de.cwd().Exists(rt, nil) + if err != nil { + panic(err) } if exists { v3 = tuple.Tuple{1}.Pack() @@ -300,9 +300,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, k4 := ss.Pack(tuple.Tuple{"children"}) var subs []string if exists { - subs, e = de.cwd().List(rt, nil) - if e != nil { - panic(e) + subs, err = de.cwd().List(rt, nil) + if err != nil { + panic(err) } } v4 := tuplePackStrings(subs) diff --git a/bindings/go/src/_stacktester/stacktester.go b/bindings/go/src/_stacktester/stacktester.go index f92a4e587af..989eca600d9 100644 --- a/bindings/go/src/_stacktester/stacktester.go +++ b/bindings/go/src/_stacktester/stacktester.go @@ -144,9 +144,9 @@ func (sm *StackMachine) popRangeOptions() fdb.RangeOptions { } func (sm *StackMachine) popPrefixRange() fdb.ExactRange { - er, e := fdb.PrefixRange(sm.waitAndPop().item.([]byte)) - if e != nil { - panic(e) + er, err := fdb.PrefixRange(sm.waitAndPop().item.([]byte)) + if err != nil { + panic(err) } return er } @@ -243,9 +243,9 @@ func (sm *StackMachine) dumpStack() { } func (sm *StackMachine) executeMutation(t fdb.Transactor, f func(fdb.Transaction) (interface{}, error), isDB bool, idx int) { - _, e := t.Transact(f) - if e != nil { - panic(e) + _, err := t.Transact(f) + if err != nil { + panic(err) } if isDB { sm.store(idx, []byte("RESULT_NOT_PRESENT")) @@ -255,17 +255,17 @@ func (sm *StackMachine) executeMutation(t fdb.Transactor, f func(fdb.Transaction func (sm *StackMachine) checkWatches(watches [4]fdb.FutureNil, expected bool) bool { for _, watch := range watches { if watch.IsReady() || expected { - e := watch.Get() - if e != nil { - switch e := e.(type) { + err := watch.Get() + if err != nil { + switch err := err.(type) { case fdb.Error: tr, tr_error := db.CreateTransaction() if tr_error != nil { panic(tr_error) } - tr.OnError(e).MustGet() + tr.OnError(err).MustGet() default: - panic(e) + panic(err) } } if !expected { @@ -279,19 +279,19 @@ func (sm *StackMachine) checkWatches(watches [4]fdb.FutureNil, expected bool) bo func (sm *StackMachine) testWatches() { for { - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Set(fdb.Key("w0"), []byte("0")) tr.Set(fdb.Key("w2"), []byte("2")) tr.Set(fdb.Key("w3"), []byte("3")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } var watches [4]fdb.FutureNil - _, e = db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err = db.Transact(func(tr fdb.Transaction) (interface{}, error) { watches[0] = tr.Watch(fdb.Key("w0")) watches[1] = tr.Watch(fdb.Key("w1")) watches[2] = tr.Watch(fdb.Key("w2")) @@ -301,8 +301,8 @@ func (sm *StackMachine) testWatches() { tr.Clear(fdb.Key("w1")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } time.Sleep(5 * time.Second) @@ -311,15 +311,15 @@ func (sm *StackMachine) testWatches() { continue } - _, e = db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err = db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Set(fdb.Key("w0"), []byte("a")) tr.Set(fdb.Key("w1"), []byte("b")) tr.Clear(fdb.Key("w2")) tr.BitXor(fdb.Key("w3"), []byte("\xff\xff")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } if sm.checkWatches(watches, true) { @@ -329,12 +329,12 @@ func (sm *StackMachine) testWatches() { } func (sm *StackMachine) testLocality() { - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Options().SetTimeout(60 * 1000) tr.Options().SetReadSystemKeys() - boundaryKeys, e := db.LocalityGetBoundaryKeys(fdb.KeyRange{fdb.Key(""), fdb.Key("\xff\xff")}, 0, 0) - if e != nil { - panic(e) + boundaryKeys, err := db.LocalityGetBoundaryKeys(fdb.KeyRange{fdb.Key(""), fdb.Key("\xff\xff")}, 0, 0) + if err != nil { + panic(err) } for i := 0; i < len(boundaryKeys)-1; i++ { @@ -361,13 +361,13 @@ func (sm *StackMachine) testLocality() { return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } } func (sm *StackMachine) logStack(entries map[int]stackEntry, prefix []byte) { - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { for index, el := range entries { var keyt tuple.Tuple keyt = append(keyt, int64(index)) @@ -389,8 +389,8 @@ func (sm *StackMachine) logStack(entries map[int]stackEntry, prefix []byte) { return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } return } @@ -404,10 +404,10 @@ func (sm *StackMachine) currentTransaction() fdb.Transaction { } func (sm *StackMachine) newTransactionWithLockHeld() { - tr, e := db.CreateTransaction() + tr, err := db.CreateTransaction() - if e != nil { - panic(e) + if err != nil { + panic(err) } trMap[sm.trName] = tr @@ -448,7 +448,7 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } }() - var e error + var err error op := inst[0].(string) if sm.verbose { @@ -531,13 +531,13 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { case op == "ON_ERROR": sm.store(idx, sm.currentTransaction().OnError(fdb.Error{int(sm.waitAndPop().item.(int64))})) case op == "GET_READ_VERSION": - _, e = rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err = rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { sm.lastVersion = rtr.GetReadVersion().MustGet() sm.store(idx, []byte("GOT_READ_VERSION")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "SET": key := fdb.Key(sm.waitAndPop().item.([]byte)) @@ -561,34 +561,34 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { sm.logStack(entries, prefix) case op == "GET": key := fdb.Key(sm.waitAndPop().item.([]byte)) - res, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + res, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.Get(key), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } sm.store(idx, res.(fdb.FutureByteSlice)) case op == "GET_ESTIMATED_RANGE_SIZE": r := sm.popKeyRange() - _, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { _ = rtr.GetEstimatedRangeSizeBytes(r).MustGet() sm.store(idx, []byte("GOT_ESTIMATED_RANGE_SIZE")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "GET_RANGE_SPLIT_POINTS": r := sm.popKeyRange() chunkSize := sm.waitAndPop().item.(int64) - _, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { _ = rtr.GetRangeSplitPoints(r, chunkSize).MustGet() sm.store(idx, []byte("GOT_RANGE_SPLIT_POINTS")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "COMMIT": sm.store(idx, sm.currentTransaction().Commit()) @@ -606,9 +606,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { entry := sm.waitAndPop() sm.store(entry.idx, entry.item) case op == "GET_COMMITTED_VERSION": - sm.lastVersion, e = sm.currentTransaction().GetCommittedVersion() - if e != nil { - panic(e) + sm.lastVersion, err = sm.currentTransaction().GetCommittedVersion() + if err != nil { + panic(err) } sm.store(idx, []byte("GOT_COMMITTED_VERSION")) case op == "GET_APPROXIMATE_SIZE": @@ -619,11 +619,11 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { case op == "GET_KEY": sel := sm.popSelector() prefix := sm.waitAndPop().item.([]byte) - res, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + res, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.GetKey(sel).MustGet(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } key := res.(fdb.Key) @@ -633,9 +633,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } else if bytes.Compare(key, prefix) < 0 { sm.store(idx, prefix) } else { - s, e := fdb.Strinc(prefix) - if e != nil { - panic(e) + s, err := fdb.Strinc(prefix) + if err != nil { + panic(err) } sm.store(idx, s) } @@ -657,11 +657,11 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { prefix = sm.waitAndPop().item.([]byte) } - res, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + res, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.GetRange(r, ro).GetSliceOrPanic(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } sm.pushRange(idx, res.([]fdb.KeyValue), prefix) @@ -705,9 +705,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { sm.store(idx, packed) } case op == "TUPLE_UNPACK": - t, e := tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + t, err := tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } for _, el := range t { sm.store(idx, []byte(tuple.Tuple{el}.Pack())) @@ -716,9 +716,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { count := sm.waitAndPop().item.(int64) tuples := make([]tuple.Tuple, count) for i := 0; i < int(count); i++ { - tuples[i], e = tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + tuples[i], err = tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } } sort.Sort(byBytes(tuples)) @@ -763,9 +763,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { }() case op == "WAIT_EMPTY": prefix := sm.waitAndPop().item.([]byte) - er, e := fdb.PrefixRange(prefix) - if e != nil { - panic(e) + er, err := fdb.PrefixRange(prefix) + if err != nil { + panic(err) } db.Transact(func(tr fdb.Transaction) (interface{}, error) { v := tr.GetRange(er, fdb.RangeOptions{}).GetSliceOrPanic() @@ -776,27 +776,27 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { }) sm.store(idx, []byte("WAITED_FOR_EMPTY")) case op == "READ_CONFLICT_RANGE": - e = sm.currentTransaction().AddReadConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) - if e != nil { - panic(e) + err = sm.currentTransaction().AddReadConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_RANGE")) case op == "WRITE_CONFLICT_RANGE": - e = sm.currentTransaction().AddWriteConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) - if e != nil { - panic(e) + err = sm.currentTransaction().AddWriteConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_RANGE")) case op == "READ_CONFLICT_KEY": - e = sm.currentTransaction().AddReadConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + err = sm.currentTransaction().AddReadConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_KEY")) case op == "WRITE_CONFLICT_KEY": - e = sm.currentTransaction().AddWriteConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + err = sm.currentTransaction().AddWriteConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_KEY")) case op == "ATOMIC_OP": @@ -855,7 +855,7 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } fdb.MustAPIVersion(apiVersion) - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Options().SetPrioritySystemImmediate() tr.Options().SetPriorityBatch() tr.Options().SetCausalReadRisky() @@ -877,8 +877,8 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { return tr.Get(fdb.Key("\xff")).MustGet(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } sm.testWatches() @@ -900,11 +900,11 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } func (sm *StackMachine) Run() { - r, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { return tr.GetRange(tuple.Tuple{sm.prefix}, fdb.RangeOptions{}).GetSliceOrPanic(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } instructions := r.([]fdb.KeyValue) @@ -931,29 +931,29 @@ func main() { clusterFile = os.Args[3] } - var e error + var err error var apiVersion int - apiVersion, e = strconv.Atoi(os.Args[2]) - if e != nil { - log.Fatal(e) + apiVersion, err = strconv.Atoi(os.Args[2]) + if err != nil { + log.Fatal(err) } if fdb.IsAPIVersionSelected() { log.Fatal("API version already selected") } - e = fdb.APIVersion(apiVersion) - if e != nil { - log.Fatal(e) + err = fdb.APIVersion(apiVersion) + if err != nil { + log.Fatal(err) } if fdb.MustGetAPIVersion() != apiVersion { log.Fatal("API version not equal to value selected") } - db, e = fdb.OpenDatabase(clusterFile) - if e != nil { - log.Fatal(e) + db, err = fdb.OpenDatabase(clusterFile) + if err != nil { + log.Fatal(err) } sm := newStackMachine(prefix, verbose) diff --git a/bindings/go/src/fdb/database.go b/bindings/go/src/fdb/database.go index 942772680c6..0c27b8da466 100644 --- a/bindings/go/src/fdb/database.go +++ b/bindings/go/src/fdb/database.go @@ -133,25 +133,25 @@ func (d Database) RebootWorker(address string, checkFile bool, suspendDuration i return err } -func retryable(wrapped func() (interface{}, error), onError func(Error) FutureNil) (ret interface{}, e error) { +func retryable(wrapped func() (interface{}, error), onError func(Error) FutureNil) (ret interface{}, err error) { for { - ret, e = wrapped() + ret, err = wrapped() // No error means success! - if e == nil { + if err == nil { return } // Check if the error chain contains an // fdb.Error var ep Error - if errors.As(e, &ep) { - e = onError(ep).Get() + if errors.As(err, &ep) { + err = onError(ep).Get() } // If OnError returns an error, then it's not // retryable; otherwise take another pass at things - if e != nil { + if err != nil { return } } @@ -180,19 +180,19 @@ func retryable(wrapped func() (interface{}, error), onError func(Error) FutureNi // See the Transactor interface for an example of using Transact with // Transaction and Database objects. func (d Database) Transact(f func(Transaction) (interface{}, error)) (interface{}, error) { - tr, e := d.CreateTransaction() + tr, err := d.CreateTransaction() // Any error here is non-retryable - if e != nil { - return nil, e + if err != nil { + return nil, err } - wrapped := func() (ret interface{}, e error) { - defer panicToError(&e) + wrapped := func() (ret interface{}, err error) { + defer panicToError(&err) - ret, e = f(tr) + ret, err = f(tr) - if e == nil { - e = tr.Commit().Get() + if err == nil { + err = tr.Commit().Get() } return @@ -224,16 +224,16 @@ func (d Database) Transact(f func(Transaction) (interface{}, error)) (interface{ // See the ReadTransactor interface for an example of using ReadTransact with // Transaction, Snapshot and Database objects. func (d Database) ReadTransact(f func(ReadTransaction) (interface{}, error)) (interface{}, error) { - tr, e := d.CreateTransaction() - if e != nil { + tr, err := d.CreateTransaction() + if err != nil { // Any error here is non-retryable - return nil, e + return nil, err } - wrapped := func() (ret interface{}, e error) { - defer panicToError(&e) + wrapped := func() (ret interface{}, err error) { + defer panicToError(&err) - ret, e = f(tr) + ret, err = f(tr) // read-only transactions are not committed and will be destroyed automatically via GC, // once all the futures go out of scope @@ -262,9 +262,9 @@ func (d Database) Options() DatabaseOptions { // If readVersion is non-zero, the boundary keys as of readVersion will be // returned. func (d Database) LocalityGetBoundaryKeys(er ExactRange, limit int, readVersion int64) ([]Key, error) { - tr, e := d.CreateTransaction() - if e != nil { - return nil, e + tr, err := d.CreateTransaction() + if err != nil { + return nil, err } if readVersion != 0 { @@ -280,9 +280,9 @@ func (d Database) LocalityGetBoundaryKeys(er ExactRange, limit int, readVersion append(Key("\xFF/keyServers/"), ek.FDBKey()...), } - kvs, e := tr.Snapshot().GetRange(ffer, RangeOptions{Limit: limit}).GetSliceWithError() - if e != nil { - return nil, e + kvs, err := tr.Snapshot().GetRange(ffer, RangeOptions{Limit: limit}).GetSliceWithError() + if err != nil { + return nil, err } size := len(kvs) diff --git a/bindings/go/src/fdb/directory/allocator.go b/bindings/go/src/fdb/directory/allocator.go index 3c4eff4fc02..db7657e3c84 100644 --- a/bindings/go/src/fdb/directory/allocator.go +++ b/bindings/go/src/fdb/directory/allocator.go @@ -66,18 +66,18 @@ func windowSize(start int64) int64 { func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subspace) (subspace.Subspace, error) { for { rr := tr.Snapshot().GetRange(hca.counters, fdb.RangeOptions{Limit: 1, Reverse: true}) - kvs, e := rr.GetSliceWithError() - if e != nil { - return nil, e + kvs, err := rr.GetSliceWithError() + if err != nil { + return nil, err } var start int64 var window int64 if len(kvs) == 1 { - t, e := hca.counters.Unpack(kvs[0].Key) - if e != nil { - return nil, e + t, err := hca.counters.Unpack(kvs[0].Key) + if err != nil { + return nil, err } start = t[0].(int64) } @@ -98,18 +98,18 @@ func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subsp allocatorMutex.Unlock() - countStr, e := countFuture.Get() - if e != nil { - return nil, e + countStr, err := countFuture.Get() + if err != nil { + return nil, err } var count int64 if countStr == nil { count = 0 } else { - e = binary.Read(bytes.NewBuffer(countStr), binary.LittleEndian, &count) - if e != nil { - return nil, e + err = binary.Read(bytes.NewBuffer(countStr), binary.LittleEndian, &count) + if err != nil { + return nil, err } } @@ -139,14 +139,14 @@ func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subsp allocatorMutex.Unlock() - kvs, e = latestCounter.GetSliceWithError() - if e != nil { - return nil, e + kvs, err = latestCounter.GetSliceWithError() + if err != nil { + return nil, err } if len(kvs) > 0 { - t, e := hca.counters.Unpack(kvs[0].Key) - if e != nil { - return nil, e + t, err := hca.counters.Unpack(kvs[0].Key) + if err != nil { + return nil, err } currentStart := t[0].(int64) if currentStart > start { @@ -154,9 +154,9 @@ func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subsp } } - v, e := candidateValue.Get() - if e != nil { - return nil, e + v, err := candidateValue.Get() + if err != nil { + return nil, err } if v == nil { tr.AddWriteConflictKey(key) diff --git a/bindings/go/src/fdb/directory/directoryLayer.go b/bindings/go/src/fdb/directory/directoryLayer.go index 396c6982c07..6bda8397e18 100644 --- a/bindings/go/src/fdb/directory/directoryLayer.go +++ b/bindings/go/src/fdb/directory/directoryLayer.go @@ -72,8 +72,8 @@ func NewDirectoryLayer(nodeSS, contentSS subspace.Subspace, allowManualPrefixes } func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transaction, path []string, layer []byte, prefix []byte, allowCreate, allowOpen bool) (DirectorySubspace, error) { - if e := dl.checkVersion(rtr, nil); e != nil { - return nil, e + if err := dl.checkVersion(rtr, nil); err != nil { + return nil, err } if prefix != nil && !dl.allowManualPrefixes { @@ -91,9 +91,9 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti if existingNode.exists() { if existingNode.isInPartition(nil, false) { subpath := existingNode.getPartitionSubpath() - enc, e := existingNode.getContents(dl, nil) - if e != nil { - return nil, e + enc, err := existingNode.getContents(dl, nil) + if err != nil { + return nil, err } return enc.(directoryPartition).createOrOpen(rtr, tr, subpath, layer, prefix, allowCreate, allowOpen) } @@ -103,7 +103,7 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti } if layer != nil { - if l, e := existingNode._layer.Get(); e != nil || bytes.Compare(l, layer) != 0 { + if l, err := existingNode._layer.Get(); err != nil || bytes.Compare(l, layer) != 0 { return nil, errors.New("the directory was created with an incompatible layer") } } @@ -115,14 +115,14 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti return nil, ErrDirNotExists } - if e := dl.checkVersion(rtr, tr); e != nil { - return nil, e + if err := dl.checkVersion(rtr, tr); err != nil { + return nil, err } if prefix == nil { - newss, e := dl.allocator.allocate(*tr, dl.contentSS) - if e != nil { - return nil, fmt.Errorf("unable to allocate new directory prefix (%s)", e.Error()) + newss, err := dl.allocator.allocate(*tr, dl.contentSS) + if err != nil { + return nil, fmt.Errorf("unable to allocate new directory prefix (%s)", err.Error()) } if !isRangeEmpty(rtr, newss) { @@ -131,17 +131,17 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti prefix = newss.Bytes() - pf, e := dl.isPrefixFree(rtr.Snapshot(), prefix) - if e != nil { - return nil, e + pf, err := dl.isPrefixFree(rtr.Snapshot(), prefix) + if err != nil { + return nil, err } if !pf { return nil, errors.New("the directory layer has manually allocated prefixes that conflict with the automatic prefix allocator") } } else { - pf, e := dl.isPrefixFree(rtr, prefix) - if e != nil { - return nil, e + pf, err := dl.isPrefixFree(rtr, prefix) + if err != nil { + return nil, err } if !pf { return nil, errors.New("the given prefix is already in use") @@ -151,9 +151,9 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti var parentNode subspace.Subspace if len(path) > 1 { - pd, e := dl.createOrOpen(rtr, tr, path[:len(path)-1], nil, nil, true, true) - if e != nil { - return nil, e + pd, err := dl.createOrOpen(rtr, tr, path[:len(path)-1], nil, nil, true, true) + if err != nil { + return nil, err } parentNode = dl.nodeWithPrefix(pd.Bytes()) } else { @@ -177,21 +177,21 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti } func (dl directoryLayer) CreateOrOpen(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { return dl.createOrOpen(tr, &tr, path, layer, nil, true, true) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Create(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { return dl.createOrOpen(tr, &tr, path, layer, nil, true, false) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } @@ -200,29 +200,29 @@ func (dl directoryLayer) CreatePrefix(t fdb.Transactor, path []string, layer []b if prefix == nil { prefix = []byte{} } - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { return dl.createOrOpen(tr, &tr, path, layer, prefix, true, false) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Open(rt fdb.ReadTransactor, path []string, layer []byte) (DirectorySubspace, error) { - r, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + r, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return dl.createOrOpen(rtr, nil, path, layer, nil, false, true) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Exists(rt fdb.ReadTransactor, path []string) (bool, error) { - r, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - if e := dl.checkVersion(rtr, nil); e != nil { - return false, e + r, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + if err := dl.checkVersion(rtr, nil); err != nil { + return false, err } node := dl.find(rtr, path).prefetchMetadata(rtr) @@ -231,25 +231,25 @@ func (dl directoryLayer) Exists(rt fdb.ReadTransactor, path []string) (bool, err } if node.isInPartition(nil, false) { - nc, e := node.getContents(dl, nil) - if e != nil { - return false, e + nc, err := node.getContents(dl, nil) + if err != nil { + return false, err } return nc.Exists(rtr, node.getPartitionSubpath()) } return true, nil }) - if e != nil { - return false, e + if err != nil { + return false, err } return r.(bool), nil } func (dl directoryLayer) List(rt fdb.ReadTransactor, path []string) ([]string, error) { - r, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - if e := dl.checkVersion(rtr, nil); e != nil { - return nil, e + r, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + if err := dl.checkVersion(rtr, nil); err != nil { + return nil, err } node := dl.find(rtr, path).prefetchMetadata(rtr) @@ -258,17 +258,17 @@ func (dl directoryLayer) List(rt fdb.ReadTransactor, path []string) ([]string, e } if node.isInPartition(nil, true) { - nc, e := node.getContents(dl, nil) - if e != nil { - return nil, e + nc, err := node.getContents(dl, nil) + if err != nil { + return nil, err } return nc.List(rtr, node.getPartitionSubpath()) } return dl.subdirNames(rtr, node.subspace) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.([]string), nil } @@ -278,9 +278,9 @@ func (dl directoryLayer) MoveTo(t fdb.Transactor, newAbsolutePath []string) (Dir } func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []string) (DirectorySubspace, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { - if e := dl.checkVersion(tr, &tr); e != nil { - return nil, e + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + if err := dl.checkVersion(tr, &tr); err != nil { + return nil, err } sliceEnd := len(oldPath) @@ -303,9 +303,9 @@ func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []stri return nil, errors.New("cannot move between partitions") } - nnc, e := newNode.getContents(dl, nil) - if e != nil { - return nil, e + nnc, err := newNode.getContents(dl, nil) + if err != nil { + return nil, err } return nnc.Move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath()) } @@ -319,30 +319,30 @@ func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []stri return nil, errors.New("the parent of the destination directory does not exist. Create it first") } - p, e := dl.nodeSS.Unpack(oldNode.subspace) - if e != nil { - return nil, e + p, err := dl.nodeSS.Unpack(oldNode.subspace) + if err != nil { + return nil, err } tr.Set(parentNode.subspace.Sub(_SUBDIRS, newPath[len(newPath)-1]), p[0].([]byte)) dl.removeFromParent(tr, oldPath) - l, e := oldNode._layer.Get() - if e != nil { - return nil, e + l, err := oldNode._layer.Get() + if err != nil { + return nil, err } return dl.contentsOfNode(oldNode.subspace, newPath, l) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { - if e := dl.checkVersion(tr, &tr); e != nil { - return false, e + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + if err := dl.checkVersion(tr, &tr); err != nil { + return false, err } if len(path) == 0 { @@ -356,22 +356,22 @@ func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) { } if node.isInPartition(nil, false) { - nc, e := node.getContents(dl, nil) - if e != nil { - return false, e + nc, err := node.getContents(dl, nil) + if err != nil { + return false, err } return nc.(directoryPartition).Remove(tr, node.getPartitionSubpath()) } - if e := dl.removeRecursive(tr, node.subspace); e != nil { - return false, e + if err := dl.removeRecursive(tr, node.subspace); err != nil { + return false, err } dl.removeFromParent(tr, path) return true, nil }) - if e != nil { - return false, e + if err != nil { + return false, err } return r.(bool), nil } @@ -379,18 +379,18 @@ func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) { func (dl directoryLayer) removeRecursive(tr fdb.Transaction, node subspace.Subspace) error { nodes := dl.subdirNodes(tr, node) for i := range nodes { - if e := dl.removeRecursive(tr, nodes[i]); e != nil { - return e + if err := dl.removeRecursive(tr, nodes[i]); err != nil { + return err } } - p, e := dl.nodeSS.Unpack(node) - if e != nil { - return e + p, err := dl.nodeSS.Unpack(node) + if err != nil { + return err } - kr, e := fdb.PrefixRange(p[0].([]byte)) - if e != nil { - return e + kr, err := fdb.PrefixRange(p[0].([]byte)) + if err != nil { + return err } tr.ClearRange(kr) @@ -421,14 +421,14 @@ func (dl directoryLayer) subdirNames(rtr fdb.ReadTransaction, node subspace.Subs var ret []string for ri.Advance() { - kv, e := ri.Get() - if e != nil { - return nil, e + kv, err := ri.Get() + if err != nil { + return nil, err } - p, e := sd.Unpack(kv.Key) - if e != nil { - return nil, e + p, err := sd.Unpack(kv.Key) + if err != nil { + return nil, err } ret = append(ret, p[0].(string)) @@ -462,14 +462,14 @@ func (dl directoryLayer) nodeContainingKey(rtr fdb.ReadTransaction, key []byte) bk, _ := dl.nodeSS.FDBRangeKeys() kr := fdb.KeyRange{bk, fdb.Key(append(dl.nodeSS.Pack(tuple.Tuple{key}), 0x00))} - kvs, e := rtr.GetRange(kr, fdb.RangeOptions{Reverse: true, Limit: 1}).GetSliceWithError() - if e != nil { - return nil, e + kvs, err := rtr.GetRange(kr, fdb.RangeOptions{Reverse: true, Limit: 1}).GetSliceWithError() + if err != nil { + return nil, err } if len(kvs) == 1 { - pp, e := dl.nodeSS.Unpack(kvs[0].Key) - if e != nil { - return nil, e + pp, err := dl.nodeSS.Unpack(kvs[0].Key) + if err != nil { + return nil, err } prevPrefix := pp[0].([]byte) if bytes.HasPrefix(key, prevPrefix) { @@ -485,17 +485,17 @@ func (dl directoryLayer) isPrefixFree(rtr fdb.ReadTransaction, prefix []byte) (b return false, nil } - nck, e := dl.nodeContainingKey(rtr, prefix) - if e != nil { - return false, e + nck, err := dl.nodeContainingKey(rtr, prefix) + if err != nil { + return false, err } if nck != nil { return false, nil } - kr, e := fdb.PrefixRange(prefix) - if e != nil { - return false, e + kr, err := fdb.PrefixRange(prefix) + if err != nil { + return false, err } bk, ek := kr.FDBRangeKeys() @@ -557,9 +557,9 @@ func (dl directoryLayer) initializeDirectory(tr fdb.Transaction) { } func (dl directoryLayer) contentsOfNode(node subspace.Subspace, path []string, layer []byte) (DirectorySubspace, error) { - p, e := dl.nodeSS.Unpack(node) - if e != nil { - return nil, e + p, err := dl.nodeSS.Unpack(node) + if err != nil { + return nil, err } prefix := p[0] diff --git a/bindings/go/src/fdb/directory/directorySubspace.go b/bindings/go/src/fdb/directory/directorySubspace.go index 3dc5998a342..bd7ba89703e 100644 --- a/bindings/go/src/fdb/directory/directorySubspace.go +++ b/bindings/go/src/fdb/directory/directorySubspace.go @@ -90,7 +90,7 @@ func (d directorySubspace) Exists(rt fdb.ReadTransactor, path []string) (bool, e return d.dl.Exists(rt, d.dl.partitionSubpath(d.path, path)) } -func (d directorySubspace) List(rt fdb.ReadTransactor, path []string) (subdirs []string, e error) { +func (d directorySubspace) List(rt fdb.ReadTransactor, path []string) (subdirs []string, err error) { return d.dl.List(rt, d.dl.partitionSubpath(d.path, path)) } diff --git a/bindings/go/src/fdb/doc.go b/bindings/go/src/fdb/doc.go index 6b5c3526a6c..68e07b2f469 100644 --- a/bindings/go/src/fdb/doc.go +++ b/bindings/go/src/fdb/doc.go @@ -32,40 +32,40 @@ extensive guidance to programming with FoundationDB, as well as API documentation for the other FoundationDB interfaces, please see https://apple.github.io/foundationdb/index.html. -Basic Usage +# Basic Usage A basic interaction with the FoundationDB API is demonstrated below: - package main + package main - import ( - "github.com/apple/foundationdb/bindings/go/src/fdb" - "log" - "fmt" - ) + import ( + "github.com/apple/foundationdb/bindings/go/src/fdb" + "log" + "fmt" + ) - func main() { - // Different API versions may expose different runtime behaviors. - fdb.MustAPIVersion(740) + func main() { + // Different API versions may expose different runtime behaviors. + fdb.MustAPIVersion(740) - // Open the default database from the system cluster - db := fdb.MustOpenDefault() + // Open the default database from the system cluster + db := fdb.MustOpenDefault() - // Database reads and writes happen inside transactions - ret, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { - tr.Set(fdb.Key("hello"), []byte("world")) - return tr.Get(fdb.Key("foo")).MustGet(), nil - // db.Transact automatically commits (and if necessary, - // retries) the transaction - }) - if e != nil { - log.Fatalf("Unable to perform FDB transaction (%v)", e) - } + // Database reads and writes happen inside transactions + ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Set(fdb.Key("hello"), []byte("world")) + return tr.Get(fdb.Key("foo")).MustGet(), nil + // db.Transact automatically commits (and if necessary, + // retries) the transaction + }) + if err != nil { + log.Fatalf("Unable to perform FDB transaction (%v)", err) + } - fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte))) - } + fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte))) + } -Futures +# Futures Many functions in this package are asynchronous and return Future objects. A Future represents a value (or error) to be available at some later @@ -79,7 +79,7 @@ and have multiple Future objects outstanding inside a single goroutine. All operations will execute in parallel, and the calling goroutine will not block until a blocking method on any one of the Futures is called. -On Panics +# On Panics Idiomatic Go code strongly frowns at panics that escape library/package boundaries, in favor of explicitly returned errors. Idiomatic FoundationDB @@ -87,28 +87,28 @@ client programs, however, are built around the idea of retryable programmer-provided transactional functions. Retryable transactions can be implemented using only error values: - ret, e := db.Transact(func (tr Transaction) (interface{}, error) { - // FoundationDB futures represent a value that will become available - futureValueOne := tr.Get(fdb.Key("foo")) - futureValueTwo := tr.Get(fdb.Key("bar")) + ret, err := db.Transact(func (tr Transaction) (interface{}, error) { + // FoundationDB futures represent a value that will become available + futureValueOne := tr.Get(fdb.Key("foo")) + futureValueTwo := tr.Get(fdb.Key("bar")) - // Both reads are being carried out in parallel + // Both reads are being carried out in parallel - // Get the first value (or any error) - valueOne, e := futureValueOne.Get() - if e != nil { - return nil, e - } + // Get the first value (or any error) + valueOne, err := futureValueOne.Get() + if err != nil { + return nil, err + } - // Get the second value (or any error) - valueTwo, e := futureValueTwo.Get() - if e != nil { - return nil, e - } + // Get the second value (or any error) + valueTwo, err := futureValueTwo.Get() + if err != nil { + return nil, err + } - // Return the two values - return []string{valueOne, valueTwo}, nil - }) + // Return the two values + return []string{valueOne, valueTwo}, nil + }) If either read encounters an error, it will be returned to Transact, which will determine if the error is retryable or not (using (Transaction).OnError). If the @@ -123,31 +123,31 @@ type also has a MustGet method, which returns the same type and value as Get, but exposes FoundationDB Errors via a panic rather than an explicitly returned error. The above example may be rewritten as: - ret, e := db.Transact(func (tr Transaction) (interface{}, error) { - // FoundationDB futures represent a value that will become available - futureValueOne := tr.Get(fdb.Key("foo")) - futureValueTwo := tr.Get(fdb.Key("bar")) + ret, err := db.Transact(func (tr Transaction) (interface{}, error) { + // FoundationDB futures represent a value that will become available + futureValueOne := tr.Get(fdb.Key("foo")) + futureValueTwo := tr.Get(fdb.Key("bar")) - // Both reads are being carried out in parallel + // Both reads are being carried out in parallel - // Get the first value - valueOne := futureValueOne.MustGet() - // Get the second value - valueTwo := futureValueTwo.MustGet() + // Get the first value + valueOne := futureValueOne.MustGet() + // Get the second value + valueTwo := futureValueTwo.MustGet() - // Return the two values - return []string{valueOne, valueTwo}, nil - }) + // Return the two values + return []string{valueOne, valueTwo}, nil + }) MustGet returns nil (which is different from empty slice []byte{}), when the key doesn't exist, and hence non-existence can be checked as follows: - val := tr.Get(fdb.Key("foobar")).MustGet() - if val == nil { - fmt.Println("foobar does not exist.") - } else { - fmt.Println("foobar exists.") - } + val := tr.Get(fdb.Key("foobar")).MustGet() + if val == nil { + fmt.Println("foobar does not exist.") + } else { + fmt.Println("foobar exists.") + } Any panic that occurs during execution of the caller-provided function will be recovered by the (Database).Transact method. If the error is an FDB Error, it @@ -159,7 +159,7 @@ Note that (Transaction).Transact also recovers panics, but does not itself retry. If the recovered value is an FDB Error, it will be returned to the caller of (Transaction).Transact; all other values will be re-panicked. -Transactions and Goroutines +# Transactions and Goroutines When using a Transactor in the fdb package, particular care must be taken if goroutines are created inside of the function passed to the Transact method. Any @@ -180,7 +180,7 @@ Given these complexities, it is generally best practice to use a single goroutine for each logical thread of interaction with FoundationDB, and allow each goroutine to block when necessary to wait for Futures to become ready. -Streaming Modes +# Streaming Modes When using GetRange methods in the FoundationDB API, clients can request large ranges of the database to iterate over. Making such a request doesn't @@ -198,7 +198,7 @@ reasonable default balance. Other streaming modes that prioritize throughput or latency are available -- see the documented StreamingMode values for specific options. -Atomic Operations +# Atomic Operations The FDB package provides a number of atomic operations on the Database and Transaction objects. An atomic operation is a single database command that diff --git a/bindings/go/src/fdb/fdb.go b/bindings/go/src/fdb/fdb.go index efe166eb9ad..1f5f1dcd854 100644 --- a/bindings/go/src/fdb/fdb.go +++ b/bindings/go/src/fdb/fdb.go @@ -145,9 +145,9 @@ func APIVersion(version int) error { return errAPIVersionNotSupported } - if e := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); e != 0 { - if e != 0 { - if e == 2203 { + if err := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); err != 0 { + if err != 0 { + if err == 2203 { maxSupportedVersion := C.fdb_get_max_api_version() if headerVersion > int(maxSupportedVersion) { return fmt.Errorf("This version of the FoundationDB Go binding is "+ @@ -157,7 +157,7 @@ func APIVersion(version int) error { } return fmt.Errorf("API version %d is not supported by the installed FoundationDB C library.", version) } - return Error{int(e)} + return Error{int(err)} } } @@ -166,7 +166,7 @@ func APIVersion(version int) error { return nil } -// Determines if an API version has already been selected, i.e., if +// Determines if an API version has already been selected, i.err., if // APIVersion or MustAPIVersion have already been called. func IsAPIVersionSelected() bool { return apiVersion != 0 @@ -240,16 +240,16 @@ func executeWithRunningNetworkThread(f func()) error { // check if meanwhile another goroutine started the network thread if !networkStarted { - if e := C.fdb_setup_network(); e != 0 { - return Error{int(e)} + if err := C.fdb_setup_network(); err != 0 { + return Error{int(err)} } networkRunning.Add(1) go func() { - e := C.fdb_run_network() + err := C.fdb_run_network() networkRunning.Done() - if e != 0 { - panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(e)), e)) + if err != 0 { + panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(err)), err)) } }() @@ -334,10 +334,10 @@ func OpenDatabase(clusterFile string) (Database, error) { var okDb bool anyy, exist := openDatabases.Load(clusterFile) if db, okDb = anyy.(Database); !exist || !okDb { - var e error - db, e = createDatabase(clusterFile) - if e != nil { - return Database{}, e + var err error + db, err = createDatabase(clusterFile) + if err != nil { + return Database{}, err } openDatabases.Store(clusterFile, db) } @@ -408,7 +408,7 @@ func createDatabase(clusterFile string) (Database, error) { // OpenWithConnectionString returns a database handle to the FoundationDB cluster identified // by the provided connection string. This method can be useful for scenarios where you want to connect -// to the database only for a short time e.g. to test different connection strings. +// to the database only for a short time err.g. to test different connection strings. // Caller must call Close() to release resources. func OpenWithConnectionString(connectionString string) (Database, error) { if apiVersion == 0 { @@ -511,11 +511,11 @@ func Printable(d []byte) string { return buf.String() } -func panicToError(e *error) { +func panicToError(err *error) { if r := recover(); r != nil { fe, ok := r.(Error) if ok { - *e = fe + *err = fe } else { panic(r) } diff --git a/bindings/go/src/fdb/fdb_test.go b/bindings/go/src/fdb/fdb_test.go index f24a5796aa6..107c2487e3b 100644 --- a/bindings/go/src/fdb/fdb_test.go +++ b/bindings/go/src/fdb/fdb_test.go @@ -34,19 +34,19 @@ import ( const API_VERSION int = 740 func ExampleOpenDefault() { - var e error + var err error - e = fdb.APIVersion(API_VERSION) - if e != nil { - fmt.Printf("Unable to set API version: %v\n", e) + err = fdb.APIVersion(API_VERSION) + if err != nil { + fmt.Printf("Unable to set API version: %v\n", err) return } // OpenDefault opens the database described by the platform-specific default // cluster file - db, e := fdb.OpenDefault() - if e != nil { - fmt.Printf("Unable to open default database: %v\n", e) + db, err := fdb.OpenDefault() + if err != nil { + fmt.Printf("Unable to open default database: %v\n", err) return } @@ -64,20 +64,20 @@ func TestVersionstamp(t *testing.T) { setVs := func(t fdb.Transactor, key fdb.Key) (fdb.FutureKey, error) { fmt.Printf("setOne called with: %T\n", t) - ret, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + ret, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.SetVersionstampedValue(key, []byte("blahblahbl\x00\x00\x00\x00")) return tr.GetVersionstamp(), nil }) - return ret.(fdb.FutureKey), e + return ret.(fdb.FutureKey), err } getOne := func(rt fdb.ReadTransactor, key fdb.Key) ([]byte, error) { fmt.Printf("getOne called with: %T\n", rt) - ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + ret, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.Get(key).MustGet(), nil }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return ret.([]byte), nil } @@ -85,20 +85,20 @@ func TestVersionstamp(t *testing.T) { var v []byte var fvs fdb.FutureKey var k fdb.Key - var e error + var err error - fvs, e = setVs(db, fdb.Key("foo")) - if e != nil { - t.Errorf("setOne failed %v", e) + fvs, err = setVs(db, fdb.Key("foo")) + if err != nil { + t.Errorf("setOne failed %v", err) } - v, e = getOne(db, fdb.Key("foo")) - if e != nil { - t.Errorf("getOne failed %v", e) + v, err = getOne(db, fdb.Key("foo")) + if err != nil { + t.Errorf("getOne failed %v", err) } t.Logf("getOne returned %s", v) - k, e = fvs.Get() - if e != nil { - t.Errorf("setOne wait failed %v", e) + k, err = fvs.Get() + if err != nil { + t.Errorf("setOne wait failed %v", err) } t.Log(k) t.Logf("setOne returned %s", k) @@ -109,30 +109,30 @@ func TestEstimatedRangeSize(t *testing.T) { db := fdb.MustOpenDefault() var f fdb.FutureInt64 - _, e := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { f = rtr.GetEstimatedRangeSizeBytes(subspace.AllKeys()) return nil, nil }) - if e != nil { - t.Error(e) + if err != nil { + t.Error(err) } - _, e = f.Get() - if e != nil { - t.Error(e) + _, err = f.Get() + if err != nil { + t.Error(err) } } func TestReadTransactionOptions(t *testing.T) { fdb.MustAPIVersion(API_VERSION) db := fdb.MustOpenDefault() - _, e := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { rtr.Options().SetAccessSystemKeys() return rtr.Get(fdb.Key("\xff/")).MustGet(), nil }) - if e != nil { - t.Errorf("Failed to read system key: %s", e) + if err != nil { + t.Errorf("Failed to read system key: %s", err) } } @@ -142,37 +142,37 @@ func ExampleTransactor() { setOne := func(t fdb.Transactor, key fdb.Key, value []byte) error { fmt.Printf("setOne called with: %T\n", t) - _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { // We don't actually call tr.Set here to avoid mutating a real database. // tr.Set(key, value) return nil, nil }) - return e + return err } setMany := func(t fdb.Transactor, value []byte, keys ...fdb.Key) error { fmt.Printf("setMany called with: %T\n", t) - _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { for _, key := range keys { setOne(tr, key, value) } return nil, nil }) - return e + return err } - var e error + var err error fmt.Println("Calling setOne with a database:") - e = setOne(db, []byte("foo"), []byte("bar")) - if e != nil { - fmt.Println(e) + err = setOne(db, []byte("foo"), []byte("bar")) + if err != nil { + fmt.Println(err) return } fmt.Println("\nCalling setMany with a database:") - e = setMany(db, []byte("bar"), fdb.Key("foo1"), fdb.Key("foo2"), fdb.Key("foo3")) - if e != nil { - fmt.Println(e) + err = setMany(db, []byte("bar"), fdb.Key("foo1"), fdb.Key("foo2"), fdb.Key("foo3")) + if err != nil { + fmt.Println(err) return } @@ -193,40 +193,40 @@ func ExampleReadTransactor() { getOne := func(rt fdb.ReadTransactor, key fdb.Key) ([]byte, error) { fmt.Printf("getOne called with: %T\n", rt) - ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + ret, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.Get(key).MustGet(), nil }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return ret.([]byte), nil } getTwo := func(rt fdb.ReadTransactor, key1, key2 fdb.Key) ([][]byte, error) { fmt.Printf("getTwo called with: %T\n", rt) - ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + ret, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r1, _ := getOne(rtr, key1) r2, _ := getOne(rtr.Snapshot(), key2) return [][]byte{r1, r2}, nil }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return ret.([][]byte), nil } - var e error + var err error fmt.Println("Calling getOne with a database:") - _, e = getOne(db, fdb.Key("foo")) - if e != nil { - fmt.Println(e) + _, err = getOne(db, fdb.Key("foo")) + if err != nil { + fmt.Println(err) return } fmt.Println("\nCalling getTwo with a database:") - _, e = getTwo(db, fdb.Key("foo"), fdb.Key("bar")) - if e != nil { - fmt.Println(e) + _, err = getTwo(db, fdb.Key("foo"), fdb.Key("bar")) + if err != nil { + fmt.Println(err) return } @@ -244,9 +244,9 @@ func ExamplePrefixRange() { fdb.MustAPIVersion(API_VERSION) db := fdb.MustOpenDefault() - tr, e := db.CreateTransaction() - if e != nil { - fmt.Printf("Unable to create transaction: %v\n", e) + tr, err := db.CreateTransaction() + if err != nil { + fmt.Printf("Unable to create transaction: %v\n", err) return } @@ -265,9 +265,9 @@ func ExamplePrefixRange() { pr, _ := fdb.PrefixRange([]byte("alphabet")) // Read and process the range - kvs, e := tr.GetRange(pr, fdb.RangeOptions{}).GetSliceWithError() - if e != nil { - fmt.Printf("Unable to read range: %v\n", e) + kvs, err := tr.GetRange(pr, fdb.RangeOptions{}).GetSliceWithError() + if err != nil { + fmt.Printf("Unable to read range: %v\n", err) } for _, kv := range kvs { fmt.Printf("%s: %s\n", string(kv.Key), string(kv.Value)) @@ -283,9 +283,9 @@ func ExampleRangeIterator() { fdb.MustAPIVersion(API_VERSION) db := fdb.MustOpenDefault() - tr, e := db.CreateTransaction() - if e != nil { - fmt.Printf("Unable to create transaction: %v\n", e) + tr, err := db.CreateTransaction() + if err != nil { + fmt.Printf("Unable to create transaction: %v\n", err) return } @@ -301,9 +301,9 @@ func ExampleRangeIterator() { // Advance will return true until the iterator is exhausted for ri.Advance() { - kv, e := ri.Get() - if e != nil { - fmt.Printf("Unable to read next value: %v\n", e) + kv, err := ri.Get() + if err != nil { + fmt.Printf("Unable to read next value: %v\n", err) return } fmt.Printf("%s is %s\n", kv.Key, kv.Value) diff --git a/bindings/go/src/fdb/range.go b/bindings/go/src/fdb/range.go index 36d35bd4e40..fdb11ccb8b4 100644 --- a/bindings/go/src/fdb/range.go +++ b/bindings/go/src/fdb/range.go @@ -161,9 +161,9 @@ func (rr RangeResult) GetSliceWithError() ([]KeyValue, error) { // complete. The current goroutine will be blocked until all reads have // completed. func (rr RangeResult) GetSliceOrPanic() []KeyValue { - kvs, e := rr.GetSliceWithError() - if e != nil { - panic(e) + kvs, err := rr.GetSliceWithError() + if err != nil { + panic(err) } return kvs } @@ -256,9 +256,9 @@ func (ri *RangeIterator) fetchNextBatch() { // asynchronous operations associated with this range did not successfully // complete. The Advance method of this RangeIterator must have returned true // prior to calling Get. -func (ri *RangeIterator) Get() (kv KeyValue, e error) { +func (ri *RangeIterator) Get() (kv KeyValue, err error) { if ri.err != nil { - e = ri.err + err = ri.err return } @@ -278,9 +278,9 @@ func (ri *RangeIterator) Get() (kv KeyValue, e error) { // complete. The Advance method of this RangeIterator must have returned true // prior to calling MustGet. func (ri *RangeIterator) MustGet() KeyValue { - kv, e := ri.Get() - if e != nil { - panic(e) + kv, err := ri.Get() + if err != nil { + panic(err) } return kv } @@ -310,9 +310,9 @@ func Strinc(prefix []byte) ([]byte, error) { func PrefixRange(prefix []byte) (KeyRange, error) { begin := make([]byte, len(prefix)) copy(begin, prefix) - end, e := Strinc(begin) - if e != nil { - return KeyRange{}, e + end, err := Strinc(begin) + if err != nil { + return KeyRange{}, err } return KeyRange{Key(begin), Key(end)}, nil } diff --git a/bindings/go/src/fdb/snapshot.go b/bindings/go/src/fdb/snapshot.go index 87ccb0c5023..4a2d7095030 100644 --- a/bindings/go/src/fdb/snapshot.go +++ b/bindings/go/src/fdb/snapshot.go @@ -46,10 +46,10 @@ type Snapshot struct { // // See the ReadTransactor interface for an example of using ReadTransact with // Transaction, Snapshot and Database objects. -func (s Snapshot) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, e error) { - defer panicToError(&e) +func (s Snapshot) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, err error) { + defer panicToError(&err) - r, e = f(s) + r, err = f(s) return } diff --git a/bindings/go/src/fdb/transaction.go b/bindings/go/src/fdb/transaction.go index 56fffd69450..3fd0ab10fe4 100644 --- a/bindings/go/src/fdb/transaction.go +++ b/bindings/go/src/fdb/transaction.go @@ -116,10 +116,10 @@ func (t Transaction) GetDatabase() Database { // // See the Transactor interface for an example of using Transact with // Transaction and Database objects. -func (t Transaction) Transact(f func(Transaction) (interface{}, error)) (r interface{}, e error) { - defer panicToError(&e) +func (t Transaction) Transact(f func(Transaction) (interface{}, error)) (r interface{}, err error) { + defer panicToError(&err) - r, e = f(t) + r, err = f(t) return } @@ -136,10 +136,10 @@ func (t Transaction) Transact(f func(Transaction) (interface{}, error)) (r inter // // See the ReadTransactor interface for an example of using ReadTransact with // Transaction, Snapshot and Database objects. -func (t Transaction) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, e error) { - defer panicToError(&e) +func (t Transaction) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, err error) { + defer panicToError(&err) - r, e = f(t) + r, err = f(t) return } @@ -189,9 +189,9 @@ func (t Transaction) Snapshot() Snapshot { // // Typical code will not use OnError directly. (Database).Transact uses // OnError internally to implement a correct retry loop. -func (t Transaction) OnError(e Error) FutureNil { +func (t Transaction) OnError(err Error) FutureNil { return &futureNil{ - future: newFuture(t.transaction, C.fdb_transaction_on_error(t.ptr, C.fdb_error_t(e.Code))), + future: newFuture(t.transaction, C.fdb_transaction_on_error(t.ptr, C.fdb_error_t(err.Code))), } } diff --git a/bindings/go/src/fdb/tuple/tuple.go b/bindings/go/src/fdb/tuple/tuple.go index a7922801057..871c65cef78 100644 --- a/bindings/go/src/fdb/tuple/tuple.go +++ b/bindings/go/src/fdb/tuple/tuple.go @@ -365,51 +365,51 @@ func (p *packer) encodeTuple(t Tuple, nested bool, versionstamps bool) { p.putByte(nestedCode) } - for i, e := range t { - switch e := e.(type) { + for i, err := range t { + switch err := err.(type) { case Tuple: - p.encodeTuple(e, true, versionstamps) + p.encodeTuple(err, true, versionstamps) case nil: p.putByte(nilCode) if nested { p.putByte(0xff) } case int: - p.encodeInt(int64(e)) + p.encodeInt(int64(err)) case int64: - p.encodeInt(e) + p.encodeInt(err) case uint: - p.encodeUint(uint64(e)) + p.encodeUint(uint64(err)) case uint64: - p.encodeUint(e) + p.encodeUint(err) case *big.Int: - p.encodeBigInt(e) + p.encodeBigInt(err) case big.Int: - p.encodeBigInt(&e) + p.encodeBigInt(&err) case []byte: - p.encodeBytes(bytesCode, e) + p.encodeBytes(bytesCode, err) case fdb.KeyConvertible: - p.encodeBytes(bytesCode, []byte(e.FDBKey())) + p.encodeBytes(bytesCode, []byte(err.FDBKey())) case string: - p.encodeBytes(stringCode, []byte(e)) + p.encodeBytes(stringCode, []byte(err)) case float32: - p.encodeFloat(e) + p.encodeFloat(err) case float64: - p.encodeDouble(e) + p.encodeDouble(err) case bool: - if e { + if err { p.putByte(trueCode) } else { p.putByte(falseCode) } case UUID: - p.encodeUUID(e) + p.encodeUUID(err) case Versionstamp: - if versionstamps == false && e.TransactionVersion == incompleteTransactionVersion { + if versionstamps == false && err.TransactionVersion == incompleteTransactionVersion { panic(fmt.Sprintf("Incomplete Versionstamp included in vanilla tuple pack")) } - p.encodeVersionstamp(e) + p.encodeVersionstamp(err) default: panic(fmt.Sprintf("unencodable element at index %d (%v, type %T)", i, t[i], t[i])) } @@ -433,7 +433,6 @@ func (p *packer) encodeTuple(t Tuple, nested bool, versionstamps bool) { // // This method will panic if it contains an incomplete Versionstamp. Use // PackWithVersionstamp instead. -// func (t Tuple) Pack() []byte { p := newPacker() p.encodeTuple(t, false, false) @@ -507,13 +506,13 @@ func (t Tuple) countIncompleteVersionstamps() int { incompleteCount := 0 for _, el := range t { - switch e := el.(type) { + switch err := el.(type) { case Versionstamp: - if e.TransactionVersion == incompleteTransactionVersion { + if err.TransactionVersion == incompleteTransactionVersion { incompleteCount++ } case Tuple: - incompleteCount += e.countIncompleteVersionstamps() + incompleteCount += err.countIncompleteVersionstamps() } } @@ -755,8 +754,8 @@ func (t Tuple) FDBRangeKeys() (fdb.KeyConvertible, fdb.KeyConvertible) { // (that is, all tuples of greater length than the Tuple of which the Tuple is a // prefix). func (t Tuple) FDBRangeKeySelectors() (fdb.Selectable, fdb.Selectable) { - b, e := t.FDBRangeKeys() - return fdb.FirstGreaterOrEqual(b), fdb.FirstGreaterOrEqual(e) + b, err := t.FDBRangeKeys() + return fdb.FirstGreaterOrEqual(b), fdb.FirstGreaterOrEqual(err) } func concat(a []byte, b ...byte) []byte { diff --git a/contrib/mockkms/get_encryption_keys.go b/contrib/mockkms/get_encryption_keys.go index be70cfbc9c2..84ad8130a6c 100644 --- a/contrib/mockkms/get_encryption_keys.go +++ b/contrib/mockkms/get_encryption_keys.go @@ -27,40 +27,40 @@ package main import ( - "encoding/json" - "fmt" - "io/ioutil" - "log" - "math/rand" - "net/http" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "math/rand" + "net/http" ) type CipherDetailRes struct { - BaseCipherId uint64 `json:"base_cipher_id"` - EncryptDomainId int64 `json:"encrypt_domain_id"` - BaseCipher string `json:"base_cipher"` + BaseCipherId uint64 `json:"base_cipher_id"` + EncryptDomainId int64 `json:"encrypt_domain_id"` + BaseCipher string `json:"base_cipher"` } type ValidationToken struct { - TokenName string `json:"token_name"` - TokenValue string `json:"token_value"` + TokenName string `json:"token_name"` + TokenValue string `json:"token_value"` } type CipherDetailReq struct { - BaseCipherId uint64 `json:"base_cipher_id"` - EncryptDomainId int64 `json:"encrypt_domain_id"` + BaseCipherId uint64 `json:"base_cipher_id"` + EncryptDomainId int64 `json:"encrypt_domain_id"` } type GetEncryptKeysResponse struct { - CipherDetails []CipherDetailRes `json:"cipher_key_details"` - KmsUrls []string `json:"kms_urls"` + CipherDetails []CipherDetailRes `json:"cipher_key_details"` + KmsUrls []string `json:"kms_urls"` } type GetEncryptKeysRequest struct { - QueryMode string `json:"query_mode"` - CipherDetails []CipherDetailReq `json:"cipher_key_details"` - ValidationTokens []ValidationToken `json:"validation_tokens"` - RefreshKmsUrls bool `json:"refresh_kms_urls"` + QueryMode string `json:"query_mode"` + CipherDetails []CipherDetailReq `json:"cipher_key_details"` + ValidationTokens []ValidationToken `json:"validation_tokens"` + RefreshKmsUrls bool `json:"refresh_kms_urls"` } type cipherMapInstanceSingleton map[uint64][]byte @@ -74,248 +74,248 @@ const ( ) const ( - maxCipherKeys = uint64(1024*1024) // Max cipher keys - maxCipherSize = 16 // Max cipher buffer size + maxCipherKeys = uint64(1024 * 1024) // Max cipher keys + maxCipherSize = 16 // Max cipher buffer size ) var ( - cipherMapInstance cipherMapInstanceSingleton // Singleton mapping of { baseCipherId -> baseCipher } + cipherMapInstance cipherMapInstanceSingleton // Singleton mapping of { baseCipherId -> baseCipher } ) // const mapping of { Location -> errorString } func errStrMap() func(int) string { - _errStrMap := map[int]string { - READ_HTTP_REQUEST_BODY : "Http request body read error", - UNMARSHAL_REQUEST_BODY_JSON : "Http request body unmarshal error", - UNSUPPORTED_QUERY_MODE : "Unsupported query_mode", - PARSE_HTTP_REQUEST : "Error parsing GetEncryptionKeys request", - MARSHAL_RESPONSE : "Error marshaling response", - } - - return func(key int) string { - return _errStrMap[key] - } + _errStrMap := map[int]string{ + READ_HTTP_REQUEST_BODY: "Http request body read error", + UNMARSHAL_REQUEST_BODY_JSON: "Http request body unmarshal error", + UNSUPPORTED_QUERY_MODE: "Unsupported query_mode", + PARSE_HTTP_REQUEST: "Error parsing GetEncryptionKeys request", + MARSHAL_RESPONSE: "Error marshaling response", + } + + return func(key int) string { + return _errStrMap[key] + } } // Caller is responsible for thread synchronization. Recommended to be invoked during package::init() func NewCipherMap(maxKeys uint64, cipherSize int) cipherMapInstanceSingleton { - if cipherMapInstance == nil { - cipherMapInstance = make(map[uint64][]byte) - - for i := uint64(1); i<= maxKeys; i++ { - cipher := make([]byte, cipherSize) - rand.Read(cipher) - cipherMapInstance[i] = cipher - } - log.Printf("KMS cipher map populate done, maxCiphers '%d'", maxCipherKeys) - } - return cipherMapInstance + if cipherMapInstance == nil { + cipherMapInstance = make(map[uint64][]byte) + + for i := uint64(1); i <= maxKeys; i++ { + cipher := make([]byte, cipherSize) + rand.Read(cipher) + cipherMapInstance[i] = cipher + } + log.Printf("KMS cipher map populate done, maxCiphers '%d'", maxCipherKeys) + } + return cipherMapInstance } func getKmsUrls() (urls []string) { - urlCount := rand.Intn(5) + 1 - for i := 1; i <= urlCount; i++ { - url := fmt.Sprintf("https://KMS/%d:%d:%d:%d", i, i, i, i) - urls = append(urls, url) - } - return + urlCount := rand.Intn(5) + 1 + for i := 1; i <= urlCount; i++ { + url := fmt.Sprintf("https://KMS/%d:%d:%d:%d", i, i, i, i) + urls = append(urls, url) + } + return } func isEncryptDomainIdValid(id int64) bool { - if id > 0 || id == -1 || id == -2 { - return true - } - return false + if id > 0 || id == -1 || id == -2 { + return true + } + return false } func abs(x int64) int64 { - if x < 0 { - return -x - } - return x + if x < 0 { + return -x + } + return x } func getBaseCipherIdFromDomainId(domainId int64) (baseCipherId uint64) { - baseCipherId = uint64(1) + uint64(abs(domainId)) % maxCipherKeys - return + baseCipherId = uint64(1) + uint64(abs(domainId))%maxCipherKeys + return } func getEncryptionKeysByKeyIds(w http.ResponseWriter, byteArr []byte) { - req := GetEncryptKeysRequest{} - err := json.Unmarshal(byteArr, &req) - if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { - var e error - if shouldInjectFault(PARSE_HTTP_REQUEST) { - e = fmt.Errorf("[FAULT] %s %s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) - } else { - e = fmt.Errorf("%s %s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - var details []CipherDetailRes - for i := 0; i < len(req.CipherDetails); i++ { - var baseCipherId = uint64(req.CipherDetails[i].BaseCipherId) - - var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) - if !isEncryptDomainIdValid(encryptDomainId) { - e := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) - sendErrorResponse(w, e) - return - } - - cipher, found := cipherMapInstance[baseCipherId] - if !found { - e := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) - sendErrorResponse(w, e) - return - } - - var detail = CipherDetailRes { - BaseCipherId: baseCipherId, - EncryptDomainId: encryptDomainId, - BaseCipher: string(cipher), - } - details = append(details, detail) - } - - var urls []string - if req.RefreshKmsUrls { - urls = getKmsUrls() - } - - resp := GetEncryptKeysResponse{ - CipherDetails: details, - KmsUrls: urls, - } - - mResp, err := json.Marshal(resp) - if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { - var e error - if shouldInjectFault(MARSHAL_RESPONSE) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - fmt.Fprintf(w, string(mResp)) + req := GetEncryptKeysRequest{} + err := json.Unmarshal(byteArr, &req) + if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { + var err error + if shouldInjectFault(PARSE_HTTP_REQUEST) { + err = fmt.Errorf("[FAULT] %s %s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) + } else { + err = fmt.Errorf("%s %s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + var details []CipherDetailRes + for i := 0; i < len(req.CipherDetails); i++ { + var baseCipherId = uint64(req.CipherDetails[i].BaseCipherId) + + var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) + if !isEncryptDomainIdValid(encryptDomainId) { + err := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) + sendErrorResponse(w, err) + return + } + + cipher, found := cipherMapInstance[baseCipherId] + if !found { + err := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) + sendErrorResponse(w, err) + return + } + + var detail = CipherDetailRes{ + BaseCipherId: baseCipherId, + EncryptDomainId: encryptDomainId, + BaseCipher: string(cipher), + } + details = append(details, detail) + } + + var urls []string + if req.RefreshKmsUrls { + urls = getKmsUrls() + } + + resp := GetEncryptKeysResponse{ + CipherDetails: details, + KmsUrls: urls, + } + + mResp, err := json.Marshal(resp) + if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { + var err error + if shouldInjectFault(MARSHAL_RESPONSE) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + fmt.Fprintf(w, string(mResp)) } func getEncryptionKeysByDomainIds(w http.ResponseWriter, byteArr []byte) { req := GetEncryptKeysRequest{} - err := json.Unmarshal(byteArr, &req) - if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { - var e error - if shouldInjectFault(PARSE_HTTP_REQUEST) { - e = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) - } else { - e = fmt.Errorf("%s '%s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - var details []CipherDetailRes - for i := 0; i < len(req.CipherDetails); i++ { - var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) - if !isEncryptDomainIdValid(encryptDomainId) { - e := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) - sendErrorResponse(w, e) - return - } - - var baseCipherId = getBaseCipherIdFromDomainId(encryptDomainId) - cipher, found := cipherMapInstance[baseCipherId] - if !found { - e := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) - sendErrorResponse(w, e) - return - } - - var detail = CipherDetailRes { - BaseCipherId: baseCipherId, - EncryptDomainId: encryptDomainId, - BaseCipher: string(cipher), - } - details = append(details, detail) - } - - var urls []string - if req.RefreshKmsUrls { - urls = getKmsUrls() - } - - resp := GetEncryptKeysResponse{ - CipherDetails: details, - KmsUrls: urls, - } - - mResp, err := json.Marshal(resp) - if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { - var e error - if shouldInjectFault(MARSHAL_RESPONSE) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - fmt.Fprintf(w, string(mResp)) + err := json.Unmarshal(byteArr, &req) + if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { + var err error + if shouldInjectFault(PARSE_HTTP_REQUEST) { + err = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) + } else { + err = fmt.Errorf("%s '%s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + var details []CipherDetailRes + for i := 0; i < len(req.CipherDetails); i++ { + var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) + if !isEncryptDomainIdValid(encryptDomainId) { + err := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) + sendErrorResponse(w, err) + return + } + + var baseCipherId = getBaseCipherIdFromDomainId(encryptDomainId) + cipher, found := cipherMapInstance[baseCipherId] + if !found { + err := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) + sendErrorResponse(w, err) + return + } + + var detail = CipherDetailRes{ + BaseCipherId: baseCipherId, + EncryptDomainId: encryptDomainId, + BaseCipher: string(cipher), + } + details = append(details, detail) + } + + var urls []string + if req.RefreshKmsUrls { + urls = getKmsUrls() + } + + resp := GetEncryptKeysResponse{ + CipherDetails: details, + KmsUrls: urls, + } + + mResp, err := json.Marshal(resp) + if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { + var err error + if shouldInjectFault(MARSHAL_RESPONSE) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + fmt.Fprintf(w, string(mResp)) } func handleGetEncryptionKeys(w http.ResponseWriter, r *http.Request) { - byteArr, err := ioutil.ReadAll(r.Body) - if err != nil || shouldInjectFault(READ_HTTP_REQUEST_BODY) { - var e error - if shouldInjectFault(READ_HTTP_REQUEST_BODY) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(READ_HTTP_REQUEST_BODY)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(READ_HTTP_REQUEST_BODY), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - var arbitrary_json map[string]interface{} - err = json.Unmarshal(byteArr, &arbitrary_json) - if err != nil || shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { - var e error - if shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - if shouldInjectFault(UNSUPPORTED_QUERY_MODE) { - err = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) - sendErrorResponse(w, err) - return - } else if arbitrary_json["query_mode"] == "lookupByKeyId" { - getEncryptionKeysByKeyIds(w, byteArr) - } else if arbitrary_json["query_mode"] == "lookupByDomainId" { - getEncryptionKeysByDomainIds(w, byteArr) - } else { - err = fmt.Errorf("%s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) - sendErrorResponse(w, err) - return - } + byteArr, err := ioutil.ReadAll(r.Body) + if err != nil || shouldInjectFault(READ_HTTP_REQUEST_BODY) { + var err error + if shouldInjectFault(READ_HTTP_REQUEST_BODY) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(READ_HTTP_REQUEST_BODY)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(READ_HTTP_REQUEST_BODY), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + var arbitrary_json map[string]interface{} + err = json.Unmarshal(byteArr, &arbitrary_json) + if err != nil || shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { + var err error + if shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + if shouldInjectFault(UNSUPPORTED_QUERY_MODE) { + err = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) + sendErrorResponse(w, err) + return + } else if arbitrary_json["query_mode"] == "lookupByKeyId" { + getEncryptionKeysByKeyIds(w, byteArr) + } else if arbitrary_json["query_mode"] == "lookupByDomainId" { + getEncryptionKeysByDomainIds(w, byteArr) + } else { + err = fmt.Errorf("%s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) + sendErrorResponse(w, err) + return + } } func initEncryptCipherMap() { - cipherMapInstance = NewCipherMap(maxCipherKeys, maxCipherSize) -} \ No newline at end of file + cipherMapInstance = NewCipherMap(maxCipherKeys, maxCipherSize) +} diff --git a/contrib/mockkms/utils.go b/contrib/mockkms/utils.go index 30457e8164d..d8d1bcf28cb 100644 --- a/contrib/mockkms/utils.go +++ b/contrib/mockkms/utils.go @@ -21,31 +21,31 @@ package main import ( - "encoding/json" + "encoding/json" "fmt" "log" "net/http" ) type ErrorDetail struct { - Detail string `json:"details"` + Detail string `json:"details"` } type ErrorResponse struct { - Err ErrorDetail `json:"error"` + Err ErrorDetail `json:"error"` } func sendErrorResponse(w http.ResponseWriter, err error) { - e := ErrorDetail{} - e.Detail = fmt.Sprintf("Error: %s", err.Error()) - resp := ErrorResponse{ - Err: e, - } + e := ErrorDetail{} + e.Detail = fmt.Sprintf("Error: %s", err.Error()) + resp := ErrorResponse{ + Err: e, + } - mResp,err := json.Marshal(resp) - if err != nil { - log.Printf("Error marshalling error response %s", err.Error()) - panic(err) - } - fmt.Fprintf(w, string(mResp)) -} \ No newline at end of file + mResp, err := json.Marshal(resp) + if err != nil { + log.Printf("Error marshalling error response %s", err.Error()) + panic(err) + } + fmt.Fprintf(w, string(mResp)) +} diff --git a/packaging/docker/samples/golang/app/main.go b/packaging/docker/samples/golang/app/main.go index 20386d27be4..f3cac5a08e0 100644 --- a/packaging/docker/samples/golang/app/main.go +++ b/packaging/docker/samples/golang/app/main.go @@ -1,6 +1,6 @@ // main.go // -// This source file is part of the FoundationDB open source project +// # This source file is part of the FoundationDB open source project // // Copyright 2013-2024 Apple Inc. and the FoundationDB project authors // @@ -8,14 +8,13 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -// package main import ( @@ -50,7 +49,7 @@ func main() { } func incrementCounter(w http.ResponseWriter, r *http.Request) { - ret, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { value := tr.Get(fdb.Key("my-counter")).MustGet() if len(value) == 0 { value = intToBytes(0) @@ -61,8 +60,8 @@ func incrementCounter(w http.ResponseWriter, r *http.Request) { return intToBytes(counter), nil }) - if e != nil { - log.Fatalf("Unable to perform FDB transaction (%v)", e) + if err != nil { + log.Fatalf("Unable to perform FDB transaction (%v)", err) } fmt.Fprintf(w, "Counter is %d", bytesToInt(ret.([]byte))) diff --git a/recipes/go-recipes/blob.go b/recipes/go-recipes/blob.go index d4cbcc91d4e..7460b237416 100644 --- a/recipes/go-recipes/blob.go +++ b/recipes/go-recipes/blob.go @@ -97,9 +97,9 @@ func main() { write_blob(db, blobspace, test) - ret, e := read_blob(db, blobspace) + ret, err := read_blob(db, blobspace) - if e == nil { + if err == nil { fmt.Println(string(ret)) } } diff --git a/recipes/go-recipes/doc.go b/recipes/go-recipes/doc.go index 561718dedb0..1e41eb7325f 100644 --- a/recipes/go-recipes/doc.go +++ b/recipes/go-recipes/doc.go @@ -58,8 +58,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i diff --git a/recipes/go-recipes/indirect.go b/recipes/go-recipes/indirect.go index 985f8371608..16f89910dcc 100644 --- a/recipes/go-recipes/indirect.go +++ b/recipes/go-recipes/indirect.go @@ -56,8 +56,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i diff --git a/recipes/go-recipes/multi.go b/recipes/go-recipes/multi.go index 3de21d6053b..1b3d36b9152 100644 --- a/recipes/go-recipes/multi.go +++ b/recipes/go-recipes/multi.go @@ -71,8 +71,8 @@ func (multi MultiMap) MultiSubtract(trtr fdb.Transactor, index, value interface{ }) } -func (multi MultiMap) MultiGet(tr fdb.ReadTransactor, index int) (ret []interface{}, e error) { - _, e = tr.ReadTransact(func(tr fdb.ReadTransaction) (interface{}, error) { +func (multi MultiMap) MultiGet(tr fdb.ReadTransactor, index int) (ret []interface{}, err error) { + _, err = tr.ReadTransact(func(tr fdb.ReadTransaction) (interface{}, error) { pr, err := fdb.PrefixRange(multi.MapSS.Pack(tuple.Tuple{index})) if err != nil { return nil, err @@ -94,7 +94,7 @@ func (multi MultiMap) MultiGet(tr fdb.ReadTransactor, index int) (ret []interfac } func (multi MultiMap) MultiGetCounts(trtr fdb.Transactor, index interface{}) (map[interface{}]int, error) { - i, e := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { + i, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { kr, err := fdb.PrefixRange(multi.MapSS.Pack(tuple.Tuple{})) if err != nil { return nil, err @@ -118,7 +118,7 @@ func (multi MultiMap) MultiGetCounts(trtr fdb.Transactor, index interface{}) (ma } return counts, nil }) - return i.(map[interface{}]int), e + return i.(map[interface{}]int), err } func (multi MultiMap) MultiIsElement(trtr fdb.Transactor, index, value interface{}) bool { diff --git a/recipes/go-recipes/priority.go b/recipes/go-recipes/priority.go index 13b6968f593..346fd00dac4 100644 --- a/recipes/go-recipes/priority.go +++ b/recipes/go-recipes/priority.go @@ -46,8 +46,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i @@ -66,20 +66,20 @@ func (prty Priority) Push(trtr fdb.Transactor, value interface{}, priority int) func (prty Priority) _NextCount(trtr fdb.Transactor, priority int) int { res, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { - kr, e := fdb.PrefixRange(prty.PrioritySS.Pack(tuple.Tuple{priority})) - if e != nil { - return nil, e + kr, err := fdb.PrefixRange(prty.PrioritySS.Pack(tuple.Tuple{priority})) + if err != nil { + return nil, err } - ks, e := tr.Snapshot().GetRange(kr, fdb.RangeOptions{1, -1, true}).GetSliceWithError() - if e != nil { - return nil, e + ks, err := tr.Snapshot().GetRange(kr, fdb.RangeOptions{1, -1, true}).GetSliceWithError() + if err != nil { + return nil, err } if len(ks) == 0 { return 0, nil } - k, e := prty.PrioritySS.Unpack(ks[0].Key) + k, err := prty.PrioritySS.Unpack(ks[0].Key) return k[0].(int) + 1, nil }) if err != nil { @@ -142,7 +142,7 @@ func main() { p.Push(db, "b", 5) p.Push(db, "c", 2) p.Push(db, "d", 4) - p.Push(db, "e", 3) + p.Push(db, "err", 3) fmt.Println(p.Peek(db, false)) fmt.Println(p.Peek(db, false)) diff --git a/recipes/go-recipes/queue.go b/recipes/go-recipes/queue.go index 64340d7f0f6..dc49b944294 100644 --- a/recipes/go-recipes/queue.go +++ b/recipes/go-recipes/queue.go @@ -55,7 +55,7 @@ func (q *Queue) NewQueue(ss subspace.Subspace) { } func (q *Queue) Dequeue(trtr fdb.Transactor) (interface{}, error) { - i, e := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { + i, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { item, err := q.FirstItem(tr) if err != nil { return nil, err @@ -63,11 +63,11 @@ func (q *Queue) Dequeue(trtr fdb.Transactor) (interface{}, error) { tr.Clear(item.(fdb.KeyValue).Key) return item.(fdb.KeyValue).Value, err }) - return i, e + return i, err } func (q *Queue) Enqueue(trtr fdb.Transactor, item interface{}) (interface{}, error) { - i, e := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { + i, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { index, err := q.LastIndex(tr) if err != nil { return nil, err @@ -82,29 +82,29 @@ func (q *Queue) Enqueue(trtr fdb.Transactor, item interface{}) (interface{}, err return nil, nil }) - return i, e + return i, err } func (q *Queue) LastIndex(trtr fdb.Transactor) (interface{}, error) { - i, e := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + i, err := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r, err := rtr.Snapshot().GetRange(q.QueueSS, fdb.RangeOptions{1, 0, true}).GetSliceWithError() if len(r) == 0 { return q.QueueSS.Pack(tuple.Tuple{0}), nil } return r[0].Key, err }) - return i, e + return i, err } func (q *Queue) FirstItem(trtr fdb.Transactor) (interface{}, error) { - i, e := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + i, err := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r, err := rtr.GetRange(q.QueueSS, fdb.RangeOptions{1, 0, false}).GetSliceWithError() if len(r) == 0 { return nil, EmptyQueueError{} } return r[0], err }) - return i, e + return i, err } func main() { @@ -129,9 +129,9 @@ func main() { q.Enqueue(db, "test2") q.Enqueue(db, "test3") for i := 0; i < 5; i++ { - item, e := q.Dequeue(db) - if e != nil { - log.Fatal(e) + item, err := q.Dequeue(db) + if err != nil { + log.Fatal(err) } fmt.Println(string(item.([]byte))) diff --git a/recipes/go-recipes/table.go b/recipes/go-recipes/table.go index d41cb2ea7a1..6f768c75225 100644 --- a/recipes/go-recipes/table.go +++ b/recipes/go-recipes/table.go @@ -45,8 +45,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i @@ -96,14 +96,14 @@ func (tbl Table) TableSetRow(trtr fdb.Transactor, row int, cols ...interface{}) func (tbl Table) TableGetRow(tr fdb.ReadTransactor, row int) ([]interface{}, error) { item, err := tr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - kr, e := fdb.PrefixRange(tbl.row.Pack(tuple.Tuple{row})) - if e != nil { - return nil, e + kr, err := fdb.PrefixRange(tbl.row.Pack(tuple.Tuple{row})) + if err != nil { + return nil, err } - slice, e := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() - if e != nil { - return nil, e + slice, err := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() + if err != nil { + return nil, err } ret := make([]interface{}, len(slice)) @@ -122,14 +122,14 @@ func (tbl Table) TableGetRow(tr fdb.ReadTransactor, row int) ([]interface{}, err func (tbl Table) TableGetCol(tr fdb.ReadTransactor, col int) ([]interface{}, error) { item, err := tr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - kr, e := fdb.PrefixRange(tbl.col.Pack(tuple.Tuple{col})) - if e != nil { - return nil, e + kr, err := fdb.PrefixRange(tbl.col.Pack(tuple.Tuple{col})) + if err != nil { + return nil, err } - slice, e := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() - if e != nil { - return nil, e + slice, err := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() + if err != nil { + return nil, err } ret := make([]interface{}, len(slice)) From 6a6a4e767c3e9b70a1060acacfae2ed8a0d5d2cd Mon Sep 17 00:00:00 2001 From: Vishesh Yadav Date: Sat, 19 Oct 2024 22:51:06 -0700 Subject: [PATCH 02/16] Update packaging/docker/samples/golang/app/main.go --- packaging/docker/samples/golang/app/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/docker/samples/golang/app/main.go b/packaging/docker/samples/golang/app/main.go index f3cac5a08e0..c8914c3207b 100644 --- a/packaging/docker/samples/golang/app/main.go +++ b/packaging/docker/samples/golang/app/main.go @@ -1,6 +1,6 @@ // main.go // -// # This source file is part of the FoundationDB open source project +// This source file is part of the FoundationDB open source project // // Copyright 2013-2024 Apple Inc. and the FoundationDB project authors // From c3c73e8f3d37755a61eccdc3d5710f237c4edbe3 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Mon, 21 Oct 2024 10:59:55 +0530 Subject: [PATCH 03/16] fixed accidental renames, renamed file from directoryLayer.go to directory_layer.go, to work on go formatting --- .../{directoryLayer.go => directory_layer.go} | 0 bindings/go/src/fdb/fdb.go | 18 +++++----- bindings/go/src/fdb/tuple/tuple.go | 36 +++++++++---------- recipes/go-recipes/priority.go | 2 +- recipes/go-recipes/table.go | 4 +-- 5 files changed, 30 insertions(+), 30 deletions(-) rename bindings/go/src/fdb/directory/{directoryLayer.go => directory_layer.go} (100%) diff --git a/bindings/go/src/fdb/directory/directoryLayer.go b/bindings/go/src/fdb/directory/directory_layer.go similarity index 100% rename from bindings/go/src/fdb/directory/directoryLayer.go rename to bindings/go/src/fdb/directory/directory_layer.go diff --git a/bindings/go/src/fdb/fdb.go b/bindings/go/src/fdb/fdb.go index 1f5f1dcd854..ed43870a95f 100644 --- a/bindings/go/src/fdb/fdb.go +++ b/bindings/go/src/fdb/fdb.go @@ -145,9 +145,9 @@ func APIVersion(version int) error { return errAPIVersionNotSupported } - if err := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); err != 0 { - if err != 0 { - if err == 2203 { + if e := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); e != 0 { + if e != 0 { + if e == 2203 { maxSupportedVersion := C.fdb_get_max_api_version() if headerVersion > int(maxSupportedVersion) { return fmt.Errorf("This version of the FoundationDB Go binding is "+ @@ -157,7 +157,7 @@ func APIVersion(version int) error { } return fmt.Errorf("API version %d is not supported by the installed FoundationDB C library.", version) } - return Error{int(err)} + return Error{int(e)} } } @@ -240,16 +240,16 @@ func executeWithRunningNetworkThread(f func()) error { // check if meanwhile another goroutine started the network thread if !networkStarted { - if err := C.fdb_setup_network(); err != 0 { - return Error{int(err)} + if e := C.fdb_setup_network(); e != 0 { + return Error{int(e)} } networkRunning.Add(1) go func() { - err := C.fdb_run_network() + e := C.fdb_run_network() networkRunning.Done() - if err != 0 { - panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(err)), err)) + if e != 0 { + panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(e)), e)) } }() diff --git a/bindings/go/src/fdb/tuple/tuple.go b/bindings/go/src/fdb/tuple/tuple.go index 871c65cef78..d6257d4aa94 100644 --- a/bindings/go/src/fdb/tuple/tuple.go +++ b/bindings/go/src/fdb/tuple/tuple.go @@ -365,51 +365,51 @@ func (p *packer) encodeTuple(t Tuple, nested bool, versionstamps bool) { p.putByte(nestedCode) } - for i, err := range t { - switch err := err.(type) { + for i, e := range t { + switch e := e.(type) { case Tuple: - p.encodeTuple(err, true, versionstamps) + p.encodeTuple(e, true, versionstamps) case nil: p.putByte(nilCode) if nested { p.putByte(0xff) } case int: - p.encodeInt(int64(err)) + p.encodeInt(int64(e)) case int64: - p.encodeInt(err) + p.encodeInt(e) case uint: - p.encodeUint(uint64(err)) + p.encodeUint(uint64(e)) case uint64: - p.encodeUint(err) + p.encodeUint(e) case *big.Int: - p.encodeBigInt(err) + p.encodeBigInt(e) case big.Int: - p.encodeBigInt(&err) + p.encodeBigInt(&e) case []byte: - p.encodeBytes(bytesCode, err) + p.encodeBytes(bytesCode, e) case fdb.KeyConvertible: - p.encodeBytes(bytesCode, []byte(err.FDBKey())) + p.encodeBytes(bytesCode, []byte(e.FDBKey())) case string: - p.encodeBytes(stringCode, []byte(err)) + p.encodeBytes(stringCode, []byte(e)) case float32: - p.encodeFloat(err) + p.encodeFloat(e) case float64: - p.encodeDouble(err) + p.encodeDouble(e) case bool: - if err { + if e { p.putByte(trueCode) } else { p.putByte(falseCode) } case UUID: - p.encodeUUID(err) + p.encodeUUID(e) case Versionstamp: - if versionstamps == false && err.TransactionVersion == incompleteTransactionVersion { + if versionstamps == false && e.TransactionVersion == incompleteTransactionVersion { panic(fmt.Sprintf("Incomplete Versionstamp included in vanilla tuple pack")) } - p.encodeVersionstamp(err) + p.encodeVersionstamp(e) default: panic(fmt.Sprintf("unencodable element at index %d (%v, type %T)", i, t[i], t[i])) } diff --git a/recipes/go-recipes/priority.go b/recipes/go-recipes/priority.go index 346fd00dac4..e127a433b99 100644 --- a/recipes/go-recipes/priority.go +++ b/recipes/go-recipes/priority.go @@ -142,7 +142,7 @@ func main() { p.Push(db, "b", 5) p.Push(db, "c", 2) p.Push(db, "d", 4) - p.Push(db, "err", 3) + p.Push(db, "e", 3) fmt.Println(p.Peek(db, false)) fmt.Println(p.Peek(db, false)) diff --git a/recipes/go-recipes/table.go b/recipes/go-recipes/table.go index 6f768c75225..bfebb4fd4cb 100644 --- a/recipes/go-recipes/table.go +++ b/recipes/go-recipes/table.go @@ -45,8 +45,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, err := tuple.Unpack(t) - if err != nil { + i, e := tuple.Unpack(t) + if e != nil { return nil } return i From 0296934cfb50d3d0c4a4770c98f2b0b1baded33d Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Wed, 16 Oct 2024 02:51:01 +0530 Subject: [PATCH 04/16] Rename error variable in go tests to err #8829 renamed the variables from e to err as mentioned in the https://go.dev/doc/effective_go --- bindings/go/src/_stacktester/directory.go | 76 +-- bindings/go/src/_stacktester/stacktester.go | 188 ++++---- bindings/go/src/fdb/database.go | 52 +- bindings/go/src/fdb/directory/allocator.go | 42 +- .../go/src/fdb/directory/directoryLayer.go | 202 ++++---- .../go/src/fdb/directory/directorySubspace.go | 2 +- bindings/go/src/fdb/doc.go | 130 ++--- bindings/go/src/fdb/fdb.go | 34 +- bindings/go/src/fdb/fdb_test.go | 134 ++--- bindings/go/src/fdb/range.go | 22 +- bindings/go/src/fdb/snapshot.go | 6 +- bindings/go/src/fdb/transaction.go | 16 +- bindings/go/src/fdb/tuple/tuple.go | 47 +- contrib/mockkms/get_encryption_keys.go | 456 +++++++++--------- contrib/mockkms/utils.go | 30 +- packaging/docker/samples/golang/app/main.go | 11 +- recipes/go-recipes/blob.go | 4 +- recipes/go-recipes/doc.go | 4 +- recipes/go-recipes/indirect.go | 4 +- recipes/go-recipes/multi.go | 8 +- recipes/go-recipes/priority.go | 20 +- recipes/go-recipes/queue.go | 22 +- recipes/go-recipes/table.go | 28 +- 23 files changed, 768 insertions(+), 770 deletions(-) diff --git a/bindings/go/src/_stacktester/directory.go b/bindings/go/src/_stacktester/directory.go index 28e18300f73..291e7a000d6 100644 --- a/bindings/go/src/_stacktester/directory.go +++ b/bindings/go/src/_stacktester/directory.go @@ -115,7 +115,7 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, } }() - var e error + var err error switch { case op == "CREATE_SUBSPACE": @@ -142,9 +142,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, if l != nil { layer = l.([]byte) } - d, e := de.cwd().CreateOrOpen(t, tupleToPath(tuples[0]), layer) - if e != nil { - panic(e) + d, err := de.cwd().CreateOrOpen(t, tupleToPath(tuples[0]), layer) + if err != nil { + panic(err) } de.store(d) case op == "CREATE": @@ -157,13 +157,13 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, p := sm.waitAndPop().item var d directory.Directory if p == nil { - d, e = de.cwd().Create(t, tupleToPath(tuples[0]), layer) + d, err = de.cwd().Create(t, tupleToPath(tuples[0]), layer) } else { // p.([]byte) itself may be nil, but CreatePrefix handles that appropriately - d, e = de.cwd().CreatePrefix(t, tupleToPath(tuples[0]), layer, p.([]byte)) + d, err = de.cwd().CreatePrefix(t, tupleToPath(tuples[0]), layer, p.([]byte)) } - if e != nil { - panic(e) + if err != nil { + panic(err) } de.store(d) case op == "OPEN": @@ -173,9 +173,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, if l != nil { layer = l.([]byte) } - d, e := de.cwd().Open(rt, tupleToPath(tuples[0]), layer) - if e != nil { - panic(e) + d, err := de.cwd().Open(rt, tupleToPath(tuples[0]), layer) + if err != nil { + panic(err) } de.store(d) case op == "CHANGE": @@ -188,16 +188,16 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, de.errorIndex = sm.waitAndPop().item.(int64) case op == "MOVE": tuples := sm.popTuples(2) - d, e := de.cwd().Move(t, tupleToPath(tuples[0]), tupleToPath(tuples[1])) - if e != nil { - panic(e) + d, err := de.cwd().Move(t, tupleToPath(tuples[0]), tupleToPath(tuples[1])) + if err != nil { + panic(err) } de.store(d) case op == "MOVE_TO": tuples := sm.popTuples(1) - d, e := de.cwd().MoveTo(t, tupleToPath(tuples[0])) - if e != nil { - panic(e) + d, err := de.cwd().MoveTo(t, tupleToPath(tuples[0])) + if err != nil { + panic(err) } de.store(d) case strings.HasPrefix(op, "REMOVE"): @@ -208,10 +208,10 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, // doesn't end up committing the version key. (Other languages have // separate remove() and remove_if_exists() so don't have this tricky // issue). - _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { - ok, e := de.cwd().Remove(tr, path) - if e != nil { - panic(e) + _, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + ok, err := de.cwd().Remove(tr, path) + if err != nil { + panic(err) } switch op[6:] { case "": @@ -222,13 +222,13 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, } return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "LIST": - subs, e := de.cwd().List(rt, sm.maybePath()) - if e != nil { - panic(e) + subs, err := de.cwd().List(rt, sm.maybePath()) + if err != nil { + panic(err) } t := make(tuple.Tuple, len(subs)) for i, s := range subs { @@ -236,9 +236,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, } sm.store(idx, t.Pack()) case op == "EXISTS": - b, e := de.cwd().Exists(rt, sm.maybePath()) - if e != nil { - panic(e) + b, err := de.cwd().Exists(rt, sm.maybePath()) + if err != nil { + panic(err) } if b { sm.store(idx, int64(1)) @@ -249,9 +249,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, tuples := sm.popTuples(1) sm.store(idx, de.css().Pack(tuples[0])) case op == "UNPACK_KEY": - t, e := de.css().Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + t, err := de.css().Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } for _, el := range t { sm.store(idx, el) @@ -288,9 +288,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, v2 := tuple.Tuple{de.cwd().GetLayer()}.Pack() k3 := ss.Pack(tuple.Tuple{"exists"}) var v3 []byte - exists, e := de.cwd().Exists(rt, nil) - if e != nil { - panic(e) + exists, err := de.cwd().Exists(rt, nil) + if err != nil { + panic(err) } if exists { v3 = tuple.Tuple{1}.Pack() @@ -300,9 +300,9 @@ func (de *DirectoryExtension) processOp(sm *StackMachine, op string, isDB bool, k4 := ss.Pack(tuple.Tuple{"children"}) var subs []string if exists { - subs, e = de.cwd().List(rt, nil) - if e != nil { - panic(e) + subs, err = de.cwd().List(rt, nil) + if err != nil { + panic(err) } } v4 := tuplePackStrings(subs) diff --git a/bindings/go/src/_stacktester/stacktester.go b/bindings/go/src/_stacktester/stacktester.go index f92a4e587af..989eca600d9 100644 --- a/bindings/go/src/_stacktester/stacktester.go +++ b/bindings/go/src/_stacktester/stacktester.go @@ -144,9 +144,9 @@ func (sm *StackMachine) popRangeOptions() fdb.RangeOptions { } func (sm *StackMachine) popPrefixRange() fdb.ExactRange { - er, e := fdb.PrefixRange(sm.waitAndPop().item.([]byte)) - if e != nil { - panic(e) + er, err := fdb.PrefixRange(sm.waitAndPop().item.([]byte)) + if err != nil { + panic(err) } return er } @@ -243,9 +243,9 @@ func (sm *StackMachine) dumpStack() { } func (sm *StackMachine) executeMutation(t fdb.Transactor, f func(fdb.Transaction) (interface{}, error), isDB bool, idx int) { - _, e := t.Transact(f) - if e != nil { - panic(e) + _, err := t.Transact(f) + if err != nil { + panic(err) } if isDB { sm.store(idx, []byte("RESULT_NOT_PRESENT")) @@ -255,17 +255,17 @@ func (sm *StackMachine) executeMutation(t fdb.Transactor, f func(fdb.Transaction func (sm *StackMachine) checkWatches(watches [4]fdb.FutureNil, expected bool) bool { for _, watch := range watches { if watch.IsReady() || expected { - e := watch.Get() - if e != nil { - switch e := e.(type) { + err := watch.Get() + if err != nil { + switch err := err.(type) { case fdb.Error: tr, tr_error := db.CreateTransaction() if tr_error != nil { panic(tr_error) } - tr.OnError(e).MustGet() + tr.OnError(err).MustGet() default: - panic(e) + panic(err) } } if !expected { @@ -279,19 +279,19 @@ func (sm *StackMachine) checkWatches(watches [4]fdb.FutureNil, expected bool) bo func (sm *StackMachine) testWatches() { for { - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Set(fdb.Key("w0"), []byte("0")) tr.Set(fdb.Key("w2"), []byte("2")) tr.Set(fdb.Key("w3"), []byte("3")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } var watches [4]fdb.FutureNil - _, e = db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err = db.Transact(func(tr fdb.Transaction) (interface{}, error) { watches[0] = tr.Watch(fdb.Key("w0")) watches[1] = tr.Watch(fdb.Key("w1")) watches[2] = tr.Watch(fdb.Key("w2")) @@ -301,8 +301,8 @@ func (sm *StackMachine) testWatches() { tr.Clear(fdb.Key("w1")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } time.Sleep(5 * time.Second) @@ -311,15 +311,15 @@ func (sm *StackMachine) testWatches() { continue } - _, e = db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err = db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Set(fdb.Key("w0"), []byte("a")) tr.Set(fdb.Key("w1"), []byte("b")) tr.Clear(fdb.Key("w2")) tr.BitXor(fdb.Key("w3"), []byte("\xff\xff")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } if sm.checkWatches(watches, true) { @@ -329,12 +329,12 @@ func (sm *StackMachine) testWatches() { } func (sm *StackMachine) testLocality() { - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Options().SetTimeout(60 * 1000) tr.Options().SetReadSystemKeys() - boundaryKeys, e := db.LocalityGetBoundaryKeys(fdb.KeyRange{fdb.Key(""), fdb.Key("\xff\xff")}, 0, 0) - if e != nil { - panic(e) + boundaryKeys, err := db.LocalityGetBoundaryKeys(fdb.KeyRange{fdb.Key(""), fdb.Key("\xff\xff")}, 0, 0) + if err != nil { + panic(err) } for i := 0; i < len(boundaryKeys)-1; i++ { @@ -361,13 +361,13 @@ func (sm *StackMachine) testLocality() { return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } } func (sm *StackMachine) logStack(entries map[int]stackEntry, prefix []byte) { - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { for index, el := range entries { var keyt tuple.Tuple keyt = append(keyt, int64(index)) @@ -389,8 +389,8 @@ func (sm *StackMachine) logStack(entries map[int]stackEntry, prefix []byte) { return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } return } @@ -404,10 +404,10 @@ func (sm *StackMachine) currentTransaction() fdb.Transaction { } func (sm *StackMachine) newTransactionWithLockHeld() { - tr, e := db.CreateTransaction() + tr, err := db.CreateTransaction() - if e != nil { - panic(e) + if err != nil { + panic(err) } trMap[sm.trName] = tr @@ -448,7 +448,7 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } }() - var e error + var err error op := inst[0].(string) if sm.verbose { @@ -531,13 +531,13 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { case op == "ON_ERROR": sm.store(idx, sm.currentTransaction().OnError(fdb.Error{int(sm.waitAndPop().item.(int64))})) case op == "GET_READ_VERSION": - _, e = rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err = rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { sm.lastVersion = rtr.GetReadVersion().MustGet() sm.store(idx, []byte("GOT_READ_VERSION")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "SET": key := fdb.Key(sm.waitAndPop().item.([]byte)) @@ -561,34 +561,34 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { sm.logStack(entries, prefix) case op == "GET": key := fdb.Key(sm.waitAndPop().item.([]byte)) - res, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + res, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.Get(key), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } sm.store(idx, res.(fdb.FutureByteSlice)) case op == "GET_ESTIMATED_RANGE_SIZE": r := sm.popKeyRange() - _, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { _ = rtr.GetEstimatedRangeSizeBytes(r).MustGet() sm.store(idx, []byte("GOT_ESTIMATED_RANGE_SIZE")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "GET_RANGE_SPLIT_POINTS": r := sm.popKeyRange() chunkSize := sm.waitAndPop().item.(int64) - _, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { _ = rtr.GetRangeSplitPoints(r, chunkSize).MustGet() sm.store(idx, []byte("GOT_RANGE_SPLIT_POINTS")) return nil, nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } case op == "COMMIT": sm.store(idx, sm.currentTransaction().Commit()) @@ -606,9 +606,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { entry := sm.waitAndPop() sm.store(entry.idx, entry.item) case op == "GET_COMMITTED_VERSION": - sm.lastVersion, e = sm.currentTransaction().GetCommittedVersion() - if e != nil { - panic(e) + sm.lastVersion, err = sm.currentTransaction().GetCommittedVersion() + if err != nil { + panic(err) } sm.store(idx, []byte("GOT_COMMITTED_VERSION")) case op == "GET_APPROXIMATE_SIZE": @@ -619,11 +619,11 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { case op == "GET_KEY": sel := sm.popSelector() prefix := sm.waitAndPop().item.([]byte) - res, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + res, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.GetKey(sel).MustGet(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } key := res.(fdb.Key) @@ -633,9 +633,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } else if bytes.Compare(key, prefix) < 0 { sm.store(idx, prefix) } else { - s, e := fdb.Strinc(prefix) - if e != nil { - panic(e) + s, err := fdb.Strinc(prefix) + if err != nil { + panic(err) } sm.store(idx, s) } @@ -657,11 +657,11 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { prefix = sm.waitAndPop().item.([]byte) } - res, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + res, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.GetRange(r, ro).GetSliceOrPanic(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } sm.pushRange(idx, res.([]fdb.KeyValue), prefix) @@ -705,9 +705,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { sm.store(idx, packed) } case op == "TUPLE_UNPACK": - t, e := tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + t, err := tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } for _, el := range t { sm.store(idx, []byte(tuple.Tuple{el}.Pack())) @@ -716,9 +716,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { count := sm.waitAndPop().item.(int64) tuples := make([]tuple.Tuple, count) for i := 0; i < int(count); i++ { - tuples[i], e = tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + tuples[i], err = tuple.Unpack(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } } sort.Sort(byBytes(tuples)) @@ -763,9 +763,9 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { }() case op == "WAIT_EMPTY": prefix := sm.waitAndPop().item.([]byte) - er, e := fdb.PrefixRange(prefix) - if e != nil { - panic(e) + er, err := fdb.PrefixRange(prefix) + if err != nil { + panic(err) } db.Transact(func(tr fdb.Transaction) (interface{}, error) { v := tr.GetRange(er, fdb.RangeOptions{}).GetSliceOrPanic() @@ -776,27 +776,27 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { }) sm.store(idx, []byte("WAITED_FOR_EMPTY")) case op == "READ_CONFLICT_RANGE": - e = sm.currentTransaction().AddReadConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) - if e != nil { - panic(e) + err = sm.currentTransaction().AddReadConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_RANGE")) case op == "WRITE_CONFLICT_RANGE": - e = sm.currentTransaction().AddWriteConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) - if e != nil { - panic(e) + err = sm.currentTransaction().AddWriteConflictRange(fdb.KeyRange{fdb.Key(sm.waitAndPop().item.([]byte)), fdb.Key(sm.waitAndPop().item.([]byte))}) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_RANGE")) case op == "READ_CONFLICT_KEY": - e = sm.currentTransaction().AddReadConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + err = sm.currentTransaction().AddReadConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_KEY")) case op == "WRITE_CONFLICT_KEY": - e = sm.currentTransaction().AddWriteConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) - if e != nil { - panic(e) + err = sm.currentTransaction().AddWriteConflictKey(fdb.Key(sm.waitAndPop().item.([]byte))) + if err != nil { + panic(err) } sm.store(idx, []byte("SET_CONFLICT_KEY")) case op == "ATOMIC_OP": @@ -855,7 +855,7 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } fdb.MustAPIVersion(apiVersion) - _, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.Options().SetPrioritySystemImmediate() tr.Options().SetPriorityBatch() tr.Options().SetCausalReadRisky() @@ -877,8 +877,8 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { return tr.Get(fdb.Key("\xff")).MustGet(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } sm.testWatches() @@ -900,11 +900,11 @@ func (sm *StackMachine) processInst(idx int, inst tuple.Tuple) { } func (sm *StackMachine) Run() { - r, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { return tr.GetRange(tuple.Tuple{sm.prefix}, fdb.RangeOptions{}).GetSliceOrPanic(), nil }) - if e != nil { - panic(e) + if err != nil { + panic(err) } instructions := r.([]fdb.KeyValue) @@ -931,29 +931,29 @@ func main() { clusterFile = os.Args[3] } - var e error + var err error var apiVersion int - apiVersion, e = strconv.Atoi(os.Args[2]) - if e != nil { - log.Fatal(e) + apiVersion, err = strconv.Atoi(os.Args[2]) + if err != nil { + log.Fatal(err) } if fdb.IsAPIVersionSelected() { log.Fatal("API version already selected") } - e = fdb.APIVersion(apiVersion) - if e != nil { - log.Fatal(e) + err = fdb.APIVersion(apiVersion) + if err != nil { + log.Fatal(err) } if fdb.MustGetAPIVersion() != apiVersion { log.Fatal("API version not equal to value selected") } - db, e = fdb.OpenDatabase(clusterFile) - if e != nil { - log.Fatal(e) + db, err = fdb.OpenDatabase(clusterFile) + if err != nil { + log.Fatal(err) } sm := newStackMachine(prefix, verbose) diff --git a/bindings/go/src/fdb/database.go b/bindings/go/src/fdb/database.go index 942772680c6..0c27b8da466 100644 --- a/bindings/go/src/fdb/database.go +++ b/bindings/go/src/fdb/database.go @@ -133,25 +133,25 @@ func (d Database) RebootWorker(address string, checkFile bool, suspendDuration i return err } -func retryable(wrapped func() (interface{}, error), onError func(Error) FutureNil) (ret interface{}, e error) { +func retryable(wrapped func() (interface{}, error), onError func(Error) FutureNil) (ret interface{}, err error) { for { - ret, e = wrapped() + ret, err = wrapped() // No error means success! - if e == nil { + if err == nil { return } // Check if the error chain contains an // fdb.Error var ep Error - if errors.As(e, &ep) { - e = onError(ep).Get() + if errors.As(err, &ep) { + err = onError(ep).Get() } // If OnError returns an error, then it's not // retryable; otherwise take another pass at things - if e != nil { + if err != nil { return } } @@ -180,19 +180,19 @@ func retryable(wrapped func() (interface{}, error), onError func(Error) FutureNi // See the Transactor interface for an example of using Transact with // Transaction and Database objects. func (d Database) Transact(f func(Transaction) (interface{}, error)) (interface{}, error) { - tr, e := d.CreateTransaction() + tr, err := d.CreateTransaction() // Any error here is non-retryable - if e != nil { - return nil, e + if err != nil { + return nil, err } - wrapped := func() (ret interface{}, e error) { - defer panicToError(&e) + wrapped := func() (ret interface{}, err error) { + defer panicToError(&err) - ret, e = f(tr) + ret, err = f(tr) - if e == nil { - e = tr.Commit().Get() + if err == nil { + err = tr.Commit().Get() } return @@ -224,16 +224,16 @@ func (d Database) Transact(f func(Transaction) (interface{}, error)) (interface{ // See the ReadTransactor interface for an example of using ReadTransact with // Transaction, Snapshot and Database objects. func (d Database) ReadTransact(f func(ReadTransaction) (interface{}, error)) (interface{}, error) { - tr, e := d.CreateTransaction() - if e != nil { + tr, err := d.CreateTransaction() + if err != nil { // Any error here is non-retryable - return nil, e + return nil, err } - wrapped := func() (ret interface{}, e error) { - defer panicToError(&e) + wrapped := func() (ret interface{}, err error) { + defer panicToError(&err) - ret, e = f(tr) + ret, err = f(tr) // read-only transactions are not committed and will be destroyed automatically via GC, // once all the futures go out of scope @@ -262,9 +262,9 @@ func (d Database) Options() DatabaseOptions { // If readVersion is non-zero, the boundary keys as of readVersion will be // returned. func (d Database) LocalityGetBoundaryKeys(er ExactRange, limit int, readVersion int64) ([]Key, error) { - tr, e := d.CreateTransaction() - if e != nil { - return nil, e + tr, err := d.CreateTransaction() + if err != nil { + return nil, err } if readVersion != 0 { @@ -280,9 +280,9 @@ func (d Database) LocalityGetBoundaryKeys(er ExactRange, limit int, readVersion append(Key("\xFF/keyServers/"), ek.FDBKey()...), } - kvs, e := tr.Snapshot().GetRange(ffer, RangeOptions{Limit: limit}).GetSliceWithError() - if e != nil { - return nil, e + kvs, err := tr.Snapshot().GetRange(ffer, RangeOptions{Limit: limit}).GetSliceWithError() + if err != nil { + return nil, err } size := len(kvs) diff --git a/bindings/go/src/fdb/directory/allocator.go b/bindings/go/src/fdb/directory/allocator.go index 3c4eff4fc02..db7657e3c84 100644 --- a/bindings/go/src/fdb/directory/allocator.go +++ b/bindings/go/src/fdb/directory/allocator.go @@ -66,18 +66,18 @@ func windowSize(start int64) int64 { func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subspace) (subspace.Subspace, error) { for { rr := tr.Snapshot().GetRange(hca.counters, fdb.RangeOptions{Limit: 1, Reverse: true}) - kvs, e := rr.GetSliceWithError() - if e != nil { - return nil, e + kvs, err := rr.GetSliceWithError() + if err != nil { + return nil, err } var start int64 var window int64 if len(kvs) == 1 { - t, e := hca.counters.Unpack(kvs[0].Key) - if e != nil { - return nil, e + t, err := hca.counters.Unpack(kvs[0].Key) + if err != nil { + return nil, err } start = t[0].(int64) } @@ -98,18 +98,18 @@ func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subsp allocatorMutex.Unlock() - countStr, e := countFuture.Get() - if e != nil { - return nil, e + countStr, err := countFuture.Get() + if err != nil { + return nil, err } var count int64 if countStr == nil { count = 0 } else { - e = binary.Read(bytes.NewBuffer(countStr), binary.LittleEndian, &count) - if e != nil { - return nil, e + err = binary.Read(bytes.NewBuffer(countStr), binary.LittleEndian, &count) + if err != nil { + return nil, err } } @@ -139,14 +139,14 @@ func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subsp allocatorMutex.Unlock() - kvs, e = latestCounter.GetSliceWithError() - if e != nil { - return nil, e + kvs, err = latestCounter.GetSliceWithError() + if err != nil { + return nil, err } if len(kvs) > 0 { - t, e := hca.counters.Unpack(kvs[0].Key) - if e != nil { - return nil, e + t, err := hca.counters.Unpack(kvs[0].Key) + if err != nil { + return nil, err } currentStart := t[0].(int64) if currentStart > start { @@ -154,9 +154,9 @@ func (hca highContentionAllocator) allocate(tr fdb.Transaction, s subspace.Subsp } } - v, e := candidateValue.Get() - if e != nil { - return nil, e + v, err := candidateValue.Get() + if err != nil { + return nil, err } if v == nil { tr.AddWriteConflictKey(key) diff --git a/bindings/go/src/fdb/directory/directoryLayer.go b/bindings/go/src/fdb/directory/directoryLayer.go index 396c6982c07..6bda8397e18 100644 --- a/bindings/go/src/fdb/directory/directoryLayer.go +++ b/bindings/go/src/fdb/directory/directoryLayer.go @@ -72,8 +72,8 @@ func NewDirectoryLayer(nodeSS, contentSS subspace.Subspace, allowManualPrefixes } func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transaction, path []string, layer []byte, prefix []byte, allowCreate, allowOpen bool) (DirectorySubspace, error) { - if e := dl.checkVersion(rtr, nil); e != nil { - return nil, e + if err := dl.checkVersion(rtr, nil); err != nil { + return nil, err } if prefix != nil && !dl.allowManualPrefixes { @@ -91,9 +91,9 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti if existingNode.exists() { if existingNode.isInPartition(nil, false) { subpath := existingNode.getPartitionSubpath() - enc, e := existingNode.getContents(dl, nil) - if e != nil { - return nil, e + enc, err := existingNode.getContents(dl, nil) + if err != nil { + return nil, err } return enc.(directoryPartition).createOrOpen(rtr, tr, subpath, layer, prefix, allowCreate, allowOpen) } @@ -103,7 +103,7 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti } if layer != nil { - if l, e := existingNode._layer.Get(); e != nil || bytes.Compare(l, layer) != 0 { + if l, err := existingNode._layer.Get(); err != nil || bytes.Compare(l, layer) != 0 { return nil, errors.New("the directory was created with an incompatible layer") } } @@ -115,14 +115,14 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti return nil, ErrDirNotExists } - if e := dl.checkVersion(rtr, tr); e != nil { - return nil, e + if err := dl.checkVersion(rtr, tr); err != nil { + return nil, err } if prefix == nil { - newss, e := dl.allocator.allocate(*tr, dl.contentSS) - if e != nil { - return nil, fmt.Errorf("unable to allocate new directory prefix (%s)", e.Error()) + newss, err := dl.allocator.allocate(*tr, dl.contentSS) + if err != nil { + return nil, fmt.Errorf("unable to allocate new directory prefix (%s)", err.Error()) } if !isRangeEmpty(rtr, newss) { @@ -131,17 +131,17 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti prefix = newss.Bytes() - pf, e := dl.isPrefixFree(rtr.Snapshot(), prefix) - if e != nil { - return nil, e + pf, err := dl.isPrefixFree(rtr.Snapshot(), prefix) + if err != nil { + return nil, err } if !pf { return nil, errors.New("the directory layer has manually allocated prefixes that conflict with the automatic prefix allocator") } } else { - pf, e := dl.isPrefixFree(rtr, prefix) - if e != nil { - return nil, e + pf, err := dl.isPrefixFree(rtr, prefix) + if err != nil { + return nil, err } if !pf { return nil, errors.New("the given prefix is already in use") @@ -151,9 +151,9 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti var parentNode subspace.Subspace if len(path) > 1 { - pd, e := dl.createOrOpen(rtr, tr, path[:len(path)-1], nil, nil, true, true) - if e != nil { - return nil, e + pd, err := dl.createOrOpen(rtr, tr, path[:len(path)-1], nil, nil, true, true) + if err != nil { + return nil, err } parentNode = dl.nodeWithPrefix(pd.Bytes()) } else { @@ -177,21 +177,21 @@ func (dl directoryLayer) createOrOpen(rtr fdb.ReadTransaction, tr *fdb.Transacti } func (dl directoryLayer) CreateOrOpen(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { return dl.createOrOpen(tr, &tr, path, layer, nil, true, true) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Create(t fdb.Transactor, path []string, layer []byte) (DirectorySubspace, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { return dl.createOrOpen(tr, &tr, path, layer, nil, true, false) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } @@ -200,29 +200,29 @@ func (dl directoryLayer) CreatePrefix(t fdb.Transactor, path []string, layer []b if prefix == nil { prefix = []byte{} } - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { return dl.createOrOpen(tr, &tr, path, layer, prefix, true, false) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Open(rt fdb.ReadTransactor, path []string, layer []byte) (DirectorySubspace, error) { - r, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + r, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return dl.createOrOpen(rtr, nil, path, layer, nil, false, true) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Exists(rt fdb.ReadTransactor, path []string) (bool, error) { - r, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - if e := dl.checkVersion(rtr, nil); e != nil { - return false, e + r, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + if err := dl.checkVersion(rtr, nil); err != nil { + return false, err } node := dl.find(rtr, path).prefetchMetadata(rtr) @@ -231,25 +231,25 @@ func (dl directoryLayer) Exists(rt fdb.ReadTransactor, path []string) (bool, err } if node.isInPartition(nil, false) { - nc, e := node.getContents(dl, nil) - if e != nil { - return false, e + nc, err := node.getContents(dl, nil) + if err != nil { + return false, err } return nc.Exists(rtr, node.getPartitionSubpath()) } return true, nil }) - if e != nil { - return false, e + if err != nil { + return false, err } return r.(bool), nil } func (dl directoryLayer) List(rt fdb.ReadTransactor, path []string) ([]string, error) { - r, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - if e := dl.checkVersion(rtr, nil); e != nil { - return nil, e + r, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + if err := dl.checkVersion(rtr, nil); err != nil { + return nil, err } node := dl.find(rtr, path).prefetchMetadata(rtr) @@ -258,17 +258,17 @@ func (dl directoryLayer) List(rt fdb.ReadTransactor, path []string) ([]string, e } if node.isInPartition(nil, true) { - nc, e := node.getContents(dl, nil) - if e != nil { - return nil, e + nc, err := node.getContents(dl, nil) + if err != nil { + return nil, err } return nc.List(rtr, node.getPartitionSubpath()) } return dl.subdirNames(rtr, node.subspace) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.([]string), nil } @@ -278,9 +278,9 @@ func (dl directoryLayer) MoveTo(t fdb.Transactor, newAbsolutePath []string) (Dir } func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []string) (DirectorySubspace, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { - if e := dl.checkVersion(tr, &tr); e != nil { - return nil, e + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + if err := dl.checkVersion(tr, &tr); err != nil { + return nil, err } sliceEnd := len(oldPath) @@ -303,9 +303,9 @@ func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []stri return nil, errors.New("cannot move between partitions") } - nnc, e := newNode.getContents(dl, nil) - if e != nil { - return nil, e + nnc, err := newNode.getContents(dl, nil) + if err != nil { + return nil, err } return nnc.Move(tr, oldNode.getPartitionSubpath(), newNode.getPartitionSubpath()) } @@ -319,30 +319,30 @@ func (dl directoryLayer) Move(t fdb.Transactor, oldPath []string, newPath []stri return nil, errors.New("the parent of the destination directory does not exist. Create it first") } - p, e := dl.nodeSS.Unpack(oldNode.subspace) - if e != nil { - return nil, e + p, err := dl.nodeSS.Unpack(oldNode.subspace) + if err != nil { + return nil, err } tr.Set(parentNode.subspace.Sub(_SUBDIRS, newPath[len(newPath)-1]), p[0].([]byte)) dl.removeFromParent(tr, oldPath) - l, e := oldNode._layer.Get() - if e != nil { - return nil, e + l, err := oldNode._layer.Get() + if err != nil { + return nil, err } return dl.contentsOfNode(oldNode.subspace, newPath, l) }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return r.(DirectorySubspace), nil } func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) { - r, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { - if e := dl.checkVersion(tr, &tr); e != nil { - return false, e + r, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + if err := dl.checkVersion(tr, &tr); err != nil { + return false, err } if len(path) == 0 { @@ -356,22 +356,22 @@ func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) { } if node.isInPartition(nil, false) { - nc, e := node.getContents(dl, nil) - if e != nil { - return false, e + nc, err := node.getContents(dl, nil) + if err != nil { + return false, err } return nc.(directoryPartition).Remove(tr, node.getPartitionSubpath()) } - if e := dl.removeRecursive(tr, node.subspace); e != nil { - return false, e + if err := dl.removeRecursive(tr, node.subspace); err != nil { + return false, err } dl.removeFromParent(tr, path) return true, nil }) - if e != nil { - return false, e + if err != nil { + return false, err } return r.(bool), nil } @@ -379,18 +379,18 @@ func (dl directoryLayer) Remove(t fdb.Transactor, path []string) (bool, error) { func (dl directoryLayer) removeRecursive(tr fdb.Transaction, node subspace.Subspace) error { nodes := dl.subdirNodes(tr, node) for i := range nodes { - if e := dl.removeRecursive(tr, nodes[i]); e != nil { - return e + if err := dl.removeRecursive(tr, nodes[i]); err != nil { + return err } } - p, e := dl.nodeSS.Unpack(node) - if e != nil { - return e + p, err := dl.nodeSS.Unpack(node) + if err != nil { + return err } - kr, e := fdb.PrefixRange(p[0].([]byte)) - if e != nil { - return e + kr, err := fdb.PrefixRange(p[0].([]byte)) + if err != nil { + return err } tr.ClearRange(kr) @@ -421,14 +421,14 @@ func (dl directoryLayer) subdirNames(rtr fdb.ReadTransaction, node subspace.Subs var ret []string for ri.Advance() { - kv, e := ri.Get() - if e != nil { - return nil, e + kv, err := ri.Get() + if err != nil { + return nil, err } - p, e := sd.Unpack(kv.Key) - if e != nil { - return nil, e + p, err := sd.Unpack(kv.Key) + if err != nil { + return nil, err } ret = append(ret, p[0].(string)) @@ -462,14 +462,14 @@ func (dl directoryLayer) nodeContainingKey(rtr fdb.ReadTransaction, key []byte) bk, _ := dl.nodeSS.FDBRangeKeys() kr := fdb.KeyRange{bk, fdb.Key(append(dl.nodeSS.Pack(tuple.Tuple{key}), 0x00))} - kvs, e := rtr.GetRange(kr, fdb.RangeOptions{Reverse: true, Limit: 1}).GetSliceWithError() - if e != nil { - return nil, e + kvs, err := rtr.GetRange(kr, fdb.RangeOptions{Reverse: true, Limit: 1}).GetSliceWithError() + if err != nil { + return nil, err } if len(kvs) == 1 { - pp, e := dl.nodeSS.Unpack(kvs[0].Key) - if e != nil { - return nil, e + pp, err := dl.nodeSS.Unpack(kvs[0].Key) + if err != nil { + return nil, err } prevPrefix := pp[0].([]byte) if bytes.HasPrefix(key, prevPrefix) { @@ -485,17 +485,17 @@ func (dl directoryLayer) isPrefixFree(rtr fdb.ReadTransaction, prefix []byte) (b return false, nil } - nck, e := dl.nodeContainingKey(rtr, prefix) - if e != nil { - return false, e + nck, err := dl.nodeContainingKey(rtr, prefix) + if err != nil { + return false, err } if nck != nil { return false, nil } - kr, e := fdb.PrefixRange(prefix) - if e != nil { - return false, e + kr, err := fdb.PrefixRange(prefix) + if err != nil { + return false, err } bk, ek := kr.FDBRangeKeys() @@ -557,9 +557,9 @@ func (dl directoryLayer) initializeDirectory(tr fdb.Transaction) { } func (dl directoryLayer) contentsOfNode(node subspace.Subspace, path []string, layer []byte) (DirectorySubspace, error) { - p, e := dl.nodeSS.Unpack(node) - if e != nil { - return nil, e + p, err := dl.nodeSS.Unpack(node) + if err != nil { + return nil, err } prefix := p[0] diff --git a/bindings/go/src/fdb/directory/directorySubspace.go b/bindings/go/src/fdb/directory/directorySubspace.go index 3dc5998a342..bd7ba89703e 100644 --- a/bindings/go/src/fdb/directory/directorySubspace.go +++ b/bindings/go/src/fdb/directory/directorySubspace.go @@ -90,7 +90,7 @@ func (d directorySubspace) Exists(rt fdb.ReadTransactor, path []string) (bool, e return d.dl.Exists(rt, d.dl.partitionSubpath(d.path, path)) } -func (d directorySubspace) List(rt fdb.ReadTransactor, path []string) (subdirs []string, e error) { +func (d directorySubspace) List(rt fdb.ReadTransactor, path []string) (subdirs []string, err error) { return d.dl.List(rt, d.dl.partitionSubpath(d.path, path)) } diff --git a/bindings/go/src/fdb/doc.go b/bindings/go/src/fdb/doc.go index 6b5c3526a6c..68e07b2f469 100644 --- a/bindings/go/src/fdb/doc.go +++ b/bindings/go/src/fdb/doc.go @@ -32,40 +32,40 @@ extensive guidance to programming with FoundationDB, as well as API documentation for the other FoundationDB interfaces, please see https://apple.github.io/foundationdb/index.html. -Basic Usage +# Basic Usage A basic interaction with the FoundationDB API is demonstrated below: - package main + package main - import ( - "github.com/apple/foundationdb/bindings/go/src/fdb" - "log" - "fmt" - ) + import ( + "github.com/apple/foundationdb/bindings/go/src/fdb" + "log" + "fmt" + ) - func main() { - // Different API versions may expose different runtime behaviors. - fdb.MustAPIVersion(740) + func main() { + // Different API versions may expose different runtime behaviors. + fdb.MustAPIVersion(740) - // Open the default database from the system cluster - db := fdb.MustOpenDefault() + // Open the default database from the system cluster + db := fdb.MustOpenDefault() - // Database reads and writes happen inside transactions - ret, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { - tr.Set(fdb.Key("hello"), []byte("world")) - return tr.Get(fdb.Key("foo")).MustGet(), nil - // db.Transact automatically commits (and if necessary, - // retries) the transaction - }) - if e != nil { - log.Fatalf("Unable to perform FDB transaction (%v)", e) - } + // Database reads and writes happen inside transactions + ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Set(fdb.Key("hello"), []byte("world")) + return tr.Get(fdb.Key("foo")).MustGet(), nil + // db.Transact automatically commits (and if necessary, + // retries) the transaction + }) + if err != nil { + log.Fatalf("Unable to perform FDB transaction (%v)", err) + } - fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte))) - } + fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte))) + } -Futures +# Futures Many functions in this package are asynchronous and return Future objects. A Future represents a value (or error) to be available at some later @@ -79,7 +79,7 @@ and have multiple Future objects outstanding inside a single goroutine. All operations will execute in parallel, and the calling goroutine will not block until a blocking method on any one of the Futures is called. -On Panics +# On Panics Idiomatic Go code strongly frowns at panics that escape library/package boundaries, in favor of explicitly returned errors. Idiomatic FoundationDB @@ -87,28 +87,28 @@ client programs, however, are built around the idea of retryable programmer-provided transactional functions. Retryable transactions can be implemented using only error values: - ret, e := db.Transact(func (tr Transaction) (interface{}, error) { - // FoundationDB futures represent a value that will become available - futureValueOne := tr.Get(fdb.Key("foo")) - futureValueTwo := tr.Get(fdb.Key("bar")) + ret, err := db.Transact(func (tr Transaction) (interface{}, error) { + // FoundationDB futures represent a value that will become available + futureValueOne := tr.Get(fdb.Key("foo")) + futureValueTwo := tr.Get(fdb.Key("bar")) - // Both reads are being carried out in parallel + // Both reads are being carried out in parallel - // Get the first value (or any error) - valueOne, e := futureValueOne.Get() - if e != nil { - return nil, e - } + // Get the first value (or any error) + valueOne, err := futureValueOne.Get() + if err != nil { + return nil, err + } - // Get the second value (or any error) - valueTwo, e := futureValueTwo.Get() - if e != nil { - return nil, e - } + // Get the second value (or any error) + valueTwo, err := futureValueTwo.Get() + if err != nil { + return nil, err + } - // Return the two values - return []string{valueOne, valueTwo}, nil - }) + // Return the two values + return []string{valueOne, valueTwo}, nil + }) If either read encounters an error, it will be returned to Transact, which will determine if the error is retryable or not (using (Transaction).OnError). If the @@ -123,31 +123,31 @@ type also has a MustGet method, which returns the same type and value as Get, but exposes FoundationDB Errors via a panic rather than an explicitly returned error. The above example may be rewritten as: - ret, e := db.Transact(func (tr Transaction) (interface{}, error) { - // FoundationDB futures represent a value that will become available - futureValueOne := tr.Get(fdb.Key("foo")) - futureValueTwo := tr.Get(fdb.Key("bar")) + ret, err := db.Transact(func (tr Transaction) (interface{}, error) { + // FoundationDB futures represent a value that will become available + futureValueOne := tr.Get(fdb.Key("foo")) + futureValueTwo := tr.Get(fdb.Key("bar")) - // Both reads are being carried out in parallel + // Both reads are being carried out in parallel - // Get the first value - valueOne := futureValueOne.MustGet() - // Get the second value - valueTwo := futureValueTwo.MustGet() + // Get the first value + valueOne := futureValueOne.MustGet() + // Get the second value + valueTwo := futureValueTwo.MustGet() - // Return the two values - return []string{valueOne, valueTwo}, nil - }) + // Return the two values + return []string{valueOne, valueTwo}, nil + }) MustGet returns nil (which is different from empty slice []byte{}), when the key doesn't exist, and hence non-existence can be checked as follows: - val := tr.Get(fdb.Key("foobar")).MustGet() - if val == nil { - fmt.Println("foobar does not exist.") - } else { - fmt.Println("foobar exists.") - } + val := tr.Get(fdb.Key("foobar")).MustGet() + if val == nil { + fmt.Println("foobar does not exist.") + } else { + fmt.Println("foobar exists.") + } Any panic that occurs during execution of the caller-provided function will be recovered by the (Database).Transact method. If the error is an FDB Error, it @@ -159,7 +159,7 @@ Note that (Transaction).Transact also recovers panics, but does not itself retry. If the recovered value is an FDB Error, it will be returned to the caller of (Transaction).Transact; all other values will be re-panicked. -Transactions and Goroutines +# Transactions and Goroutines When using a Transactor in the fdb package, particular care must be taken if goroutines are created inside of the function passed to the Transact method. Any @@ -180,7 +180,7 @@ Given these complexities, it is generally best practice to use a single goroutine for each logical thread of interaction with FoundationDB, and allow each goroutine to block when necessary to wait for Futures to become ready. -Streaming Modes +# Streaming Modes When using GetRange methods in the FoundationDB API, clients can request large ranges of the database to iterate over. Making such a request doesn't @@ -198,7 +198,7 @@ reasonable default balance. Other streaming modes that prioritize throughput or latency are available -- see the documented StreamingMode values for specific options. -Atomic Operations +# Atomic Operations The FDB package provides a number of atomic operations on the Database and Transaction objects. An atomic operation is a single database command that diff --git a/bindings/go/src/fdb/fdb.go b/bindings/go/src/fdb/fdb.go index efe166eb9ad..1f5f1dcd854 100644 --- a/bindings/go/src/fdb/fdb.go +++ b/bindings/go/src/fdb/fdb.go @@ -145,9 +145,9 @@ func APIVersion(version int) error { return errAPIVersionNotSupported } - if e := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); e != 0 { - if e != 0 { - if e == 2203 { + if err := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); err != 0 { + if err != 0 { + if err == 2203 { maxSupportedVersion := C.fdb_get_max_api_version() if headerVersion > int(maxSupportedVersion) { return fmt.Errorf("This version of the FoundationDB Go binding is "+ @@ -157,7 +157,7 @@ func APIVersion(version int) error { } return fmt.Errorf("API version %d is not supported by the installed FoundationDB C library.", version) } - return Error{int(e)} + return Error{int(err)} } } @@ -166,7 +166,7 @@ func APIVersion(version int) error { return nil } -// Determines if an API version has already been selected, i.e., if +// Determines if an API version has already been selected, i.err., if // APIVersion or MustAPIVersion have already been called. func IsAPIVersionSelected() bool { return apiVersion != 0 @@ -240,16 +240,16 @@ func executeWithRunningNetworkThread(f func()) error { // check if meanwhile another goroutine started the network thread if !networkStarted { - if e := C.fdb_setup_network(); e != 0 { - return Error{int(e)} + if err := C.fdb_setup_network(); err != 0 { + return Error{int(err)} } networkRunning.Add(1) go func() { - e := C.fdb_run_network() + err := C.fdb_run_network() networkRunning.Done() - if e != 0 { - panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(e)), e)) + if err != 0 { + panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(err)), err)) } }() @@ -334,10 +334,10 @@ func OpenDatabase(clusterFile string) (Database, error) { var okDb bool anyy, exist := openDatabases.Load(clusterFile) if db, okDb = anyy.(Database); !exist || !okDb { - var e error - db, e = createDatabase(clusterFile) - if e != nil { - return Database{}, e + var err error + db, err = createDatabase(clusterFile) + if err != nil { + return Database{}, err } openDatabases.Store(clusterFile, db) } @@ -408,7 +408,7 @@ func createDatabase(clusterFile string) (Database, error) { // OpenWithConnectionString returns a database handle to the FoundationDB cluster identified // by the provided connection string. This method can be useful for scenarios where you want to connect -// to the database only for a short time e.g. to test different connection strings. +// to the database only for a short time err.g. to test different connection strings. // Caller must call Close() to release resources. func OpenWithConnectionString(connectionString string) (Database, error) { if apiVersion == 0 { @@ -511,11 +511,11 @@ func Printable(d []byte) string { return buf.String() } -func panicToError(e *error) { +func panicToError(err *error) { if r := recover(); r != nil { fe, ok := r.(Error) if ok { - *e = fe + *err = fe } else { panic(r) } diff --git a/bindings/go/src/fdb/fdb_test.go b/bindings/go/src/fdb/fdb_test.go index f24a5796aa6..107c2487e3b 100644 --- a/bindings/go/src/fdb/fdb_test.go +++ b/bindings/go/src/fdb/fdb_test.go @@ -34,19 +34,19 @@ import ( const API_VERSION int = 740 func ExampleOpenDefault() { - var e error + var err error - e = fdb.APIVersion(API_VERSION) - if e != nil { - fmt.Printf("Unable to set API version: %v\n", e) + err = fdb.APIVersion(API_VERSION) + if err != nil { + fmt.Printf("Unable to set API version: %v\n", err) return } // OpenDefault opens the database described by the platform-specific default // cluster file - db, e := fdb.OpenDefault() - if e != nil { - fmt.Printf("Unable to open default database: %v\n", e) + db, err := fdb.OpenDefault() + if err != nil { + fmt.Printf("Unable to open default database: %v\n", err) return } @@ -64,20 +64,20 @@ func TestVersionstamp(t *testing.T) { setVs := func(t fdb.Transactor, key fdb.Key) (fdb.FutureKey, error) { fmt.Printf("setOne called with: %T\n", t) - ret, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + ret, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { tr.SetVersionstampedValue(key, []byte("blahblahbl\x00\x00\x00\x00")) return tr.GetVersionstamp(), nil }) - return ret.(fdb.FutureKey), e + return ret.(fdb.FutureKey), err } getOne := func(rt fdb.ReadTransactor, key fdb.Key) ([]byte, error) { fmt.Printf("getOne called with: %T\n", rt) - ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + ret, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.Get(key).MustGet(), nil }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return ret.([]byte), nil } @@ -85,20 +85,20 @@ func TestVersionstamp(t *testing.T) { var v []byte var fvs fdb.FutureKey var k fdb.Key - var e error + var err error - fvs, e = setVs(db, fdb.Key("foo")) - if e != nil { - t.Errorf("setOne failed %v", e) + fvs, err = setVs(db, fdb.Key("foo")) + if err != nil { + t.Errorf("setOne failed %v", err) } - v, e = getOne(db, fdb.Key("foo")) - if e != nil { - t.Errorf("getOne failed %v", e) + v, err = getOne(db, fdb.Key("foo")) + if err != nil { + t.Errorf("getOne failed %v", err) } t.Logf("getOne returned %s", v) - k, e = fvs.Get() - if e != nil { - t.Errorf("setOne wait failed %v", e) + k, err = fvs.Get() + if err != nil { + t.Errorf("setOne wait failed %v", err) } t.Log(k) t.Logf("setOne returned %s", k) @@ -109,30 +109,30 @@ func TestEstimatedRangeSize(t *testing.T) { db := fdb.MustOpenDefault() var f fdb.FutureInt64 - _, e := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { f = rtr.GetEstimatedRangeSizeBytes(subspace.AllKeys()) return nil, nil }) - if e != nil { - t.Error(e) + if err != nil { + t.Error(err) } - _, e = f.Get() - if e != nil { - t.Error(e) + _, err = f.Get() + if err != nil { + t.Error(err) } } func TestReadTransactionOptions(t *testing.T) { fdb.MustAPIVersion(API_VERSION) db := fdb.MustOpenDefault() - _, e := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + _, err := db.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { rtr.Options().SetAccessSystemKeys() return rtr.Get(fdb.Key("\xff/")).MustGet(), nil }) - if e != nil { - t.Errorf("Failed to read system key: %s", e) + if err != nil { + t.Errorf("Failed to read system key: %s", err) } } @@ -142,37 +142,37 @@ func ExampleTransactor() { setOne := func(t fdb.Transactor, key fdb.Key, value []byte) error { fmt.Printf("setOne called with: %T\n", t) - _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { // We don't actually call tr.Set here to avoid mutating a real database. // tr.Set(key, value) return nil, nil }) - return e + return err } setMany := func(t fdb.Transactor, value []byte, keys ...fdb.Key) error { fmt.Printf("setMany called with: %T\n", t) - _, e := t.Transact(func(tr fdb.Transaction) (interface{}, error) { + _, err := t.Transact(func(tr fdb.Transaction) (interface{}, error) { for _, key := range keys { setOne(tr, key, value) } return nil, nil }) - return e + return err } - var e error + var err error fmt.Println("Calling setOne with a database:") - e = setOne(db, []byte("foo"), []byte("bar")) - if e != nil { - fmt.Println(e) + err = setOne(db, []byte("foo"), []byte("bar")) + if err != nil { + fmt.Println(err) return } fmt.Println("\nCalling setMany with a database:") - e = setMany(db, []byte("bar"), fdb.Key("foo1"), fdb.Key("foo2"), fdb.Key("foo3")) - if e != nil { - fmt.Println(e) + err = setMany(db, []byte("bar"), fdb.Key("foo1"), fdb.Key("foo2"), fdb.Key("foo3")) + if err != nil { + fmt.Println(err) return } @@ -193,40 +193,40 @@ func ExampleReadTransactor() { getOne := func(rt fdb.ReadTransactor, key fdb.Key) ([]byte, error) { fmt.Printf("getOne called with: %T\n", rt) - ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + ret, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { return rtr.Get(key).MustGet(), nil }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return ret.([]byte), nil } getTwo := func(rt fdb.ReadTransactor, key1, key2 fdb.Key) ([][]byte, error) { fmt.Printf("getTwo called with: %T\n", rt) - ret, e := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + ret, err := rt.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r1, _ := getOne(rtr, key1) r2, _ := getOne(rtr.Snapshot(), key2) return [][]byte{r1, r2}, nil }) - if e != nil { - return nil, e + if err != nil { + return nil, err } return ret.([][]byte), nil } - var e error + var err error fmt.Println("Calling getOne with a database:") - _, e = getOne(db, fdb.Key("foo")) - if e != nil { - fmt.Println(e) + _, err = getOne(db, fdb.Key("foo")) + if err != nil { + fmt.Println(err) return } fmt.Println("\nCalling getTwo with a database:") - _, e = getTwo(db, fdb.Key("foo"), fdb.Key("bar")) - if e != nil { - fmt.Println(e) + _, err = getTwo(db, fdb.Key("foo"), fdb.Key("bar")) + if err != nil { + fmt.Println(err) return } @@ -244,9 +244,9 @@ func ExamplePrefixRange() { fdb.MustAPIVersion(API_VERSION) db := fdb.MustOpenDefault() - tr, e := db.CreateTransaction() - if e != nil { - fmt.Printf("Unable to create transaction: %v\n", e) + tr, err := db.CreateTransaction() + if err != nil { + fmt.Printf("Unable to create transaction: %v\n", err) return } @@ -265,9 +265,9 @@ func ExamplePrefixRange() { pr, _ := fdb.PrefixRange([]byte("alphabet")) // Read and process the range - kvs, e := tr.GetRange(pr, fdb.RangeOptions{}).GetSliceWithError() - if e != nil { - fmt.Printf("Unable to read range: %v\n", e) + kvs, err := tr.GetRange(pr, fdb.RangeOptions{}).GetSliceWithError() + if err != nil { + fmt.Printf("Unable to read range: %v\n", err) } for _, kv := range kvs { fmt.Printf("%s: %s\n", string(kv.Key), string(kv.Value)) @@ -283,9 +283,9 @@ func ExampleRangeIterator() { fdb.MustAPIVersion(API_VERSION) db := fdb.MustOpenDefault() - tr, e := db.CreateTransaction() - if e != nil { - fmt.Printf("Unable to create transaction: %v\n", e) + tr, err := db.CreateTransaction() + if err != nil { + fmt.Printf("Unable to create transaction: %v\n", err) return } @@ -301,9 +301,9 @@ func ExampleRangeIterator() { // Advance will return true until the iterator is exhausted for ri.Advance() { - kv, e := ri.Get() - if e != nil { - fmt.Printf("Unable to read next value: %v\n", e) + kv, err := ri.Get() + if err != nil { + fmt.Printf("Unable to read next value: %v\n", err) return } fmt.Printf("%s is %s\n", kv.Key, kv.Value) diff --git a/bindings/go/src/fdb/range.go b/bindings/go/src/fdb/range.go index 36d35bd4e40..fdb11ccb8b4 100644 --- a/bindings/go/src/fdb/range.go +++ b/bindings/go/src/fdb/range.go @@ -161,9 +161,9 @@ func (rr RangeResult) GetSliceWithError() ([]KeyValue, error) { // complete. The current goroutine will be blocked until all reads have // completed. func (rr RangeResult) GetSliceOrPanic() []KeyValue { - kvs, e := rr.GetSliceWithError() - if e != nil { - panic(e) + kvs, err := rr.GetSliceWithError() + if err != nil { + panic(err) } return kvs } @@ -256,9 +256,9 @@ func (ri *RangeIterator) fetchNextBatch() { // asynchronous operations associated with this range did not successfully // complete. The Advance method of this RangeIterator must have returned true // prior to calling Get. -func (ri *RangeIterator) Get() (kv KeyValue, e error) { +func (ri *RangeIterator) Get() (kv KeyValue, err error) { if ri.err != nil { - e = ri.err + err = ri.err return } @@ -278,9 +278,9 @@ func (ri *RangeIterator) Get() (kv KeyValue, e error) { // complete. The Advance method of this RangeIterator must have returned true // prior to calling MustGet. func (ri *RangeIterator) MustGet() KeyValue { - kv, e := ri.Get() - if e != nil { - panic(e) + kv, err := ri.Get() + if err != nil { + panic(err) } return kv } @@ -310,9 +310,9 @@ func Strinc(prefix []byte) ([]byte, error) { func PrefixRange(prefix []byte) (KeyRange, error) { begin := make([]byte, len(prefix)) copy(begin, prefix) - end, e := Strinc(begin) - if e != nil { - return KeyRange{}, e + end, err := Strinc(begin) + if err != nil { + return KeyRange{}, err } return KeyRange{Key(begin), Key(end)}, nil } diff --git a/bindings/go/src/fdb/snapshot.go b/bindings/go/src/fdb/snapshot.go index 87ccb0c5023..4a2d7095030 100644 --- a/bindings/go/src/fdb/snapshot.go +++ b/bindings/go/src/fdb/snapshot.go @@ -46,10 +46,10 @@ type Snapshot struct { // // See the ReadTransactor interface for an example of using ReadTransact with // Transaction, Snapshot and Database objects. -func (s Snapshot) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, e error) { - defer panicToError(&e) +func (s Snapshot) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, err error) { + defer panicToError(&err) - r, e = f(s) + r, err = f(s) return } diff --git a/bindings/go/src/fdb/transaction.go b/bindings/go/src/fdb/transaction.go index 56fffd69450..3fd0ab10fe4 100644 --- a/bindings/go/src/fdb/transaction.go +++ b/bindings/go/src/fdb/transaction.go @@ -116,10 +116,10 @@ func (t Transaction) GetDatabase() Database { // // See the Transactor interface for an example of using Transact with // Transaction and Database objects. -func (t Transaction) Transact(f func(Transaction) (interface{}, error)) (r interface{}, e error) { - defer panicToError(&e) +func (t Transaction) Transact(f func(Transaction) (interface{}, error)) (r interface{}, err error) { + defer panicToError(&err) - r, e = f(t) + r, err = f(t) return } @@ -136,10 +136,10 @@ func (t Transaction) Transact(f func(Transaction) (interface{}, error)) (r inter // // See the ReadTransactor interface for an example of using ReadTransact with // Transaction, Snapshot and Database objects. -func (t Transaction) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, e error) { - defer panicToError(&e) +func (t Transaction) ReadTransact(f func(ReadTransaction) (interface{}, error)) (r interface{}, err error) { + defer panicToError(&err) - r, e = f(t) + r, err = f(t) return } @@ -189,9 +189,9 @@ func (t Transaction) Snapshot() Snapshot { // // Typical code will not use OnError directly. (Database).Transact uses // OnError internally to implement a correct retry loop. -func (t Transaction) OnError(e Error) FutureNil { +func (t Transaction) OnError(err Error) FutureNil { return &futureNil{ - future: newFuture(t.transaction, C.fdb_transaction_on_error(t.ptr, C.fdb_error_t(e.Code))), + future: newFuture(t.transaction, C.fdb_transaction_on_error(t.ptr, C.fdb_error_t(err.Code))), } } diff --git a/bindings/go/src/fdb/tuple/tuple.go b/bindings/go/src/fdb/tuple/tuple.go index a7922801057..871c65cef78 100644 --- a/bindings/go/src/fdb/tuple/tuple.go +++ b/bindings/go/src/fdb/tuple/tuple.go @@ -365,51 +365,51 @@ func (p *packer) encodeTuple(t Tuple, nested bool, versionstamps bool) { p.putByte(nestedCode) } - for i, e := range t { - switch e := e.(type) { + for i, err := range t { + switch err := err.(type) { case Tuple: - p.encodeTuple(e, true, versionstamps) + p.encodeTuple(err, true, versionstamps) case nil: p.putByte(nilCode) if nested { p.putByte(0xff) } case int: - p.encodeInt(int64(e)) + p.encodeInt(int64(err)) case int64: - p.encodeInt(e) + p.encodeInt(err) case uint: - p.encodeUint(uint64(e)) + p.encodeUint(uint64(err)) case uint64: - p.encodeUint(e) + p.encodeUint(err) case *big.Int: - p.encodeBigInt(e) + p.encodeBigInt(err) case big.Int: - p.encodeBigInt(&e) + p.encodeBigInt(&err) case []byte: - p.encodeBytes(bytesCode, e) + p.encodeBytes(bytesCode, err) case fdb.KeyConvertible: - p.encodeBytes(bytesCode, []byte(e.FDBKey())) + p.encodeBytes(bytesCode, []byte(err.FDBKey())) case string: - p.encodeBytes(stringCode, []byte(e)) + p.encodeBytes(stringCode, []byte(err)) case float32: - p.encodeFloat(e) + p.encodeFloat(err) case float64: - p.encodeDouble(e) + p.encodeDouble(err) case bool: - if e { + if err { p.putByte(trueCode) } else { p.putByte(falseCode) } case UUID: - p.encodeUUID(e) + p.encodeUUID(err) case Versionstamp: - if versionstamps == false && e.TransactionVersion == incompleteTransactionVersion { + if versionstamps == false && err.TransactionVersion == incompleteTransactionVersion { panic(fmt.Sprintf("Incomplete Versionstamp included in vanilla tuple pack")) } - p.encodeVersionstamp(e) + p.encodeVersionstamp(err) default: panic(fmt.Sprintf("unencodable element at index %d (%v, type %T)", i, t[i], t[i])) } @@ -433,7 +433,6 @@ func (p *packer) encodeTuple(t Tuple, nested bool, versionstamps bool) { // // This method will panic if it contains an incomplete Versionstamp. Use // PackWithVersionstamp instead. -// func (t Tuple) Pack() []byte { p := newPacker() p.encodeTuple(t, false, false) @@ -507,13 +506,13 @@ func (t Tuple) countIncompleteVersionstamps() int { incompleteCount := 0 for _, el := range t { - switch e := el.(type) { + switch err := el.(type) { case Versionstamp: - if e.TransactionVersion == incompleteTransactionVersion { + if err.TransactionVersion == incompleteTransactionVersion { incompleteCount++ } case Tuple: - incompleteCount += e.countIncompleteVersionstamps() + incompleteCount += err.countIncompleteVersionstamps() } } @@ -755,8 +754,8 @@ func (t Tuple) FDBRangeKeys() (fdb.KeyConvertible, fdb.KeyConvertible) { // (that is, all tuples of greater length than the Tuple of which the Tuple is a // prefix). func (t Tuple) FDBRangeKeySelectors() (fdb.Selectable, fdb.Selectable) { - b, e := t.FDBRangeKeys() - return fdb.FirstGreaterOrEqual(b), fdb.FirstGreaterOrEqual(e) + b, err := t.FDBRangeKeys() + return fdb.FirstGreaterOrEqual(b), fdb.FirstGreaterOrEqual(err) } func concat(a []byte, b ...byte) []byte { diff --git a/contrib/mockkms/get_encryption_keys.go b/contrib/mockkms/get_encryption_keys.go index be70cfbc9c2..84ad8130a6c 100644 --- a/contrib/mockkms/get_encryption_keys.go +++ b/contrib/mockkms/get_encryption_keys.go @@ -27,40 +27,40 @@ package main import ( - "encoding/json" - "fmt" - "io/ioutil" - "log" - "math/rand" - "net/http" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "math/rand" + "net/http" ) type CipherDetailRes struct { - BaseCipherId uint64 `json:"base_cipher_id"` - EncryptDomainId int64 `json:"encrypt_domain_id"` - BaseCipher string `json:"base_cipher"` + BaseCipherId uint64 `json:"base_cipher_id"` + EncryptDomainId int64 `json:"encrypt_domain_id"` + BaseCipher string `json:"base_cipher"` } type ValidationToken struct { - TokenName string `json:"token_name"` - TokenValue string `json:"token_value"` + TokenName string `json:"token_name"` + TokenValue string `json:"token_value"` } type CipherDetailReq struct { - BaseCipherId uint64 `json:"base_cipher_id"` - EncryptDomainId int64 `json:"encrypt_domain_id"` + BaseCipherId uint64 `json:"base_cipher_id"` + EncryptDomainId int64 `json:"encrypt_domain_id"` } type GetEncryptKeysResponse struct { - CipherDetails []CipherDetailRes `json:"cipher_key_details"` - KmsUrls []string `json:"kms_urls"` + CipherDetails []CipherDetailRes `json:"cipher_key_details"` + KmsUrls []string `json:"kms_urls"` } type GetEncryptKeysRequest struct { - QueryMode string `json:"query_mode"` - CipherDetails []CipherDetailReq `json:"cipher_key_details"` - ValidationTokens []ValidationToken `json:"validation_tokens"` - RefreshKmsUrls bool `json:"refresh_kms_urls"` + QueryMode string `json:"query_mode"` + CipherDetails []CipherDetailReq `json:"cipher_key_details"` + ValidationTokens []ValidationToken `json:"validation_tokens"` + RefreshKmsUrls bool `json:"refresh_kms_urls"` } type cipherMapInstanceSingleton map[uint64][]byte @@ -74,248 +74,248 @@ const ( ) const ( - maxCipherKeys = uint64(1024*1024) // Max cipher keys - maxCipherSize = 16 // Max cipher buffer size + maxCipherKeys = uint64(1024 * 1024) // Max cipher keys + maxCipherSize = 16 // Max cipher buffer size ) var ( - cipherMapInstance cipherMapInstanceSingleton // Singleton mapping of { baseCipherId -> baseCipher } + cipherMapInstance cipherMapInstanceSingleton // Singleton mapping of { baseCipherId -> baseCipher } ) // const mapping of { Location -> errorString } func errStrMap() func(int) string { - _errStrMap := map[int]string { - READ_HTTP_REQUEST_BODY : "Http request body read error", - UNMARSHAL_REQUEST_BODY_JSON : "Http request body unmarshal error", - UNSUPPORTED_QUERY_MODE : "Unsupported query_mode", - PARSE_HTTP_REQUEST : "Error parsing GetEncryptionKeys request", - MARSHAL_RESPONSE : "Error marshaling response", - } - - return func(key int) string { - return _errStrMap[key] - } + _errStrMap := map[int]string{ + READ_HTTP_REQUEST_BODY: "Http request body read error", + UNMARSHAL_REQUEST_BODY_JSON: "Http request body unmarshal error", + UNSUPPORTED_QUERY_MODE: "Unsupported query_mode", + PARSE_HTTP_REQUEST: "Error parsing GetEncryptionKeys request", + MARSHAL_RESPONSE: "Error marshaling response", + } + + return func(key int) string { + return _errStrMap[key] + } } // Caller is responsible for thread synchronization. Recommended to be invoked during package::init() func NewCipherMap(maxKeys uint64, cipherSize int) cipherMapInstanceSingleton { - if cipherMapInstance == nil { - cipherMapInstance = make(map[uint64][]byte) - - for i := uint64(1); i<= maxKeys; i++ { - cipher := make([]byte, cipherSize) - rand.Read(cipher) - cipherMapInstance[i] = cipher - } - log.Printf("KMS cipher map populate done, maxCiphers '%d'", maxCipherKeys) - } - return cipherMapInstance + if cipherMapInstance == nil { + cipherMapInstance = make(map[uint64][]byte) + + for i := uint64(1); i <= maxKeys; i++ { + cipher := make([]byte, cipherSize) + rand.Read(cipher) + cipherMapInstance[i] = cipher + } + log.Printf("KMS cipher map populate done, maxCiphers '%d'", maxCipherKeys) + } + return cipherMapInstance } func getKmsUrls() (urls []string) { - urlCount := rand.Intn(5) + 1 - for i := 1; i <= urlCount; i++ { - url := fmt.Sprintf("https://KMS/%d:%d:%d:%d", i, i, i, i) - urls = append(urls, url) - } - return + urlCount := rand.Intn(5) + 1 + for i := 1; i <= urlCount; i++ { + url := fmt.Sprintf("https://KMS/%d:%d:%d:%d", i, i, i, i) + urls = append(urls, url) + } + return } func isEncryptDomainIdValid(id int64) bool { - if id > 0 || id == -1 || id == -2 { - return true - } - return false + if id > 0 || id == -1 || id == -2 { + return true + } + return false } func abs(x int64) int64 { - if x < 0 { - return -x - } - return x + if x < 0 { + return -x + } + return x } func getBaseCipherIdFromDomainId(domainId int64) (baseCipherId uint64) { - baseCipherId = uint64(1) + uint64(abs(domainId)) % maxCipherKeys - return + baseCipherId = uint64(1) + uint64(abs(domainId))%maxCipherKeys + return } func getEncryptionKeysByKeyIds(w http.ResponseWriter, byteArr []byte) { - req := GetEncryptKeysRequest{} - err := json.Unmarshal(byteArr, &req) - if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { - var e error - if shouldInjectFault(PARSE_HTTP_REQUEST) { - e = fmt.Errorf("[FAULT] %s %s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) - } else { - e = fmt.Errorf("%s %s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - var details []CipherDetailRes - for i := 0; i < len(req.CipherDetails); i++ { - var baseCipherId = uint64(req.CipherDetails[i].BaseCipherId) - - var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) - if !isEncryptDomainIdValid(encryptDomainId) { - e := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) - sendErrorResponse(w, e) - return - } - - cipher, found := cipherMapInstance[baseCipherId] - if !found { - e := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) - sendErrorResponse(w, e) - return - } - - var detail = CipherDetailRes { - BaseCipherId: baseCipherId, - EncryptDomainId: encryptDomainId, - BaseCipher: string(cipher), - } - details = append(details, detail) - } - - var urls []string - if req.RefreshKmsUrls { - urls = getKmsUrls() - } - - resp := GetEncryptKeysResponse{ - CipherDetails: details, - KmsUrls: urls, - } - - mResp, err := json.Marshal(resp) - if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { - var e error - if shouldInjectFault(MARSHAL_RESPONSE) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - fmt.Fprintf(w, string(mResp)) + req := GetEncryptKeysRequest{} + err := json.Unmarshal(byteArr, &req) + if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { + var err error + if shouldInjectFault(PARSE_HTTP_REQUEST) { + err = fmt.Errorf("[FAULT] %s %s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) + } else { + err = fmt.Errorf("%s %s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + var details []CipherDetailRes + for i := 0; i < len(req.CipherDetails); i++ { + var baseCipherId = uint64(req.CipherDetails[i].BaseCipherId) + + var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) + if !isEncryptDomainIdValid(encryptDomainId) { + err := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) + sendErrorResponse(w, err) + return + } + + cipher, found := cipherMapInstance[baseCipherId] + if !found { + err := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) + sendErrorResponse(w, err) + return + } + + var detail = CipherDetailRes{ + BaseCipherId: baseCipherId, + EncryptDomainId: encryptDomainId, + BaseCipher: string(cipher), + } + details = append(details, detail) + } + + var urls []string + if req.RefreshKmsUrls { + urls = getKmsUrls() + } + + resp := GetEncryptKeysResponse{ + CipherDetails: details, + KmsUrls: urls, + } + + mResp, err := json.Marshal(resp) + if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { + var err error + if shouldInjectFault(MARSHAL_RESPONSE) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + fmt.Fprintf(w, string(mResp)) } func getEncryptionKeysByDomainIds(w http.ResponseWriter, byteArr []byte) { req := GetEncryptKeysRequest{} - err := json.Unmarshal(byteArr, &req) - if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { - var e error - if shouldInjectFault(PARSE_HTTP_REQUEST) { - e = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) - } else { - e = fmt.Errorf("%s '%s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - var details []CipherDetailRes - for i := 0; i < len(req.CipherDetails); i++ { - var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) - if !isEncryptDomainIdValid(encryptDomainId) { - e := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) - sendErrorResponse(w, e) - return - } - - var baseCipherId = getBaseCipherIdFromDomainId(encryptDomainId) - cipher, found := cipherMapInstance[baseCipherId] - if !found { - e := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) - sendErrorResponse(w, e) - return - } - - var detail = CipherDetailRes { - BaseCipherId: baseCipherId, - EncryptDomainId: encryptDomainId, - BaseCipher: string(cipher), - } - details = append(details, detail) - } - - var urls []string - if req.RefreshKmsUrls { - urls = getKmsUrls() - } - - resp := GetEncryptKeysResponse{ - CipherDetails: details, - KmsUrls: urls, - } - - mResp, err := json.Marshal(resp) - if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { - var e error - if shouldInjectFault(MARSHAL_RESPONSE) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - fmt.Fprintf(w, string(mResp)) + err := json.Unmarshal(byteArr, &req) + if err != nil || shouldInjectFault(PARSE_HTTP_REQUEST) { + var err error + if shouldInjectFault(PARSE_HTTP_REQUEST) { + err = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr)) + } else { + err = fmt.Errorf("%s '%s' err '%v'", errStrMap()(PARSE_HTTP_REQUEST), string(byteArr), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + var details []CipherDetailRes + for i := 0; i < len(req.CipherDetails); i++ { + var encryptDomainId = int64(req.CipherDetails[i].EncryptDomainId) + if !isEncryptDomainIdValid(encryptDomainId) { + err := fmt.Errorf("EncryptDomainId not valid '%d'", encryptDomainId) + sendErrorResponse(w, err) + return + } + + var baseCipherId = getBaseCipherIdFromDomainId(encryptDomainId) + cipher, found := cipherMapInstance[baseCipherId] + if !found { + err := fmt.Errorf("BaseCipherId not found '%d'", baseCipherId) + sendErrorResponse(w, err) + return + } + + var detail = CipherDetailRes{ + BaseCipherId: baseCipherId, + EncryptDomainId: encryptDomainId, + BaseCipher: string(cipher), + } + details = append(details, detail) + } + + var urls []string + if req.RefreshKmsUrls { + urls = getKmsUrls() + } + + resp := GetEncryptKeysResponse{ + CipherDetails: details, + KmsUrls: urls, + } + + mResp, err := json.Marshal(resp) + if err != nil || shouldInjectFault(MARSHAL_RESPONSE) { + var err error + if shouldInjectFault(MARSHAL_RESPONSE) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(MARSHAL_RESPONSE)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(MARSHAL_RESPONSE), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + fmt.Fprintf(w, string(mResp)) } func handleGetEncryptionKeys(w http.ResponseWriter, r *http.Request) { - byteArr, err := ioutil.ReadAll(r.Body) - if err != nil || shouldInjectFault(READ_HTTP_REQUEST_BODY) { - var e error - if shouldInjectFault(READ_HTTP_REQUEST_BODY) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(READ_HTTP_REQUEST_BODY)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(READ_HTTP_REQUEST_BODY), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - var arbitrary_json map[string]interface{} - err = json.Unmarshal(byteArr, &arbitrary_json) - if err != nil || shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { - var e error - if shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { - e = fmt.Errorf("[FAULT] %s", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON)) - } else { - e = fmt.Errorf("%s err '%v'", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON), err) - } - log.Println(e.Error()) - sendErrorResponse(w, e) - return - } - - if shouldInjectFault(UNSUPPORTED_QUERY_MODE) { - err = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) - sendErrorResponse(w, err) - return - } else if arbitrary_json["query_mode"] == "lookupByKeyId" { - getEncryptionKeysByKeyIds(w, byteArr) - } else if arbitrary_json["query_mode"] == "lookupByDomainId" { - getEncryptionKeysByDomainIds(w, byteArr) - } else { - err = fmt.Errorf("%s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) - sendErrorResponse(w, err) - return - } + byteArr, err := ioutil.ReadAll(r.Body) + if err != nil || shouldInjectFault(READ_HTTP_REQUEST_BODY) { + var err error + if shouldInjectFault(READ_HTTP_REQUEST_BODY) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(READ_HTTP_REQUEST_BODY)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(READ_HTTP_REQUEST_BODY), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + var arbitrary_json map[string]interface{} + err = json.Unmarshal(byteArr, &arbitrary_json) + if err != nil || shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { + var err error + if shouldInjectFault(UNMARSHAL_REQUEST_BODY_JSON) { + err = fmt.Errorf("[FAULT] %s", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON)) + } else { + err = fmt.Errorf("%s err '%v'", errStrMap()(UNMARSHAL_REQUEST_BODY_JSON), err) + } + log.Println(err.Error()) + sendErrorResponse(w, err) + return + } + + if shouldInjectFault(UNSUPPORTED_QUERY_MODE) { + err = fmt.Errorf("[FAULT] %s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) + sendErrorResponse(w, err) + return + } else if arbitrary_json["query_mode"] == "lookupByKeyId" { + getEncryptionKeysByKeyIds(w, byteArr) + } else if arbitrary_json["query_mode"] == "lookupByDomainId" { + getEncryptionKeysByDomainIds(w, byteArr) + } else { + err = fmt.Errorf("%s '%s'", errStrMap()(UNSUPPORTED_QUERY_MODE), arbitrary_json["query_mode"]) + sendErrorResponse(w, err) + return + } } func initEncryptCipherMap() { - cipherMapInstance = NewCipherMap(maxCipherKeys, maxCipherSize) -} \ No newline at end of file + cipherMapInstance = NewCipherMap(maxCipherKeys, maxCipherSize) +} diff --git a/contrib/mockkms/utils.go b/contrib/mockkms/utils.go index 30457e8164d..d8d1bcf28cb 100644 --- a/contrib/mockkms/utils.go +++ b/contrib/mockkms/utils.go @@ -21,31 +21,31 @@ package main import ( - "encoding/json" + "encoding/json" "fmt" "log" "net/http" ) type ErrorDetail struct { - Detail string `json:"details"` + Detail string `json:"details"` } type ErrorResponse struct { - Err ErrorDetail `json:"error"` + Err ErrorDetail `json:"error"` } func sendErrorResponse(w http.ResponseWriter, err error) { - e := ErrorDetail{} - e.Detail = fmt.Sprintf("Error: %s", err.Error()) - resp := ErrorResponse{ - Err: e, - } + e := ErrorDetail{} + e.Detail = fmt.Sprintf("Error: %s", err.Error()) + resp := ErrorResponse{ + Err: e, + } - mResp,err := json.Marshal(resp) - if err != nil { - log.Printf("Error marshalling error response %s", err.Error()) - panic(err) - } - fmt.Fprintf(w, string(mResp)) -} \ No newline at end of file + mResp, err := json.Marshal(resp) + if err != nil { + log.Printf("Error marshalling error response %s", err.Error()) + panic(err) + } + fmt.Fprintf(w, string(mResp)) +} diff --git a/packaging/docker/samples/golang/app/main.go b/packaging/docker/samples/golang/app/main.go index 20386d27be4..f3cac5a08e0 100644 --- a/packaging/docker/samples/golang/app/main.go +++ b/packaging/docker/samples/golang/app/main.go @@ -1,6 +1,6 @@ // main.go // -// This source file is part of the FoundationDB open source project +// # This source file is part of the FoundationDB open source project // // Copyright 2013-2024 Apple Inc. and the FoundationDB project authors // @@ -8,14 +8,13 @@ // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -// package main import ( @@ -50,7 +49,7 @@ func main() { } func incrementCounter(w http.ResponseWriter, r *http.Request) { - ret, e := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { value := tr.Get(fdb.Key("my-counter")).MustGet() if len(value) == 0 { value = intToBytes(0) @@ -61,8 +60,8 @@ func incrementCounter(w http.ResponseWriter, r *http.Request) { return intToBytes(counter), nil }) - if e != nil { - log.Fatalf("Unable to perform FDB transaction (%v)", e) + if err != nil { + log.Fatalf("Unable to perform FDB transaction (%v)", err) } fmt.Fprintf(w, "Counter is %d", bytesToInt(ret.([]byte))) diff --git a/recipes/go-recipes/blob.go b/recipes/go-recipes/blob.go index d4cbcc91d4e..7460b237416 100644 --- a/recipes/go-recipes/blob.go +++ b/recipes/go-recipes/blob.go @@ -97,9 +97,9 @@ func main() { write_blob(db, blobspace, test) - ret, e := read_blob(db, blobspace) + ret, err := read_blob(db, blobspace) - if e == nil { + if err == nil { fmt.Println(string(ret)) } } diff --git a/recipes/go-recipes/doc.go b/recipes/go-recipes/doc.go index 561718dedb0..1e41eb7325f 100644 --- a/recipes/go-recipes/doc.go +++ b/recipes/go-recipes/doc.go @@ -58,8 +58,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i diff --git a/recipes/go-recipes/indirect.go b/recipes/go-recipes/indirect.go index 985f8371608..16f89910dcc 100644 --- a/recipes/go-recipes/indirect.go +++ b/recipes/go-recipes/indirect.go @@ -56,8 +56,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i diff --git a/recipes/go-recipes/multi.go b/recipes/go-recipes/multi.go index 3de21d6053b..1b3d36b9152 100644 --- a/recipes/go-recipes/multi.go +++ b/recipes/go-recipes/multi.go @@ -71,8 +71,8 @@ func (multi MultiMap) MultiSubtract(trtr fdb.Transactor, index, value interface{ }) } -func (multi MultiMap) MultiGet(tr fdb.ReadTransactor, index int) (ret []interface{}, e error) { - _, e = tr.ReadTransact(func(tr fdb.ReadTransaction) (interface{}, error) { +func (multi MultiMap) MultiGet(tr fdb.ReadTransactor, index int) (ret []interface{}, err error) { + _, err = tr.ReadTransact(func(tr fdb.ReadTransaction) (interface{}, error) { pr, err := fdb.PrefixRange(multi.MapSS.Pack(tuple.Tuple{index})) if err != nil { return nil, err @@ -94,7 +94,7 @@ func (multi MultiMap) MultiGet(tr fdb.ReadTransactor, index int) (ret []interfac } func (multi MultiMap) MultiGetCounts(trtr fdb.Transactor, index interface{}) (map[interface{}]int, error) { - i, e := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { + i, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { kr, err := fdb.PrefixRange(multi.MapSS.Pack(tuple.Tuple{})) if err != nil { return nil, err @@ -118,7 +118,7 @@ func (multi MultiMap) MultiGetCounts(trtr fdb.Transactor, index interface{}) (ma } return counts, nil }) - return i.(map[interface{}]int), e + return i.(map[interface{}]int), err } func (multi MultiMap) MultiIsElement(trtr fdb.Transactor, index, value interface{}) bool { diff --git a/recipes/go-recipes/priority.go b/recipes/go-recipes/priority.go index 13b6968f593..346fd00dac4 100644 --- a/recipes/go-recipes/priority.go +++ b/recipes/go-recipes/priority.go @@ -46,8 +46,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i @@ -66,20 +66,20 @@ func (prty Priority) Push(trtr fdb.Transactor, value interface{}, priority int) func (prty Priority) _NextCount(trtr fdb.Transactor, priority int) int { res, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { - kr, e := fdb.PrefixRange(prty.PrioritySS.Pack(tuple.Tuple{priority})) - if e != nil { - return nil, e + kr, err := fdb.PrefixRange(prty.PrioritySS.Pack(tuple.Tuple{priority})) + if err != nil { + return nil, err } - ks, e := tr.Snapshot().GetRange(kr, fdb.RangeOptions{1, -1, true}).GetSliceWithError() - if e != nil { - return nil, e + ks, err := tr.Snapshot().GetRange(kr, fdb.RangeOptions{1, -1, true}).GetSliceWithError() + if err != nil { + return nil, err } if len(ks) == 0 { return 0, nil } - k, e := prty.PrioritySS.Unpack(ks[0].Key) + k, err := prty.PrioritySS.Unpack(ks[0].Key) return k[0].(int) + 1, nil }) if err != nil { @@ -142,7 +142,7 @@ func main() { p.Push(db, "b", 5) p.Push(db, "c", 2) p.Push(db, "d", 4) - p.Push(db, "e", 3) + p.Push(db, "err", 3) fmt.Println(p.Peek(db, false)) fmt.Println(p.Peek(db, false)) diff --git a/recipes/go-recipes/queue.go b/recipes/go-recipes/queue.go index 64340d7f0f6..dc49b944294 100644 --- a/recipes/go-recipes/queue.go +++ b/recipes/go-recipes/queue.go @@ -55,7 +55,7 @@ func (q *Queue) NewQueue(ss subspace.Subspace) { } func (q *Queue) Dequeue(trtr fdb.Transactor) (interface{}, error) { - i, e := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { + i, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { item, err := q.FirstItem(tr) if err != nil { return nil, err @@ -63,11 +63,11 @@ func (q *Queue) Dequeue(trtr fdb.Transactor) (interface{}, error) { tr.Clear(item.(fdb.KeyValue).Key) return item.(fdb.KeyValue).Value, err }) - return i, e + return i, err } func (q *Queue) Enqueue(trtr fdb.Transactor, item interface{}) (interface{}, error) { - i, e := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { + i, err := trtr.Transact(func(tr fdb.Transaction) (interface{}, error) { index, err := q.LastIndex(tr) if err != nil { return nil, err @@ -82,29 +82,29 @@ func (q *Queue) Enqueue(trtr fdb.Transactor, item interface{}) (interface{}, err return nil, nil }) - return i, e + return i, err } func (q *Queue) LastIndex(trtr fdb.Transactor) (interface{}, error) { - i, e := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + i, err := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r, err := rtr.Snapshot().GetRange(q.QueueSS, fdb.RangeOptions{1, 0, true}).GetSliceWithError() if len(r) == 0 { return q.QueueSS.Pack(tuple.Tuple{0}), nil } return r[0].Key, err }) - return i, e + return i, err } func (q *Queue) FirstItem(trtr fdb.Transactor) (interface{}, error) { - i, e := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + i, err := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r, err := rtr.GetRange(q.QueueSS, fdb.RangeOptions{1, 0, false}).GetSliceWithError() if len(r) == 0 { return nil, EmptyQueueError{} } return r[0], err }) - return i, e + return i, err } func main() { @@ -129,9 +129,9 @@ func main() { q.Enqueue(db, "test2") q.Enqueue(db, "test3") for i := 0; i < 5; i++ { - item, e := q.Dequeue(db) - if e != nil { - log.Fatal(e) + item, err := q.Dequeue(db) + if err != nil { + log.Fatal(err) } fmt.Println(string(item.([]byte))) diff --git a/recipes/go-recipes/table.go b/recipes/go-recipes/table.go index d41cb2ea7a1..6f768c75225 100644 --- a/recipes/go-recipes/table.go +++ b/recipes/go-recipes/table.go @@ -45,8 +45,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, e := tuple.Unpack(t) - if e != nil { + i, err := tuple.Unpack(t) + if err != nil { return nil } return i @@ -96,14 +96,14 @@ func (tbl Table) TableSetRow(trtr fdb.Transactor, row int, cols ...interface{}) func (tbl Table) TableGetRow(tr fdb.ReadTransactor, row int) ([]interface{}, error) { item, err := tr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - kr, e := fdb.PrefixRange(tbl.row.Pack(tuple.Tuple{row})) - if e != nil { - return nil, e + kr, err := fdb.PrefixRange(tbl.row.Pack(tuple.Tuple{row})) + if err != nil { + return nil, err } - slice, e := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() - if e != nil { - return nil, e + slice, err := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() + if err != nil { + return nil, err } ret := make([]interface{}, len(slice)) @@ -122,14 +122,14 @@ func (tbl Table) TableGetRow(tr fdb.ReadTransactor, row int) ([]interface{}, err func (tbl Table) TableGetCol(tr fdb.ReadTransactor, col int) ([]interface{}, error) { item, err := tr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { - kr, e := fdb.PrefixRange(tbl.col.Pack(tuple.Tuple{col})) - if e != nil { - return nil, e + kr, err := fdb.PrefixRange(tbl.col.Pack(tuple.Tuple{col})) + if err != nil { + return nil, err } - slice, e := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() - if e != nil { - return nil, e + slice, err := rtr.GetRange(kr, fdb.RangeOptions{0, -1, false}).GetSliceWithError() + if err != nil { + return nil, err } ret := make([]interface{}, len(slice)) From e5ca8c6dc2e8f718384d38dd98e0429037e1fdf2 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Mon, 21 Oct 2024 10:59:55 +0530 Subject: [PATCH 05/16] fixed accidental renames, renamed file from directoryLayer.go to directory_layer.go, to work on go formatting --- .../{directoryLayer.go => directory_layer.go} | 0 bindings/go/src/fdb/fdb.go | 18 +++++----- bindings/go/src/fdb/tuple/tuple.go | 36 +++++++++---------- recipes/go-recipes/priority.go | 2 +- recipes/go-recipes/table.go | 4 +-- 5 files changed, 30 insertions(+), 30 deletions(-) rename bindings/go/src/fdb/directory/{directoryLayer.go => directory_layer.go} (100%) diff --git a/bindings/go/src/fdb/directory/directoryLayer.go b/bindings/go/src/fdb/directory/directory_layer.go similarity index 100% rename from bindings/go/src/fdb/directory/directoryLayer.go rename to bindings/go/src/fdb/directory/directory_layer.go diff --git a/bindings/go/src/fdb/fdb.go b/bindings/go/src/fdb/fdb.go index 1f5f1dcd854..ed43870a95f 100644 --- a/bindings/go/src/fdb/fdb.go +++ b/bindings/go/src/fdb/fdb.go @@ -145,9 +145,9 @@ func APIVersion(version int) error { return errAPIVersionNotSupported } - if err := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); err != 0 { - if err != 0 { - if err == 2203 { + if e := C.fdb_select_api_version_impl(C.int(version), C.int(headerVersion)); e != 0 { + if e != 0 { + if e == 2203 { maxSupportedVersion := C.fdb_get_max_api_version() if headerVersion > int(maxSupportedVersion) { return fmt.Errorf("This version of the FoundationDB Go binding is "+ @@ -157,7 +157,7 @@ func APIVersion(version int) error { } return fmt.Errorf("API version %d is not supported by the installed FoundationDB C library.", version) } - return Error{int(err)} + return Error{int(e)} } } @@ -240,16 +240,16 @@ func executeWithRunningNetworkThread(f func()) error { // check if meanwhile another goroutine started the network thread if !networkStarted { - if err := C.fdb_setup_network(); err != 0 { - return Error{int(err)} + if e := C.fdb_setup_network(); e != 0 { + return Error{int(e)} } networkRunning.Add(1) go func() { - err := C.fdb_run_network() + e := C.fdb_run_network() networkRunning.Done() - if err != 0 { - panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(err)), err)) + if e != 0 { + panic(fmt.Sprintf("Unhandled error in FoundationDB network thread: %v (%v)\n", C.GoString(C.fdb_get_error(e)), e)) } }() diff --git a/bindings/go/src/fdb/tuple/tuple.go b/bindings/go/src/fdb/tuple/tuple.go index 871c65cef78..d6257d4aa94 100644 --- a/bindings/go/src/fdb/tuple/tuple.go +++ b/bindings/go/src/fdb/tuple/tuple.go @@ -365,51 +365,51 @@ func (p *packer) encodeTuple(t Tuple, nested bool, versionstamps bool) { p.putByte(nestedCode) } - for i, err := range t { - switch err := err.(type) { + for i, e := range t { + switch e := e.(type) { case Tuple: - p.encodeTuple(err, true, versionstamps) + p.encodeTuple(e, true, versionstamps) case nil: p.putByte(nilCode) if nested { p.putByte(0xff) } case int: - p.encodeInt(int64(err)) + p.encodeInt(int64(e)) case int64: - p.encodeInt(err) + p.encodeInt(e) case uint: - p.encodeUint(uint64(err)) + p.encodeUint(uint64(e)) case uint64: - p.encodeUint(err) + p.encodeUint(e) case *big.Int: - p.encodeBigInt(err) + p.encodeBigInt(e) case big.Int: - p.encodeBigInt(&err) + p.encodeBigInt(&e) case []byte: - p.encodeBytes(bytesCode, err) + p.encodeBytes(bytesCode, e) case fdb.KeyConvertible: - p.encodeBytes(bytesCode, []byte(err.FDBKey())) + p.encodeBytes(bytesCode, []byte(e.FDBKey())) case string: - p.encodeBytes(stringCode, []byte(err)) + p.encodeBytes(stringCode, []byte(e)) case float32: - p.encodeFloat(err) + p.encodeFloat(e) case float64: - p.encodeDouble(err) + p.encodeDouble(e) case bool: - if err { + if e { p.putByte(trueCode) } else { p.putByte(falseCode) } case UUID: - p.encodeUUID(err) + p.encodeUUID(e) case Versionstamp: - if versionstamps == false && err.TransactionVersion == incompleteTransactionVersion { + if versionstamps == false && e.TransactionVersion == incompleteTransactionVersion { panic(fmt.Sprintf("Incomplete Versionstamp included in vanilla tuple pack")) } - p.encodeVersionstamp(err) + p.encodeVersionstamp(e) default: panic(fmt.Sprintf("unencodable element at index %d (%v, type %T)", i, t[i], t[i])) } diff --git a/recipes/go-recipes/priority.go b/recipes/go-recipes/priority.go index 346fd00dac4..e127a433b99 100644 --- a/recipes/go-recipes/priority.go +++ b/recipes/go-recipes/priority.go @@ -142,7 +142,7 @@ func main() { p.Push(db, "b", 5) p.Push(db, "c", 2) p.Push(db, "d", 4) - p.Push(db, "err", 3) + p.Push(db, "e", 3) fmt.Println(p.Peek(db, false)) fmt.Println(p.Peek(db, false)) diff --git a/recipes/go-recipes/table.go b/recipes/go-recipes/table.go index 6f768c75225..bfebb4fd4cb 100644 --- a/recipes/go-recipes/table.go +++ b/recipes/go-recipes/table.go @@ -45,8 +45,8 @@ func _pack(t interface{}) []byte { } func _unpack(t []byte) tuple.Tuple { - i, err := tuple.Unpack(t) - if err != nil { + i, e := tuple.Unpack(t) + if e != nil { return nil } return i From 989b5aaaa2b218c520d25957c52aa443c9612b77 Mon Sep 17 00:00:00 2001 From: Vishesh Yadav Date: Sat, 19 Oct 2024 22:51:06 -0700 Subject: [PATCH 06/16] Update packaging/docker/samples/golang/app/main.go --- packaging/docker/samples/golang/app/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/docker/samples/golang/app/main.go b/packaging/docker/samples/golang/app/main.go index f3cac5a08e0..c8914c3207b 100644 --- a/packaging/docker/samples/golang/app/main.go +++ b/packaging/docker/samples/golang/app/main.go @@ -1,6 +1,6 @@ // main.go // -// # This source file is part of the FoundationDB open source project +// This source file is part of the FoundationDB open source project // // Copyright 2013-2024 Apple Inc. and the FoundationDB project authors // From aa05aea9cf4eb1d3dee4053be823f2a515b9ff5c Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Mon, 21 Oct 2024 12:50:57 +0530 Subject: [PATCH 07/16] renamed directoryPartition.go -> directory_partition.go and directorySubspace.go -> directory_subspace.go --- .../directory/{directoryPartition.go => directory_partition.go} | 0 .../fdb/directory/{directorySubspace.go => directory_subspace.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename bindings/go/src/fdb/directory/{directoryPartition.go => directory_partition.go} (100%) rename bindings/go/src/fdb/directory/{directorySubspace.go => directory_subspace.go} (100%) diff --git a/bindings/go/src/fdb/directory/directoryPartition.go b/bindings/go/src/fdb/directory/directory_partition.go similarity index 100% rename from bindings/go/src/fdb/directory/directoryPartition.go rename to bindings/go/src/fdb/directory/directory_partition.go diff --git a/bindings/go/src/fdb/directory/directorySubspace.go b/bindings/go/src/fdb/directory/directory_subspace.go similarity index 100% rename from bindings/go/src/fdb/directory/directorySubspace.go rename to bindings/go/src/fdb/directory/directory_subspace.go From 8604396e642ebdbf1081f068e4fa37f51fcc5d86 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Mon, 21 Oct 2024 22:27:26 +0530 Subject: [PATCH 08/16] updated: comments in files that were renamed, fixed accidental rename in bindings/go/src/fdb/fdb.go --- bindings/go/src/fdb/directory/directory_layer.go | 2 +- bindings/go/src/fdb/directory/directory_partition.go | 2 +- bindings/go/src/fdb/directory/directory_subspace.go | 2 +- bindings/go/src/fdb/fdb.go | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bindings/go/src/fdb/directory/directory_layer.go b/bindings/go/src/fdb/directory/directory_layer.go index 6bda8397e18..b69d5ee6ca6 100644 --- a/bindings/go/src/fdb/directory/directory_layer.go +++ b/bindings/go/src/fdb/directory/directory_layer.go @@ -1,5 +1,5 @@ /* - * directoryLayer.go + * directory_layer.go * * This source file is part of the FoundationDB open source project * diff --git a/bindings/go/src/fdb/directory/directory_partition.go b/bindings/go/src/fdb/directory/directory_partition.go index 8661e92606d..2e15de2dcfe 100644 --- a/bindings/go/src/fdb/directory/directory_partition.go +++ b/bindings/go/src/fdb/directory/directory_partition.go @@ -1,5 +1,5 @@ /* - * directoryPartition.go + * directory_partition.go * * This source file is part of the FoundationDB open source project * diff --git a/bindings/go/src/fdb/directory/directory_subspace.go b/bindings/go/src/fdb/directory/directory_subspace.go index bd7ba89703e..a4a791733d2 100644 --- a/bindings/go/src/fdb/directory/directory_subspace.go +++ b/bindings/go/src/fdb/directory/directory_subspace.go @@ -1,5 +1,5 @@ /* - * directorySubspace.go + * directory_subspace.go * * This source file is part of the FoundationDB open source project * diff --git a/bindings/go/src/fdb/fdb.go b/bindings/go/src/fdb/fdb.go index ed43870a95f..12e52c8b5aa 100644 --- a/bindings/go/src/fdb/fdb.go +++ b/bindings/go/src/fdb/fdb.go @@ -166,7 +166,7 @@ func APIVersion(version int) error { return nil } -// Determines if an API version has already been selected, i.err., if +// Determines if an API version has already been selected, i.e., if // APIVersion or MustAPIVersion have already been called. func IsAPIVersionSelected() bool { return apiVersion != 0 From 499ea6c7ef054fffb6cd5e56a45736aa800ea78f Mon Sep 17 00:00:00 2001 From: h4ck3r-04 <164143622+h4ck3r-04@users.noreply.github.com> Date: Sun, 27 Oct 2024 19:22:16 +0530 Subject: [PATCH 09/16] Update doc.go Removed unintentional whitespaces due to formatter --- bindings/go/src/fdb/doc.go | 56 +++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 28 deletions(-) diff --git a/bindings/go/src/fdb/doc.go b/bindings/go/src/fdb/doc.go index 68e07b2f469..0f297810d07 100644 --- a/bindings/go/src/fdb/doc.go +++ b/bindings/go/src/fdb/doc.go @@ -36,34 +36,34 @@ https://apple.github.io/foundationdb/index.html. A basic interaction with the FoundationDB API is demonstrated below: - package main - - import ( - "github.com/apple/foundationdb/bindings/go/src/fdb" - "log" - "fmt" - ) - - func main() { - // Different API versions may expose different runtime behaviors. - fdb.MustAPIVersion(740) - - // Open the default database from the system cluster - db := fdb.MustOpenDefault() - - // Database reads and writes happen inside transactions - ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { - tr.Set(fdb.Key("hello"), []byte("world")) - return tr.Get(fdb.Key("foo")).MustGet(), nil - // db.Transact automatically commits (and if necessary, - // retries) the transaction - }) - if err != nil { - log.Fatalf("Unable to perform FDB transaction (%v)", err) - } - - fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte))) - } +package main + +import ( + "github.com/apple/foundationdb/bindings/go/src/fdb" + "log" + "fmt" +) + +func main() { + // Different API versions may expose different runtime behaviors. + fdb.MustAPIVersion(740) + + // Open the default database from the system cluster + db := fdb.MustOpenDefault() + + // Database reads and writes happen inside transactions + ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) { + tr.Set(fdb.Key("hello"), []byte("world")) + return tr.Get(fdb.Key("foo")).MustGet(), nil + // db.Transact automatically commits (and if necessary, + // retries) the transaction + }) + if err != nil { + log.Fatalf("Unable to perform FDB transaction (%v)", err) + } + + fmt.Printf("hello is now world, foo was: %s\n", string(ret.([]byte))) +} # Futures From cb7249e6c154d8b727d74f1497dd6b38bc2128c1 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 <164143622+h4ck3r-04@users.noreply.github.com> Date: Mon, 28 Oct 2024 14:02:01 +0530 Subject: [PATCH 10/16] Update get_encryption_keys.go removed unintentional whitespaces in get_encryption_keys.go From 9250cd913e6d7467e07aec02739b2c8dd047db33 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Mon, 28 Oct 2024 16:34:15 +0530 Subject: [PATCH 11/16] removed accidental whitespaces in get_encryption_keys.go --- contrib/mockkms/get_encryption_keys.go | 54 +++++++++++++------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/contrib/mockkms/get_encryption_keys.go b/contrib/mockkms/get_encryption_keys.go index 84ad8130a6c..fbf60572ff1 100644 --- a/contrib/mockkms/get_encryption_keys.go +++ b/contrib/mockkms/get_encryption_keys.go @@ -27,59 +27,59 @@ package main import ( - "encoding/json" - "fmt" - "io/ioutil" - "log" - "math/rand" - "net/http" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "math/rand" + "net/http" ) type CipherDetailRes struct { - BaseCipherId uint64 `json:"base_cipher_id"` - EncryptDomainId int64 `json:"encrypt_domain_id"` - BaseCipher string `json:"base_cipher"` + BaseCipherId uint64 `json:"base_cipher_id"` + EncryptDomainId int64 `json:"encrypt_domain_id"` + BaseCipher string `json:"base_cipher"` } type ValidationToken struct { - TokenName string `json:"token_name"` - TokenValue string `json:"token_value"` + TokenName string `json:"token_name"` + TokenValue string `json:"token_value"` } type CipherDetailReq struct { - BaseCipherId uint64 `json:"base_cipher_id"` - EncryptDomainId int64 `json:"encrypt_domain_id"` + BaseCipherId uint64 `json:"base_cipher_id"` + EncryptDomainId int64 `json:"encrypt_domain_id"` } type GetEncryptKeysResponse struct { - CipherDetails []CipherDetailRes `json:"cipher_key_details"` - KmsUrls []string `json:"kms_urls"` + CipherDetails []CipherDetailRes `json:"cipher_key_details"` + KmsUrls []string `json:"kms_urls"` } type GetEncryptKeysRequest struct { - QueryMode string `json:"query_mode"` - CipherDetails []CipherDetailReq `json:"cipher_key_details"` - ValidationTokens []ValidationToken `json:"validation_tokens"` - RefreshKmsUrls bool `json:"refresh_kms_urls"` + QueryMode string `json:"query_mode"` + CipherDetails []CipherDetailReq `json:"cipher_key_details"` + ValidationTokens []ValidationToken `json:"validation_tokens"` + RefreshKmsUrls bool `json:"refresh_kms_urls"` } type cipherMapInstanceSingleton map[uint64][]byte const ( - READ_HTTP_REQUEST_BODY = iota - UNMARSHAL_REQUEST_BODY_JSON - UNSUPPORTED_QUERY_MODE - PARSE_HTTP_REQUEST - MARSHAL_RESPONSE + READ_HTTP_REQUEST_BODY = iota + UNMARSHAL_REQUEST_BODY_JSON + UNSUPPORTED_QUERY_MODE + PARSE_HTTP_REQUEST + MARSHAL_RESPONSE ) const ( - maxCipherKeys = uint64(1024 * 1024) // Max cipher keys - maxCipherSize = 16 // Max cipher buffer size + maxCipherKeys = uint64(1024 * 1024) // Max cipher keys + maxCipherSize = 16 // Max cipher buffer size ) var ( - cipherMapInstance cipherMapInstanceSingleton // Singleton mapping of { baseCipherId -> baseCipher } + cipherMapInstance cipherMapInstanceSingleton // Singleton mapping of { baseCipherId -> baseCipher } ) // const mapping of { Location -> errorString } From fa520ccb39f528666b2db06ea2d98c17b00448ad Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Tue, 29 Oct 2024 11:56:55 +0530 Subject: [PATCH 12/16] fixed few minor issues while renaming variable --- bindings/go/src/fdb/tuple/tuple.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/bindings/go/src/fdb/tuple/tuple.go b/bindings/go/src/fdb/tuple/tuple.go index d6257d4aa94..dd6f7a61f4e 100644 --- a/bindings/go/src/fdb/tuple/tuple.go +++ b/bindings/go/src/fdb/tuple/tuple.go @@ -506,13 +506,13 @@ func (t Tuple) countIncompleteVersionstamps() int { incompleteCount := 0 for _, el := range t { - switch err := el.(type) { + switch e := el.(type) { case Versionstamp: - if err.TransactionVersion == incompleteTransactionVersion { + if e.TransactionVersion == incompleteTransactionVersion { incompleteCount++ } case Tuple: - incompleteCount += err.countIncompleteVersionstamps() + incompleteCount += e.countIncompleteVersionstamps() } } @@ -754,8 +754,8 @@ func (t Tuple) FDBRangeKeys() (fdb.KeyConvertible, fdb.KeyConvertible) { // (that is, all tuples of greater length than the Tuple of which the Tuple is a // prefix). func (t Tuple) FDBRangeKeySelectors() (fdb.Selectable, fdb.Selectable) { - b, err := t.FDBRangeKeys() - return fdb.FirstGreaterOrEqual(b), fdb.FirstGreaterOrEqual(err) + b, e := t.FDBRangeKeys() + return fdb.FirstGreaterOrEqual(b), fdb.FirstGreaterOrEqual(e) } func concat(a []byte, b ...byte) []byte { From 04b995a13329e390920441423e719c0098e201d4 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Wed, 30 Oct 2024 21:45:54 +0530 Subject: [PATCH 13/16] updated: minor tweaks, typos --- bindings/go/src/fdb/fdb.go | 2 +- recipes/go-recipes/queue.go | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/bindings/go/src/fdb/fdb.go b/bindings/go/src/fdb/fdb.go index 12e52c8b5aa..d4c62168609 100644 --- a/bindings/go/src/fdb/fdb.go +++ b/bindings/go/src/fdb/fdb.go @@ -408,7 +408,7 @@ func createDatabase(clusterFile string) (Database, error) { // OpenWithConnectionString returns a database handle to the FoundationDB cluster identified // by the provided connection string. This method can be useful for scenarios where you want to connect -// to the database only for a short time err.g. to test different connection strings. +// to the database only for a short time e.g. to test different connection strings. // Caller must call Close() to release resources. func OpenWithConnectionString(connectionString string) (Database, error) { if apiVersion == 0 { diff --git a/recipes/go-recipes/queue.go b/recipes/go-recipes/queue.go index dc49b944294..fae8b1b1771 100644 --- a/recipes/go-recipes/queue.go +++ b/recipes/go-recipes/queue.go @@ -86,25 +86,25 @@ func (q *Queue) Enqueue(trtr fdb.Transactor, item interface{}) (interface{}, err } func (q *Queue) LastIndex(trtr fdb.Transactor) (interface{}, error) { - i, err := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + i, e := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r, err := rtr.Snapshot().GetRange(q.QueueSS, fdb.RangeOptions{1, 0, true}).GetSliceWithError() if len(r) == 0 { return q.QueueSS.Pack(tuple.Tuple{0}), nil } return r[0].Key, err }) - return i, err + return i, e } func (q *Queue) FirstItem(trtr fdb.Transactor) (interface{}, error) { - i, err := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { + i, e := trtr.ReadTransact(func(rtr fdb.ReadTransaction) (interface{}, error) { r, err := rtr.GetRange(q.QueueSS, fdb.RangeOptions{1, 0, false}).GetSliceWithError() if len(r) == 0 { return nil, EmptyQueueError{} } return r[0], err }) - return i, err + return i, e } func main() { From 7d3a283a3954d8da87e8d65a58e82d76023257b4 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Fri, 8 Nov 2024 13:37:55 +0530 Subject: [PATCH 14/16] updated: CMakeLists --- bindings/go/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/go/CMakeLists.txt b/bindings/go/CMakeLists.txt index 58b9815dbfa..b82e0920607 100644 --- a/bindings/go/CMakeLists.txt +++ b/bindings/go/CMakeLists.txt @@ -8,17 +8,17 @@ set(SRCS src/fdb/directory/directory.go src/fdb/doc.go src/fdb/transaction.go - src/fdb/directory/directoryLayer.go + src/fdb/directory/directory_layer.go src/fdb/errors.go src/fdb/keyselector.go src/fdb/tuple/tuple.go src/fdb/cluster.go - src/fdb/directory/directoryPartition.go + src/fdb/directory/directory_partition.go src/fdb/fdb.go src/fdb/range.go src/fdb/tuple/tuple_test.go src/fdb/database.go - src/fdb/directory/directorySubspace.go + src/fdb/directory/directory_subspace.go src/fdb/fdb_test.go src/fdb/snapshot.go From 27ca34e83d4b01dc09257d4b564dd03eae2caad0 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Fri, 8 Nov 2024 13:41:08 +0530 Subject: [PATCH 15/16] removed: named return value --- bindings/go/src/fdb/directory/directory_subspace.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/go/src/fdb/directory/directory_subspace.go b/bindings/go/src/fdb/directory/directory_subspace.go index a4a791733d2..d9948e203d3 100644 --- a/bindings/go/src/fdb/directory/directory_subspace.go +++ b/bindings/go/src/fdb/directory/directory_subspace.go @@ -90,7 +90,7 @@ func (d directorySubspace) Exists(rt fdb.ReadTransactor, path []string) (bool, e return d.dl.Exists(rt, d.dl.partitionSubpath(d.path, path)) } -func (d directorySubspace) List(rt fdb.ReadTransactor, path []string) (subdirs []string, err error) { +func (d directorySubspace) List(rt fdb.ReadTransactor, path []string) ([]string, error) { return d.dl.List(rt, d.dl.partitionSubpath(d.path, path)) } From 5659f310b1e1bce24633c9569d75e3748e4cbfc9 Mon Sep 17 00:00:00 2001 From: h4ck3r-04 Date: Fri, 8 Nov 2024 13:43:51 +0530 Subject: [PATCH 16/16] handling nil Pointer exception --- recipes/go-recipes/priority.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/recipes/go-recipes/priority.go b/recipes/go-recipes/priority.go index e127a433b99..fb8cca8949b 100644 --- a/recipes/go-recipes/priority.go +++ b/recipes/go-recipes/priority.go @@ -80,6 +80,9 @@ func (prty Priority) _NextCount(trtr fdb.Transactor, priority int) int { return 0, nil } k, err := prty.PrioritySS.Unpack(ks[0].Key) + if err != nil { + return nil, err + } return k[0].(int) + 1, nil }) if err != nil {