Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generic interface for transact/read transact. #11302

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions bindings/go/go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
module github.com/apple/foundationdb/bindings/go

go 1.18

// The FoundationDB go bindings currently have no external golang dependencies outside of
// the go standard library.
116 changes: 116 additions & 0 deletions bindings/go/src/fdb/transact.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* database.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* database.go
* transact.go

*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2024 Apple Inc. and the FoundationDB project authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* 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
*
* 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.
*/

// FoundationDB Go API
package fdb

func Transact(db Database, f func(t Transaction) error) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a better name would be TransactWithoutReturnValue?

_, err := db.Transact(func(t Transaction) (interface{}, error) {
return nil, f(t)
})
return err
}

func Transact1[T any](db Database, f func(t Transaction) (T, error)) (T, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you provide some examples in which cases those different Transact1, Transact2 and Transact3 are needed?

v, err := db.Transact(func(t Transaction) (interface{}, error) {
return f(t)
})
if v == nil {
var empty T
return empty, err
}
return v.(T), nil
}

func Transact2[T1 any, T2 any](db Database, f func(t Transaction) (T1, T2, error)) (T1, T2, error) {
v, err := db.Transact(func(t Transaction) (interface{}, error) {
t1, t2, err := f(t)
return []any{t1, t2}, err
})
if v == nil {
var t1 T1
var t2 T2
return t1, t2, err
}
a := v.([]interface{})
return a[0].(T1), a[1].(T2), err
}

func Transact3[T1 any, T2 any, T3 any](db Database, f func(t Transaction) (T1, T2, T3, error)) (T1, T2, T3, error) {
v, err := db.Transact(func(t Transaction) (interface{}, error) {
t1, t2, t3, err := f(t)
return []any{t1, t2, t3}, err
})
if v == nil {
var t1 T1
var t2 T2
var t3 T3
return t1, t2, t3, err
}
a := v.([]interface{})
return a[0].(T1), a[1].(T2), a[2].(T3), err
}

func ReadTransact(db Database, f func(t ReadTransaction) error) error {
_, err := db.ReadTransact(func(t ReadTransaction) (interface{}, error) {
return nil, f(t)
})
return err
}

func ReadTransact1[T any](db Database, f func(t ReadTransaction) (T, error)) (T, error) {
v, err := db.ReadTransact(func(t ReadTransaction) (interface{}, error) {
return f(t)
})
if v == nil {
var empty T
return empty, err
}
return v.(T), nil
}

func ReadTransact2[T1 any, T2 any](db Database, f func(t ReadTransaction) (T1, T2, error)) (T1, T2, error) {
v, err := db.ReadTransact(func(t ReadTransaction) (interface{}, error) {
t1, t2, err := f(t)
return []any{t1, t2}, err
})
if v == nil {
var t1 T1
var t2 T2
return t1, t2, err
}
a := v.([]interface{})
return a[0].(T1), a[1].(T2), err
}

func ReadTransact3[T1 any, T2 any, T3 any](db Database, f func(t ReadTransaction) (T1, T2, T3, error)) (T1, T2, T3, error) {
v, err := db.ReadTransact(func(t ReadTransaction) (interface{}, error) {
t1, t2, t3, err := f(t)
return []any{t1, t2, t3}, err
})
if v == nil {
var t1 T1
var t2 T2
var t3 T3
return t1, t2, t3, err
}
a := v.([]interface{})
return a[0].(T1), a[1].(T2), a[2].(T3), err
}